Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to use Memory Transactions to rollback/undo #425

Closed
MojiParc opened this issue Jun 15, 2022 · 5 comments
Closed

Question: how to use Memory Transactions to rollback/undo #425

MojiParc opened this issue Jun 15, 2022 · 5 comments

Comments

@MojiParc
Copy link

Xbim.IO.Memory.Transaction class

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

Thanks for the wonderful job!

@martin1cerny
Copy link
Member

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?

@MojiParc
Copy link
Author

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

@martin1cerny
Copy link
Member

You can do that.

@MojiParc
Copy link
Author

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

@martin1cerny
Copy link
Member

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.

@andyward andyward changed the title Question Question: how to use Memory Transactions to rollback/undo Sep 9, 2022
@andyward andyward closed this as completed Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants