New interfaces have been added to the API related to Transactions. These interfaces will provide a more comprehensive set of options for managing changes being made to the Revit document.
At this time, the new transaction interfaces are not compatible with the automatically-opened transactions created around ExternalCommand callbacks and VSTA macros. So, Autodesk recommends that you not use these interfaces until compatible changes have been made to those frameworks. In order to experiment with the new interfaces, a document needs to be opened or created while in an external command. In the new document, either the old transaction interfaces or the new ones can be used, as long as they are not used together in the same document.
There are three main classes among the new interfaces plus a few supporting classes. The main classes represent three different (but related) transaction contexts.
Transaction — a context required in order to make any changes to a Revit model. Only one transaction can be open at a time; nesting is not allowed. Each transaction must have a name, which will be listed on the Undo menu in Revit once a transaction is successfully submitted.
SubTransaction — can be used to enclose a set of model modifying commands. Sub-transactions are optional. They are not required in order to modify the model. They are a convenience tool to allow logical splitting of larger tasks into smaller ones. Sub-transaction can only be created within an already opened transaction and must be closed (either committed or rolled back) before the transaction is closed (committed or rolled back). Unlike transactions, sub-transaction may be nested, but any nested sub-transaction must be closed before the enclosing sub-transaction is closed. Sub-transactions do not have name, for they do not appear on the Undo menu in Revit.
Transaction group — allows grouping together several independent transactions, which gives the owner of a group an opportunity to treat many transactions at once. When a transaction group is to be closed, it can be rolled back, which means that all already submitted transactions will be rolled back at once. If not rolled back, a group can be either submitted or assimilated. In the former case, all submitted transactions (within the group) will be left as they were. In the later case, transactions within the group will be merged together into one single transaction that will bear the group’s name. A transaction group can only be started when there is no transaction open yet, and must be closed only after all enclosed transactions are closed (rolled back or committed). Transaction groups can be nested, but any nested group must be closed before the enclosing group is closed. Transaction groups are optional. They are not required in order to make modifications to a model.
All three transaction objects share some common methods:
| Start | Will start the context. |
| Commit | Ends the context and commits all changes to the document. |
| RollBack | Ends the context and discards all changes to the document. |
| GetStatus | Returns the current status of a transaction object |
Besides having the GetStatus returning the current status, both Commit and RollBack methods also return a status indicating whether or not the method was successful. Available status values include:
| Uninitialized | The initial value after object is instantiated; the context has not started yet |
| Started | Set after a transaction object successfully started (Start method was called) |
| Committed | Set after a transaction object successfully committed (Commit method was called) |
| RolledBack | Set after a transaction object successfully rolled back (RollBack method was called) |
| Pending | Set after transaction object was attempted to be either submitted or rolled back, but due to failures that process could not be finished yet and is waiting for the end-user’s response (in a modeless dialog). Once the failure processing is finished, the status will be automatically updated (to either Committed or RolledBack status). |
The new Transaction interfaces replace the old APIs:
- Document.BeginTransaction()
- Document.EndTransaction()
- Document.AbortTransaction()
which have been removed from the API.