Undo transformation (C#)

Hello, I’m translating some curves using the Transform API. I’m can correctly translate curves but, after pressing CTRL+Z to undo the transformation, Rhino says “Nothing to undo”. Here is the code I’m using:

Transform translation = Transform.Translation(50, 0, 0);
currentActiveDocument.Objects.Transform(curveToTranslate, translation, true);

I even tried using TransformWithHistory (with History enabled of course) but it always says that there is nothing to undo. I’m probably missing something here, maybe translation isn’t the correct method in this case? Thanks in advance.

Are you doing this inside a command?
If not, maybe this can help: https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_RhinoDoc_BeginUndoRecord.htm

2 Likes

Are you doing this in the context of a command? If you are, the undo should work.
If you don’t do this in a command, use the following to start and finalize an undo record:

var id = doc.BeginUndoRecord("Start transformation");
// perform transformation
doc.EndUndoRecord(id);
3 Likes

That was the missing piece. Thanks to both of you @rgr and @menno, it works flawlessly.

I marked rgr’s answer as solution because it shows up higher but I can see that you both posted at the same time! Thanks again!

1 Like