ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / GraphicElement Class / Clone Method
String
Example

In This Topic
    Clone Method (GraphicElement)
    In This Topic
    Generates a cloned copy of an existing GraphicElement on a page layout. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public GraphicElement Clone( 
       string suffix
    )
    Public Function Clone( _
       Optional ByVal suffix As String _
    ) As GraphicElement

    Parameters

    suffix
    String

    Return Value

    Exceptions
    ExceptionDescription
    This method must be called within the lambda passed to QueuedTask.Run.
    Map has reached maximum graphics count limit of 4000 elements. One or more elements cannot be created.
    Map has reached maximum graphics size limit of 10 MB. One or more elements cannot be created.
    Remarks
    The suffix parameter is an optional string that is used to tag each newly created GraphicElement. The new element will get the same element name as the parent graphic plus the suffix value along with a numeric sequencer. This makes it possible, if needed, to find and remove unwanted cloned items.
    Example
    CloneGraphic
    //This example finds a layout element by name and clones it a specified number of times and applies an accumlative offset for each 
    //cloned element.
    
    //Added references
    using ArcGIS.Core.CIM;                             //CIM
    using ArcGIS.Desktop.Core;                         //Project
    using ArcGIS.Desktop.Layouts;                      //Layout class
    using ArcGIS.Desktop.Framework.Threading.Tasks;    //QueuedTask
    
    public class GraphicElementExample2
    {
      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;
        });
      }
    }
    GraphicElement_Clone
    //Note: call within QueuedTask.Run() 
    GraphicElement cloneElm = graElm.Clone("Clone");
    Clone an element
    //Clone a layout graphic element and apply an offset.
    
    //Perform on the worker thread
    QueuedTask.Run(() =>
    {
      // Reference and load the layout associated with the layout item
      Layout layout = layoutItem.GetLayout();
      if (layout != null)
      {
        // Reference a graphic 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);
        }
      }
    });
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also