Hi , I am trying to create a small script to scale a curve till the curve length reaches a limit. I tried writing a gh python code, I am not able to understand Coerce 3d Point issue . Kindly help me with that Code . this is the code I have written .
import rhinoscriptsyntax as rs
create a while loop for scaling a curve till it reaches the limit .
Also the length of a curve is directly proportional to its size.
You can scale uniformly a curve by TargetLength / CurveLength and it will reach target length in 1 step, no loop needed.
Instead of using rhinoscriptsyntax, like you do, I use the API, which is a better fit for GHPython, and you don’t have to deal with coercing issues. These mostly stem from Grasshopper expecting geometry, but rhinoscriptsyntax mostly returning GUIDs, which are object IDs of sorts.
import Rhino.Geometry as rg
def scale_curve(curve, max_growth, step=0.01):
if not curve.IsClosed:
return
crv_length = curve.GetLength()
max_length = crv_length + crv_length / 100 * max_growth
if crv_length >= max_length:
return [curve]
new_curves = []
while crv_length < max_length:
if step == 0:
break
curve.Scale(1.0 + step)
new_curves.append(curve.DuplicateCurve())
crv_length = curve.GetLength()
return new_curves
if __name__ == "__main__":
a = scale_curve(Curve, 15.0, Step)
I basically recreated your process, but integrated some exit strategies for the while loop.
Generally, these must be pretty watertight, otherwise you’ll end up with an infinite loop that will crash Rhino.
I compute the maximum curve length from a growth percentage, instead of using a static maximum length. This way you don’t have to check the initial curve length each time to define a higher value as maximum length. This is especially practical, if you want to apply the scaling to multiple curves, instead of just one!