Ghpython recursively split curve while keeping splits and removing curves

ghPythonRecursiveCurveSplit.gh (6.9 KB) I have a python script component that recursively splits a curve until the resulting curve segments are below a minimum limit. I am so close but am not sure how to take the final step. What I have now keeps all line segments. What I want is to only keep the last segments of each recursion, if that makes sense.

Normalize the Curve.Domain to 0 to 1.0.
Split an curve at a given parameter.
For each of the resulting curves,
if each is longer than the minimum,
remove the curve from the list and split it at the parameter.
add results to list of curves to be split
if each is shorter than the minimum, add them to the final result and do not add them to the list to be split.

import rhinoscriptsyntax as rs
import  Rhino.Geometry as rg
import ghpythonlib.treehelpers as th 

def splitCurveByRatio(Curve,Ratio, Limit, outputCurves):
    crv = rs.coercecurve(Curve,1)
    if crv in outputCurves:
        print("Removing")
        outputCurves.remove(crv)
    crv.Domain = rg.Interval(0.0, 1.0)
    cL = rs.CurveLength(crv)
    newCrvs = rs.SplitCurve(crv, Ratio, delete_input=False)
    #print splitCurves
    if newCrvs:
        for i in newCrvs:
            if rs.CurveLength(i) >= Limit:
                #print rs.CurveLength(i)
                outputCurves.append(i)
                splitCurveByRatio(i, Ratio, Limit, outputCurves)
            else:
                
                return

if r > 1.0:
    r = 1.0/r
outputList = []
splitCurveByRatio(C,r,L,outputList)

a little bit of a re-write but i guess this does what you need

def splitCurveByRatio(Curve,Ratio, Limit, outputCurves):
    crv = rs.coercecurve(Curve,1)
    crv.Domain = rg.Interval(0.0, 1.0)
    if crv.GetLength() >= Limit:
        newCrvs = rs.SplitCurve(crv, Ratio, delete_input=False)
        if rs.CurveLength(newCrvs[0])<Limit and rs.CurveLength(newCrvs[1])<Limit:
            outputCurves.append(crv)
            return
        if rs.CurveLength(newCrvs[0])>=Limit:
            splitCurveByRatio(newCrvs[0], Ratio, Limit, outputCurves)
        if rs.CurveLength(newCrvs[1])>=Limit:
            splitCurveByRatio(newCrvs[1], Ratio, Limit, outputCurves)
1 Like