Collapse / Dissolve crv shorter than "X"

Hello everyone :slight_smile:

I have a closed curve and I would like to automatically dissolve all crvs if they are shorter than “X”.
This is my attempt but I got stuck. Can someone help me? Is my attempt even a good approach?

My idea is:

to get the vertices of the all crvs
replace the vertices of the short crv(s) with a single midpoint
connect all vertices / points to a closed crv again

But I dont know how to remove the endpoints of the short crv from an overall list of all vertices of the closed crv.
I’ll send the file if you want to take a look for yourself.

Dissolve crv shorter than x.3dm (60.1 KB)
Dissolve Crv.gh (7.7 KB)

Thank you for helping!

If they are polylines (straight segments) you can do this:


Dissolve Crv_2024Feb8a.gh (5.0 KB)

Note that one point disappears with ‘Tolerance’=0 as a result of using Discontinuity. Wait, that middle point was created using MidPt :exclamation: :roll_eyes:

‘Leave One’ instead of ‘Average’ on CullPt gives a different result.

3 Likes

Assuming polylines, their underlying RhinoCommon class actually has two methods that does exactly this, which are not exposed in Grasshopper (see documentation here and here):


240208_RemoveSmallPolylineSegments_00.gh (7.2 KB)

3 Likes

Seems like you have an old version of Rhino / Grasshopper / or Python because I am getting an error:
Note that I am using rhino 8 but I tried it with my rhino 7 version and still got the same error.
Do you know a solution? I am not very familiar with python :confused:

After I opened your gh file:

In Rhino 8 (but got same error in Rhino 7):

Why does it say ‘PolilineCurve’ if I target a ‘Polyline’?

I’m on the latest Rhino 7 Service Release (7.37.24004.15001, 2024-01-04). What is the error you’re getting?

Based on your bottom screenshot, it looks like you need to set the Polyline input parameter type hint to Polyline. Here’s how with the Rhino 7 GHPython component:

I believe it’s roughly the same with the new Rhino 8 script editor, but you might also have to run it in IronPython mode. That said, you can also just keep using GHPython, if you come across issues with the new editor (there is quite a bit of early day jank/issues).

Edit: Just had a look in Rhino 8 and it appears this is another case where CPython-RhinoCommon interop appears borked (i.e. a Polyline type is passed as list):


240208_RemoveSmallPolylineSegments_Rhino8_00.gh (14.2 KB)

Edit 2: I’m guessing this casting bug has to with this (Ping @eirannejad)

1 Like

There is a difference between Polyline and PolylineCurve, see Polyline versus PolylineCurve - Rhino Developer - McNeel Forum

Here is a version for py3 in Rhino 8 using the code below.
2024-02-09_collapse_polyline_py.gh (7.9 KB)

import Rhino.Geometry as rg
polyline = rg.Polyline(polyline)
polyline.CollapseShortSegments(tolerance)

a = polyline.ToPolylineCurve()

the script node casts a polyline to a list of Point3d, which is why you’ll need to create a Polyline first in order to run the code. Might be a bug.

EDIT: On a side note: Personally I’m not a fan of using upper case variables as that can lead to bugs, especially when calling them the same as classes from libraries used. In this case e.g. a from Rhino.Geometry import Polyline will cause problems.

2 Likes

Thank you for the code! The result is almost perfect. See below:


Why is this happening? Any idea?

My guess is the curve is not closed, hard to tell without the file.

1 Like

Two of the curves in the Rhino file (first post in this thread) are closed, the third is separate segments.

P.S. Oops, sorry, just realized that it’s a different polyline that failed. ALWAYS best to post code :exclamation:

Ah thanks. I exploded and rejoined them - that fixed the problem :smiley: