Inventor VB.NET handling events

Inventor VB.NET handling events

Patrick1323
Enthusiast Enthusiast
3,611 Views
6 Replies
Message 1 of 7

Inventor VB.NET handling events

Patrick1323
Enthusiast
Enthusiast

Hello,

i want to run some code after an inventor document is set as "active".

I'm new to event handling in VB.NET and don't know where to start.

 

regards

0 Likes
3,612 Views
6 Replies
Replies (6)
Message 2 of 7

Jef_E
Collaborator
Collaborator

@elefantensocke wrote:

i want to run some code after an inventor document is set as "active".


Hi,

 

I'm also trying succesfully add a handler to the actived document, to do some cool stuff with it.

But it's not giving the result I was hoping for..

 

Are you using an AddIn to access the Events?

 

I'm currently using this in my AddIn but it's being 'triggered' 2x when I change document

' Application Events object.
Private m_AppEvents As ApplicationEvents

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

	' This method is called by Inventor when it loads the AddIn.
	' The AddInSiteObject provides access to the Inventor Application object.
	' The FirstTime flag indicates if the AddIn is loaded for the first time.

	' Initialize AddIn members.
	m_inventorApplication = addInSiteObject.Application

	' TODO:  Add ApplicationAddInServer.Activate implementation.
	' e.g. event initialization, command creation etc.

	CreateRibbonControls()

	' Create the dockable window
	CreateDockableWindow()

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

I suppose we are trying to do the same? When you switch or open document: do something..



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

Inventor 2014 SP2
0 Likes
Message 3 of 7

Anonymous
Not applicable

The application events fire twice, once before the event (in this case activating the document) and again afterwards.

so you need an if query to tell it when to do so.

 

I've added this line in just after I've declare the addinserver as below:

Private WithEvents oAppEvents As ApplicationEvents

 

Namespace YourGreatAddin

    <ProgIdAttribute("YourGreatAddin.StandardAddInServer"), _
    GuidAttribute("E6EA2F87-0494-4C0D-9E05-63DEB55637FB")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer
        'Application events object
        Private WithEvents oAppEvents As ApplicationEvents

  

Then this code further down, after all the addin activation and user interface subs:

This gives you the option of firing the code before or after, either way it will only fire once.

 

        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
            'Will fire after activating document
            If BeforeOrAfter = EventTimingEnum.kBefore Then
                'Put all your cool code here
            End If
            'Will fire after activating document
            If BeforeOrAfter = EventTimingEnum.kAfter Then
                'Put all your cool code here
            End If

        End Sub

 

with the line oAppevents line added you should be able to select "oAppEvents" from the left dropdown and the right will the show a list of all available actions. (this is based upon Visual studio pro 2010)

 

Hope this helps.

Message 4 of 7

Jef_E
Collaborator
Collaborator

Figured out how to avoid the double trigger of the code.

 

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

            ' This method is called by Inventor when it loads the AddIn.
            ' The AddInSiteObject provides access to the Inventor Application object.
            ' The FirstTime flag indicates if the AddIn is loaded for the first time.

            ' Initialize AddIn members.
            m_inventorApplication = addInSiteObject.Application

            ' TODO:  Add ApplicationAddInServer.Activate implementation.
            ' e.g. event initialization, command creation etc.

            CreateRibbonControls()

            ' Create the dockable window
            CreateDockableWindow()

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

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

        End Sub

Add this to the code that is beeing fired

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

            If BeforeOrAfter = EventTimingEnum.kAfter Then
                dc.GetCurrentDocumentDescription()
            End If

        End Sub

 



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

Inventor 2014 SP2
0 Likes
Message 5 of 7

Jef_E
Collaborator
Collaborator

@Anonymous wrote:

The application events fire twice, once before the event (in this case activating the document) and again afterwards.

so you need an if query to tell it when to do so.

 

I've added this line in just after I've declare the addinserver as below:

Private WithEvents oAppEvents As ApplicationEvents

 

Namespace YourGreatAddin

    <ProgIdAttribute("YourGreatAddin.StandardAddInServer"), _
    GuidAttribute("E6EA2F87-0494-4C0D-9E05-63DEB55637FB")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer
        'Application events object
        Private WithEvents oAppEvents As ApplicationEvents

  

Then this code further down, after all the addin activation and user interface subs:

This gives you the option of firing the code before or after, either way it will only fire once.

 

        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
            'Will fire after activating document
            If BeforeOrAfter = EventTimingEnum.kBefore Then
                'Put all your cool code here
            End If
            'Will fire after activating document
            If BeforeOrAfter = EventTimingEnum.kAfter Then
                'Put all your cool code here
            End If

        End Sub

 

with the line oAppevents line added you should be able to select "oAppEvents" from the left dropdown and the right will the show a list of all available actions. (this is based upon Visual studio pro 2010)

 

Hope this helps.


 

 

You beat me, just figured that out 🙂

 

The section with events is cleaner. Thanks for that input. Grabbed my method from the www and it worked, never looked much into that.

 



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

Inventor 2014 SP2
0 Likes
Message 6 of 7

Jef_E
Collaborator
Collaborator

I implemented the event trigger, like you suggested.. but now it's not doing anything 😕

 

 

Namespace EM_AddIn
    <ProgIdAttribute("EM_AddIn.StandardAddInServer"),
    GuidAttribute("2d4ce7ec-a84f-4f4c-b46f-d7b3c7cc51da")>
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        ' Inventor application object.
        Public Shared m_inventorApplication As Inventor.Application

        ' Application Events object.
        Private WithEvents oAppEvents As ApplicationEvents

        ' User Input events object.
        Private WithEvents oUserInputEvent As UserInputEvents
        Private Sub OnActivateDocument(ByVal DocumentObject As Inventor._Document,
                                       ByVal BeforeOrAfter As Inventor.EventTimingEnum,
                                       ByVal Context As Inventor.NameValueMap,
                                       ByRef HandlingCode As Inventor.HandlingCodeEnum) _
                                       Handles oAppEvents.OnActivateDocument

            If BeforeOrAfter = EventTimingEnum.kAfter Then
                dc.GetCurrentDocumentDescription()
            End If

        End Sub
        Private Sub OnDocumentSelectedEvent() _
            Handles oUserInputEvent.OnSelect,
            oUserInputEvent.OnUnSelect
            dc.GetCurrentDocumentDescription()
        End Sub

 EDIT

 

Never mind, forgot the initialization...

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

            ' This method is called by Inventor when it loads the AddIn.
            ' The AddInSiteObject provides access to the Inventor Application object.
            ' The FirstTime flag indicates if the AddIn is loaded for the first time.

            ' Initialize AddIn members.
            m_inventorApplication = addInSiteObject.Application

            ' TODO:  Add ApplicationAddInServer.Activate implementation.
            ' e.g. event initialization, command creation etc.
            oAppEvents = m_inventorApplication.ApplicationEvents
            oUserInputEvent = m_inventorApplication.CommandManager.UserInputEvents
End Sub

 



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

Inventor 2014 SP2
0 Likes
Message 7 of 7

Anonymous
Not applicable

Strange,

 

here's a copy of my standardaddinserver.vb:

(mind and change the GUID)

Imports Inventor
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Namespace CompanyDrawingTools

    <ProgIdAttribute("SimpleAddIn.StandardAddInServer"), _
    GuidAttribute("E6EA2F87-0494-4C0D-9E05-63DEB55637FB")> _
    Public Class StandardAddInServer
    Implements Inventor.ApplicationAddInServer
    'Application events object
    Private WithEvents oAppEvents As ApplicationEvents


#Region "Data Members"

    Private m_inventorApplication As Inventor.Application

    'buttons
    Private m_addSlotOptionButton As AddSlotOptionButton
    Private m_drawSlotButton As DrawSlotButton
    Private m_toggleSlotStateButton As ToggleSlotStateButton
    Private m_PrintPDFButton As PrintPDFButton
    Private m_StudboltButton As StudboltButton

    'combo-boxes
    Private m_slotWidthComboBoxDefinition As ComboBoxDefinition
    Private m_slotHeightComboBoxDefinition As ComboBoxDefinition

    'events
    Private m_userInterfaceEvents As UserInterfaceEvents

    ' ribbon panel
    Private m_DrawingSlotRibbonPanel As RibbonPanel
    Private m_AssemsblyStudboltRibbonPanel As RibbonPanel

#End Region

#Region "ApplicationAddInServer Members"

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

        Try
            'the Activate method is called by Inventor when it loads the addin
            'the AddInSiteObject provides access to the Inventor Application object
            'the FirstTime flag indicates if the addin is loaded for the first time

            'initialize AddIn members
            m_inventorApplication = addInSiteObject.Application
            InvApp = addInSiteObject.Application
            Button.InventorApplication = m_inventorApplication

            'initialize event handlers
            m_userInterfaceEvents = m_inventorApplication.UserInterfaceManager.UserInterfaceEvents
            oAppEvents = m_inventorApplication.ApplicationEvents

            'retrieve the GUID for this class
            Dim addInCLSID As GuidAttribute
            addInCLSID = CType(System.Attribute.GetCustomAttribute(GetType(StandardAddInServer), GetType(GuidAttribute)), GuidAttribute)
            Dim addInCLSIDString As String
            addInCLSIDString = "{" & addInCLSID.Value & "}"



            Catch ex As Exception

        End Try

    End Sub

    Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate

        'the Deactivate method is called by Inventor when the AddIn is unloaded
        'the AddIn will be unloaded either manually by the user or
        'when the Inventor session is terminated

        Try
            'release objects


            Marshal.ReleaseComObject(m_inventorApplication)
            m_inventorApplication = Nothing

            System.GC.WaitForPendingFinalizers()
            System.GC.Collect()

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation

        'if you want to return an interface to another client of this addin,
        'implement that interface in a class and return that class object 
        'through this property

        Get
            Return Nothing
        End Get

    End Property

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

        'this method was used to notify when an AddIn command was executed
        'the CommandID parameter identifies the command that was executed

        'Note:this method is now obsolete, you should use the new
        'ControlDefinition objects to implement commands, they have
        'their own event sinks to notify when the command is executed

    End Sub

   

   

   

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

        If BeforeOrAfter = EventTimingEnum.kAfter Then
            Dim Desc As String = InvApp.ActiveDocument.DisplayName
                MsgBox("Document is called " & Desc)
        End If

    End Sub

#End Region

    End Class

End Namespace

 

This returns a message box with the name of the inventor document when activated just to show it working.

0 Likes