Rhino7 GHPython Runtime Error: Unable to add line

If you use rhinocommon in Grasshopper, instead of rhinoscriptsyntax, you can forget about coercion issues, geometry getting falsely baked to Rhino, switching contexts, etc.
Simply look how simple and elegant @AndersDeleuran has solved this!

It really isn’t much more difficult than using rhinoscriptsyntax, and you can do exactly the same, since rhinoscriptsyntax only wraps rhinocommon in a pseudo-userfriendly fashion.
You can look at each of its functions like this, if you want to retrace how something is done, that you don’t know how to:

import rhinoscriptsyntax as rs
import inspect

print inspect.getsource(rs.AddLine)

This great functionality was pointed out by @Dancergraham a while back.
It outputs the source code of the rs.AddLine, like so:

def AddLine(start, end):
    """Adds a line curve to the current model.
    Parameters:
      start, end (point|guid) end points of the line
    Returns:
      guid: id of the new curve object
    Example:
      import rhinoscriptsyntax as rs
      start = rs.GetPoint("Start of line")
      if start:
          end = rs.GetPoint("End of line")
          if end: rs.AddLine(start, end)
    See Also:
      CurveEndPoint
      CurveStartPoint
      IsLine
    """
    start = rhutil.coerce3dpoint(start, True)
    end = rhutil.coerce3dpoint(end, True)
    rc = scriptcontext.doc.Objects.AddLine(start, end)
    if rc==System.Guid.Empty: raise Exception("Unable to add line to document")
    scriptcontext.doc.Views.Redraw()
    return rc

You can clearly see how many hoops it has to jump through to just to produce the geometry in Rhino, which you don’t even need at this moment or ever in Grasshopper (at least if you stick to the usual baking workflow). This also means a worse performance than using rhinocommon, with which you can abstract and simplify things much more.

import Rhino.Geometry as rg

pt1 = rg.Point3d(0, 1, 0)
pt2 = rg.Point3d(0, 1, 0)
line = rg.Line(pt1, pt2)

if not line.IsValid:
    raise RuntimeError("This is not a valid line")

# Output
a = rg.LineCurve(line)

The entire API is pretty well documented here.

4 Likes