# How to iterate through each vertex in a line?

I am working with ArcObjects in building functionality for a toolbar inside of ArcMap. Once I start an edit session, I use the tools to manually draw a line. I am trying to loop through this line get (or display in a message box) each point in the line. For ease of use, I was trying to do this on a simple button click from my toolbar. I've been researching and thought of using the IPointCollection Interface but I'm not completely sure if this will work or be helpful in accomplishing my goal. I am looking more so for a starting point to this.

• could you convert the geometry to WKT then an array. Then iterate over the points in the array? Commented Mar 29, 2016 at 22:28

You can use IPointCollection this way:

``````public void IterateVerticesIPointCollection(IPolyline line)
{
// Cast the polyline to IPointCollection
IPointCollection pointColl = (IPointCollection)line;
// Iterate the array...
for (int i = 0; i < pointColl.PointCount; i++)
{
IPoint point = pointColl.get_Point(i);
// ...and do something with each vertex
}
}
``````

I think IPointCollection can be enough for you, but if you have to read large polylines or do some heavy process it's better to use IPointCollection4, it's a lot faster:

``````public void IterateVerticesIPointCollection4(IPolyline line)
{
// Cast the polyline to IPointCollection4
IPointCollection4 pointColl = (IPointCollection4)line;
// Create an array to store the vertices
WKSPointZ[] vertices = new WKSPointZ[pointColl.PointCount];
// Store the vertices in the array
pointColl.QueryWKSPointZs(0, pointColl.PointCount, out vertices[0]);
// Now we can iterate the array...
for (int i = 0; i < vertices.Length; i++)
{
// ...and do something with each vertex
}
}
``````

And I almost forgot that your polylines could have more than one part, in this case is better to iterate each part and then use IPointCollection/IPointCollection4 to do something with each vertex.

``````IGeometryCollection geometryCollection = (IGeometryCollection)polyline;
for (int i = 0; i < geometryCollection.GeometryCount; i++)
{
IPointCollection pointCollection = (IPointCollection)geometryCollection.get_Geometry(i);

}
``````
• I appreciate your response. I will give this a shot and see what happens. I'm curious about the actual Polyline that has been drawn inside of ArcMap though. Won't I have to grab this line somehow from ArcMap to be passed into the function? Commented Mar 30, 2016 at 14:18