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.
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?
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.
So THAT is what that red dot is for… 10 years into scripting and I finally test out the debuggin… about time maybe? 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?
@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().