The ITransactionFinalizer interface of Revit API can be implemented to do something after a Transaction has been committed or rolled back.
It provides two methods to be implemented, the OnCommitted() and the OnRolledBack(). The former will be called when the transaction of concern has been committed and the latter rolled back.
Here is a sample implementation in VB.NET:
#Region "Namespaces"
Imports System.Text
Imports System.Linq
Imports System.Xml
Imports System.Reflection
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Windows.Forms
Imports System.IO
Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.DB.Events
Imports Autodesk.Revit.DB.Architecture
Imports Autodesk.Revit.DB.Structure
Imports Autodesk.Revit.DB.Mechanical
Imports Autodesk.Revit.DB.Electrical
Imports Autodesk.Revit.DB.Plumbing
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection
Imports Autodesk.Revit.UI.Events
Imports Autodesk.Revit.Collections
Imports Autodesk.Revit.Exceptions
Imports Autodesk.Revit.Utility
Imports RvtApplication = Autodesk.Revit.ApplicationServices.Application
Imports RvtDocument = Autodesk.Revit.DB.Document
#End Region
Public Class SampleTransactionFinalizer
Implements Autodesk.Revit.DB.ITransactionFinalizer
Sub OnCommitted(ByVal document As Document, ByVal strTransactionName As String) Implements Autodesk.Revit.DB.ITransactionFinalizer.OnCommitted
Dim msg As String = String.Format("Transaction {0} on document {1} has been committed.", strTransactionName, document.Title)
TaskDialog.Show(Me.ToString(), msg)
End Sub
Sub OnRolledBack(ByVal document As Document, ByVal strTransactionName As String) Implements Autodesk.Revit.DB.ITransactionFinalizer.OnRolledBack
Dim msg As String = String.Format("Transaction {0} on document {1} has been rolled back.", strTransactionName, document.Title)
TaskDialog.Show(Me.ToString(), msg)
End Sub
End Class
As can be seen, implementing the interface is pretty easy. However, hooking it up with a transaction is not so.
The only method which has something to do with ITransactionFinalizer implementations is the SetTransactionFinalizer() of the FailureHandlingOptions class. The FailureHandlingOptions instances can only be got through calling the method GetFailureHandlingOptions() of Transaction instances.
If a FailureHandlingOptions instance were trying to be created, an error saying ‘The type 'Autodesk.Revit.DB.FailureHandlingOptions' has no constructors defined.’ would just occur.
Dim trans As New Autodesk.Revit.DB.Transaction(CachedDoc, "SampleTransactionFinalizer Test")
trans.Start()
'FailureHandlingOptions fhOpts = new FailureHandlingOptions(); //ERROR: The type 'Autodesk.Revit.DB.FailureHandlingOptions' has no constructors defined
Dim fhOpts As Autodesk.Revit.DB.FailureHandlingOptions = trans.GetFailureHandlingOptions()
fhOpts.SetTransactionFinalizer(New SampleTransactionFinalizer())
Try
' Do something with the trans.
' ...
trans.Commit(fhOpts)
Catch ex As Exception
trans.RollBack(fhOpts)
End Try
and the code is called by an external command, the following TaskDialog will appear:
If an exception occurs and the transaction is rolled back another TaskDialog looking like this will appear:
Transaction Finalizer of RevitAddinWizard can help implement the interface automatically in VB.NET.
Recent Comments