Grasshopper

algorithmic modeling for Rhino

Hello,

Can anyone point me in the right direction regarding the method that I should use to detect when a user changes the canvas focus from one document to another?

In particular I wish to detect when the focus is not set to the document that contains a custom component. I'd like the custom component to know when its parent canvas is active or not active because I am linking it to a some code that I need to explicitly control. It's some display code.

I am guessing that I need to know the relevant event.

A small sample of code would be appreciated if possible.

Views: 906

Replies to This Discussion

The GH_Canvas has a DocumentChanged event that tells you both the document that was loaded before and the one that got loaded just now.

David,

I'm having some difficulty. For some reason I cannot produce a method that I can subscribe to that refers to DocumentChanged.

As an example I am using methods like this (below) to trigger processes. Is it possible to use this form with document changed - if not what form should I be using?

private void DocumentRemoved(GH_DocumentServer sender, GH_Document doc)
{
//Code
}

via

Grasshopper.Instances.DocumentServer.DocumentRemoved += DocumentRemoved;

The event tells you the signature you need to use to handle it. It should look like:

void DocumentChanged(GH_Canvas sender, GH_CanvasDocumentChangedEventArgs e)

{

  // Your code here...

}

David,

Understood, I should have been more specific. My problem seems to sit in the namespace with neither GH_Canvas or GH_CanvasDocumentChangedEventArgs being recognized either when I make the method or when I try to make an event to subscribe to (no recognizable tab complete for example) what am I doing wrong?

Those classes are defined in Grasshopper.GUI.Canvas

So unless you've imported that namespace, you'll have to prefix that to any class.

David,

Yes, I understand that and am looking at the prefix Grasshopper.GUI.Canvas which then lands me with the following difficulty which is - what does the subscription look like? I'm not having luck with it.

Since it's not a static event, you'll first have to get an instance of the canvas class. Typically there's only one canvas and you can reach it via the Instances class:

GH_Canvas canvas = Grasshopper.Instances.ActiveCanvas;

It may be null if the window has never been loaded or if the GrasshopperUnloadPlugin command was run.

Then you can register your handlers with this instance:

canvas.DocumentChanged += MyDocChangedEventHandler;

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service