EventWatcherPlugin cannot work after Navisworks launches

By Xiaodong Liang

Issue

We tested with the SDK sample apinetexamplesAutomationMessageClientServerMessageClientPlugin. It has one EventWatcherPlugin which delegates some events of the application. In theory, these events are scribed when NW launches. Thus they will work in whole sessions of Navisworks. The events are fired when Navisworks is launching. But after it launches, no matter how you open/add/new/close document, these events are not fired any more!

Solution

Navisworks is currently an SDI Document. A single Document  is created at start-up and destroyed at shut-down.  The ‘ActiveDocument’ stays the same throughout the lifetime of the program, including when you  load/unload a file.
What you need to be watching is: Autodesk.Navisworks.Api.Application.ActiveDocument.Models.CollectionChanged

Snippet of the required code here is as below:

void ActiveDocument_Models_CollectionChanged(object sender, 
                                             System.EventArgs e)
{
    MessageBox.Show("ActiveDocument_Models_CollectionChanged");
}
 
void Application_ActiveDocumentChanged(object sender, 
                                      System.EventArgs e)
{
    MessageBox.Show("ActiveDocumentChanged");
    Autodesk.Navisworks.Api.Application.ActiveDocument.Models.
        CollectionChanged+= ActiveDocument_Models_CollectionChanged;
}

Comments

2 responses to “EventWatcherPlugin cannot work after Navisworks launches”

  1. fuling wen Avatar
    fuling wen

    Dear Mr liang
    I have a problem, I set up a response function to an event, but in this function, it triggered this event again ,it caused my program crash。Is there any way to not triggered (ex:not send the event message)in my function?
    this is my code
    Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Changed += new EventHandler(CurrentSlection_changed);
    private void CurrentSlection_changed(object sender , EventArgs e)
    {
    Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Add(fatherItem);
    }

  2. Reza Rostami Nikoo Avatar
    Reza Rostami Nikoo

    Hello. Although this is a question that has been asked for many years ago,nut I answer it for other people who may have this question.
    I think the code should be changed like this: when the event happens, you need to delete callback from events, and after adding an item to the selection, add it back to the event:
    Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Changed +=
    new EventHandler(CurrentSlection_changed);
    private void CurrentSlection_changed(object sender , EventArgs e)
    {
    // delete your call back from event
    Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Changed -= new
    EventHandler(CurrentSlection_changed);
    // adding items to your selection
    Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Add(fatherItem);
    // add it back to the event
    Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Changed +=
    new EventHandler(CurrentSlection_changed);
    }

Leave a Reply to Reza Rostami NikooCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading