ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Core.Geometry Namespace / PolygonBuilderEx Class / CreatePolygon Method / CreatePolygon(SpatialReference) Method
(Optional) The SpatialReference. The default value is null.
Example

In This Topic
    CreatePolygon(SpatialReference) Method
    In This Topic
    Convenience method to create a new instance of the Polygon class.
    Syntax
    Public Overloads Shared Function CreatePolygon( _
       Optional ByVal spatialReference As SpatialReference _
    ) As Polygon
    public static Polygon CreatePolygon( 
       SpatialReference spatialReference
    )

    Parameters

    spatialReference
    (Optional) The SpatialReference. The default value is null.

    Return Value

    Example
    Create an N-sided regular polygon
    {
      // <summary>
      // Create an N-sided regular polygon.  A regular sided polygon is a polygon that is equiangular (all angles are equal in measure) 
      // and equilateral (all sides are equal in length).  See https://en.wikipedia.org/wiki/Regular_polygon
      // </summary>
      // <param name="numSides">The number of sides in the polygon.</param>
      // <param name="center">The center of the polygon.</param>
      // <param name="radius">The distance from the center of the polygon to a vertex.</param>
      // <param name="rotation">The rotation angle in radians of the start point of the polygon. The start point will be
      // rotated counterclockwise from the positive x-axis.</param>
      // <returns>N-sided regular polygon.</returns>
      // <exception cref="ArgumentException">Number of sides is less than 3.</exception>
      static Polygon CreateRegularPolygon(int numSides, Coordinate2D center, double radius, double rotation)
      {
        if (numSides < 3)
          throw new ArgumentException();
    
        Coordinate2D[] coords = new Coordinate2D[numSides + 1];
    
        double centerX = center.X;
        double centerY = center.Y;
        double x = radius * Math.Cos(rotation) + centerX;
        double y = radius * Math.Sin(rotation) + centerY;
        Coordinate2D start = new Coordinate2D(x, y);
        coords[0] = start;
    
        double da = 2 * Math.PI / numSides;
        for (int i = 1; i < numSides; i++)
        {
          x = radius * Math.Cos(i * da + rotation) + centerX;
          y = radius * Math.Sin(i * da + rotation) + centerY;
    
          coords[i] = new Coordinate2D(x, y);
        }
    
        coords[numSides] = start;
    
        return PolygonBuilderEx.CreatePolygon(coords);
      }
    }
    Get the individual parts of a multipart feature
    IEnumerable<Geometry> MultipartToSinglePart(Geometry inputGeometry)
    {
      // list holding the part(s) of the input geometry
      List<Geometry> singleParts = new List<Geometry>();
    
      // check if the input is a null pointer or if the geometry is empty
      if (inputGeometry == null || inputGeometry.IsEmpty)
        return singleParts;
    
      // based on the type of geometry, take the parts/points and add them individually into a list
      switch (inputGeometry.GeometryType)
      {
        case GeometryType.Envelope:
          singleParts.Add(inputGeometry.Clone() as Envelope);
          break;
        case GeometryType.Multipatch:
          singleParts.Add(inputGeometry.Clone() as Multipatch);
          break;
        case GeometryType.Multipoint:
          var multiPoint = inputGeometry as Multipoint;
    
          foreach (var point in multiPoint.Points)
          {
            // add each point of collection as a standalone point into the list
            singleParts.Add(point);
          }
          break;
        case GeometryType.Point:
          singleParts.Add(inputGeometry.Clone() as MapPoint);
          break;
        case GeometryType.Polygon:
          var polygonNew = inputGeometry as Polygon;
    
          foreach (var polygonPart in polygonNew.Parts)
          {
            // use the PolygonBuilderEx turning the segments into a standalone 
            // polygon instance
            singleParts.Add(PolygonBuilderEx.CreatePolygon(polygonPart));
          }
          break;
        case GeometryType.Polyline:
          var polylineNew = inputGeometry as Polyline;
    
          foreach (var polylinePart in polyline.Parts)
          {
            // use the PolylineBuilderEx turning the segments into a standalone
            // polyline instance
            singleParts.Add(PolylineBuilderEx.CreatePolyline(polylinePart));
          }
          break;
        case GeometryType.Unknown:
          break;
        default:
          break;
      }
      return singleParts;
    }
    Create Lasso Polygon, Freehand Element
    {
      //Note: Must be on QueuedTask.Run
    
      List<Coordinate2D> plyCoords = new List<Coordinate2D>();
      plyCoords.Add(new Coordinate2D(1, 1));
      plyCoords.Add(new Coordinate2D(1.25, 2));
      plyCoords.Add(new Coordinate2D(1.5, 1.1));
      plyCoords.Add(new Coordinate2D(1.75, 2));
      plyCoords.Add(new Coordinate2D(2, 1.1));
      plyCoords.Add(new Coordinate2D(2.25, 2));
      plyCoords.Add(new Coordinate2D(2.5, 1.1));
      plyCoords.Add(new Coordinate2D(2.75, 2));
      plyCoords.Add(new Coordinate2D(3, 1));
      Polygon poly = PolygonBuilderEx.CreatePolygon(plyCoords);
    
      //Set symbology, create and add element to layout
      CIMStroke outline = SymbolFactory.Instance.ConstructStroke(
                  ColorFactory.Instance.BlackRGB, 2.0, SimpleLineStyle.Solid);
      CIMPolygonSymbol polySym = SymbolFactory.Instance.ConstructPolygonSymbol(
               ColorFactory.Instance.RedRGB, SimpleFillStyle.ForwardDiagonal, outline);
    
      ElementFactory.Instance.CreateGraphicElement(
        layout, poly, polySym, "New Lasso");
    }
    Create Polygon Element
    {
      //Note: Must be on QueuedTask.Run
    
      //Build geometry
      List<Coordinate2D> plyCoords = new List<Coordinate2D>();
      plyCoords.Add(new Coordinate2D(1, 7));
      plyCoords.Add(new Coordinate2D(2, 7));
      plyCoords.Add(new Coordinate2D(2, 6.7));
      plyCoords.Add(new Coordinate2D(3, 6.7));
      plyCoords.Add(new Coordinate2D(3, 6.1));
      plyCoords.Add(new Coordinate2D(1, 6.1));
      Polygon poly = PolygonBuilderEx.CreatePolygon(plyCoords);
    
      //Set symbology, create and add element to layout
      CIMStroke outline = SymbolFactory.Instance.ConstructStroke(
        ColorFactory.Instance.BlueRGB, 2.0, SimpleLineStyle.DashDotDot);
      CIMPolygonSymbol polySym = SymbolFactory.Instance.ConstructPolygonSymbol(
        ColorFactory.Instance.RedRGB, SimpleFillStyle.ForwardDiagonal, outline);
    
      ElementFactory.Instance.CreateGraphicElement(
        layout, poly, polySym, "New Polygon", false);
    }
    Create Rectangle Paragraph Text Element 1
    {
      //Note: Must be on QueuedTask.Run
      //Create rectangle text with background and border symbology.  
      //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));
      Polygon poly = PolygonBuilderEx.CreatePolygon(plyCoords);
    
      //Set symbology, 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;
    
      GraphicElement polyTxtElm = ElementFactory.Instance.CreateTextGraphicElement(
        layout, 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);
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also