Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can't get OnExecute event to fire for my VB.Net add-in

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
PeopleInstance
1025 Views, 2 Replies

Can't get OnExecute event to fire for my VB.Net add-in

I've been struggling for a week to get this to work, but I can't get OnExecute to do anything. I've created a basic form that I can get to show up. I've also got a custom button that shows up as expected. When I add a Private Sub customButton_OnExecute() per Brian Ekins' tutorial, nothing happens when I click the button. Here's my code as of right now. I'm not sure where else to look for an answer for this. Any  help of direction would be most appreciated.

 

Imports Inventor

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


Namespace iLogicPOC
    <ProgIdAttribute("iLogicPOC.StandardAddInServer"), _
    GuidAttribute("51003206-914f-4244-8140-803c09a678bc")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        ' Inventor application object.
        Private m_InventorApplication As Inventor.Application = Nothing
        Public WithEvents customButton As ButtonDefinition = Nothing
        Private WithEvents m_uiEvents As UserInterfaceEvents = Nothing
        Private m_clientID As String = "{51003206-914f-4244-8140-803c09a678bc}"

#Region "ApplicationAddInServer Members"

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

            ' Save reference to the Application object in member variable.
            m_InventorApplication = addInSiteObject.Application

            ' Get a reference to the UserInterfaceManager object. 
            Dim UIManager As Inventor.UserInterfaceManager = _
                                                   m_InventorApplication.UserInterfaceManager

            ' Get a reference to the ControlDefinitions object. 
            Dim controlDefs As ControlDefinitions = _
                                      m_InventorApplication.CommandManager.ControlDefinitions

            ' Get the images from the resources.  They are stored as .Net images and the
            ' PictureConverter class is used to convert them to IPictureDisp objects, which
            ' the Inventor API requires. 
            Dim smallPicture As stdole.IPictureDisp = _
                    PictureConverter.ImageToPictureDisp(My.Resources.rectffSml)

            Dim largePicture As stdole.IPictureDisp = _
                    PictureConverter.ImageToPictureDisp(My.Resources.rectffLrg)

            ' Create the button definition. 
            customButton = controlDefs.AddButtonDefinition("RECTFF", "UIRibbonSampleOne", _
                                                 CommandTypesEnum.kNonShapeEditCmdType, _
                                                 m_clientID, , , smallPicture, largePicture)

            ' Call the function to add information to the user-interface.
            If firstTime Then
                CreateUserInterface()
            End If

            ' Connect to UI events to be able to handle a UI reset.
            m_uiEvents = m_InventorApplication.UserInterfaceManager.UserInterfaceEvents
        End Sub


        ' Creates the add-in’s UI in the ribbon.
        Private Sub CreateUserInterface()
            ' Get a reference to the UserInterfaceManager object. 
            Dim UIManager As Inventor.UserInterfaceManager = _
                                                   m_inventorApplication.UserInterfaceManager

            ' Get the zero doc ribbon.
            Dim zeroRibbon As Inventor.Ribbon = UIManager.Ribbons.Item("ZeroDoc")

            ' Get the getting started tab.
            Dim startedTab As Inventor.RibbonTab = zeroRibbon.RibbonTabs.Item("id_GetStarted")

            ' Get the new features panel.
            Dim newFeaturesPanel As Inventor.RibbonPanel
            newFeaturesPanel = startedTab.RibbonPanels.Item("id_Panel_GetStartedWhatsNew")

            ' Add a button to the panel, using the previously created button definition.
            newFeaturesPanel.CommandControls.AddButton(customButton, True)
        End Sub

        Private Sub customButton_OnExecute(ByVal Context As Inventor.NameValueMap)
            Dim mainForm As New Form1
            mainForm.Show()
        End Sub
        Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate

            ' This 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.

            ' TODO:  Add ApplicationAddInServer.Deactivate implementation

            ' Release objects.
            m_InventorApplication = Nothing

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

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

            ' This property is provided to allow the AddIn to expose an API 
            ' of its own to other programs. Typically, this  would be done by
            ' implementing the AddIn's API interface in a class and returning 
            ' that class object through this property.

            Get
                Return Nothing
            End Get

        End Property

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

            ' Note:this method is now obsolete, you should use the 
            ' ControlDefinition functionality for implementing commands.

        End Sub

#End Region
        <System.ComponentModel.DesignerCategory("")> _
        Friend Class PictureConverter
            Inherits System.Windows.Forms.AxHost

            Private Sub New()
                MyBase.New(String.Empty)
            End Sub

            Public Shared Function ImageToPictureDisp( _
                                   ByVal image As System.Drawing.Image) As stdole.IPictureDisp
                Return CType(GetIPictureDispFromPicture(image), stdole.IPictureDisp)
            End Function
        End Class


        '' This property uses reflection to get the value for the GuidAttribute attached to the class.
        'Public Shared ReadOnly Property AddInGuid(ByVal t As Type) As String
        '    Get
        '        Dim guid As String = ""
        '        Try
        '            Dim customAttributes() As Object = t.GetCustomAttributes(GetType(GuidAttribute), False)
        '            Dim guidAttribute As GuidAttribute = CType(customAttributes(0), GuidAttribute)
        '            guid = "{" + guidAttribute.Value.ToString() + "}"
        '        Finally
        '            AddInGuid = guid
        '        End Try
        '    End Get
        'End Property

    End Class
End Namespace

 

 

2 REPLIES 2
Message 2 of 3

Hi,

 

You can use "ControlDefinition functionality for implementing commands."

 

Something like AddHandler _defComando1.OnExecute, AddressOf Command3Execute

 

Then have a method Command3Execute

 

Regards
Santosh
Message 3 of 3

Wooohoooo! It's good to see that working. I guess it just never clicked that I would need to add the event handler before I used it. I think I've been spoiled up to this point! Everything works as it is supposed to. Here's the code I added:

 

            'Link the button objects with the event
            AddHandler customButton.OnExecute, AddressOf customButton_OnExecute

 

Here's all of the code in present form for the StandardAddInServer module:

 

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


Namespace iLogicPOC
    <ProgIdAttribute("iLogicPOC.StandardAddInServer"), _
    GuidAttribute("51003206-914f-4244-8140-803c09a678bc")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        ' Inventor application object.
        Private m_InventorApplication As Inventor.Application = Nothing
        Public WithEvents customButton As ButtonDefinition = Nothing
        Private WithEvents m_uiEvents As UserInterfaceEvents = Nothing
        Private m_clientID As String = "{51003206-914f-4244-8140-803c09a678bc}"

#Region "ApplicationAddInServer Members"

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

            ' Save reference to the Application object in member variable.
            m_InventorApplication = addInSiteObject.Application

            ' Get a reference to the UserInterfaceManager object. 
            Dim UIManager As Inventor.UserInterfaceManager = _
                                                   m_InventorApplication.UserInterfaceManager

            ' Get a reference to the ControlDefinitions object. 
            Dim controlDefs As ControlDefinitions = _
                                      m_InventorApplication.CommandManager.ControlDefinitions

            ' Get the images from the resources.  They are stored as .Net images and the
            ' PictureConverter class is used to convert them to IPictureDisp objects, which
            ' the Inventor API requires. 
            Dim smallPicture As stdole.IPictureDisp = _
                    PictureConverter.ImageToPictureDisp(My.Resources.rectffSml)

            Dim largePicture As stdole.IPictureDisp = _
                    PictureConverter.ImageToPictureDisp(My.Resources.rectffLrg)

            ' Create the button definition. 
            customButton = controlDefs.AddButtonDefinition("My Button", "UIRibbonSampleOne", _
                                                 CommandTypesEnum.kNonShapeEditCmdType, _
                                                 m_clientID, , , smallPicture, largePicture)

            'Link the button objects with the event
            AddHandler customButton.OnExecute, AddressOf customButton_OnExecute

            ' Call the function to add information to the user-interface.
            If firstTime Then
                CreateUserInterface()
            End If

            ' Connect to UI events to be able to handle a UI reset.
            m_uiEvents = m_InventorApplication.UserInterfaceManager.UserInterfaceEvents
        End Sub


        ' Creates the add-in’s UI in the ribbon.
        Private Sub CreateUserInterface()
            ' Get a reference to the UserInterfaceManager object. 
            Dim UIManager As Inventor.UserInterfaceManager = _
                                                   m_inventorApplication.UserInterfaceManager

            ' Get the zero doc ribbon.
            Dim zeroRibbon As Inventor.Ribbon = UIManager.Ribbons.Item("ZeroDoc")

            ' Get the getting started tab.
            Dim startedTab As Inventor.RibbonTab = zeroRibbon.RibbonTabs.Item("id_GetStarted")

            ' Get the new features panel.
            Dim newFeaturesPanel As Inventor.RibbonPanel
            newFeaturesPanel = startedTab.RibbonPanels.Item("id_Panel_GetStartedWhatsNew")

            ' Add a button to the panel, using the previously created button definition.
            newFeaturesPanel.CommandControls.AddButton(customButton, True)
        End Sub
        Private Sub customButton_OnExecute()
            Dim mainForm As New Form1
            mainForm.Show()
        End Sub
        Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate

            ' This 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.

            ' TODO:  Add ApplicationAddInServer.Deactivate implementation

            ' Release objects.
            m_InventorApplication = Nothing

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

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

            ' This property is provided to allow the AddIn to expose an API 
            ' of its own to other programs. Typically, this  would be done by
            ' implementing the AddIn's API interface in a class and returning 
            ' that class object through this property.

            Get
                Return Nothing
            End Get

        End Property

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

            ' Note:this method is now obsolete, you should use the 
            ' ControlDefinition functionality for implementing commands.

        End Sub

#End Region
        <System.ComponentModel.DesignerCategory("")> _
        Friend Class PictureConverter
            Inherits System.Windows.Forms.AxHost

            Private Sub New()
                MyBase.New(String.Empty)
            End Sub

            Public Shared Function ImageToPictureDisp( _
                                   ByVal image As System.Drawing.Image) As stdole.IPictureDisp
                Return CType(GetIPictureDispFromPicture(image), stdole.IPictureDisp)
            End Function
        End Class
    End Class
End Namespace

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report