Understanding GUIDs in Rhino Objects

It can sometimes be unclear how Global Unique Identifiers (Guids) are associated with objects in Rhino, particularly when working with the RhinoCommon API. This article clarifies the relationship between RhinoObjects, their attributes, and GUIDs, offering a comprehensive guide for developers.

Clarifying RhinoObject and GUIDs

In Rhino, a RhinoObject is a runtime object. This means it exists while Rhino is running and primarily tracks dynamic data such as selection status. It’s crucial to understand that a RhinoObject itself does not inherently possess a GUID.

Instead, the GUID for a RhinoObject is stored within its associated ObjectAttributes. Therefore, when you access RhinoObject.Id, you are effectively retrieving the ObjectId from its ObjectAttributes through RhinoObject.Attributes.ObjectId. You can use either method to obtain the GUID, depending on your coding preference for verbosity.

This distinction is important to grasp: the geometry of a Rhino object is stored directly within the RhinoObject. To access this geometry, you must first retrieve the RhinoObject and then query it for its geometric representation. There is no alternative pathway to obtain the geometry without referencing the RhinoObject.

Exploring ObjRef and Object Selection

The ObjRef (Object Reference) acts as a pointer to a RhinoObject. While ObjRef instances can be created in various ways, they are most commonly generated during object selection operations, such as when using GetObject.

An ObjRef encapsulates detailed information about the object selection process. This includes the referenced RhinoObject, the selected geometry (or sub-object geometry), the precise pick location, and other relevant selection data.

Consider the example of selecting a curve in Rhino using the following code snippet:

var go = new GetObject();
go.SetCommandPrompt("Select curve");
go.GeometryFilter = ObjectType.Curve;
go.SubObjectSelect = false;
go.Get(); // get one object

Upon successful selection, an ObjRef is created and can be accessed via the GetObject instance:

var objRef = go.Object(0); // Get the one object

If your primary goal is to retrieve the selected curve geometry, you can directly utilize the ObjRef to obtain the Curve object:

var curve = objRef.Curve();

Behind the scenes, the ObjRef.Curve() method performs the following actions:

var rhinoObject = objRef.Object();
if (null != rhinoObject && rhinoObject is CurveObject curveObject)
    return curveObject.CurveGeometry();
else
    return null;

This demonstrates how the curve geometry is ultimately extracted from the associated RhinoObject.

Alternatively, you can also retrieve the selected Curve geometry using a slightly different approach:

var rhinoObject = objRef.Object();
if (null != rhinoObject && null != rhinoObject.Geometry)
    return rhinoObject.Geometry as Curve;
return null;

These examples illustrate multiple methods for accessing the Curve geometry, providing flexibility depending on your specific needs and coding style.

To retrieve the GUID of the selected Rhino object through the ObjRef, you can use:

var objectId = objRef.ObjectId;

Internally, objRef.ObjectId executes the following:

var rhinoObject = objRef.Object();
if (null != rhinoObject)
    return rhinoObject.Attributes.ObjectId;
else
    return Guid.Empty;

Again, this highlights the consistent theme: accessing the GUID ultimately involves referencing the RhinoObject and its ObjectAttributes.

Conclusion

Understanding the distinction between RhinoObjects and ObjectAttributes, and the role of ObjRefs, is crucial for effectively working with GUIDs and geometry in the RhinoCommon API. This guide has demonstrated that while RhinoObjects manage runtime data and contain geometry, GUIDs are properties of the ObjectAttributes. ObjRefs serve as valuable tools for accessing both the RhinoObject and its associated information during selection operations. By grasping these relationships, developers can confidently manipulate and retrieve object data within the Rhino environment.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *