Hi, can someone please help me in writing python code to divide a curve length by ratio? I want to divide a line and have points at 10%, 25%, 25%, 30%, and 10% of the total length.
Hello - here is a quick test - is that what you need?
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
def test():
while True:
pct = 10
if sc.sticky.has_key('DIV_PCT'):
pct = sc.sticky['DIV_PCT']
go = Rhino.Input.Custom.GetObject()
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
go.SubObjectSelect=True
opPct = Rhino.Input.Custom.OptionDouble(pct,.01, 100)
go.AddOptionDouble('Percent',opPct)
go.AcceptNumber(True, False)
rc = go.GetMultiple(1,0)
if go.CommandResult()!=Rhino.Commands.Result.Success:
return go.CommandResult()
if rc==Rhino.Input.GetResult.Object:
ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)]
objs = [go.Object(i) for i in range(go.ObjectCount)]
crvs = [obj.Curve() for obj in objs]
break
elif rc==Rhino.Input.GetResult.Option:
pct = opPct.CurrentValue
sc.sticky['DIV_PCT'] = pct
continue
elif rc==Rhino.Input.GetResult.Number:
pct = go.Number()
if pct > 100:
print 'Enter a number less than 100'
continue
sc.sticky['DIV_PCT'] = pct
continue
div = pct/100
for crv in crvs:
length = crv.GetLength()
pars = crv.DivideByLength(length*div, False)
pts = [crv.PointAt(par) for par in pars]
rs.AddPoints(pts)
test()
-Pascal
Thanks Pascal, for your quick response. Your code generated something that looks like image 1, I want to be able to generate something that looks like image 2.
Can you please help me with that?
Thanks,
Maryam
Here is a quick sample, Try this:
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
crvid = rs.GetObjects("Select Curve", rs.filter.curve, False, False, None, False)
crv = rs.coercecurve(crvid, -1, False)
length = crv.GetLength()
pct = 0
while pct < 1:
input = rs.GetInteger("Enter Percentage Number", None, 0, 100)
div = input/100 + pct
divide = crv.DivideByLength(length*div, False)
pt = crv.PointAtLength(divide[0])
rs.AddPoint(pt)
pct = div
Select Curve and then enter percentage u need (10, 25, 25, 30), it will add the points!