Hello,
I am having trouble making Undo work properly. Let me first give a simplified explanation of what I am working on. I have blocks which I edit through an “EditMode” which is just a new DetailView.
This is my current structure;
- Start EditMode
- Temporary geometry baked.
- Geometry transformed and transforms stored.
- Begin my custom UndoRecord - method
- Final method to apply all transforms at once to block.
- End UndoRecord
- Purge all Temp Geometries
So I should be able to Undo the changes done through the final method, but the other changes while in the edit mode should not be in the UndoRecords.
With this, I can make the required undo work, but the Undos for the temporary geometry, which I purged, also works. The temporary geometry is recreated on Undo. This is my code, boiled down to the basics, in which I am also trying to clear the UndoRecords for the temporary geometries. But, in this case there is no Undo at all after.
class SampleCode
{
private List<uint> TempUndo { get; set; } = new List<uint>();
private uint MainUndo { get; set; }
private RhinoDoc doc = RhinoDoc.ActiveDoc;
private List<Guid> Geometry { get; set; }
private bool update;
public SampleCode()
{
RhinoApp.Idle += OnRhinoIdle;
Geometry = BakeTemporaryGeometry();
}
private void ModifyBlock()
{
// Modifies block with stored transforms of temporary geometry
MainUndo = RhinoDoc.ActiveDoc.BeginUndoRecord("Edits");
ApplyTransforms();
update = true;
}
private void OnRhinoIdle(object sender, EventArgs e)
{
var uRecNo = doc.NextUndoRecordSerialNumber - 1;
if (!TempUndo.Contains(uRecNo) && uRecNo != MainUndo)
{
TempUndo.Add(uRecNo);
}
if (update)
{
update = false;
doc.EndUndoRecord(MainUndo);
foreach (var no in TempUndo)
{
doc.ClearUndoRecords(no, true);
}
foreach (Guid guid in Geometry)
{
doc.Objects.Purge(doc.Objects.FindId(guid));
}
}
}
}
Sorry for the long post. Any help is appreciated !