DocumentChanged and TransactionGroupRolledBack problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm implementing Revit add-in that monitors how user modifies Revit document. My code relies on ControlledApplication.DocumentChanged event to act in correspondence with added, modified, and deleted elements. Unfortunately, I found the scenario when elements are changed, but DocumentChangedEventArgs doesn't contain any added, modified, or deleted element ids.
At first, I noticed such a scenario in the Manage Links window. After a while, I was able to implement the code that reproduces this problem.
I have a command that creates transaction group with single transaction in it. Note, that there is transaction group rollback in the end. The code is the following:
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var doc = commandData.Application.ActiveUIDocument.Document;
using (var trGroup = new TransactionGroup(doc))
{
trGroup.Start("Test group");
using (var firstTransaction = new Transaction(doc))
{
firstTransaction.Start("Test transaction");
doc.ProjectInformation.Name = "Test Value";
firstTransaction.Commit();
}
trGroup.RollBack();
}
return Result.Succeeded;
}
Also, I have DocumentChanged event handler that shows message box with data from the event args:
private void ControlledApplication_DocumentChanged(object sender, DocumentChangedEventArgs e)
{
var sb = new StringBuilder();
e.GetTransactionNames().ToList().ForEach(t => sb.AppendLine(t));
MessageBox.Show($"Added ids count: {e.GetAddedElementIds().Count}, Modified ids count: {e.GetModifiedElementIds().Count}, " +
$"Deleted ids count: {e.GetDeletedElementIds().Count}, Operation: {e.Operation}, Transactions: {sb}", "DocumentChanged");
}
When the command is executed, 2 message boxes appear one after another:
The first message box is shown after firstTransaction.Commit():
This is correct. Modified ids contain the id of the ProjectInfo that was modified.
The second message box is shown after trGroup.RollBack():
Note, that after the group rollback DocumentChanged receives no element ids. Expected behavior is that there should be element id of the element that was rollbacked. This is critical to my application because it gets unsynchronized with the document. Thus, I have the following questions:
- The first idea I have is to record transaction names with affected element ids when the transaction group is started. I couldn’t find anything useful event or flag that indicates that the group transaction is started in Revit API. Could you suggest any API that could help me?
- If there is no such API, I should record all the transactions to be able to recall what elements were affected by the group rollback. I can’t hold infinite list in memory. Thus, I need to record some quantity of transactions. The question is what is the maximum number of transactions I should record in this case?
Reproduces on Revit 2022, 2023, and 2024.
Developer Advocacy and Support +