Just for educational purposes, I tried to set up my first C# script in pyRevit. I saw there was an example in the pyRevitDev Extension, so I enabled it. Then I started to receive popup dialogs every time I’d open a document.
Looking into the code, it appears this code in the startup.py is registering an event:
# test code for creating event handlers =======================================
# define event handler
def docopen_eventhandler(sender, args):
forms.alert('Document Opened: {}'.format(args.PathName))
# add to DocumentOpening
# type is EventHandler[DocumentOpeningEventArgs] so create that correctly
HOST_APP.app.DocumentOpening += \
framework.EventHandler[DB.Events.DocumentOpeningEventArgs](
docopen_eventhandler
)
Disabling the extension does not seem to unregister this event handler. Any tips on how to disable?
I looked into the Test IronPython Events pushbutton, which looks like the following:
# pylint: skip-file
from pyrevit import HOST_APP, framework
from pyrevit import UI, DB
dc = __revit__.Application.GetType().GetEvent("DocumentChanged")
def docchanged_eventhandler(sender, args):
UI.TaskDialog.Show("ironpython", str(sender))
UI.TaskDialog.Show("ironpython", str(args))
UI.TaskDialog.Show("ironpython", "docchanged_eventhandler")
docchanged_handler = \
framework.EventHandler[DB.Events.DocumentChangedEventArgs](
docchanged_eventhandler
)
HOST_APP.app.DocumentChanged += docchanged_handler
HOST_APP.app.DocumentChanged -= docchanged_handler
# dc.AddEventHandler(__revit__.Application, docchanged_handler)
dc.RemoveEventHandler(__revit__.Application, docchanged_handler)
However, the RemoveEventHandler option does not seem to work, and based on this post, I suspect it’s because it’s trying to remove the docchanged_handler from this script, which is technically a separate object from the first time it ran and added the event handler.
So now I’ve got literally every event popping up three dialog boxes…