Grasshopper

algorithmic modeling for Rhino

Hi all,

I am working on a plugin that I want to run some code before saving the grasshopper file. Is there a way to override the save command in grasshopper? If not, is there a way to change the shortcut key, binding Crtl-S with some other custom function?

Thanks,

Alan

Views: 427

Replies to This Discussion

Hi Alan,

there is no event associated with save operations in general, so you cannot do stuff before a save. It might technically be possible to replace the Save menu item with some custom menu item, but the menu merely invokes the logic elsewhere and there's no way to hack that.

May I ask what sort of thing you want to accomplish before a save? And do you also want to do that before every autosave?

David,

I implement a file lock in the plugin and need to unlock the file before saving. I do not need it for autosave. Replacing the menu item sounds like a solution. 

Thanks,

Alan

Well the saving of the file happens in several stages:

  1. A runtime type-safe database is made by serializing all objects in the GH_Document. This is the part where GH_Component etc. derived types get to write their data to the archive.
  2. The archive is converted into a byte array, this happens entirely within GH_IO.dll
  3. The byte array is compressed, this happens entirely within GH_IO.dll
  4. The byte array is written to the disk at a safe, unoccupied location.
  5. If there's already a file in the destination spot, it is deleted.
  6. Finally the written file is moved from the temp folder into the final location.

So if your plugin is locking and unlocking the *.gh file, then you can do it at any time prior to step 5. 

If you want to replace the Save item, you'll need to wait until the Grasshopper.Instances.DocumentEditor is constructed, then iterate over its controls until you find one that's of type MenuStrip. Once you have this main menu, find the File item, then find the Save item and register your events.

I just remembered that you'll have problems replacing it with another one because the shortcut logic is special and hard-coded. If you replace the Save item with another item then Grasshopper will not recognize it as ashortcuttable menu item, which will definitely mess with the Preferences UI and may also mess with the actual shortcut handling.

David,

After I replace the menu item, the shortcut actually works. Crtl-S points to the new save item. It also shows up correctly in the Preferences>Interface>Shortcuts, although it is greyed out.

One thing I fail to override is the GH_MultiDocSave(), which pops up when closing the file with unsave changes. It does not seems like I can do anything about it.

Alan

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