Click or drag to resize

RhinoDocAddCustomUndoEvent Method (String, EventHandlerCustomUndoEventArgs, Object)

Add a custom undo event so you can undo private plug-in data when the user performs an undo or redo

Namespace:  Rhino
Assembly:  RhinoCommon (in RhinoCommon.dll)
Since: 5.0
Syntax
public bool AddCustomUndoEvent(
	string description,
	EventHandler<CustomUndoEventArgs> handler,
	Object tag
)

Parameters

description
Type: SystemString

[Missing <param name="description"/> documentation for "M:Rhino.RhinoDoc.AddCustomUndoEvent(System.String,System.EventHandler{Rhino.Commands.CustomUndoEventArgs},System.Object)"]

handler
Type: SystemEventHandlerCustomUndoEventArgs

[Missing <param name="handler"/> documentation for "M:Rhino.RhinoDoc.AddCustomUndoEvent(System.String,System.EventHandler{Rhino.Commands.CustomUndoEventArgs},System.Object)"]

tag
Type: SystemObject

[Missing <param name="tag"/> documentation for "M:Rhino.RhinoDoc.AddCustomUndoEvent(System.String,System.EventHandler{Rhino.Commands.CustomUndoEventArgs},System.Object)"]

Return Value

Type: Boolean

[Missing <returns> documentation for "M:Rhino.RhinoDoc.AddCustomUndoEvent(System.String,System.EventHandler{Rhino.Commands.CustomUndoEventArgs},System.Object)"]

Examples
using System;
using System.Runtime.InteropServices;
using Rhino;

[Guid("954B8E21-51F2-4115-BD6B-DE67EE874C74")]
public class ex_customundoCommand : Rhino.Commands.Command
{
  public override string EnglishName { get { return "cs_CustomUndoCommand"; } }

  double MyFavoriteNumber { get; set; }

  protected override Rhino.Commands.Result RunCommand(RhinoDoc doc, Rhino.Commands.RunMode mode)
  {
    // Rhino automatically sets up an undo record when a command is run,
    // but... the undo record is not saved if nothing changes in the
    // document (objects added/deleted, layers changed,...)
    // 
    // If we have a command that doesn't change things in the document,
    // but we want to have our own custom undo called then we need to do
    // a little extra work

    double d = MyFavoriteNumber;
    if (Rhino.Input.RhinoGet.GetNumber("Favorite number", true, ref d) == Rhino.Commands.Result.Success)
    {
      double current_value = MyFavoriteNumber;
      doc.AddCustomUndoEvent("Favorite Number", OnUndoFavoriteNumber, current_value);
      MyFavoriteNumber = d;
    }
    return Rhino.Commands.Result.Success;
  }

  // event handler for custom undo
  void OnUndoFavoriteNumber(object sender, Rhino.Commands.CustomUndoEventArgs e)
  {
    // !!!!!!!!!!
    // NEVER change any setting in the Rhino document or application.  Rhino
    // handles ALL changes to the application and document and you will break
    // the Undo/Redo commands if you make any changes to the application or
    // document. This is meant only for your own private plug-in data
    // !!!!!!!!!!

    // This function can be called either by undo or redo
    // In order to get redo to work, add another custom undo event with the
    // current value.  If you don't want redo to work, just skip adding
    // a custom undo event here
    double current_value = MyFavoriteNumber;
    e.Document.AddCustomUndoEvent("Favorite Number", OnUndoFavoriteNumber, current_value);

    double old_value = (double)e.Tag;
    RhinoApp.WriteLine("Going back to your favorite = {0}", old_value);
    MyFavoriteNumber = old_value;
  }
}
Python
import Rhino
import scriptcontext


def OnUndoFavoriteNumber(sender, e):
    """!!!!!!!!!!
    NEVER change any setting in the Rhino document or application.  Rhino
    handles ALL changes to the application and document and you will break
    the Undo/Redo commands if you make any changes to the application or
    document. This is meant only for your own private plug-in data
    !!!!!!!!!!

    This function can be called either by undo or redo
    In order to get redo to work, add another custom undo event with the
    current value.  If you don't want redo to work, just skip adding
    a custom undo event here
    """
    current_value = scriptcontext.sticky["FavoriteNumber"]
    e.Document.AddCustomUndoEvent("Favorite Number", OnUndoFavoriteNumber, current_value)

    old_value = e.Tag
    print "Going back to your favorite =", old_value
    scriptcontext.sticky["FavoriteNumber"]= old_value;


def TestCustomUndo():
    """Rhino automatically sets up an undo record when a command is run,
       but... the undo record is not saved if nothing changes in the
       document (objects added/deleted, layers changed,...)

       If we have a command that doesn't change things in the document,
       but we want to have our own custom undo called then we need to do
       a little extra work
    """
    current_value = 0
    if scriptcontext.sticky.has_key("FavoriteNumber"):
        current_value = scriptcontext.sticky["FavoriteNumber"]
    rc, new_value = Rhino.Input.RhinoGet.GetNumber("Favorite number", True, current_value)
    if rc!=Rhino.Commands.Result.Success: return

    scriptcontext.doc.AddCustomUndoEvent("Favorite Number", OnUndoFavoriteNumber, current_value);
    scriptcontext.sticky["FavoriteNumber"] = new_value

if __name__=="__main__":
    TestCustomUndo()
See Also