[csharp] Create 3d object set user text with object's guid as value

Bonus:
Here is how you can add an object to the rhinodoc using an guid of your choice via
ObjectAttributes.ObjectId.

import Rhino
import System
import scriptcontext as sc

def addGuidFieldToUserText():
    
    # any line, doesn't matter
    line = Rhino.Geometry.Line(Rhino.Geometry.Point3d.Origin, Rhino.Geometry.Point3d(20, 20, 0))
    
    # create new guid
    id = System.Guid.NewGuid()
    
    # create new attributes
    attributes = Rhino.DocObjects.ObjectAttributes()
    
    # set your user string to the object attributes
    attributes.SetUserString("ID", str(id))
    
    # set the guid to add with to the attributes
    attributes.ObjectId = id
    
    # add to doc, guid is returned
    sc.doc.Objects.AddLine(line, attributes)
    
    # redraw
    sc.doc.Views.Redraw()
    
    
if __name__ == "__main__":
    addGuidFieldToUserText()
1 Like