How to activate an iupdater on Revit Startup

Hi everyone!
Hoping you are doing well,
I am trying to implement some iupdaters on Revit, the issue I am facing is that I can not make Revit to activate the iupdaters at the startup, at the moment for activate them I need to restart pyrevit, this is where my iupdater code is placed:

image

It seems that it is being read as expected (this code is just detecting when a Mechanical Equipment is modified in its Geometry), but once a document has been opened, the updater does nothing, and the only way to activate it is reloading pyRevit, as in the image below:

testiupdater

and this is the code I am using, (Thanks to @Gerhard.P since I am using a code of him modified for testing purposes)

from Autodesk.Revit.DB import IUpdater, UpdaterId, ElementId, UpdaterRegistry, Element
from Autodesk.Revit.DB import BuiltInCategory, ChangePriority, ChangeType, FilteredElementCollector, ElementCategoryFilter
from Autodesk.Revit.UI import TaskDialog, TaskDialogCommonButtons, TaskDialogResult
from System import Guid

class RevitEquipUpdater(IUpdater):
    def __init__(self, doc):
        self._doc = doc

    def Execute(self, data):
        # Identify moved Mech Equipment instances by checking for geometry changes
        modified_ids = data.GetModifiedElementIds()
        for id in modified_ids:
            equip_instance = self._doc.GetElement(id)
            # Check if the modified element is indeed a Mech Equipment instance
            if equip_instance.Category.Id.IntegerValue == int(BuiltInCategory.OST_MechanicalEquipment):
                # Alert the user about the movement
                TaskDialog.Show("Mechanical Instance Moved", "A Mechanical Equipment instance has been moved: " + equip_instance.Name)

    def GetUpdaterId(self):
        return self.updater_id

    def GetUpdaterName(self):
        return 'MechanicalEquipmentMovementUpdater'

    def GetAdditionalInformation(self):
        return 'Alerts the user if a Mechanical Equipment instance is moved.'

    def GetChangePriority(self):
        return ChangePriority.FreeStandingComponents

    def Initialize(self):
        # This is where you can add trigger conditions for the updater
        pass

    def Uninitialize(self):
        pass

# Example setup for the updater (assuming __revit__ and doc are already defined)
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application

# Create an instance of the updater
updater = RevitEquipUpdater(doc)

# Create a unique Guid for the updater
guid_string = "76414032-04ec-4438-971f-295fb25282e9"
guid = Guid(guid_string)

# Create an UpdaterId
updater_id = UpdaterId(app.ActiveAddInId, guid)

# Set the identifier in the updater instance
updater.updater_id = updater_id

# If the updater is already registered, ask the user if they want to unregister it
if UpdaterRegistry.IsUpdaterRegistered(updater_id, doc):
    dialog_result = TaskDialog.Show("Updater Options", 
                                    "The updater is already registered. Do you want to unregister it?", 
                                    TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No)

    # Depending on the user's choice, unregister the updater
    if dialog_result == TaskDialogResult.Yes:
        # User chose to unregister the updater
        UpdaterRegistry.UnregisterUpdater(updater_id, doc)
        TaskDialog.Show('Success', 'Updater has been unregistered!')

else:
    # If the updater is not registered, then register it
    UpdaterRegistry.RegisterUpdater(updater, doc, True)
    EquipFilter = ElementCategoryFilter(BuiltInCategory.OST_MechanicalEquipment)
    UpdaterRegistry.AddTrigger(updater_id, EquipFilter, Element.GetChangeTypeGeometry())
    TaskDialog.Show('Success', 'Updater has been registered and trigger has been set!')

So my question is, Is it possible to automatically register the iupdater when a revit document is opened and unregister when the document is closed?, and if so, How can I fix my code in order to get that result?

Thanks in advance!

Hello @Gil_Granados,

A updater has to be registered to a document and on revit startup there is no document.
You need an event that triggers the updater registration, like OnViewActivated or doc created.

In pyRevit I register updaters with hook scripts:

updater_doc-created & updater_doc-opened

And I have the code in the library folder so i can register updaters with buttons or hook scripts.

So my hook script looks like this:

import View_Names
import Wall_Openings

from pyrevit import HOST_APP, EXEC_PARAMS

doc = __eventargs__.Document
app = __revit__

if doc and not doc.IsFamilyDocument:
    View_Names.update_view_names(doc, app)
    Wall_Openings.update_wall_openings(doc, app)
1 Like