Get add-in to show up in CommandManager

Get add-in to show up in CommandManager

mr_ensing
Advocate Advocate
747 Views
3 Replies
Message 1 of 4

Get add-in to show up in CommandManager

mr_ensing
Advocate
Advocate

Case:

An existing add-in without buttons or other interface elements. It is triggered by ApplicationEvents.

The goal:

I'd like to access the add-in from VBA. Apparently the way to do this is via the CommandManager.

The problem:

The code basically is the add-in template with a private sub DoStuff added. 

There is a comment above Public Sub ExecuteCommand  about it being obsolete. But what must i do to get this thing available in the CommandManager?

Code:

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

Namespace SaveCheck2
    <ProgIdAttribute("SaveCheck2.StandardAddInServer"), _
    GuidAttribute("c68fc4b0-7712-432e-8ad5-7fdf9fb5adc7")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        'Private WithEvents m_uiEvents As UserInterfaceEvents
        'Private WithEvents m_sampleButton As ButtonDefinition

        Private m_inventorApplication As Inventor.Application
        Private WithEvents m_ApplicationEvents As Inventor.ApplicationEvents

#Region "ApplicationAddInServer Members"

        ' 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. However, with the introduction of the ribbon this argument is always true.
        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
            ' Initialize AddIn members.
            'g_inventorApplication = addInSiteObject.Application

            ' Connect to the user-interface events to handle a ribbon reset.
            'm_uiEvents = g_inventorApplication.UserInterfaceManager.UserInterfaceEvents

            ' TODO: Add button definitions.

            ' Sample to illustrate creating a button definition.
			'Dim largeIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.YourBigImage)
			'Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.YourSmallImage)
            'Dim controlDefs As Inventor.ControlDefinitions = g_inventorApplication.CommandManager.ControlDefinitions
            'm_sampleButton = controlDefs.AddButtonDefinition("Command Name", "Internal Name", CommandTypesEnum.kShapeEditCmdType, AddInClientID)

            ' Add to the user interface, if it's the first time.
            'If firstTime Then
            ' AddToUserInterface()
            'End If

            m_inventorApplication = addInSiteObject.Application
            m_ApplicationEvents = m_inventorApplication.ApplicationEvents

        End Sub

        Private Sub m_ApplicationEvents_OnSaveDocument(ByVal DocumentObject As Inventor.Document, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles m_ApplicationEvents.OnSaveDocument

            If (BeforeOrAfter = Inventor.EventTimingEnum.kBefore) Then
                Call DoStuff(DocumentObject)
            End If

        End Sub

        Private Sub DoStuff(ByVal DocumentObject As Inventor.Document)
            'some copying of iProperties and other basic idw-commands
        End Sub

        ' 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.
        Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate

            ' TODO:  Add ApplicationAddInServer.Deactivate implementation

            ' Release objects.
            'm_uiEvents = Nothing
            'g_inventorApplication = Nothing

            Marshal.ReleaseComObject(m_inventorApplication)
            m_inventorApplication = Nothing

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

        ' 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.
        Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation
            Get
                Return Nothing
            End Get
        End Property

        ' Note:this method is now obsolete, you should use the 
        ' ControlDefinition functionality for implementing commands.
        Public Sub ExecuteCommand(ByVal commandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand
        End Sub

#End Region

#Region "User interface definition"
        ' Sub where the user-interface creation is done.  This is called when
        ' the add-in loaded and also if the user interface is reset.
        Private Sub AddToUserInterface()
            ' This is where you'll add code to add buttons to the ribbon.

            '** Sample to illustrate creating a button on a new panel of the Tools tab of the Part ribbon.

            '' Get the part ribbon.
            'Dim partRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Part")

            '' Get the "Tools" tab.
            'Dim toolsTab As RibbonTab = partRibbon.RibbonTabs.Item("id_TabTools")

            '' Create a new panel.
            'Dim customPanel As RibbonPanel = toolsTab.RibbonPanels.Add("Sample", "MysSample", AddInClientID)

            '' Add a button.
            'customPanel.CommandControls.AddButton(m_sampleButton)
        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

        ' Sample handler for the button.
        'Private Sub m_sampleButton_OnExecute(Context As NameValueMap) Handles m_sampleButton.OnExecute
        '    MsgBox("Button was clicked.")
        'End Sub
#End Region

    End Class
End Namespace

Public Module Globals
    ' Inventor application object.
    Public g_inventorApplication As Inventor.Application
End Module
0 Likes
Accepted solutions (1)
748 Views
3 Replies
Replies (3)
Message 2 of 4

Frederick_Law
Mentor
Mentor
Accepted solution
m_sampleButton = controlDefs.AddButtonDefinition("Command Name", "Internal Name", CommandTypesEnum.kShapeEditCmdType, AddInClientID)

I believe it's the "Internal Name". 

If you create a button_def, you can use Internal Name in VBA.

You don't need to add icon or show the button.

Message 3 of 4

mr_ensing
Advocate
Advocate

Thanks.

I had to un-comment some lines and add some deleted lines back in. But the line you referredto is error free now:

m_sampleButton = controlDefs.AddButtonDefinition("SaveCheck2Command", "SaveCheck2InternalName", CommandTypesEnum.kShapeEditCmdType, AddInClientID)

I also un-commented this sub and added a call to my DoStuff sub:

        ' Sample handler for the button.
        Private Sub m_sampleButton_OnExecute(Context As NameValueMap) Handles m_sampleButton.OnExecute
            'MsgBox("Button was clicked.")
            Dim DocumentObject As Inventor.Document = m_inventorApplication.ActiveDocument
            Call DoStuff(DocumentObject)
        End Sub

 But, so far it does not show up in my list of commands.

0 Likes
Message 4 of 4

mr_ensing
Advocate
Advocate

I've been at it for a while. Not exactly sure when, but at some point it started working. 

Marking your comment as solution.

0 Likes