Trouble using ApplicationEvents when creating a addin

Trouble using ApplicationEvents when creating a addin

K.clement
Contributor Contributor
378 Views
6 Replies
Message 1 of 7

Trouble using ApplicationEvents when creating a addin

K.clement
Contributor
Contributor

Hello, 

I have a question about Applicationevents when coding a addin.

I'm using the the autodesk template for visual studio, but have trouble triggering the applicationevents.

I created a new .vb file in the addin with this code:

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

Module MyNewModule
Public Class MyNewClass

Public WithEvents oApplicationEvents As Inventor.ApplicationEvents

Private Sub MyNewClass_Initialize()
oApplicationEvents = oApp.ApplicationEvents
end sub

Private Sub oApplicationEvents_OnSaveDocument(ByVal DocumentObject As Document, ByVal BeforeOrAfter As EventTimingEnum, ByVal Context As NameValueMap, HandlingCode As HandlingCodeEnum)
If BeforeOrAfter = EventTimingEnum.kAfter Then
MsgBox("Hello there!")
End If
end sub

end Class
end module

I know that I have to do something like this, but I'm having trouble making the msgbox/sub trigger.

I found this in The API Documentation : https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-A7B286A0-5234-49B0-8C89-00411A6B1173 

 

Can anyone with a bigbrain correct my code? (It's basicly the same as the EventTrigger in inventor) 

Thx allot k.

 

 

 

 

 

 

 

0 Likes
Accepted solutions (1)
379 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @K.clement.  I don't really do add-ins, due to corporate restrictions, but I have created several event handler codes in vb.net/iLogic, so I may may be able to help some.  Some things are a bit different in VBA than they are in vb.net.  In VBA you can only use the WithEvents call-out, but in vb.net you can use both WithEvents/Handles and AddHandler/RemoveHandler call-outs for declaring object variables that you will be accessing the Events of.  I believe in vb.net you have to specify 'ByRef' before the HandlingCode variable, for one.  Also, I believe that when using the WithEvents call-out, you should then use the Handles keyword at the end of the Sub routine's definition line, followed by which event it is meant to handle.  (Sub DoSomething(oInput1 As Object) Handles Object.EventName)  I'm not too sure if the 'initialize' sub is going to do what you are hoping it will, but I'm thinking that sub routines need to be called to run before they will do anything, similar to using a New() routine.  Also, I just have to assume that the 'oApp' variable has been declared somewhere and a value set to it, but that seems to obvious.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

K.clement
Contributor
Contributor
I'll tryout some thing, and ill let you know. oApp is just my inventor application
0 Likes
Message 4 of 7

JelteDeJong
Mentor
Mentor

Your "oApplicationEvents_OnSaveDocument" should be like this.

Private Sub oApplicationEvents_OnSaveDocument(DocumentObject As _Document, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) Handles oApplicationEvents.OnSaveDocument
    If BeforeOrAfter = EventTimingEnum.kAfter Then
        MsgBox("Hello there!")
    End If
End Sub

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 5 of 7

K.clement
Contributor
Contributor
Hey , you are the guy from http://www.hjalte.nl/ ?
Thx for the website, I used it a lot these last weeks 🙂
I'll give you an update tomorrow
0 Likes
Message 6 of 7

K.clement
Contributor
Contributor

 

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

Module MyNewModule
    Public Class MyNewClass

        Public WithEvents oApplicationEvents As Inventor.ApplicationEvents

        Private Sub MyNewClass_Initialize()
            oApplicationEvents = oApp.ApplicationEvents
        End Sub

        Private Sub oApplicationEvents_OnSaveDocument(DocumentObject As _Document, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) Handles oApplicationEvents.OnSaveDocument
            MsgBox("Hello there!")
            If BeforeOrAfter = EventTimingEnum.kBefore Then
                MsgBox(DocumentObject.FullFileName)
            End If
        End Sub

    End Class
End Module

 

hmm, The messagebox still does not trigger =(

additional question, why do you type a underscore before document?

 

 

0 Likes
Message 7 of 7

K.clement
Contributor
Contributor
Accepted solution

 

 

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

Module MyNewModule1
    Public Class MyNewClass
        Implements Inventor.ApplicationAddInServer
        Public WithEvents oApplicationEvents As Inventor.ApplicationEvents
        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
            oApplicationEvents = App.ApplicationEvents
        End Sub
        Private Sub oApplicationEvents_OnSaveDocument(DocumentObject As _Document, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) Handles oApplicationEvents.OnSaveDocument
            If BeforeOrAfter = EventTimingEnum.kBefore Then
                MsgBox(DocumentObject.FullFileName)
            End If
        End Sub
        Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate
            oApplicationEvents = Nothing
            App = 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
    End Class
End Module

 

 

Aha!! It looks like doing this whole shabang works! 

but is there not a better way to do this?

without the activate, deactivate, execute, ...

It also only works when I put in in the default .vb file, "StandardAddInServer" it does not work when I put it in another file