6

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?

2 Answers 2

8

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<IPolyline> 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;
}
7
  • :: Thanks.. for the Code... But I am getting an Enumeration of polylines from this, How can I convert this into a Feature layer containing all these Polylines Thanks in advance Commented Feb 18, 2013 at 6:44
  • @AvinashNirankari do you want to create a feature layer that will contain all these polylines?
    – Emi
    Commented Feb 18, 2013 at 7:07
  • @Emi :: Yes... Actually I want to use InterpolateShape tool on all of these so I need one layer containing all these polylines.... Commented Feb 18, 2013 at 9:39
  • Do you know LINQ? You can work with IEnumerable<T>'s in any way that you need. I purposely made the function generic so you (and other readers) could adapt it to your needs.
    – blah238
    Commented Feb 18, 2013 at 9:59
  • So what is your problem now? creating a polyline fature layer?
    – Emi
    Commented Feb 18, 2013 at 13:02
4

This answer is not actually the answer of the actual question. It's the answer of your comment. Because of the code snippet, I wrote it as a answer:

void AddFeatures(List<IPolyline> polylines, IFeatureClass featureClass)
{
    if (featureClass == null)
        return;
    foreach (var polyline in polylines)
    {
        IFeature feature = featureClass.CreateFeature();
        feature.Shape = polyline;

        feature.Store();
    }
    _activeView.Refresh();
}
4
  • :: The code you have given is not giving any errors but it is not giving the correct results.... It is adding the only the first segment not the whole line.... Commented Feb 19, 2013 at 12:56
  • Are you sure about all lines are polylines? because it works for me correctly
    – Emi
    Commented Feb 19, 2013 at 14:15
  • check these things at debug mode: 1. count of polylines > 1; 2. x,y value of the from point and to point of 1st polyline and other polylines are different.
    – Emi
    Commented Feb 19, 2013 at 14:23
  • :: Oh sorry..!! I was doing it the wrong way at first.... It worked... Thanks a Lot for your help..... Commented Feb 20, 2013 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.