XbimEssentials icon indicating copy to clipboard operation
XbimEssentials copied to clipboard

Question

Open MojiParc opened this issue 3 years ago • 5 comments

Xbim.IO.Memory.Transaction class

Does anyone have an example of how to use the methods: Undo/Redo/DoReversibleAction?

Thanks for the wonderful job!

MojiParc avatar Jun 15 '22 09:06 MojiParc

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?

martin1cerny avatar Jun 15 '22 10:06 martin1cerny

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;
    }

MojiParc avatar Jun 15 '22 13:06 MojiParc

You can do that.

martin1cerny avatar Jun 16 '22 11:06 martin1cerny

The question was how? Do you have an example of using them?

MojiParc avatar Jun 16 '22 13:06 MojiParc

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.

martin1cerny avatar Jun 20 '22 15:06 martin1cerny