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

How can I create an object (say a line) and after adding it to the document create a user text with value = lines’s guid?

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // TODO: start here modifying the behaviour of your command.
            // ---
            RhinoApp.WriteLine("The {0} command will add a line right now.", EnglishName);

            Point3d pt0;
            using (GetPoint getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt("Please select the start point");
                if (getPointAction.Get() != GetResult.Point)
                {
                    RhinoApp.WriteLine("No start point was selected.");
                    return getPointAction.CommandResult();
                }
                pt0 = getPointAction.Point();
            }

            Point3d pt1;
            using (GetPoint getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt("Please select the end point");
                getPointAction.SetBasePoint(pt0, true);
                getPointAction.DynamicDraw +=
                  (sender, e) => e.Display.DrawLine(pt0, e.CurrentPoint, System.Drawing.Color.DarkRed);
                if (getPointAction.Get() != GetResult.Point)
                {
                    RhinoApp.WriteLine("No end point was selected.");
                    return getPointAction.CommandResult();
                }
                pt1 = getPointAction.Point();
            }
            // creating the line from point0 and point1
            Rhino.Geometry.Line line1 = new Line(pt0, pt1);
            
            
            // adding the line to the document
            doc.Objects.AddLine(line1);
            
            // This fails :) the python approach
            Guid line_id = doc.Objects.AddLine(line1);
            RhinoApp.WriteLine(line_id);
            
            doc.Views.Redraw();
            RhinoApp.WriteLine("The {0} command added one line to the document.", EnglishName);

            // ---
            RhinoApp.WriteLine("Line length is: ", pt0.DistanceTo(pt1).ToString());
            RhinoApp.WriteLine("The distance between the two points is {0} {1}.", line1.Length, doc.ModelUnitSystem.ToString().ToLower());
            
            return Result.Success;
        }

RhinoApp.Wrtieline(line_id.ToString());

should work.

What IDE are you using? VS will get you a detailed error message if you try your approach.

1 Like

Awesome.

Thanks @rgr.

Now I have to figure out how to add the user text, because I didn’t see it in Rhino.Geometry.Line :thinking:

Get the object, get the attributes(or att them when adding the object to the doc) and just add the user text.

can this be done?

https://developer.rhino3d.com/api/RhinoCommon/html/Overload_Rhino_DocObjects_Tables_ObjectTable_AddLine.htm

your first port of call should always be the documentation.

1 Like

I was testing SharpDevelop, unfortunately this PC is with German interface, so none of the errors is worth reading :smiley:

:thinking:, if I add the properties while adding the line to the document, would I have the guid of the line at that time?

Still need some help:

//doc.Objects.AddLine(line1);
            Rhino.DocObjects.ObjectAttributes line_attributes = new Rhino.DocObjects.ObjectAttributes();
            line_attributes.SetUserString("ID","ID should go here");
            // print UUID
            Guid line_id = doc.Objects.AddLine(line1,line_attributes);
            String id_value = line_id.ToString();
            RhinoApp.WriteLine(id_value);
            
            doc.Views.Redraw();
            
            // adding the usertext now
            Rhino.DocObjects.ObjRef objref = line_id;
            
            rhino_object = objref.Object();
            
            rhino_object.Attributes.SetUserString("ID",id_value);

This results in:

The error message is pretty clear: You are trying to assign a guid object to a variable declared of type ObjRef. I’m surprised the compiler did even let you do that…
I’ll show you below a python smaple close to your way of doing this right now, it’s all Rhinocommon

I did not want to compile a C# plugin for this, so i will leave the translation to you :slight_smile:

import Rhino
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))
    
    # add to doc, guid is returned
    id = sc.doc.Objects.AddLine(line)
    
    # create objRef from guid
    objRef = Rhino.DocObjects.ObjRef(id)
    
    # get rhinoObject from objRef
    obj = objRef.Object()
    
    # set your user string to the object attributes
    obj.Attributes.SetUserString("ID", str(id))
    
    # very important: commit changes to the object in the rhinodoc
    obj.CommitChanges()
    
    # redraw
    sc.doc.Views.Redraw()
    
    
if __name__ == "__main__":
    addGuidFieldToUserText()
1 Like

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

Hi @lando.schumpich,

Thanks for the examples. I was looking for a csharp example but maybe this is even better. I can understand it better and it resembles the csharp code a lot.
As it appears using rhinoscriptsyntax messed up my understanding of rhinocommon usage to a certain extent.

For what its worth, if you use Visual Studio and install the english language pack, all error messages will be in english, with I think the sole exception of a null item but that’s still easy to see in autos or when hovering over the item.

1 Like

problem is I cannot install VS on my work PC.