Simplify Polylines - tool

Hi guys, since “simplify” often leaves me with curves that still are too complex I made a Simplify Polylines script based on points distance to new line segments. So it samples all points within tolerance against the new polyline segments and adds the points that deviates.

It’s pretty cool and pretty fast so shout out if you want to test it out and I’ll PM it when ready for testing.
Then I’ll make a plugin of it and put it on the packagemanager once it has been tested and refined if needed.

3 Likes

Isn’t that what this does?

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Polyline_ReduceSegments.htm

1 Like

Hey, I’ll test it out!
Is it a Rhino command for that too?

It will be cool to see how they compare.

Anyway, here’s another real life test:

Good for 1:200 prints

Thanks for the heads up Mitch!

I did a comparison and the results are close!
(except that the built in function is MUCH faster… :smiley: ) )

Here’s the script for the built in function:

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc
def runScript():
    crv_ids = rs.GetObjects("Polylines to make simple",rs.filter.curve, preselect=True)
    if not crv_ids: return
    rs.UnselectAllObjects()
    tolerance = 0.1
    tolerance = rs.RealBox("Tolerance?",tolerance,title="Simplify polyline", minimum=0.001)
    if not tolerance: return
    rs.EnableRedraw(False)
    crvTol=0.001
    angleTol=0.1
    for crv_id in crv_ids:
        crv=rs.coercecurve(crv_id)
        crv2=crv.ToPolyline(crvTol, angleTol, 0, 0)
        crv3 = crv2.ToPolyline()
        crv3.ReduceSegments(tolerance)
        try:
            rs.SelectObject(sc.doc.Objects.AddPolyline(crv3))
        except:
            pass
    rs.EnableRedraw(True)
runScript()

I updated it to work on curves too, not only polylines.
@Helvetosaur do you know why I have to run ToPolyline to times and make new curves of the results?

I expected this to work but it didn’t:

crv=rs.coercecurve(crv_id)
crv.ToPolyline(crvTol, angleTol, 0, 0)
crv.ReduceSegments(tolerance)
1 Like

Hi Jorgen - The first run of ToPolyline( with stuff in here) makes a PolylineCurve, that is, still part of the Curve uber category and, I think, not doing anything for you here if the input is a polyline to begin with; the second one, with no arguments, spits out the polyline object, which is not much more than just a list of points really, that the ReduceSegments() needs as input.

You could just add the reduced polyline to the doc via sc.doc.Objects.AddPolyline(crv2) or replace the input with sc.doc.Objects.Replace(crv_id, crv2)

(assumes you’ve skipped the first ToPolyline() and are on crv2)

-Pascal

So THAT is what that red dot is for… :open_mouth: 10 years into scripting and I finally test out the debuggin… about time maybe? :smiley: That opens up a door for me (embarrassing to say, and I guess the price to pay for never taking any classes or reading any books on the matter)

Thanks! Having to run ToPolyline twice makes a bit more sense now. But I don’t fully understand the difference between a PolyLineCurve and a PolyLine I must say. When would I need a polylinecurve instead?

Hi Jørgen

In my understanding :
PolylineCurve derives from the Curve class:

PolyCurve Class (rhino3d.com)

While Polyline derives from the Point3dList:

Polyline Class (rhino3d.com)

… thus you can use a PolylineCurve (but not a Polyline) anywhere a Curve object is needed.

The same goes for ArcCurve and LineCurve.

@Holo - Hi Jorgen - I do not think, in your example, you need to run it twice - the first one is ‘wasted’ if the input is already a polyline curve, you can go straight to the second one - if you accept non-polylines as input, then I guess the first one makes sense.

You can filter the selection for polylines only with a ‘custom_filter=’ in rs.GetObject().

-Pascal

1 Like

You can also use the simpler Curve.TryGetPolyline method to get the underlying Polyline:

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Curve_TryGetPolyline.htm

Note that it returns two items when you implement it in Python, a Boolean the tells you if the cast worked, and the Polyline.

2 Likes