ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Core.Geometry Namespace / PolygonBuilderEx Class / CreatePolygon Method / CreatePolygon(IEnumerable<MapPoint>,AttributeFlags,SpatialReference) Method
Points to create the polygon. Line segments are created from the points.
AttributeFlags to set the HasZ, HasM and HasID attributes. If no attributes should be set, pass AttributeFlags.None. If all attributes should be set, pass AttributeFlags.AllAttributes. Use the bitwise OR operator to set two attributes. For example, to set HasZ = true and HasM = true, pass AttributeFlags.HasZ | AttributeFlags.HasM.
(Optional) The SpatialReference. The default value is null. The spatial reference of each point is ignored.
Example

In This Topic
    CreatePolygon(IEnumerable<MapPoint>,AttributeFlags,SpatialReference) Method
    In This Topic
    Convenience method to create a new instance of the Polygon class.
    Syntax
    Public Overloads Shared Function CreatePolygon( _
       ByVal points As IEnumerable(Of MapPoint), _
       ByVal attributes As AttributeFlags, _
       Optional ByVal spatialReference As SpatialReference _
    ) As Polygon

    Parameters

    points
    Points to create the polygon. Line segments are created from the points.
    attributes
    AttributeFlags to set the HasZ, HasM and HasID attributes. If no attributes should be set, pass AttributeFlags.None. If all attributes should be set, pass AttributeFlags.AllAttributes. Use the bitwise OR operator to set two attributes. For example, to set HasZ = true and HasM = true, pass AttributeFlags.HasZ | AttributeFlags.HasM.
    spatialReference
    (Optional) The SpatialReference. The default value is null. The spatial reference of each point is ignored.

    Return Value

    Exceptions
    ExceptionDescription
    points is null.
    Example
    Builder Properties
    {
      // list of points
      List<MapPoint> pointsForBuilder = new List<MapPoint>
    {
      MapPointBuilderEx.CreateMapPoint(0, 0, 2, 3, 1),
      MapPointBuilderEx.CreateMapPoint(1, 1, 5, 6),
      MapPointBuilderEx.CreateMapPoint(2, 1, 6),
      MapPointBuilderEx.CreateMapPoint(0, 0)
    };
    
      // will have attributes because it is created with convenience method
      Polyline polylineWithAttrs = PolylineBuilderEx.CreatePolyline(pointsForBuilder);
      bool hasZ = polylineWithAttrs.HasZ;          // hasZ = true
      bool hasM = polylineWithAttrs.HasM;          // hasM = true
      bool hasID = polylineWithAttrs.HasID;        // hasID = true
    
      // will not have attributes because it is specified as a parameter
      Polyline polylineWithoutAttrs =
        PolylineBuilderEx.CreatePolyline(pointsForBuilder, AttributeFlags.None);
      hasZ = polylineWithoutAttrs.HasZ;          // hasZ = false
      hasM = polylineWithoutAttrs.HasM;          // hasM = false
      hasID = polylineWithoutAttrs.HasID;        // hasID = false
    
      // will have attributes because it is created with convenience method
      Polygon polygonWithAttrs = PolygonBuilderEx.CreatePolygon(pointsForBuilder);
      hasZ = polygonWithAttrs.HasZ;               // hasZ = true
      hasM = polygonWithAttrs.HasM;               // hasM = true
      hasID = polygonWithAttrs.HasID;             // hasID = true
    
      // will not have attributes because it is specified as a parameter
      Polygon polygonWithoutAttrs =
            PolygonBuilderEx.CreatePolygon(pointsForBuilder, AttributeFlags.None);
      hasZ = polygonWithoutAttrs.HasZ;               // hasZ = false
      hasM = polygonWithoutAttrs.HasM;               // hasM = false
      hasID = polygonWithoutAttrs.HasID;             // hasID = false
    
      // will not have attributes because it is specified as a parameter
      PolylineBuilderEx polylineB =
                 new PolylineBuilderEx(pointsForBuilder, AttributeFlags.None);
      hasZ = polylineB.HasZ;                      // hasZ = false
      hasM = polylineB.HasM;                      // hasM = false
      hasID = polylineB.HasID;                    // hasID = false
    
      // will have attributes because it is passed an attributed polyline
      polylineB = new PolylineBuilderEx(polylineWithAttrs);
      hasZ = polylineB.HasZ;                      // hasZ = true
      hasM = polylineB.HasM;                      // hasM = true
      hasID = polylineB.HasID;                    // hasID = true
    
      // will not have attributes because it is passed a non-attributed polyline
      polylineB = new PolylineBuilderEx(polylineWithoutAttrs);
      hasZ = polylineB.HasZ;                      // hasZ = false
      hasM = polylineB.HasM;                      // hasM = false
      hasID = polylineB.HasID;                    // hasID = false
    
      // will not have attributes because it is specified as a parameter
      PolygonBuilderEx polygonB = new PolygonBuilderEx(pointsForBuilder, AttributeFlags.None);
      hasZ = polygonB.HasZ;                       // hasZ = false
      hasM = polygonB.HasM;                       // hasM = false
      hasID = polygonB.HasID;                     // hasID = false
    
      // will have attributes because it is passed an attributed polygon
      polygonB = new PolygonBuilderEx(polygonWithAttrs);
      hasZ = polygonB.HasZ;                       // hasZ = true
      hasM = polygonB.HasM;                       // hasM = true
      hasID = polygonB.HasID;                     // hasID = true
    
      // will not have attributes because it is passed a non-attributed polygon
      polygonB = new PolygonBuilderEx(polygonWithoutAttrs);
      hasZ = polygonB.HasZ;                       // hasZ = true
      hasM = polygonB.HasM;                       // hasM = true
      hasID = polygonB.HasID;                     // hasID = true
    }
    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