The IUpdater interface of Revit API provides a means to monitor element status such as addition, deletion, and modification.
The IUpdater has a few methods, for example, Execute, GetAdditionalInformation, GetChangePriority, GetUpdaterId, and GetUpdaterName. The core method is the Execute(). It offers an UpdaterData parameter which provides information about elements just added, deleted, or modified in a document. An UpdaterId instance needs to be created and returned from the GetUpdaterId() method. The GetChangePriority() method indicates changes of what kind of elements should inform the updater first.
The following 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 ElementUpdater1
Implements Autodesk.Revit.DB.IUpdater
Private mAddinId As Autodesk.Revit.DB.AddInId
Private mUpdaterId As Autodesk.Revit.DB.UpdaterId
Public Sub New(ByVal id As Autodesk.Revit.DB.AddInId)
mAddinId = id
mUpdaterId = New Autodesk.Revit.DB.UpdaterId(mAddinId, New Guid("69c641d9-c838-42bb-983f-d30b8d01d4bb"))
End Sub
#Region "IUpdater Members"
Public Sub Execute(ByVal data As Autodesk.Revit.DB.UpdaterData) Implements Autodesk.Revit.DB.IUpdater.Execute
Dim doc As Document = data.GetDocument()
Dim addedIds As ICollection(Of Autodesk.Revit.DB.ElementId) = data.GetAddedElementIds()
For Each id As Autodesk.Revit.DB.ElementId In addedIds
Dim elem As Autodesk.Revit.DB.Element = doc.Element(id)
MessageBox.Show(String.Format("{0}({1}) has been added.", elem.Name, id.IntegerValue))
Next
Dim deletedIds As ICollection(Of Autodesk.Revit.DB.ElementId) = data.GetDeletedElementIds()
For Each id As Autodesk.Revit.DB.ElementId In deletedIds
MessageBox.Show(String.Format("{0} has been deleted.", id.IntegerValue))
Next
Dim modifiedIds As ICollection(Of Autodesk.Revit.DB.ElementId) = data.GetModifiedElementIds()
For Each id As Autodesk.Revit.DB.ElementId In modifiedIds
MessageBox.Show(String.Format("{0} has been modified.", id.IntegerValue))
Next
End Sub
Public Function GetAdditionalInformation() As String Implements Autodesk.Revit.DB.IUpdater.GetAdditionalInformation
Return "ElementUpdater1"
End Function
Public Function GetChangePriority() As Autodesk.Revit.DB.ChangePriority Implements Autodesk.Revit.DB.IUpdater.GetChangePriority
Return Autodesk.Revit.DB.ChangePriority.FloorsRoofsStructuralWalls
End Function
Public Function GetUpdaterId() As Autodesk.Revit.DB.UpdaterId Implements Autodesk.Revit.DB.IUpdater.GetUpdaterId
Return mUpdaterId
End Function
Public Function GetUpdaterName() As String Implements Autodesk.Revit.DB.IUpdater.GetUpdaterName
Return "ElementUpdater1"
End Function
#End Region
End Class
The following code registers the updater against Walls and adds triggers of element addition, deletion and modification in the OnStartup event of an external application:
' Register the ElementUpdater1
Dim updater1 As New ElementUpdater1(uiApp.ActiveAddInId)
Autodesk.Revit.DB.UpdaterRegistry.RegisterUpdater(updater1)
Dim catFilter As New Autodesk.Revit.DB.ElementCategoryFilter(Autodesk.Revit.DB.BuiltInCategory.OST_Walls)
Autodesk.Revit.DB.UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Autodesk.Revit.DB.Element.GetChangeTypeElementAddition())
Autodesk.Revit.DB.UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Autodesk.Revit.DB.Element.GetChangeTypeElementDeletion())
Autodesk.Revit.DB.UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Autodesk.Revit.DB.Element.GetChangeTypeGeometry())
The following code unregisters the updater in the OnShutdown event of the same external application:
' Unregister the ElementUpdater1
Autodesk.Revit.DB.UpdaterRegistry.UnregisterUpdater(New ElementUpdater1(uiApp.ActiveAddInId).GetUpdaterId())
All the above VB.NET code can be created automatically by the Element Updater of the RevitAddinWizard through a few clicks.
Recent Comments