Twice as many PolyCurve segments as expected (18.3 KB)

Long time Grasshopper user, total C# / RhinoCommon noob:

I am trying to explode a polyline from grasshopper using C# and RhinoCommon. Each segment is one half of what I expected to get. By checking PolyCurve.SegmentCount I can see that the issue is with how the PolyCurve is instantiated, and not related to PolyCurve.Explode(). I have tried simplifying the initial Curve as well, but to no avail.

File is attached and code is below. Any advice would be greatly appreciated.

PolyCurve iPolyCurve = x.ToArcsAndLines(0.001, 1, 0.01, 1000000); //convert to PolyCurve to explode
B = iPolyCurve.SegmentCount;

Curve segments = iPolyCurve.Explode();

List edges = new List(); // iterate over Curve to create generic list for Grasshopper
for (int i = 0; i < segments.Length; i++)

A = edges;

No need for your loop and in your new list you didn’t declare the type < Curve >.

Curve segments = iPolyCurve.Explode();
List < Curve > edges = segments.ToList();
A = edges;

However you could just output segments. It is ok to output an array.


Thanks so much for your comment! I have a couple of questions about your suggestions, but my main concern is the original issue that the segments are half the size of what I would expect (see the attached file). If I explode a rectangle in Grasshopper it would (obviously) have four edges, but my RhinoCommon method splits each of those edges at the midpoint and returns a total of 8 edges…

Do you have any insight into that issue? I really appreciate your help.

When you convert to Polycurve you are creating a curve which has more segments based on the .ToArcsAndLines method. For this curve you can simply do the following:

A = x.DuplicateSegments(); (9.3 KB)

Wow… That is so crazy frustrating… I can’t tell you how long I got stuck trying to convert between different types of curves to access different methods. I should have started in with RhinoCommon a long time ago.

Thanks so much for your help Michael!

If you have an actual Polyline (i.e. not a PolylineCurve or PolyCurve) you can/should call GetSegments on it. You can cast by setting the input typehint (this is Python, but it’s the same in C#):

Or in the code itself: (16.8 KB)

PS. you can syntax lightlight your code here on Discourse using markdown.

1 Like

Thanks for the tip Anders. Much appreciated.

1 Like

Sure, I just wasn’t sure it would always be a polyline.


What would be the should reason if it is always polyline? I assume it is just a faster method if we know the curve is a polyline? Is the actual output ever any different in any case?

I was just referring to the best practice of using the least complex (and most appropriate) datatype to represent something. I’d imagine that in this case implementing Polyline instead of PolylineCurve could lead to performance benefits on large enough cases.

Yes, they return different types:

Which again, may or may not be an issue :slight_smile:

1 Like