Script issue with getting GUID

I have this script that I’ve hacked together. The scirpt creates a planar surface from either open or closed shapes, including surface edges and curves. It even works on multiple open or closed shapes at once. It works great, unless I try to use it on what seems like the simplest case - a single closed planar polyline (or polycurve.) This is because I’m having trouble getting the GUID if there’s only one object selected by the user. If there is more than one object selected, then when I use rs.joincurves, the output is GUID, but I can’t use rs.JoinCurves on a single item without getting an error
single closed planar polyline.3dm (33.8 KB)
. How to fix? See the line where I comment ‘Problem is here’.

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs


def GetCurvesAndEdges():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select planar curves and edges to build surface.")
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
    go.EnablePreSelect(False, True)
    go.SubObjectSelect = True
    rc = go.GetMultiple(1, 0)
    if rc != Rhino.Input.GetResult.Object: return 
    
    results = [obj_ref.Curve().ToNurbsCurve() for obj_ref in go.Objects()]

    curves = []
    objcount = 0
    Cap = 0

    for obj in results:
        objcount = objcount + 1
        curve = rs.coercecurve(obj)
        if curve:
            curves.append(curve)

    # If there are no curves, return
    if not curves:
        print("No valid curves selected.")
        return

    # Create a joined polyline from the selected curves
    if (objcount > 1):
        polyline = rs.JoinCurves(curves)
        rs.SelectObjects(polyline)
    else:
        # problem is here. If only one closed polyline or polycurve is selected, then I can't get the guid and future steps fail.
        polyline = curves
        

    rs.EnableRedraw(False)

    for strNewCurve in polyline:
        if rs.IsCurvePlanar(strNewCurve) and not(rs.IsLine(strNewCurve)):
            if rs.IsCurveClosed(strNewCurve):
                arrToCap=strNewCurve
                rs.AddPlanarSrf (arrToCap)
                Cap = Cap + 1
                rs.DeleteObject (strNewCurve)
            else:
                arrObjectsToJoin=[]
                arrStartPoint = rs.CurveStartPoint(strNewCurve)
                arrEndPoint = rs.CurveEndPoint(strNewCurve)
                LineR = rs.AddLine(arrStartPoint, arrEndPoint)
                arrObjectsToJoin.append(LineR)
                arrObjectsToJoin.append(strNewCurve)
                arrJoinedCurves = rs.JoinCurves(arrObjectsToJoin)
                rs.AddPlanarSrf(arrJoinedCurves)
                Cap = Cap + 1
                rs.DeleteObjects (arrJoinedCurves)	
                rs.DeleteObject (LineR)
                if (objcount > 1): rs.DeleteObject (strNewCurve)
        else:
            if (objcount > 1): rs.DeleteObject (strNewCurve)

    rs.EnableRedraw(True)

    if Cap == 0:
        print ('No surfaces created.')
    elif Cap == 1:
        print ('One surface created.')	
    else:
        print (str(Cap) + ' planar surfaces were created.')


GetCurvesAndEdges()

Hi @phcreates, that is because your curves and edge-curves in the results variable are virtual objects. in order to use the curves (which are in the list results) together with rs methods, they must be added to the document first. So you have an object id to work with. You can do it like below and directly get the object id’s of the added curve objects:

results = [obj_ref.Curve().ToNurbsCurve() for obj_ref in go.Objects()]
if not results: return
    
# add results to the document so they can be used with rs methods
crv_ids = [scriptcontext.doc.Objects.AddCurve(crv) for crv in results]

print crv_ids

btw. to get the amount of curves in the list crv_ids, instead of counting you can just query len(crv_ids) or crv_ids.Count:

print "{} curves added to the document".format(len(crv_ids))

To access only a single curve and get it from the list just access crv_ids[0]

_
c.