public static Task <bool> CloneElementAsync(string LayoutName, string ElementName, double offset, int numCopies)
        {
            //Reference a layoutitem in a project by name
            LayoutProjectItem layoutItem = Project.Current.GetItems <LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals(LayoutName));

            if (layoutItem == null)
            {
                return(Task.FromResult(false));
            }

            return(QueuedTask.Run <bool>(() =>
            {
                //Reference and load the layout associated with the layout item
                Layout lyt = layoutItem.GetLayout();

                //Reference a element by name
                GraphicElement graElm = lyt.FindElement(ElementName) as GraphicElement;
                if (graElm == null)
                {
                    return false;
                }

                //Loop through the number of copies, clone, and set the offsets for each new element
                double orig_offset = offset;
                while (numCopies != 0)
                {
                    GraphicElement cloneElm = graElm.Clone("Clone");
                    cloneElm.SetX(cloneElm.GetX() + offset);
                    cloneElm.SetY(cloneElm.GetY() - offset);
                    offset = offset + orig_offset;
                    numCopies = numCopies - 1;
                }

                return true;
            }));
        }
示例#2
0
        public void snippets_UpdateElements()
        {
            double x = 0;
            double y = 0;

            #region Update Text Element properties

            // Reference a layoutitem in a project by name
            LayoutProjectItem layoutItem = Project.Current.GetItems <LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
            if (layoutItem != null)
            {
                QueuedTask.Run(() =>
                {
                    // Reference and load the layout associated with the layout item
                    Layout layout = layoutItem.GetLayout();
                    if (layout != null)
                    {
                        // Reference a text element by name
                        TextElement txtElm = layout.FindElement("MyTextElement") as TextElement;
                        if (txtElm != null)
                        {
                            // Change placement properties
                            txtElm.SetAnchor(Anchor.CenterPoint);
                            txtElm.SetX(x);
                            txtElm.SetY(y);

                            // Change TextProperties
                            TextProperties txtProperties = new TextProperties("Hello world", "Times New Roman", 48, "Regular");
                            txtElm.SetTextProperties(txtProperties);
                        }
                    }
                });
            }
            #endregion

            #region Update a picture element

            QueuedTask.Run(() =>
            {
                // Reference and load the layout associated with the layout item
                Layout layout = layoutItem.GetLayout();
                if (layout != null)
                {
                    // Reference a picture element by name
                    PictureElement picElm = layout.FindElement("MyPicture") as PictureElement;
                    // Change the path to a new source
                    if (picElm != null)
                    {
                        picElm.SetSourcePath(@"D:\MyData\Pics\somePic.jpg");
                    }
                }
            });
            #endregion

            #region Update a map surround

            QueuedTask.Run(() =>
            {
                // Reference and load the layout associated with the layout item
                Layout layout = layoutItem.GetLayout();
                if (layout != null)
                {
                    // Reference a scale bar element by name
                    MapSurround scaleBar = layout.FindElement("MyScaleBar") as MapSurround;

                    // Reference a map frame element by name
                    MapFrame mf = layout.FindElement("MyMapFrame") as MapFrame;

                    if ((scaleBar != null) && (mf != null))
                    {
                        //Set the scale bar to the newly referenced map frame
                        scaleBar.SetMapFrame(mf);
                    }
                }
            });
            #endregion

            #region Lock an element

            // The Locked property is displayed in the TOC as a lock symbol next to each element.
            // If locked the element can't be selected in the layout using the graphic
            // selection tools.

            QueuedTask.Run(() =>
            {
                // Reference and load the layout associated with the layout item
                Layout layout = layoutItem.GetLayout();
                if (layout != null)
                {
                    //Reference an element by name
                    Element element = layout.FindElement("MyElement");
                    if (element != null)
                    {
                        // Modify the Locked property via the CIM
                        CIMElement CIMElement = element.GetDefinition() as CIMElement;
                        CIMElement.Locked     = true;
                        element.SetDefinition(CIMElement);
                    }
                }
            });

            #endregion

            #region Update an elements transparency

            QueuedTask.Run(() =>
            {
                // Reference and load the layout associated with the layout item
                Layout layout = layoutItem.GetLayout();
                if (layout != null)
                {
                    // Reference a element by name
                    GraphicElement graphicElement = layout.FindElement("MyElement") as GraphicElement;
                    if (graphicElement != null)
                    {
                        // Modify the Transparency property that exists only in the CIMGraphic class.
                        CIMGraphic CIMGraphic   = graphicElement.Graphic as CIMGraphic;
                        CIMGraphic.Transparency = 50; // mark it 50% transparent
                        graphicElement.SetGraphic(CIMGraphic);
                    }
                }
            });
            #endregion

            double xOffset = 0;
            double yOffset = 0;
            #region Clone an element

            QueuedTask.Run(() =>
            {
                // Reference and load the layout associated with the layout item
                Layout layout = layoutItem.GetLayout();
                if (layout != null)
                {
                    // Reference a element by name
                    GraphicElement graphicElement = layout.FindElement("MyElement") as GraphicElement;
                    if (graphicElement != null)
                    {
                        // clone and set the new x,y
                        GraphicElement cloneElement = graphicElement.Clone("Clone");
                        cloneElement.SetX(cloneElement.GetX() + xOffset);
                        cloneElement.SetY(cloneElement.GetY() + yOffset);
                    }
                }
            });
            #endregion
        }