Use coerced objects in rhinoscriptsyntax functions

Hello,

is there a method to convert coerced objects to guid so that they can be used in rhinoscriptsyntax functions? Look on my code below (If I run the script I get an error "Parameter must be a Guid or string representing a Guid. It’s clear for me why.):

import rhinoscriptsyntax as rs
import Rhino as rh
import scriptcontext as sc


if C and S and D and N:
    curve = C
    surface = S
    distance = float(D)
    number = int(N)

    curve_coe = rs.coercecurve(curve)
    surface_coe = rs.coercesurface(surface)
    curve_ext = curve_coe.ExtendOnSurface(rh.Geometry.CurveEnd.Both, surface_coe)

    rs.PullCurve(surface_coe, curve_ext) #<= Error

I know it can be done easily with rhino common, but what is when I want to continue with rhinoscriptsyntax. Thx.

Dimitrij

Hi Dimitrij,

looking at the code for rs.PullCurve()

def PullCurve(surface, curve, delete_input=False):
    """Pulls a curve object to a surface object
    Parameters:
      surface (guid): the surface's identifier
      curve (guid): the curve's identifier
      delete_input (bool, optional) should the input items be deleted
    Returns:
      list(guid, ...): of new curves if successful
      None: on error
    Example:
      import rhinoscriptsyntax as rs
      curve = rs.GetObject("Select curve to pull", rs.filter.curve )
      surface = rs.GetObject("Select surface that pulls", rs.filter.surface )
      rs.PullCurve(surface, curve)
    See Also:
      IsSurface
    """
    crvobj = rhutil.coercerhinoobject(curve, True, True)
    brep = rhutil.coercebrep(surface, True)
    curve = rhutil.coercecurve(curve, -1, True)
    tol = scriptcontext.doc.ModelAbsoluteTolerance
    curves = Rhino.Geometry.Curve.PullToBrepFace(curve, brep.Faces[0], tol)
    rc = [scriptcontext.doc.Objects.AddCurve(curve) for curve in curves]
    if rc:
        if delete_input and crvobj:
            scriptcontext.doc.Objects.Delete(crvobj, True)
        scriptcontext.doc.Views.Redraw()
        return rc

The problem lies with the code expecting a GUID for the curve in

crvobj = rhutil.coercerhinoobject(curve, True, True)

You could strip the method and create your own:

def MyPullCurve(surface, curve):

    brep = surface.ToBrep()
    curve = rhutil.coercecurve(curve, -1, True)
    tol = scriptcontext.doc.ModelAbsoluteTolerance
    curves = Rhino.Geometry.Curve.PullToBrepFace(curve, brep.Faces[0], tol)
    return curves

You will need to import the scriptcontext module or hardcode the tolerance

Does this make sense?

Hi Willem,

thanks for the answer. It would be make sense if I need only one function. But if I want to use more that one function that it will be really tricky to rewrite each function, then I could also jump the the rhinocommon without wrapping it in a function. What I want to know, if it is possible to convert the curve guid in a object with “rs.coerce…()” why it is not possible to reverse this step back.

Dimitrij

Because the information is lost.
rs.coerce grabs just the geometry, the attributes are not passed along.

Thinking about it, you could replace the geometry from the original curve with the new one.
Check out scriptcontext.doc.Objects.Replace()

you can basically swap one geometry for another for an object.

does this make sense?

-Willem

Hi Willem,

perfect, it works, this is what I want. Many thanks.

Regard,

Dimitrij

1 Like