Grasshopper

algorithmic modeling for Rhino

Hi,

I am working on a bunch of tools to help organize the objects on the canvas. At the moment I am using GH_Components as containers for these, but ideally, these functions would get their own keyboard shortcut, such as <ctrl-L> for "Insert generic parameters to the left of every selected object".

Is it possible to create custom keyboard shortcuts? And if not, does anyone have any suggestions as to how to easily call these custom functions?

Views: 2029

Replies to This Discussion

Although you can find the menu on the Grasshopper window by iterating over the controls in Grasshopper.Instances.DocumentEditor.Controls, and, once found, you can insert menu items into it, the Grasshopper window does have its own shortcut handling logic. Just assigning a shortcut combo to a new menu item won't do the trick.

The steps needed for add menu items to GH and have them respond to shortcuts are:

  • Get a hold of the document editor form once it exists.
  • Find the main menu control on the form.
  • Insert your own items.
  • Handle the GH_DocumentEditor.AggregateShortcutMenuItems event and add your menu items to the list.

To be honest I've never tested this...

But it does work, even if it isn't particularly elegant.

Attachments:

Btw. the reason it's so complicated is because Grasshopper has centralised shortcut preferences. Now, looking back, it's clear that this could have been handled better, but we're stuck with the current system.

Note how the new item is included in the preferences:

The field is static, meaning it is shared amongst all instances of that class. When a script file is compiled, the resulting type is stored inside a dictionary and if the exact same script occurs somewhere else it'll retrieve the type from there, rather than compiling anew. So static fields are shared amongst all C# component instances that run the exact same code.

If you change the script slightly (even just whitespace), it will compile again and you'll get a second menu item. 

Incidentally you could also add buttons to the toolbar, or even add additional toolbars to the main form if you'd feel so inclined. None of that will allow you to use shortcut keys, but it might still be an interesting alternative for quick-access tools.

Thanks so much David.

That is working beautifully! I had a bit of a challenge getting it to work in my plugin, but adding the lines below seems to make it work. Is this how you would go about it as well?

public override GH_LoadingInstruction PriorityLoad()
{
Instances.CanvasCreated += RegisterMenuItems;
return GH_LoadingInstruction.Proceed;
}

protected void RegisterMenuItems(GH_Canvas canvas)
{
Instances.CanvasCreated -= RegisterMenuItems;
GH_DocumentEditor documentEditor = Instances.DocumentEditor;

if (documentEditor != null)
{
this.CreateTidyMenu(documentEditor);
}
}

Thanks again!

I'd forgotten about the CanvasCreated event. Yes, this is certainly the best way to do it I think.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service