Undoing command modifications

Hi @Dale,

Sorry to revive this, but what would be the right approach to perform the ‘Undo’ right after the command then?

I tried to catch the Rhino.Command.EndCommand event at plugin level:

public MyPlugIn()
            Instance = this;
            Command.EndCommand += OnCommandFinished;

And undo like this:

 private void OnCommandFinished(object sender, CommandEventArgs e)
            if (e.CommandId == new Guid("<MyCommandID>"))
                RhinoApp.WriteLine("Command terminated: {0}", e.CommandEnglishName);
                var result = e.Document.Undo();

Result is following:

  1. ‘OnCommandFinished’ gets triggered, but the Undo fails :frowning:
  2. manually run ‘Undo’ command also works as expected

Do I need to add some extra steps? It seems Rhino doesn’t know what to undo, even if the command is finished…
Thanks in advance.

@jeffoulet - I’ve moved this to the Rhino Developer category.

Hi @jeffoulet,

You cannot call RhinoDoc.Undo while a command is active.

Why are you doing this?

– Dale

Hi @dale,

Thanks for replying, and sorry, I maybe should have start a new thread…

I wrote a command to export referenced geometries from worksession based on the ‘Export’ command (basically merging referenced file together).
It is perfomed as @pascal’s suggestion (please see original thread here):

  • first the layer structure is created in the current document (one root layer per referenced Rhino file)
  • then the referenced geometries are duplicated in the current document with original attributes and exported
  • finally, running the command ‘Undo’ deletes the newly created geometries and layers and reset the document to previous state.

It is working fine, but unfortunately the last step have to be executed manually and I was wondering if there is any way to trigger the Undo right after the export command is done.
I also want to understand why the EndCommand event gets triggered although the command is still active. :thinking:

Following alternatives come in my mind:

  • keep track of all newly created objects (geometries + layers) and delete them after the export
  • perform the Undo once Rhino gets to Idle mode
  • rethink the whole export thing and start messing up with Rhino.FileIO :grimacing:

Which one seems the more reasonable to you?
Thanks in advance.

I’d try this option.

– Dale