GH Python - How to get out result from function?


I try to create a simple script to implement Rhino’s CloseCurve function in GH.

The curve stays open.

one attempt:

import rhinoscriptsyntax as rs

print oCrv
print tol

#ooCrv = rs.coercecurve(oCrv)
#print ooCrv
if not rs.IsCurveClosed(oCrv) and rs.IsCurveClosable(oCrv):
    ooCrv = oCrv
    cCrv = rs.CloseCurve( ooCrv, tolerance=tol )
    print cCrv

    ccCrv = oCrv

#cCrv = rs.CloseCurve( ooCrv, tolerance=tol )

#cCrv = ooCrv
print cCrv

another attempt:

import rhinoscriptsyntax as rs

print oCrv
print tol

ooCrv = rs.coercecurve(oCrv)
print ooCrv
def CloCrv(oCrv): 
    if not rs.IsCurveClosed(ooCrv) and rs.IsCurveClosable(ooCrv):
        rs.CloseCurve( ooCrv, tolerance=tol )
#        ooCrv = oCrv
#        cCrv = rs.CloseCurve( ooCrv, tolerance=tol )
#        print cCrv

cCrv = (CloCrv(oCrv))
print cCrv

As you can see I already played around some unsuccesfully.
How can I get out the result: a closed Curve?

Kind regards

Several issues

  • you coerce the input parameter to a curve, but then use that as argument to rhinoscriptsyntax methods. Those methods take the GUID of the curve instead of the actual curve
  • You use negative tolerance
  • You need to give the tolerance also to IsClosable, otherwise the document tolerance or zero tolerance is used. That will give False for curves with a wid gap (like the 5.0 you have).
  • In your first sample you typo cCrv as ccCrv in the else-block. This typo makes you end up with None/null, since the IsClosable test fails.

Here my version with direct curve usage:

import rhinoscriptsyntax as rs

ooCrv = rs.coercecurve(oCrv)

if not ooCrv.IsClosed  and ooCrv.IsClosable(tol):
    if ooCrv.MakeClosed(tol):
        cCrv = ooCrv
    cCrv = oCrv

Wow, that came fast!
Thank you very much jesterKing.

My mistake was not following the explanations for the subfunctions in

However, I would prefer this (now working) solution:

import rhinoscriptsyntax as rs

if not rs.IsCurveClosed(oCrv) and rs.IsCurveClosable(oCrv,tol):
    cCrv = rs.CloseCurve( oCrv, tol )
    cCrv = oCrv

For learning purposes I have one question left:

Where did you get “…MakeClosed…” from?

I looked it up from the RhinoCommon SDK API documentation here:

This function is used by the rs.CloseCurve(crvid,tol) function under the hood to do its thing.

1 Like