Welcome to the fifth tutorial of
Navisworks® API series, this post is to cover how to subscribe/unsubscribe the Application & Document
Events and another type of plugin called
EventWatcherPlugin that can’t be seen from the Navisworks® user-interface. In this tutorial, we’ll create an event watcher add-ins that override the transparency of all the unselected model items in the document.
And this task is Lab-9 exercise from Navisworks® SDK.(figure 1.0)
So, first things first, setting up Navisworks® Add-Ins project in the visual studio, you can follow
Creating Navisworks Add-Ins tutorial to set up the project. When you are ready, in
Plugin attribute omit two properties
Tooltip since our add-ins no longer display in the UI (there is no issue even if you provided). In the
EventWatcherPlugin interface which includes
OnLoaded – method called when application start after Add-Ins is loaded/created and
OnUnloading – method called when application closing before Add-Ins is unloaded. These are the recommended places to subscribe & unsubscribe API
Events accordingly. (figure 1.1)
OnLoaded method, subscribe an Application event named
ActiveDocumentChanged – occurs when the ActiveDocument has changed. Every event must provide an event handler method, we will write a method,
Application_ActiveDocumentChanged and provide as an event handler method. And to unsubscribe the
ActiveDocumentChanged event in
OnUnloading method respectively.(figure 1.2)
Application_ActiveDocumentChanged event handler method, check the active document status and to subscribe
CurrentSelection.Changed a document event. So, why can’t we just subscribe this event, in the first place, in OnLoaded method ? No, it will raise an error. The reason is – while the application loading, the active document object is does not existed yet. (figure 1.3)
CurrentSelection_Changed event handler method, get the current selected items and check whether null or has any selected items. Create an empty modelitemcollection, copy & invert the selected items and override the transparency. Finally, reset all the modelitems transparency when there is no modelitem is selected. (figure 1.4)
Below demo is just to explicitly show how methods are being called when events are triggered.
Well, that’s the end of the tutorial, you can download the complete project source code from here. Cheers!
Founder and Developer @twentytwo.space