ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / GraphicElement Class / SetGraphic Method
CIMGraphic
Example

In This Topic
    SetGraphic Method (GraphicElement)
    In This Topic
    Applies the changes made to a CIMGraphic back to the GraphicElement. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void SetGraphic( 
       CIMGraphic cimGraphic
    )
    Public Sub SetGraphic( _
       ByVal cimGraphic As CIMGraphic _
    ) 

    Parameters

    cimGraphic
    CIMGraphic
    Exceptions
    ExceptionDescription
    This method must be called within the lambda passed to QueuedTask.Run.
    Example
    Modify symbology of a Graphic Element
    //within a queued Task
    //get the first line element in the layer
    var ge = graphicsLayer.FindElement("Line 10") as GraphicElement;
    var graphic = ge.GetGraphic();
    if (graphic is CIMLineGraphic lineGraphic)
    {
      //change its symbol
      lineGraphic.Symbol =
       SymbolFactory.Instance.ConstructLineSymbol(
         SymbolFactory.Instance.ConstructStroke(
     ColorFactory.Instance.BlueRGB, 2, SimpleLineStyle.DashDot)).MakeSymbolReference();
      //apply the change
      ge.SetGraphic(lineGraphic);
    }
    Create_PolygonText
    //Create polygon paragraph text with basic text properties.
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Build geometry
      List<Coordinate2D> plyCoords = new List<Coordinate2D>();
      plyCoords.Add(new Coordinate2D(3.5, 7));
      plyCoords.Add(new Coordinate2D(4.5, 7));
      plyCoords.Add(new Coordinate2D(4.5, 6.7));
      plyCoords.Add(new Coordinate2D(5.5, 6.7));
      plyCoords.Add(new Coordinate2D(5.5, 6.1));
      plyCoords.Add(new Coordinate2D(3.5, 6.1));
      //At 2.x - Polygon poly = PolygonBuilder.CreatePolygon(plyCoords);
      Polygon poly = PolygonBuilderEx.CreatePolygon(plyCoords);
    
      //Set symbolology, create and add element to layout
      CIMTextSymbol sym = SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.GreyRGB, 10, "Arial", "Regular");
      string text = "Some Text String that is really long and is <BOL>forced to wrap to other lines</BOL> so that we can see the effects." as String;
      //At 2.x - GraphicElement polyTxtElm = LayoutElementFactory.Instance.CreatePolygonParagraphGraphicElement(layout, poly, text, sym);
      //         polyTxtElm.SetName("New Polygon Text"); 
      GraphicElement polyTxtElm = ElementFactory.Instance.CreateTextGraphicElement(
                                    layout, TextType.RectangleParagraph, poly, sym, text, "New Polygon Text");
    
      //(Optionally) Modify paragraph border 
      CIMGraphic polyTxtGra = polyTxtElm.GetGraphic();
      CIMParagraphTextGraphic cimPolyTxtGra = polyTxtGra as CIMParagraphTextGraphic;
      cimPolyTxtGra.Frame.BorderSymbol = new CIMSymbolReference();
      cimPolyTxtGra.Frame.BorderSymbol.Symbol = SymbolFactory.Instance.ConstructLineSymbol(ColorFactory.Instance.GreyRGB, 1.0, SimpleLineStyle.Solid);
      polyTxtElm.SetGraphic(polyTxtGra);
    });
    
    Create_CircleText
    //Create circle paragraph text with basic text settings and optionally a modified border.
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Build geometry
      Coordinate2D center = new Coordinate2D(4.5, 4);
      //At 2.x - EllipticArcBuilder eabCir = new EllipticArcBuilder(center, 0.5, esriArcOrientation.esriArcClockwise);
      var eabCir = new EllipticArcBuilderEx(center, 0.5, ArcOrientation.ArcClockwise);
      EllipticArcSegment cir = eabCir.ToSegment();
      var polyCir = PolygonBuilderEx.CreatePolygon(
                      PolylineBuilderEx.CreatePolyline(cir));
    
      //Set symbolology, create and add element to layout
      CIMTextSymbol sym = SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.GreenRGB, 10, "Arial", "Regular");
      string text = "Circle, circle, circle, circle, circle, circle, circle, circle, circle, circle, circle";
      //At 2.x - GraphicElement cirTxtElm = LayoutElementFactory.Instance.CreateCircleParagraphGraphicElement(layout, cir, text, sym);
      //         cirTxtElm.SetName("New Circle Text");
      GraphicElement cirTxtElm = ElementFactory.Instance.CreateTextGraphicElement(
                                    layout, TextType.CircleParagraph, polyCir, sym, text, "New Circle Text");
    
      //(Optionally) Modify paragraph border 
      CIMGraphic cirTxtGra = cirTxtElm.GetGraphic();
      CIMParagraphTextGraphic cimCirTxtGra = cirTxtGra as CIMParagraphTextGraphic;
      cimCirTxtGra.Frame.BorderSymbol = new CIMSymbolReference();
      cimCirTxtGra.Frame.BorderSymbol.Symbol = SymbolFactory.Instance.ConstructLineSymbol(ColorFactory.Instance.GreyRGB, 1.0, SimpleLineStyle.Solid);
      cirTxtElm.SetGraphic(cirTxtGra);
    });
    Create_EllipseText
    //Create ellipse paragraph text with basic text settings and optionally a modified border.
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Build geometry
      Coordinate2D center = new Coordinate2D(4.5, 2.75);
      //At 2.x - EllipticArcBuilder eabElp = new EllipticArcBuilder(center, 0, 1, 0.45, esriArcOrientation.esriArcClockwise);
      var eabElp = new EllipticArcBuilderEx(center, 0, 1, 0.45, ArcOrientation.ArcClockwise);
      EllipticArcSegment ellipse = eabElp.ToSegment();
      var polyElp = PolygonBuilderEx.CreatePolygon(
                      PolylineBuilderEx.CreatePolyline(ellipse));
    
      //Set symbolology, create and add element to layout
      CIMTextSymbol sym = SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.BlueRGB, 10, "Arial", "Regular");
      string text = "Ellipse, ellipse, ellipse, ellipse, ellipse, ellipse, ellipse, ellipse, ellipse, ellipse, ellipse, ellipse";
      //At 2.x - GraphicElement elpTxtElm = LayoutElementFactory.Instance.CreateEllipseParagraphGraphicElement(layout, ellipse, text, sym);
      //         elpTxtElm.SetName("New Ellipse Text");
      GraphicElement elpTxtElm = ElementFactory.Instance.CreateTextGraphicElement(
                                    layout, TextType.EllipseParagraph, polyElp, sym, text, "New Ellipse Text");
    
      //(Optionally) Modify paragraph border 
      CIMGraphic elpTxtGra = elpTxtElm.GetGraphic();
      CIMParagraphTextGraphic cimElpTxtGra = elpTxtGra as CIMParagraphTextGraphic;
      cimElpTxtGra.Frame.BorderSymbol = new CIMSymbolReference();
      cimElpTxtGra.Frame.BorderSymbol.Symbol = SymbolFactory.Instance.ConstructLineSymbol(ColorFactory.Instance.GreyRGB, 1.0, SimpleLineStyle.Solid);
      elpTxtElm.SetGraphic(elpTxtGra);
    });
    CIMGraphic
    //This example references a graphic element on a layout and sets its Transparency property (which is not available in the managed API)
    //by accessing the element's CIMGraphic.
    
    //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 GraphicElementExample1
    {
      public static Task<bool> UpdateElementTransparencyAsync(string LayoutName, string ElementName, int TransValue)
      {
        //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;
    
          //Modify the Transparency property that exists only in the CIMGraphic class.
          CIMGraphic CIMGra = graElm.GetGraphic() as CIMGraphic;
          CIMGra.Transparency = TransValue;             //e.g., TransValue = 50
          graElm.SetGraphic(CIMGra);
    
          return true;
        });
      }
    }
    GraphicElement_Get_And_SetGraphic
    //Note: must call within QueuedTask.Run() 
    var CIMGra = graElm.GetGraphic() as CIMGraphic;
    //TODO - make changes to CIMGraphic
    //...
    graElm.SetGraphic(CIMGra);
    Create Rectangle Paragraph Text Element 1
    //Create rectangle text with background and border symbology.  
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Build 2D polygon geometry
      List<Coordinate2D> plyCoords = new List<Coordinate2D>();
      plyCoords.Add(new Coordinate2D(3.5, 7));
      plyCoords.Add(new Coordinate2D(4.5, 7));
      plyCoords.Add(new Coordinate2D(4.5, 6.7));
      plyCoords.Add(new Coordinate2D(5.5, 6.7));
      plyCoords.Add(new Coordinate2D(5.5, 6.1));
      plyCoords.Add(new Coordinate2D(3.5, 6.1));
      //At 2.x - Polygon poly = PolygonBuilder.CreatePolygon(plyCoords);
      Polygon poly = PolygonBuilderEx.CreatePolygon(plyCoords);
    
      //Set symbolology, create and add element to layout
      //Also notice how formatting tags are using within the text string.
      CIMTextSymbol sym = SymbolFactory.Instance.ConstructTextSymbol(
                        ColorFactory.Instance.GreyRGB, 10, "Arial", "Regular");
      string text = "Some Text String that is really long and is " +
                    "<BOL>forced to wrap to other lines</BOL> so that " +
                    "we can see the effects." as String;
      //At 2.x - GraphicElement polyTxtElm =
      //           LayoutElementFactory.Instance.CreatePolygonParagraphGraphicElement(
      //                                      layout, poly, text, sym);
      //         polyTxtElm.SetName("New Polygon Text");
    
      GraphicElement polyTxtElm = ElementFactory.Instance.CreateTextGraphicElement(
        container, TextType.RectangleParagraph, poly, sym, text, "Polygon Paragraph");
    
      //(Optionally) Modify paragraph border 
      CIMGraphic polyTxtGra = polyTxtElm.GetGraphic();
      CIMParagraphTextGraphic cimPolyTxtGra = polyTxtGra as CIMParagraphTextGraphic;
      cimPolyTxtGra.Frame.BorderSymbol = new CIMSymbolReference();
      cimPolyTxtGra.Frame.BorderSymbol.Symbol =
                   SymbolFactory.Instance.ConstructLineSymbol(
                              ColorFactory.Instance.GreyRGB, 1.0, SimpleLineStyle.Solid);
      polyTxtElm.SetGraphic(polyTxtGra);
    });
    Create a new picture element with advanced symbol settings
    //Create a picture element and also set background and border symbology.
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Build 2D envelope geometry
      Coordinate2D pic_ll = new Coordinate2D(6, 1);
      Coordinate2D pic_ur = new Coordinate2D(8, 2);
      //At 2.x - Envelope env = EnvelopeBuilder.CreateEnvelope(pic_ll, pic_ur);
      Envelope env = EnvelopeBuilderEx.CreateEnvelope(pic_ll, pic_ur);
    
      //Create and add element to layout
      string picPath = @"C:\Temp\WhitePass.jpg";
      //At 2.x - GraphicElement picElm =
      //    LayoutElementFactory.Instance.CreatePictureGraphicElement(
      //                                             layout, env, picPath);
      //         picElm.SetName("New Picture");
      //
      GraphicElement picElm = ElementFactory.Instance.CreatePictureGraphicElement(
                                           layout, env, picPath, "New Picture");
    
      //(Optionally) Modify the border and shadow 
      CIMGraphic picGra = picElm.GetGraphic();
      CIMPictureGraphic cimPicGra = picGra as CIMPictureGraphic;
      cimPicGra.Frame.BorderSymbol = new CIMSymbolReference();
      cimPicGra.Frame.BorderSymbol.Symbol =
            SymbolFactory.Instance.ConstructLineSymbol(
                   ColorFactory.Instance.BlueRGB, 2.0, SimpleLineStyle.Solid);
    
      cimPicGra.Frame.ShadowSymbol = new CIMSymbolReference();
      cimPicGra.Frame.ShadowSymbol.Symbol =
                  SymbolFactory.Instance.ConstructPolygonSymbol(
                        ColorFactory.Instance.BlackRGB, SimpleFillStyle.Solid);
    
      //Update the element
      picElm.SetGraphic(picGra);
    });
    Update an elements transparency
    //Update an element's transparency using the CIM.
    
    //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 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.GetGraphic() as CIMGraphic;
          CIMGraphic.Transparency = 50; // mark it 50% transparent
          graphicElement.SetGraphic(CIMGraphic);
        }
      }
    });
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also