This document is archived and information here might be outdated.  Recommended version.


How to create a polyline (ArcObjects .NET 10.5 SDK)

How to create a polyline


Summary
A polyline can be built in several ways depending on user input. This topic shows the most commonly used approaches to efficiently create a polyline.

In this topic


Building a polyline using points

The following code example shows how to build a polyline using a collection of points. This approach is preferred when you have a sequence of vertices (points) as input. The resulting polyline path will only contain straight line segments.
[VB.NET]
Sub CreatePolylineByPoints()
    
    'Build a polyline from a sequence of vertices (points).
    Add arrays of points To a geometry using the IGeometryBridge2 interface On the
    'GeometryEnvironment singleton object.
    Dim pGeoBrg As ESRI.ArcGIS.Geometry.IGeometryBridge2
    pGeoBrg=New ESRI.ArcGIS.Geometry.GeometryEnvironment
    
    'pPointColl is the new polyline.
    Dim pPointColl As ESRI.ArcGIS.Geometry.IPointCollection4
    pPointColl=New ESRI.ArcGIS.Geometry.Polyline
    
    'TODO:
    'Set pPointColl.SpatialReference='Define the spatial reference of the new polyline.
    
    Dim aWKSPointBuffer() As ESRI.ArcGIS.esriSystem.WKSPoint
    Dim cPoints As Long
    cPoints=4 'The number of points in the first part.
    ReDim aWKSPointBuffer(0 To cPoints - 1)
    
    'TODO:
    'aWKSPointBuffer='Read cPoints into the point buffer.
    
    pGeoBrg.SetWKSPoints(pPointColl, aWKSPointBuffer)
    'pPointColl has now been defined.
    
End Sub
[C#]
void CreatePolylineByPoints()
{
    //Build a polyline from a sequence of vertices (points). 
    //Add arrays of points to a geometry using the IGeometryBridge2 interface on the 
    //GeometryEnvironment singleton object.
    ESRI.ArcGIS.Geometry.IGeometryBridge2 pGeoBrg=new
        ESRI.ArcGIS.Geometry.GeometryEnvironment()as IGeometryBridge2;

    //pPointColl is the new polyline.
    ESRI.ArcGIS.Geometry.IPointCollection4 pPointColl=new
        ESRI.ArcGIS.Geometry.PolylineClass();

    //TODO:
    //Set pPointColl.SpatialReference='Define the spatial reference of the new polyline.

    long cPoints=4; //The number of points in the first part.
    ESRI.ArcGIS.esriSystem.WKSPoint[] aWKSPointBuffer=new
        ESRI.ArcGIS.esriSystem.WKSPoint[cPoints];

    //TODO:
    //aWKSPointBuffer='Read cPoints into the point buffer.

    pGeoBrg.SetWKSPoints(pPointColl, ref aWKSPointBuffer);
    //pPointColl has now been defined.
}

Building a polyline using segments

In the following code example, the multipart polyline is built segment-by-segment. This approach gives you the most control if you're using advanced construction techniques or curved segments (circular arcs, Bézier curves, and so on).
[VB.NET]
Sub CreatePolylineBySegments()
    
    'Build a polyline segment-by-segment.
    Dim pSegPoly As ESRI.ArcGIS.Geometry.IPolyline
    pSegPoly=New ESRI.ArcGIS.Geometry.Polyline
    'Set pSegPoly.SpatialReference='Always define the spatial reference of new top-level geometries.
    
    'Create the segments and paths. If this was a single-part polyline, you could add
    'segments directly to the polyline.
    'You cannot reuse the same path object. Also, when paths are added to the polyline, the
    'polyline takes ownership of the paths. You cannot reuse a path to build another polyline.
    'These 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 Line As ESRI.ArcGIS.Geometry.ILine
    Line=New ESRI.ArcGIS.Geometry.Line
    Dim path1 As ESRI.ArcGIS.Geometry.ISegmentCollection, path2 As ESRI.ArcGIS.Geometry.ISegmentCollection
    path1=New ESRI.ArcGIS.Geometry.Path
    path2=New ESRI.ArcGIS.Geometry.Path
    'The first part of the polyline contains a half circle arc segment.
    path1.AddSegment(cArc)
    'The second part of the polyline contains a Bézier curve segment and a line segment.
    path2.AddSegment(bCur)
    path2.AddSegment(Line)
    Dim pGeoColl As ESRI.ArcGIS.Geometry.IGeometryCollection
    pGeoColl=pSegPoly
    pGeoColl.AddGeometry(path1)
    pGeoColl.AddGeometry(path2)
    
    'At this point, a _shell_ geometry has been constructed. It consists of one
    'polyline containing two paths, each containing one segment.
    'However, the coordinates of those segments have not been defined.
    'Because you still have references to those segments, you can define their coordinates now.
    Dim pPnt As ESRI.ArcGIS.Geometry.IPoint
    pPnt=New ESRI.ArcGIS.Geometry.Point
    pPnt.X=-10
    pPnt.Y=0
    cArc.PutCoordsByAngle(pPnt, 0, 3.14159265358979, 10.0#) 'A half circle arc.
    Dim pntArray(0 To 3) As ESRI.ArcGIS.Geometry.IPoint
    Dim i As Long
    For 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=20
    pntArray(3).Y=0
    bCur.PutCoords(4, pntArray(0))
    Line.FromPoint.PutCoords(20, 0)
    Line.ToPoint.PutCoords(30, 0)
    
    'pPolyline has now been defined. When changing segment coordinates directly in this way,
    'be sure 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 the
    'IGeometryCollection interface, such as AddGeometry, it automatically invalidates any
    'cached properties.
    pGeoColl.GeometriesChanged()
    
End Sub
[C#]
void CreatePolylineBySegments()
{
    //Build a polyline segment-by-segment.
    ESRI.ArcGIS.Geometry.IPolyline pSegPoly=new ESRI.ArcGIS.Geometry.PolylineClass
        ();
    //Set pSegPoly.SpatialReference='Always define the spatial reference of new top-level geometries.
    //Create the segments and paths. If this was a single-part polyline, you could add
    //segments directly to the polyline.
    //You cannot reuse the same path object. Also, when paths are added to the polyline, the 
    //polyline takes ownership of the paths. You cannot reuse a path to build another polyline.
    //These restrictions also apply to segments.
    ESRI.ArcGIS.Geometry.ICircularArc cArc=new
        ESRI.ArcGIS.Geometry.CircularArcClass();
    ESRI.ArcGIS.Geometry.IBezierCurve bCur=new
        ESRI.ArcGIS.Geometry.BezierCurveClass();
    ESRI.ArcGIS.Geometry.ILine line=new ESRI.ArcGIS.Geometry.LineClass();
    ESRI.ArcGIS.Geometry.ISegmentCollection path1=new
        ESRI.ArcGIS.Geometry.PathClass();
    ESRI.ArcGIS.Geometry.ISegmentCollection path2=new
        ESRI.ArcGIS.Geometry.PathClass();
    object obj=Type.Missing;
    //The first part of the polyline contains a half circle arc segment.
    path1.AddSegment((ISegment)cArc, ref obj, ref obj);
    //The second part of the polyline contains a Bézier curve segment and a line segment.
    path2.AddSegment((ISegment)bCur, ref obj, ref obj);
    path2.AddSegment((ISegment)line, ref obj, ref obj);
    ESRI.ArcGIS.Geometry.IGeometryCollection pGeoColl=pSegPoly as
        IGeometryCollection;
    pGeoColl.AddGeometry((IGeometry)path1, ref obj, ref obj);
    pGeoColl.AddGeometry((IGeometry)path2, ref obj, ref obj);
    //At this point, a _shell_ geometry has been constructed. It consists of one
    //polyline containing two paths, each containing one segment.
    //However, the coordinates of those segments have not been defined.
    //Because you still have references to those segments, you can define their coordinates now.
    ESRI.ArcGIS.Geometry.IPoint pPnt=new ESRI.ArcGIS.Geometry.PointClass();
    pPnt.X= - 10;
    pPnt.Y=0;
    cArc.PutCoordsByAngle(pPnt, 0, 3.14159265358979, 10); //A half circle arc.
    ESRI.ArcGIS.Geometry.IPoint[] pntArray=new IPoint[4];
    for (int i=0; i <= 3; i++)
        pntArray[i]=new ESRI.ArcGIS.Geometry.PointClass();
    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=20;
    pntArray[3].Y=0;
    bCur.PutCoords(4, ref pntArray[0]);
    line.FromPoint.PutCoords(20, 0);
    line.ToPoint.PutCoords(30, 0);
    //pPolyline has now been defined. When changing segment coordinates directly in this way, 
    //be sure 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 the 
    //IGeometryCollection interface, such as AddGeometry, it automatically invalidates any 
    //cached properties.
    pGeoColl.GeometriesChanged();
}

Creating a polyline using existing geometries

A polyline can be created based on a topological relationship between existing geometries. In the following code example, a polyline is generated by intersecting two existing polygons:
[VB.NET]
Sub CreatePolylineFromExistingGeometries(ByRef pPolygon1 As ESRI.ArcGIS.Geometry.IPolygon, ByRef pPolygon2 As ESRI.ArcGIS.Geometry.IPolygon)
    
    'Build a new polyline by intersecting 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.IPolyline
    pPoly=pTopoOp2.Intersect(pPolygon2, ESRI.ArcGIS.Geometry.esriGeometryDimension.esriGeometry1Dimension)
    'pPoly is the new generated polyline.
    
End Sub
[C#]
void CreatePolylineFromExistingGeometries(ESRI.ArcGIS.Geometry.IPolygon pPolygon1,
    ESRI.ArcGIS.Geometry.IPolygon pPolygon2)
{
    //Build a new polyline by intersecting two existing polygons.
    ESRI.ArcGIS.Geometry.ITopologicalOperator2 pTopoOp2=pPolygon1 as
        ITopologicalOperator2;
    //Simplify.
    pTopoOp2.IsKnownSimple_2=false;
    pTopoOp2.Simplify();

    ESRI.ArcGIS.Geometry.IPolyline pPoly=pTopoOp2.Intersect(pPolygon2,
        ESRI.ArcGIS.Geometry.esriGeometryDimension.esriGeometry1Dimension)as
        IPolyline;
    //pPoly is the new generated polyline.
}