How to execute Undo?

How to execute Undo?

cheahjyRevit
Participant Participant
966 Views
6 Replies
Message 1 of 7

How to execute Undo?

cheahjyRevit
Participant
Participant

I want to propose routes that can connect two conduits together. Each time I proposed a route, I will create conduit and commit the transaction group. When user want to select another route, I want to undo the previous transaction group. That's where the problem occurs when I try to undo the previous transaction.

 

I have tried with Undo Postable Command, but it only undo the following transaction but not the previous one. (Please correct me if I am wrong with the usage of this Postable Command).

 

Hope that someone can provide guidance with how to execute undo. Thank you in advance.

 

Below is my code.

        public void GenerateRouting(UIApplication uiApp)
        {
            Document doc = uiApp.ActiveUIDocument.Document;

            if (_routingCounter != 0)
            {
                RevitCommandId id = RevitCommandId.LookupPostableCommandId(PostableCommand.Undo);
                uiApp.PostCommand(id);
            }

            if (_firstElement != null && _secondElement != null)
            {
                List<char> sequence;

                if (CustomizeRouting)
                {
                    sequence = GetDirectionSequence();
                }
                else
                {
                    sequence = _directionSequences[_routingCounter];
                    _routingCounter = (_routingCounter + 1) % _directionSequences.Count();
                }

                XYZ tempStartPoint = _actualStartPoint;
                try
                {
                    TransactionGroup transactionGroup = new TransactionGroup(doc, "Generate Route Transaction Group");
                    transactionGroup.Start();

                    using (Transaction st = new Transaction(doc, "Create Conduit"))
                    {
                        st.Start();
                        CreateConduit(doc, sequence, _actualEndPoint, tempStartPoint);
                        st.Commit();
                        transactionGroup.Assimilate();
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

 

 

0 Likes
Accepted solutions (2)
967 Views
6 Replies
Replies (6)
Message 2 of 7

jeremy_tammik
Alumni
Alumni
Accepted solution

I am not sure, but I seem to remember that programmatic driving of undo operations possibly are not supported.

  

How about making a note of the new conduits and other elements created in your current attempt, and simply deleting them again afterwards?

  

 

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 7

ricaun
Advisor
Advisor
Accepted solution

The PostCommand only gonna be executed when your code finish, not gonna undo in the exact moment you call PostCommand.

 

Work with Undo and PostCommand is tricky. Is easier to record every ElementId you created and delete like @jeremy_tammik suggest.

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 4 of 7

cheahjyRevit
Participant
Participant

@ricaun @jeremy_tammik Thank you for your suggestions. Will proceed with it.

0 Likes
Message 5 of 7

tuan253461
Participant
Participant

you can use UIFrameworkServices;
QuickAccessToolBarService.performMultipleUndoRedoOperations(true, 1);

Message 6 of 7

jeremy_tammik
Alumni
Alumni

Wow. Thank you for that interesting pointer. Sounds like a useful solution to me. Have you tested it thoroughly? Is it reliable enough for production code?

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 7 of 7

ricaun
Advisor
Advisor

That's cool, I know that UIFrameworkServices has some interesting services, this QuickAccessToolBarService for undo and redo was not in my knowledge.

 

I like the list of transaction names.

IList<string> undoItems = QuickAccessToolBarService.collectUndoRedoItems(true);

 

Works very well to undo and redo without using PostCommand.

Undo-Redo - 2024-03-19 11-06-41.gif

 

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes