Transaction.Commit
Method
Description:
Commits all changes made to the model during the transaction.
Commits all changes made to the model during the transaction.
Remarks:
By committing a transaction, all changes made to the model during the transaction are accepted. A new undo item will appear in the Undo menu in Revit, which allows the user to undo the changes. The undo item will have this transaction's name. Commit may only be called for a transaction that has been started. (Use the GetStatus() method to check the current state.) Be aware that committing may fail or can be delayed (as a result of failure handling.) Callers should always check the returned status to test whether a transaction was committed successfully. Only after a transaction is successfully committed (or rolled back as a result of handling transaction failures), it may be started again.
By committing a transaction, all changes made to the model during the transaction are accepted. A new undo item will appear in the Undo menu in Revit, which allows the user to undo the changes. The undo item will have this transaction's name. Commit may only be called for a transaction that has been started. (Use the GetStatus() method to check the current state.) Be aware that committing may fail or can be delayed (as a result of failure handling.) Callers should always check the returned status to test whether a transaction was committed successfully. Only after a transaction is successfully committed (or rolled back as a result of handling transaction failures), it may be started again.
Examples
public bool CreateLevel(Autodesk.Revit.DB.Document document, double elevation)
{
// All and any transaction should be enclosed in a 'using'
// block or guarded within a try-catch-finally blocks
// to guarantee that a transaction does not out-live its scope.
using (Transaction transaction = new Transaction(document, "Creating Level"))
{
// Must start a transaction to be able to modify a document
if( TransactionStatus.Started == transaction.Start())
{
if (null != Level.Create(document, elevation))
{
// For many various reasons, a transaction may not be committed
// if the changes made during the transaction do not result a valid model.
// If committing a transaction fails or is canceled by the end user,
// the resulting status would be RolledBack instead of Committed.
return (TransactionStatus.Committed == transaction.Commit());
}
// For we were unable to create the level, we will roll the transaction back
// (although on this simplified case we know there weren't any other changes)
transaction.RollBack();
}
}
return false;
}
Exceptions
Exception | Condition |
---|---|
InvalidOperationException | The current status of the transaction is not 'Started'. Transaction must be started before calling Commit or Rollback. -or- The transaction's document is currently in failure mode. No transaction operations are permitted until failure handling is finished. |