New part event - vb.net addin

New part event - vb.net addin

Eyal.Admon
Contributor Contributor
718 Views
4 Replies
Message 1 of 5

New part event - vb.net addin

Eyal.Admon
Contributor
Contributor

Hello,

I have an addin that adds a button to launch a simple sub.

I want to have the sub run everytime the user creates a new part.

when i create a new part nothing happens.

i have this in my code:(only showing relevent stuff)

 

 Public module Globals

    Public m_inventorApplication As Inventor.Application

.....

 

Public Class StandardAddInServer

    Private WithEvents m_AppEvents As FileUIEvents

....

 

public sub activate 

  m_inventorApplication = addInSiteObject.Application

  m_AppEvents = m_inventorApplication.FileUIEvents

---

and i'm trying to run this sub:

 

Private Sub m_AppEvents_Onnewfile(TemplateDir As String, ParentHWND As Long, TemplateFileName As String, Context As NameValueMap, HandlingCode As HandlingCodeEnum) Handles m_AppEvents.OnFileNewDialog Event


msgbox("new file")
End Sub

 

 

 

 

Thanks.

 

0 Likes
Accepted solutions (1)
719 Views
4 Replies
Replies (4)
Message 2 of 5

yan.gauthier
Advocate
Advocate

Hi, 

 

you need to subscribe to the event.
I am unsure of how it would be done in VB.NET, but here is an example in C#:

 

AddInServer.InvApp.ActiveEditDocument.DocumentEvents.OnChangeSelectSet += DocumentEvents_OnChangeSelectSet;

private void DocumentEvents_OnChangeSelectSet(EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
        {
            if (BeforeOrAfter == EventTimingEnum.kAfter)
            {
                if (AddInServer.InvApp.ActiveEditDocument.SelectSet.Count == 1 && AddInServer.InvApp.ActiveEditDocument.SelectSet[1] is ComponentOccurrence componentOccurrence)
                {
                    syncContext.Post(o => eSForm.ReloadViewModel(componentOccurrence, new ESModel(componentOccurrence)), null);
                }
                
            }
            HandlingCode = HandlingCodeEnum.kEventNotHandled; // Let inventor do his things
        }

according to this source, you need to use the AddHandler keywords in order to do something similar

 

https://stackoverflow.com/questions/7220438/subscribing-events-in-vb-net

 

Regards,

0 Likes
Message 3 of 5

WCrihfield
Mentor
Mentor

When coding with vb.net, there are two main ways to set-up event 'listeners'/handlers.  (AddHandler...,AddressOf... / RemoveHandler...,AddressOf...) & (WithEvents / Handles).  When using 'WithEvents & Handles', you must declare the WithEvents variable at the level above the Sub that is to 'handle' the event, not within another Sub at the same level.  When using AddHandler, RemoveHandler & AddressOf, you can use them within different subs at the same level.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 5

Eyal.Admon
Contributor
Contributor

Hi, thank you for your reply...

I got the addin to catch an OnActivateDocument event with this:

 

 

 Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        Private m_inventorApplication As Inventor.Application
        Dim WithEvents m_AppEvents As ApplicationEvents
 
   Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
            
               
                m_inventorApplication = addInSiteObject.Application
                m_AppEvents = m_inventorApplication.ApplicationEvents
end sub


Private Sub m_AppEvents_OnActivateDocument(ByVal DocumentObject As Inventor._Document, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles m_AppEvents.OnActivateDocument
            MsgBox("test")
        End Sub

 

I still cant catch a new part event though...

0 Likes
Message 5 of 5

Eyal.Admon
Contributor
Contributor
Accepted solution

Thanks for the reply. 

i got this to work with:

 Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        Private m_inventorApplication As Inventor.Application
        Dim WithEvents e_AppEvents As FileUIEvents
 
   Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
            
               
                m_inventorApplication = addInSiteObject.Application
                e_AppEvents = m_inventorApplication.FileUIEvents
end sub


 Private Sub e_AppEvents_Onnewfile() Handles e_AppEvents.OnFileNewDialog
            MsgBox("new file")
        End Sub
0 Likes