I am writing an addin, which needs to split a polyline into a number of segments taking the number of segments as an input from user. How can I accomplish this?
Answer
Use IGeometryBridge2.SplitAtDistances()
. Also see the documentation on the equivalent IPolycurve2.SplitAtDistances()
and the singular IPolycurve.SplitAtDistance()
methods for more explanation.
The IGeometryBridge2
version must be used in .NET.
Edit: This code works for me:
private static IEnumerable SplitPolylineIntoEqualSegments(IPolyline polyline, int numSegments)
{
var geombridge = (IGeometryBridge2)new GeometryEnvironmentClass();
var paths = (IGeometryCollection)polyline;
for (int i = 0; i < paths.GeometryCount; i++)
{
var path = (IPath)paths.get_Geometry(i);
var distances = new double[Math.Max(1, numSegments - 1)];
for (int j = 0; j < distances.Length; j++)
{
distances[j] = (j + 1.0) / numSegments;
}
var polyline2 = PathToPolyline(path);
geombridge.SplitAtDistances((IPolycurve2)polyline2, ref distances, true, true);
var splitpaths = (IGeometryCollection)polyline2;
for (int k = 0; k < splitpaths.GeometryCount; k++)
{
var splitpath = (IPath)splitpaths.get_Geometry(k);
yield return PathToPolyline(splitpath);
}
}
}
private static IPolyline PathToPolyline(IPath path)
{
var polyline = (IPolyline)new PolylineClass();
var geomcoll = (IGeometryCollection)polyline;
geomcoll.AddGeometry(path);
geomcoll.GeometriesChanged();
return polyline;
}
No comments:
Post a Comment