I’m in the middle of a grasshopper hackathon that last until tomorrow evening and I am having troubles finding a way to:
When grasshopper starts I want to launch an application.
Basically this is most likely a super simple thing, just need to subscribe to some event or something, I’m a professional coder so feel free to go quite detailed:)
All the help is greatly appreciated:)
Thanks
Edit: I’m using rhino 5 and grasshopper 0.9.0.0.7.6
If Grasshopper is loading a GHA of yours, then you can put all the code you want inside a Grasshopper.Kernel.GH_AssemblyPriority derived class. This class will be loaded first if it exists. Of course any component derived class will also be loaded on startup, so you can put your app init code there as well, but then you have to figure out how to not start an external all the next time your component gets instantiated.
Question1:
Is there a function or somehting I can override that is runs after the canvas is loaded so that I cal subscribe to objectsAdded event?
Qustion2:
What about an event that triggers when grasshopper is closing? I would like to un-subscribe this event there. Or is it needed? Will grasshopper do that automatically for me when it closes?
It’s not needed. GH only closes when Rhino closes and the entire AppDomain is shut down. Everything will be unloaded and unregistered automatically. So unless you want to do something there’s no need.
It doesn’t make sense to do this before any documents are loaded, so you could postpone registering the events until the first time Grasshopper.Instances.DocumentServer.DocumentAdded event is raised. However this does not necessarily guarantee that the window and canvas are already loaded.
In recentish versions of GH for Rhino6 there’s a static event on GH_DocumentEditor called AggregateShortcutMenuItems, which is raised only once, after the menu of the editor has been populated. It was added to allow plugins to more easily insert their own items into the menu, but you can use it as a cue for the availability of the main canvas.
Don’t forget to unregister the DocumentAdded event, you don’t want to get called every time a document is loaded.
It would technically be possible to load a document before the UI has been invoked. I think none of the standard commands allow for this, but it’s probably possible through code.
Hang on. If you’re listening to ObjectAdded events on the GH_Documents directly then this is a weird approach. I thought you were handling the ObjectAdded events that the canvas relays from whatever document it is currently showing. Because there is only ever one canvas so you don’t have to worry about oversubscribing or unsubscribing from events, whereas there can be lots of documents coming into and going out of existence all the time.