Question
Xbim.IO.Memory.Transaction class
Does anyone have an example of how to use the methods: Undo/Redo/DoReversibleAction?
Thanks for the wonderful job!
Thanks, we are glad you like the Toolkit. These methods are used internally my IModel implementations and by IPersistEntity implementations. They are used to store actions so that the transaction as a block can be rolled back. You can obviously use it to manage your own actions as well. What do you need to achieve?
Hey Martin, consider following extention method in which I replace the OuterBound of a IIfcFace object with a new once. My goal is to use your methods (Undo/Redo/DoReversibleAction) to roll back the action after the transaction has been commited.
public static IIfcFaceBound ReplaceOuterBoundPolygon(this IIfcFace e, Polygon3D polygon)
{
var model = e.Model as MemoryModel;
if (model == null)
return null;
IIfcFaceBound bound = null;
using (var txn = model.BeginTransaction("Modification"))
{
bound = e.Bounds.OfType<IIfcFaceOuterBound>().FirstOrDefault();
if (bound.Bound is IIfcPolyLoop pl)
{
foreach (var p in pl.Polygon.ToList())
{
model.Delete(p);
}
pl.Polygon.Clear();
pl.Polygon.AddRange(polygon.Vertices.Select(v => e.Model.Instances.New<IfcCartesianPoint>(cp =>
{
cp.SetXYZ(v.X, v.Y, v.Z);
})));
}
txn.Commit();
}
return bound;
}
You can do that.
The question was how? Do you have an example of using them?
You can't undo the committed transaction. But you can keep the reference to the transaction and call RollBack() when you want to revert all the changes. That closes the transaction in the same way as Commit() does. Or, you can call Undo() and Redo() as many times as you want. Bear in mind that when transaction goes off the scope and it is not finished, it is rolled back automatically.