The following code shows how to build a polygon using a collection of points. This approach is preferred when the user has a sequence of vertices as input. The resulting polygon ring will only contain a straight line segment:
[VB.NET]
Sub CreatePolygonByPoints()
'Build polygon from a sequence of points. 'At ArcGIS 9.2, the recommended way to add arrays of points to a geometry is to use'the IGeometryBridge2 interface on the GeometryEnvironment singleton object.Dim pGeoBrg As ESRI.ArcGIS.Geometry.IGeometryBridge2
pGeoBrg = New ESRI.ArcGIS.Geometry.GeometryEnvironment
Dim pPointColl As ESRI.ArcGIS.Geometry.IPointCollection4
pPointColl = New ESRI.ArcGIS.Geometry.Polygon
'Set pPointColl.SpatialReference = 'Define the spatial reference of the new polygonDim aWKSPointBuffer() As ESRI.ArcGIS.esriSystem.WKSPoint
Dim cPoints AsLong
cPoints = 4 'The number of points in the first partReDim aWKSPointBuffer(0 To cPoints - 1)
'aWKSPointBuffer = 'Read cPoints into the point buffer
pGeoBrg.SetWKSPoints(pPointColl, aWKSPointBuffer)
'pPointColl has now been definedEndSub
Building a polygon using segments
In this code example, the multipart polygon is built segment–by–segment. It gives the most control if you are using advanced construction techniques or curved segments (circular arcs, Bèzier curves, and so on). See the following:
[VB.NET]
Sub CreatePolygonBySegments()
'Build a polygon segment-by-segment.Dim pSegPoly As ESRI.ArcGIS.Geometry.IPolygon
pSegPoly = New ESRI.ArcGIS.Geometry.Polygon
'Set pSegPoly.SpatialReference = 'Always define the spatial reference of new top-level geometries.'Create the segments and rings. If this were a single-part polygon, segments can be'added directly to the polygon and the ring created internally.'You cannot reuse the same ring object. Also, when rings are added to the polygon,'the polygon takes ownership of the rings. You cannot reuse a ring for building another polygon.'These same restrictions also apply to segments.Dim cArc As ESRI.ArcGIS.Geometry.ICircularArc
cArc = New ESRI.ArcGIS.Geometry.CircularArc
Dim bCur As ESRI.ArcGIS.Geometry.IBezierCurve
bCur = New ESRI.ArcGIS.Geometry.BezierCurve
Dim ring1 As ESRI.ArcGIS.Geometry.ISegmentCollection, ring2 As ESRI.ArcGIS.Geometry.ISegmentCollection
ring1 = New ESRI.ArcGIS.Geometry.Ring
ring2 = New ESRI.ArcGIS.Geometry.Ring
ring1.AddSegment(cArc)
ring2.AddSegment(bCur)
Dim pGeoColl As ESRI.ArcGIS.Geometry.IGeometryCollection
pGeoColl = pSegPoly
pGeoColl.AddGeometry(ring1)
pGeoColl.AddGeometry(ring2)
'At this point, a _shell_geometry has been constructed. The _shell_geometry consists of'one polygon containing two rings; each containing one segment.'However, the coordinates of those segments have not been defined.'Because references to those segments exist, their coordinates can be 'defined now.Dim pPnt As ESRI.ArcGIS.Geometry.IPoint
pPnt = New ESRI.ArcGIS.Geometry.Point
pPnt.X = -10
pPnt.Y = 0
cArc.PutCoordsByAngle(pPnt, 0, 2 * 3.14159265358979, 10.0#)
Dim pntArray(0 To 3) As ESRI.ArcGIS.Geometry.IPoint
Dim i AsLongFor i = 0 To 3
pntArray(i) = New ESRI.ArcGIS.Geometry.Point
Next i
pntArray(0).X = 10
pntArray(0).Y = 0
pntArray(1).X = 10
pntArray(1).Y = 10
pntArray(2).X = 20
pntArray(2).Y = 10
pntArray(3).X = 10
pntArray(3).Y = 0
bCur.PutCoords(4, pntArray(0))
'pPolygon has now been defined. When changing segment coordinates directly'like this, be careful to let the top-level geometry know that'things have changed underneath it so that it can delete any cached properties'that it maintains, such as envelope, length, area, and so on.'When you use certain methods on the top-level geometry implementation'of IGeometryCollection interface, like AddGeometry, it automatically'invalidates any cached properties.
pGeoColl.GeometriesChanged()
EndSub
Creating a polygon using existing geometries
A polygon can be created based on a topological relationship between existing geometries. In the following code example, a polygon is generated by uniting two existing polygons:
[VB.NET]
Sub CreatePolygonFromExistingGeometries(ByRef pPolygon1 As ESRI.ArcGIS.Geometry.IPolygon, ByRef pPolygon2 As ESRI.ArcGIS.Geometry.IPolygon)
'Build a new polygon by uniting two existing polygons.Dim pTopoOp2 As ESRI.ArcGIS.Geometry.ITopologicalOperator2
pTopoOp2 = pPolygon1
'Simplify.
pTopoOp2.IsKnownSimple_2 = False
pTopoOp2.Simplify()
Dim pPoly As ESRI.ArcGIS.Geometry.IPolygon
pPoly = pTopoOp2.Union(pPolygon2)
'pPoly is the new generated polygon.EndSub