Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to execute Undo?

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
cheahjyRevit
523 Views, 6 Replies

How to execute Undo?

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)
                {

                }
            }
        }

 

 

6 REPLIES 6
Message 2 of 7

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
Message 3 of 7
ricaun
in reply to: cheahjyRevit

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

Message 4 of 7
cheahjyRevit
in reply to: cheahjyRevit

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

Message 5 of 7
tuan253461
in reply to: cheahjyRevit

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

Message 6 of 7
jeremy_tammik
in reply to: tuan253461

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
Message 7 of 7
ricaun
in reply to: tuan253461

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report