Have a GUID, need to access the underlying object

rhinocommon
rhinocommon-v6

(Tom D.) #1

Hi all,

I’m feeling very silly, but I am in the process of learning RhinoCommon and I have a very basic question:

I have stored the reference to an object as a GUID. I now want to lookup that object to either get its properties, or modify its properties. How do I get a handle to that object in RhinoCommon? Any tips?

Thanks!

Tom


#2

You can use FinId method described here:
http://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_DocObjects_Tables_ObjectTable_FindId.htm


(Dale Fugier) #3

Hi @piroshki,

From a command, you can do something like this:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  Guid guid = <assign_your_guid_here>;
  RhinoObject rh_obj = doc.Objects.FindId(guid);
  if (null != rh_obj)
  {
    // TODO...
  }

  return Result.Success;
}

– Dale


(Tom D.) #4

Hi Dale,

Thanks for the response which makes perfect sense for a command. I was struggling to find a way to connect to the doc object outside of a RunCommand block.

I ended up with this in my plugin constructor:

RhinoDoc currentDoc = RhinoDoc.ActiveDoc;
myForm = new TD_PilotForm();
myForm.myModel.setDoc(currentDoc);

so that now within my model I have a reference to the current doc which I can use to read/write to. Hopefully that isn’t too bad an approach…

Cheers,

Tom.


(Dale Fugier) #5

Hi @piroshki,

Without more context, I cannot comment on your code. But keep mind that the document can change. So it often best not to hold onto it; obtaining as needed. Or, use some of the document event watchers so you know when the document has changed.

– Dale


(Tom D.) #6

Understood about the context.

The only way I have found to get a handle on the current document is by calling RhinoDoc.ActiveDoc.

I am happy to call this every time I need it, but there is a note in the help files saying this is not OK - that one should get a reference once and keep using it… This is what the help file says:

WARNING!! Do not use the ActiveDoc if you don’t have to. Under Mac Rhino the ActiveDoc can change while a command is running. Use the doc that is passed to you in your RunCommand function or continue to use the same doc after the first call to ActiveDoc.


(Tom D.) #7

Actually, I changed my code to:

    RhinoDoc currentDoc = RhinoDoc.ActiveDoc;
    myForm = new TD_PilotForm();
    myForm.myModel.setDoc(ref currentDoc);

so I am not creating a new copy, just passing a reference to the active doc. Maybe that makes more sense. In any case is seems to be working…