AddIn event on document change

AddIn event on document change

Jef_E
Collaborator Collaborator
1,681 Views
5 Replies
Message 1 of 6

AddIn event on document change

Jef_E
Collaborator
Collaborator

Hi!

 

I'm writing an addin that works with iProperties of the active document or the selected document. But I can't find the right event to trigger my code.

 

What I have now is 2 events,

 

first when the user selects an item (in assembly)  this works fine for me.

' Add event handler for user selected object event
m_UserInputEvent = m_inventorApplication.CommandManager.UserInputEvents
AddHandler m_UserInputEvent.OnSelect AddressOf OnDocumentChangeEvent

But I want also an event when the user changes document open or switches active document on the bottom with tabs.

' Add event handler for OnDocumentChange event.
m_AppEvents = m_inventorApplication.ApplicationEvents
AddHandler m_AppEvents.OnActivateDocument, AddressOf OnDocumentChangeEvent

This second event fires more than I like, on open document 2x and on other unwanted moments. What event can I use for this?

 



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
1,682 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

Hi,

 

this is how I add event triggers to the standardAddInServer:

at the start of the namespace i add the line

 

 Private WithEvents oAppEvents As ApplicationEvents

as shown:

 

 

 Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer
        'Application events object
        Private WithEvents oAppEvents As ApplicationEvents

 

This means I can them select oAppEvents from the left dropdown above yor code window, and from the right one you have a full set of event triggers ready. to use.

I Can then choose OnActivateDocument and I get the empty sub as below:

      Private Sub oAppEvents_OnActivateDocument(ByVal DocumentObject As Inventor._Document, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles oAppEvents.OnActivateDocument

        End Sub

As you might've guessed, any code in here will fire upon a docuement being made active, eg switching between two docs.

 

but this trigger, as with all triggers will fire twice, once before the event and once after it. this is why you're  seeing it happen twice,

 

To prevent this you need to tell the sub when you want it to fire, either before or after the event like this:

 

        Private Sub oAppEvents_OnActivateDocument(ByVal DocumentObject As Inventor._Document, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles oAppEvents.OnActivateDocument
            'fire after document is made active
            If BeforeOrAfter = EventTimingEnum.kAfter Then

            End If

            'fire before document is made active
            If BeforeOrAfter = EventTimingEnum.kBefore Then

            End If
        End Sub

 

hope this helps.

0 Likes
Message 3 of 6

Anonymous
Not applicable

hello,

 

I have tried this exactly as in your example, but for some reason it does not fire the sub.

 

what could be the cause?

 

Thank you!

0 Likes
Message 4 of 6

marcin_otręba
Advisor
Advisor

You trying to adress two different events to one sub?

OnDocumentChangeEvent

it will not work this way.

 

 

Hi, maybe you want to vote my:

Ideas

or check my apps:

DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 5 of 6

marcin_otręba
Advisor
Advisor

@SutherNe Solution is almost comlete only one small thing that you need set oAppevents

 

 

best way to add it in addin activate method :

 

g_inventorApplication = addInSiteObject.Application

 

oAppevents= g_inventorApplication.ApplicationEvents()

 

dont forget to declare g_inventorApplication like:

 

Public g_inventorApplication As Inventor.Application

 

or use your own object for inventor...

Hi, maybe you want to vote my:

Ideas

or check my apps:

DrawingTools   View&ColoringTools   MRUFolders

Message 6 of 6

Anonymous
Not applicable

Thanks, that did the trick!

0 Likes