Can I get a RhinoObject from a Geometry?

For example, is it possible to get a RhinoObject from Rhino.Geometry.Curve in the C# grasshopper custom component.

No. You can create a RhinoObject with that geometry. What are you trying to do?

Thank you for your reply.

I am trying to create a component that takes a Curve and a string like in the image and sets an attribute user text.
Even though SetUserString(“Message”, text) returns true, when I select the geometry in Rhino I find that no attribute user text is defined.
I am thinking of using Rhino.DocObject.Attributes.SetUserString() instead of the SetUserString().
That’s why I want to get the RhonoObject that Curve belongs to, but I may have misunderstood how Grasshopper works somehow.

A geometry can exist without being associated with a RhinoObject. This is how Grasshopper creates and uses geometry. That geometry can have user strings defined.

You can also create a RhinoObject and assign it attribute user strings which is what shows up in the Rhino UI. Note that the geometry also has the user strings set, but there isn’t a UI in Rhino to show it. You can see it if you run the GetUserText command. That will show you both the “object” (geometry) user strings and the “attribute” (RhinoObject) user strings.

This gh example shows both.
userText.gh (9.9 KB)

1 Like

Thanks for the example.

I see, I can create a new geometry as shown in the example.

I want to set an attribute user text on an existing geometry. How should I handle this case?

In this case you need to get the RhinoObject, set the Attribute User String, and Commit the changes.

In this example I pass the Rhino Object’s ID to the script component, search the Doc’s object table with that ID. Once I have the object I go to its attributes and SetUserString(). Finally, I call CommitChanges() to update the object.

attributeTextRhinoObject.gh (7.7 KB)

2 Likes

Luis,

I see, I should have got the RhonoObject as GUID.

In my case, I implemented the following as a custom component in Windows.
I was able to achieve the process I was aiming for.

Your comment was very helpful. Thank you very much.
Yuichi

protected override void RegisterInputParams(GH_InputParamManager pManager)
{
    pManager.AddGenericParameter("guid", "guid", "guid", GH_ParamAccess.item);
    pManager.AddTextParameter("text", "text", "text", GH_ParamAccess.item);
}

protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
}

protected override void SolveInstance(IGH_DataAccess DA)
{
    Guid guid = Guid.Empty;
    string text = string.Empty;

    if (!DA.GetData(0, ref guid)) return;
    if (!DA.GetData(1, ref text)) return;

    if (guid == Guid.Empty) return;
    if (string.IsNullOrEmpty(text)) return;
    
    var doc = Rhino.RhinoDoc.ActiveDoc;
    var ro = doc.Objects.FindId(guid);

    ro.Attributes.SetUserString("Message", text);
    ro.CommitChanges();
}

2 Likes