Cant not undo after use RhinoDoc.ReplaceRhinoObject event

rhinocommon

#1

Hi all:

       public static Transform xf = new Transform();
       public TestPlugIn()
		{
            TestPlugIn.Instance = this;
            RhinoDoc.BeforeTransformObjects += new System.EventHandler<Rhino.DocObjects.RhinoTransformObjectsEventArgs>(RhinoDocOnBeforeTransformObjects);
            RhinoDoc.ReplaceRhinoObject += new System.EventHandler<Rhino.DocObjects.RhinoReplaceObjectEventArgs>(RhinoReplaceObjectsEventArgs);
		}
       private void RhinoDocOnBeforeTransformObjects(object sender, RhinoTransformObjectsEventArgs e)
        {
            xf = e.Transform;
        }
        private void RhinoReplaceObjectsEventArgs(object sender, RhinoReplaceObjectEventArgs e)
        {
            id_re = e.ObjectId.ToString().Trim();
            var id = new System.Guid("0541332c-91ab-454e-b839-5f93557e42c0");
            RhinoObject refobj = RhinoDoc.ActiveDoc.Objects.Find(id);
            RhinoDoc.ActiveDoc.Objects.Transform(refobj, xf, true);
        }

This is my core, it cant run, but after I use “ctrl + z” to un do, it is restore like this :


It is any way to solve it?


(Dale Fugier) #2

Hi @pythonuser,

Its never a good idea to do what you are doing where you are doing it. Rather, when your RhinoReplaceObjectsEventArgs delegate is called, set flag and save some data somewhere. Then in a RhinoApp.Idle event handler, check your flag (to make sure something happened) and then do what you want.

In regards to why your transformation is not undoable, only ‘stuff’ that happens in between a ‘begin undo recording’ and ‘end undo recording’ is undoable. Rhino records this automatically for commands. If you not in a command, then you will need to do something like this:

var sn = doc.BeginUndoRecord("My Stuff");
// TODO...
if (sn > 0)
 doc.EndUndoRecord(sn);

Another option is to script a command that you’ve written. See my reply to this thread:

– Dale


#3

Hi @dale
Think you, the core have run good by you way.
By the way, how cant I get the command name of undo\redo done, for example:
I move a object first, and then rotate it, and then I undo by “ctrl + z”, from there, it will undo the rotate step, now how cant I get the command name of undo restored ?


(Dale Fugier) #4

Hi @pythonuser,

I am sorry, but I do not understand what you are saying or reporting. Do you have some sample code that demonstrates your problem?

– Dale


#5

HI @dale,
Thanks for your reply.
My english is not good, I’m very sorry about this.
For example:
I move a sphere

   And undo it, how cant I get the name of command which undoing


(Dale Fugier) #6

Hi @pythonuser,

If you want to know an when undo or redo event has occurred, then subscribe to the Rhino.Commands.Command.UndoRedo event.

public TestPlugIn()
{
  TestPlugIn.Instance = this;
  RhinoDoc.BeforeTransformObjects += OnBeforeTransformObjects;
  RhinoDoc.ReplaceRhinoObject += OnReplaceRhinoObject;
  Command.UndoRedo += OnUndoRedo;
}

public static void OnBeforeTransformObjects(object sender, RhinoTransformObjectsEventArgs e)
{
  // TODO
}

public static void OnReplaceRhinoObject(object sender, RhinoReplaceObjectEventArgs e)
{
  // TODO
}

public static void OnUndoRedo(object sender, UndoRedoEventArgs e)
{
  // TODO
}

Note, UndoRedoEventArgs will give you the id of the command being undone or redone. But it will not give you the command’s name.

– Dale


#7

Hi @dale,
Think you.