RhinoDoc.Undo() makes RhinoDoc.UndoActive stay true forever

Hi @stevebaer and @dale.

When I use the RhinoDoc.Undo() method to undo the previous action it seems that it will not change the RhinoDoc.UndoActive property back to false after the action is done. Is this a bug or am I maybe missing something?

-Matti

Hi @mattipi,

Can you provided a simple example, that we can run here, that reproduces what you are seeing?

Thanks,

– Dale

Sorry to necro this, but seemed better than creating a new thread with the same question. Here’s a simple example that I just ran into.

Context: when this control has focus, I want to be able to trigger Rhino’s Undo/Redo.

If I just call RunScript("_Undo") everything works as expected, but if I want to use the Doc to do this, I have to add the EndUndoRecord which seems .. wrong?

private void TreeOnKeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyData)
    {
        case Keys.Control | Keys.Z:
        {
            e.Handled = e.SuppressKeyPress = true;
            // At this point, we have:
            // RhinoDoc.ActiveDoc.UndoRecordingIsActive = false
            // RhinoDoc.ActiveDoc.CurrentUndoRecordSerialNumber = 0
            if (!RhinoDoc.ActiveDoc.Undo())
                RhinoApp.WriteLine("Nothing to undo.");

            // Now we have:
            // RhinoDoc.ActiveDoc.UndoRecordingIsActive = true
            // RhinoDoc.ActiveDoc.CurrentUndoRecordSerialNumber > 0
            // So I need to add:
            if (RhinoDoc.ActiveDoc.UndoRecordingIsActive)
                RhinoDoc.ActiveDoc.EndUndoRecord(RhinoDoc.ActiveDoc.CurrentUndoRecordSerialNumber);
                    return;
        }
        case <...>
}

Hi @dmlook,

When you call RhinoDoc.Undo() outside a command, Rhino starts an undo record for the undo operation. Normally the end of the running command closes that record. Your key handler isn’t running a command, so nothing closes it, and UndoActive and UndoRecordingIsActive stay true.

Calling EndUndoRecord afterward, as you’re doing, is the right way to handle this. It closes the record, and Redo keeps working. Here’s a slightly tidier version:

var doc = RhinoDoc.ActiveDoc;
var wasRecording = doc.UndoRecordingIsActive;
if (!doc.Undo())
  RhinoApp.WriteLine("Nothing to undo.");
else if (!wasRecording && doc.UndoRecordingIsActive)
  doc.EndUndoRecord(doc.CurrentUndoRecordSerialNumber);

The same applies to RhinoDoc.Redo().

The other option is RhinoApp.RunScript("_Undo", false), which runs the Undo command and handles all of this for you.

– Dale