Rhino Python Editor Question

Slowly picking up RhinoPython, like again…Really appreciate for the help!
it is a basic question while trying to read the McNeel tutorial, about the “Conditional Evaluation - if”:

import rhinoscriptsyntax as rs
somenumber = rs.GetReal(“Line length”)
line = rs.AddLine( (0,0,0), (somenumber,0,0) )
if line is None:
print(“Something went wrong”)
else:
print(“Line curve inserted with id”, line)

Question: if I type in “0”, it will stop immediately after line 3 with the traceback “Unable to add line to document”,

ASK:
Is there a way for me to bypass this “null” data, and keep going to the “if” statement.

Thank you very much!
Cheers,
Ran

try this

import rhinoscriptsyntax as rs

somenumber = rs.GetReal("Line length")

p0 = rs.Rhino.Geometry.Point3d (0,0,0)
p1 = rs.Rhino.Geometry.Point3d (somenumber,0,0)
line_temp = rs.Rhino.Geometry.Line(p0, p1)
#line = rs.AddLine( (0,0,0), (somenumber,0,0) )


if line_temp.IsValid:
    line = rs.Rhino.RhinoDoc.ActiveDoc.Objects.AddLine(line_temp)
    print("Line curve inserted with id", line)
else:
    print("Something went wrong")

just FYI, aside from the functions found here python also binds to the c# rhinocommon

I would run it this way. The AddLine function is failing with a 0 length line. So this will protect against that.

import rhinoscriptsyntax as rs

somenumber = rs.GetReal("Line length")

if somenumber !=  0:
    line = rs.AddLine( (0,0,0), (somenumber,0,0) )
    print("Line curve inserted with id", line)
else:    
    print("Something went wrong")

Probably should mension that using rhinocommon allows a different logic, in that the line is created before adding it to the document. This is effective in grasshopper:

Hi all, you might prevent that a number smaller than the tolerance can be entered eg.

#! python 2

import rhinoscriptsyntax as rs

def DoSomething():
    
    tolerance = rs.UnitAbsoluteTolerance()
    somenumber = rs.GetReal("Line length", 10, minimum=tolerance)
    
    if somenumber is not None:
        print "You entered: {}".format(somenumber)
    
DoSomething()

_
c.

1 Like