Onchange Event (Inventor Add-In)

Onchange Event (Inventor Add-In)

Anonymous
Not applicable
929 Views
3 Replies
Message 1 of 4

Onchange Event (Inventor Add-In)

Anonymous
Not applicable

Hello Everyone,

 

I have a problem with the onchange event. Please help me in this case.

 

Here's my code: Add-in!

 

Imports Inventor
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Namespace TTF
<ProgIdAttribute("TTF.StandardAddInServer"), _
GuidAttribute(g_simpleAddInClientID)> _
Public Class StandardAddInServer
Implements Inventor.ApplicationAddInServer

 

Private WithEvents m_uiEvents As UserInterfaceEvents

Private WithEvents m_onChangeEvents As DocumentEvents

 

#Region "ApplicationAddInServer Members"


Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
Try


g_inventorApplication = addInSiteObject.Application


m_uiEvents = g_inventorApplication.UserInterfaceManager.UserInterfaceEvents

 

If (Not g_inventorApplication.ActiveDocument Is Nothing) Then
m_onChangeEvents = g_inventorApplication.ActiveDocument.DocumentEvents
End If

 

' Add to the user interface, if it's the first time.
' If this add-in doesn't have a UI but runs in the background listening
' to events, you can delete this.
If firstTime Then
AddToUserInterface()
End If
Catch ex As Exception
MsgBox("Unexpected failure in the activation of the add-in ""TTF""" & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub


Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate
' Release objects.

m_onChangeEvents = Nothing

m_uiEvents = Nothing
g_inventorApplication = Nothing

System.GC.Collect()
System.GC.WaitForPendingFinalizers()
End Sub


Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation
Get
Return Nothing
End Get
End Property


Public Sub ExecuteCommand(ByVal commandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand

End Sub

Private Sub m_onchangeEvents_OnChange(ByVal ReasonsForChange As CommandTypesEnum,
ByVal BeforeOrAfter As EventTimingEnum,
ByVal Context As NameValueMap,
ByRef HandlingCode As HandlingCodeEnum) Handles m_onChangeEvents.OnChange

MsgBox("Onchange")

End Sub


#End Region

#Region "User interface definition"
Private Sub AddToUserInterface()

 

End Sub

Private Sub m_uiEvents_OnResetRibbonInterface(Context As NameValueMap) Handles m_uiEvents.OnResetRibbonInterface
' The ribbon was reset, so add back the add-ins user-interface.
AddToUserInterface()
End Sub


#End Region

End Class
End Namespace


Public Module Globals
' Inventor application object.
Public g_inventorApplication As Inventor.Application

' The unique ID for this add-in. If this add-in is copied to create a new add-in
' you need to update this ID along with the ID in the .manifest file, the .addin file
' and create a new ID for the typelib GUID in AssemblyInfo.vb
Public Const g_simpleAddInClientID As String = "45065451-f806-4680-8d84-4d2166b2b77a"
Public Const g_addInClientID As String = "{" & g_simpleAddInClientID & "}"
End Module

0 Likes
Accepted solutions (1)
930 Views
3 Replies
Replies (3)
Message 2 of 4

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Anonymous 

I tested it this way and it works for me 🙂

 

I use ApplicationEvents.OnActivateDocument to see when the active document changes.

Then I set m_onChangeEvents to be the document events for that document.

 

Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        Private WithEvents m_uiEvents As UserInterfaceEvents

        Private WithEvents m_onChangeEvents As DocumentEvents

        Private WithEvents oAppEvents As ApplicationEvents




#Region "ApplicationAddInServer Members"


        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
            Try


                g_inventorApplication = addInSiteObject.Application

                oAppEvents = g_inventorApplication.ApplicationEvents

                m_uiEvents = g_inventorApplication.UserInterfaceManager.UserInterfaceEvents





                ' Add to the user interface, if it's the first time.
                ' If this add-in doesn't have a UI but runs in the background listening
                ' to events, you can delete this.
                If firstTime Then
                    AddToUserInterface()
                End If
            Catch ex As Exception
                MsgBox("Unexpected failure in the activation of the add-in ""TTF""" & vbCrLf & vbCrLf & ex.Message)
            End Try
        End Sub


        Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate
            ' Release objects.

            m_onChangeEvents = Nothing

            m_uiEvents = Nothing
            g_inventorApplication = Nothing

            System.GC.Collect()
            System.GC.WaitForPendingFinalizers()
        End Sub


        Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation
            Get
                Return Nothing
            End Get
        End Property


        Public Sub ExecuteCommand(ByVal commandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand

        End Sub

        Private Sub m_onchangeEvents_OnChange(ByVal ReasonsForChange As CommandTypesEnum,
        ByVal BeforeOrAfter As EventTimingEnum,
        ByVal Context As NameValueMap,
        ByRef HandlingCode As HandlingCodeEnum) Handles m_onChangeEvents.OnChange

            MsgBox("Onchange")

        End Sub


#End Region

#Region "User interface definition"
        Private Sub AddToUserInterface()



        End Sub

        Private Sub m_uiEvents_OnResetRibbonInterface(Context As NameValueMap) Handles m_uiEvents.OnResetRibbonInterface
            ' The ribbon was reset, so add back the add-ins user-interface.
            AddToUserInterface()
        End Sub

        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
                m_onChangeEvents = DocumentObject.DocumentEvents
            End If

        End Sub


#End Region

    End Class
0 Likes
Message 3 of 4

JhoelForshav
Mentor
Mentor

Also, see how I used BeforeOrAfter to prevent oAppEvents_OnActivateDocument from fireing twice.

Your sub m_onchangeEvents_OnChange will give one messagebox before the document change and one after the change as it is written now 🙂

 

 

And i see now that i forgot to set oAppEvents to nothing in the deactivate sub... But you get the idea.

Message 4 of 4

Anonymous
Not applicable

Many thanks Mr. Jhoel Forshav

0 Likes