C# - close open curves




I’m trying to close open curves using C# like CloseCrv command in Rhino. To begin with, I’m not sure what the right method is in Rhino Common. It looks like Curve.MakeClosed method is the right one, but it only outputs boolean values, not closed curves. Am I looking at the wrong method? Without knowing it right, I started writing a code like this.
Please excuse my level of coding, if it has too many simple mistakes.
I searched other topics related with “close curves”, but I don’t understand how I actually get closed curves instead of just testing in other posts I found.

Dongyeopclose curves.gh (11.5 KB)

(Michael Pryor) #2

There is no method for close curve. You will have to make your own. Look at my plug-in Pufferfish which has quite an extensive close curve component.

If you want to do what Rhino does (or in the case of my components linear option) you should test if a curve is closeable. If it is not closed then get the end points, make a line, join the line to the open curve (or test for a tolerance, if end points are very close to each other then you move the endpoints to each other rather than make a short line segment)

The Curve.MakeClosed method just informs you if it is at all possible to close that curve. For instance, A line is not closed, but it is also not closeable.

(Tom) #3


are you talking about adding a new line segment to close the curve, or do you want to use exiting controlpoints and use them to close a curve?

“MakeClosed” modifies first and last cps to make a single curve closed, that’s why it doesn’t work for lines and nurbs with less then 3 points.

Edit: What Michael said

Here are both:

Closing.gh (8.6 KB)


Thank you Michael. It’s great that you have the close curve component in Pufferfish, which I discovered a few months ago and recommended it to be part of our company-wide Grasshopper installation.
It’d be nice, if you could share the coding for this component. If it can’t be shared, I’d understand. I’ve been making some user objects dependent on other components like your plugins, but I’m starting to look at a way to make components free of dependency issues. This is actually part of the consistent curve offset question you helped a lot, because I wanted to close curves to get the centroid of curves before offsetting curves.


Thank you so much! At first, I thought that I understood your codes, but I’m actually not clear when I tried to use them. The confusing part is that MakeClosed method makes boolean values, and I don’t know which part of your codes actually close curves. image Is this red circled part making curves closed?
Did this code make “crv” closed in the process of making boolean values?

(Michael Pryor) #6

because I wanted to close curves to get the centroid of curves before offsetting curves.

@TomTom code is more than sufficient for that (and much more elegant than mine would be for this purpose).

(Tom) #7

Rhinocommon often uses a rather old school design pattern. Most methods return if the command succeeded or not, instead using traditional exception handling. Usually this is used on low level functionality in order to eliminate overhead and to ensure the code keeps running.
I did the same, just encapsulated the method call with the check for the open curve. MakeClosed returns a Boolean so I return it as well. MakeClosed modifies the instance (crv). I didn’t had to use the ref keyword(without it would also work) but it hints you, that you not only pass the curve in as an argument, but it also gets modified in there, so you can use it further. For further understanding read more about the ref keyword in C#. A third alternative would have been using a Blendcurve. I think this is what Michael components does.

(Michael Pryor) #8

MakeClosed closes a curve if it is within tolerance by moving the endpoint to the start point. In your case you are better off using the first method of adding a line. MakeClosed is good when gaps are really small, however with large gaps/tolerances the shape of curve will change as you see in TomTom’s screenshot. I guess it will give you a less accurate area because the curve can become smaller in area.

(Michael Pryor) #9

Yes, some options use blend curves, another uses the line method, another uses average of the end points + set end point / set start point to be like make closed but not favor the start location.


Wow! This is another huge one for me to understand how to work with RhinoCommon. I wasn’t clear why MakeClosed method didn’t output closed curves, but made boolean values instead. Thanks a lot for your kind explanation.


Thank you for your help again!! It’s always helpful to get your detailed advice.