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)
{
}
}
}
Solved! Go to Solution.
Solved by ricaun. Go to Solution.
Solved by jeremy_tammik. Go to 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?
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.
you can use UIFrameworkServices;
QuickAccessToolBarService.performMultipleUndoRedoOperations(true, 1);
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?
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.
Can't find what you're looking for? Ask the community or share your knowledge.