I have a script that I want to analyse the length of segments in a polyline and add points so that no segment of the line is longer than the desired maximum length. I have it working fine with one line but not the other and have no idea why one is working and not the other
HELP_Segment_length_add_null_pixels.gh (15.3 KB)
I’m a dilettante with Python so don’t understand yet why this is broken - perhaps this doesn’t help at all?
Starting at line 13:
subsegments = []
points = rs.DivideCurveLength(segment, subsegment_length)
print type(points)
if type(points) == 'list':
for i in range(len(points) - 1):
subsegment = rs.AddLine(points[i], points[i + 1])
subsegments.append(subsegment)
modified_segments.extend(subsegments)
P.S. Hey, this looks OK? I’m not sure I understand it yet… Added an extra “else” statement.
Starting at line 7:
for segment in segments:
segment_length = rs.CurveLength(segment)
if segment_length > max_length:
num_subsegments = int(segment_length / max_length)
subsegment_length = segment_length / num_subsegments
print 'segment_length=', segment_length
print 'num_subsegments=', num_subsegments
print 'subsegment_length=', subsegment_length
subsegments = []
points = rs.DivideCurveLength(segment, subsegment_length)
print type(points)
if type(points) == 'list':
for i in range(len(points) - 1):
subsegment = rs.AddLine(points[i], points[i + 1])
subsegments.append(subsegment)
modified_segments.extend(subsegments)
else:
modified_segments.append(segment)
else:
modified_segments.append(segment)
Hi Joseph. Thanks so much for your help.
The script now seems to be running but not dividing the longer segments into shorter segments as per the script that is working
Yeah, I can see that. Adding this statement at the end, just before ‘rs.JoinCurves(modified_segments)’, shows the same number of segments as the input curve(s). 
print 'modified_segments=', len(modified_segments)
Aha! Perseverance furthers. Python dilettante!! This:
if type(points) == list:
Not this:
if type(points) == 'list':
Also changed from this:
num_subsegments = int(segment_length / max_length)
to this:
num_subsegments = round(segment_length / max_length)`
(‘round()’ instead of ‘int()’, though I’m not sure anymore what is correct?)
null_pixels_2023Jun7a.gh (19.1 KB)
And stopped ignoring ‘max_length’ slider.
1 Like
Hi Joseph,
Amazing!!! Thank you!
this works perfectly! I really appreciate your help.
Sam
I’m not sure it “works perfectly”? Needs a complete review because some of what I did might be wrong. (like ‘round()’ should be ‘ceiling()’?)
Would be interesting to do the same thing with standard components, without Python…
Well in any case I appreciate your help. Thanks Again
Here is the same thing with standard components, without Python:
null_pixels_2023Jun8a.gh (25.2 KB)
Mostly the same as Python with a few extra points - I was right about “‘round()’ should be ‘ceiling()’” so I added that to the Python ( math.ceil()
) which makes the points match.
There is still an error in the Python though, as it returns three polylines instead of two? I’ll leave that for you to fix.
Hi Joseph, You really have gone above and beyond with your help. Amazing + All the best