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;
}
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
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()
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()
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.