How do I get the current *GH_Document* object from inside a component?

Hi guys,

How do I get the current GH_Document object from inside a component?
I want to use GH_Document.FindComponent(Guid) to check if a component I’m interacting with is present, and to get a refercence to it.

Thanks,
Thomas

The current document, or the document that owns the component in question?

The current document (which may be null) can be accessed via the Canvas.

Grasshopper.Instances.ActiveCanvas.Document

Even the ActiveCanvas may be null in weird fringe cases.

The document which owns the component in question can be gotten via the OnPingDocument() method. Note that this may also return a null value as it is technically possible for a component to not belong to any document.

1 Like

Thanks! Both the searching and the searched component should be in the same document.
In that case, I guess that OnPingDocument() should will do it.