Assign objects to new layer using Rhinocommon (GHPython)

I’m feeling a bit foolish that I can’t figure this out. The example below is functioning as expected, BUT…for my own edification, I would like to do it without using rhinoscript.

In a ghpython component, I have a for loop that looks at the hatchtable of the active Rhino Doc, then for each item, it makes a corresponding layer, then it constructs the parts needed to make the hatch, creates the hatch, then assigns the objects to the corresponding layer. I couldn’t figure out how to assign the layer using RhinoCommon, so I punted and used rhinoscript. Can someone point me in the right direction for how to assign it using RhinoCommon? below is the relevant section: (outside of this part, there are additional bits that already check if the layer is there before creating it).

\\Requisite other bits before
RawHatchTable = sc.doc.ActiveDoc.HatchPatterns

        for h in RawHatchTable:
            if h.HasName == True:
                MakeLayer(h.Name)
                centerPT = Rhino.Geometry.Point3d(0,spacing,0)
                labelPT = Rhino.Geometry.Point3d(R+P2, spacing, 0)
                border = Rhino.Geometry.Circle(centerPT, R)
                b = Rhino.Geometry.Circle(centerPT, R).ToNurbsCurve()
                c = Rhino.Geometry.Hatch.Create(b, counter, 0, S)
                hatchObj = Rhino.RhinoDoc.ActiveDoc.Objects.AddHatch(c[0])
                crvObj = Rhino.RhinoDoc.ActiveDoc.Objects.AddCircle(border)
                txtObj = rs.AddText(h.Name, labelPT, 1.0)
                rs.ObjectLayer(hatchObj, h.Name)
                rs.ObjectLayer(crvObj, h.Name)
                rs.ObjectLayer(txtObj, h.Name)
                spacing += R*2+P1
                counter += 1
            else:
                pass
\\Requisite other bits after

so, I learned/re-learned about creating and assigning attributes today. For anyone else thinking about this…in this case, it makes much more sense to just use rhinoscriptsyntax., (rs.objectlayer() and rs.AddText(), etc… BUT, for the sake of completing this investigation, here is some (probably overly verbose) code that is RhinoCommon only. (From a GHPython component)

\\Requisite other bits before
RawHatchTable = sc.doc.ActiveDoc.HatchPatterns
for h in RawHatchTable:
        if h.HasName == True:
            MakeLayer(h.Name) # function to check if layer exists, if not, make the layer
            #create some objects required for Hatch/Curve/TextPostioning
            centerPT = Rhino.Geometry.Point3d(0,spacing,0)
            labelPT = Rhino.Geometry.Point3d(R+P2, spacing, 0)
            labelPL = Rhino.Geometry.Plane(labelPT, Rhino.Geometry.Vector3d(0,0,1))
            border = Rhino.Geometry.Circle(centerPT, R)
            b = Rhino.Geometry.Circle(centerPT, R).ToNurbsCurve() #convert to curve for use with Hatch
            c = Rhino.Geometry.Hatch.Create(b, counter, 0, S) #instantiate Hatch object
            
            layer_index = sc.doc.Layers.Find(h.Name, True) #get the  layer index from created layer
            attribs = sc.doc.CreateDefaultAttributes() #insantiate attributes object
            attribs.LayerIndex = layer_index # define layer index attribute
        
            text_entity = Rhino.Geometry.TextEntity() # instantiate textEntity object
            text_entity.Plane = labelPL
            text_entity.Text = h.Name
            text_entity.Justification = Rhino.Geometry.TextJustification.MiddleCenter
            text_entity.FontIndex = Rhino.RhinoDoc.ActiveDoc.Fonts.FindOrCreate("Arial", False, False)
       
            #add objects with attributes to Rhino Doc
            hatchObj = sc.doc.Objects.AddHatch(c[0], attribs)
            crvObj = sc.doc.Objects.AddCircle(border, attribs)
             txtObj = sc.doc.Objects.AddText(text_entity, attribs)
                
            #increment some counters for spacing/layout
            spacing += R*2+P1
            counter += 1
        else:
            pass
\\Requisite other bits after
1 Like