ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / GraphicsLayerExtensions Class / AddElements Method
The collection of graphics to be added
A collection of element names or null (optional)
True to set the element selected after the add (default is true) (optional)
Additional element properties or null
Example

In This Topic
    AddElements Method
    In This Topic
    Add a collection of ArcGIS.Desktop.Layouts.GraphicElement based on the list of ArcGIS.Core.CIM.CIMGraphic. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax

    Parameters

    graphicsLayer
    graphics
    The collection of graphics to be added
    elementNames
    A collection of element names or null (optional)
    select
    True to set the element selected after the add (default is true) (optional)
    elementInfos
    Additional element properties or null

    Return Value

    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    The value cannot be null: graphics
    Number of elementNames does not match the number of graphics
    Number of elementInfos does not match the number of graphics
    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 map view displaying the graphics layer should be initialized.
    o ArcGIS.Core.CIM.CIMInkGraphic is not supported
    o ArcGIS.Core.CIM.CIMMultiPatchGraphic is not supported
    The default behavior is that elements will not be selected after the add (ie default selection behavior is false whereas for single element adds, the default selection behavior is true. Element names and elementInfos must either match the input number of CIMGraphics or be null.
    Example
    Bulk Graphics creation
    //Point Feature layer to convert into graphics
    var lyr = MapView.Active?.Map?.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
    //Graphics layer to store the graphics to
    var gl = MapView.Active.Map.GetLayersAsFlattenedList().OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();
    if (lyr == null) return;
    QueuedTask.Run(() =>
    {
      //Point symbol for graphics
      var pointSymbol = SymbolFactory.Instance.ConstructPointSymbol(CIMColor.CreateRGBColor(100, 255, 40), 10, SimpleMarkerStyle.Circle);
      //Collection to hold the point graphics
      var listGraphicElements = new List<CIMGraphic>();
      //Iterate through each point feature in the feature layer
      using (RowCursor rows = lyr.Search()) //execute
      {
        int i = 0;
        while (rows.MoveNext())
        {
          using (var feature = rows.Current as Feature)
          {
            //Create a point graphic for the feature
            var crimePt = feature.GetShape() as MapPoint;
            if (crimePt != null)
            {
              var cimGraphicElement = new CIMPointGraphic
              {
                Location = crimePt, //MapPoint
                Symbol = pointSymbol.MakeSymbolReference()
              };
              //Add the point feature to the collection
              listGraphicElements.Add(cimGraphicElement);
              i++;
            }
          }
        }
      }
      //Magic happens...Add all the features to the Graphics layer 
      gl.AddElements(listGraphicElements);
    });
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also