How to Add Child Controls to the Context Menu in Autodesk Inventor

by Chandra shekar Gopal,

In Autodesk Inventor, customizing the context menu (the right-click menu) can significantly enhance user experience by adding commonly used commands or custom tools. However, there is no direct method currently available for adding child controls directly to the context menu. While this functionality isn’t natively supported, there is a workaround using VBA (Visual Basic for Applications) to achieve this.

In this blog, we will walk through the steps and code required to add child controls (such as “Update”, “Delete”, and “Copy”) to the context menu in Autodesk Inventor.


The Workaround: Using VBA to Add Child Controls to the Context Menu

Although there isn’t a built-in option for directly adding child controls to the context menu, you can work around this by creating a custom command bar and populating it with buttons. You can then add this custom command bar as a popup to the existing context menu.

Here’s a simple guide to implement this workaround:

Step 1: Initialize the User Input Events

In your VBA code, you need to start by initializing the UserInputEvents object, which listens for user actions, including the context menu interaction.


Private WithEvents UIE As UserInputEvents
Public Sub Init()
   Set UIE = ThisApplication.CommandManager.UserInputEvents
   Do
   DoEvents
   Loop
End Sub

Step 2: Handling the Context Menu

Next, you can use the UIE_OnContextMenu event to handle the right-click event and add the custom controls to the context menu. In this example, we are creating a custom command bar (called "CustomBar") and populating it with buttons.


Private Sub UIE_OnContextMenu(ByVal SelectionDevice As SelectionDeviceEnum, _
                              ByVal AdditionalInfo As NameValueMap, _
                              ByVal CommandBar As CommandBar)

    Dim oBar As CommandBar

    On Error Resume Next
    ' Delete any existing "CustomBar"
    Set oBar = ThisApplication.UserInterfaceManager.CommandBars("CustomBar")
    oBar.Delete
    On Error GoTo 0

    ' Create a new custom command bar
    Set oBar = ThisApplication.UserInterfaceManager.CommandBars.Add( _
        "CustomBar", "CustomBar", kPopUpCommandBar)

    ' Adding child controls (buttons)
    oBar.Controls.AddButton ThisApplication.CommandManager.ControlDefinitions("AppLocalUpdateCmd")
    oBar.Controls.AddButton ThisApplication.CommandManager.ControlDefinitions("AppDeleteCmd")
    oBar.Controls.AddButton ThisApplication.CommandManager.ControlDefinitions("AppCopyCmd")

    ' Add the custom bar as a popup in the context menu
    CommandBar.Controls.AddPopup oBar

End Sub

Step 3: Running the Code

Once the VBA code is executed, a custom command bar named "CustomBar" will be added to the context menu with the following child controls:

  • Update
  • Delete
  • Copy

This setup allows you to extend the context menu with additional functionality that can be customized according to your needs.

Here’s what the context menu would look like after the code runs (as shown in the image below):

Context menu

Conclusion

While Autodesk Inventor does not currently provide a direct method to add child controls to the context menu, this workaround using VBA allows you to create custom command bars and enhance the user interface with added functionality. With this method, you can improve your workflow by providing quick access to commands like Update, Delete, and Copy right from the context menu.

As always, keep an eye on Autodesk’s updates, as future releases may provide a more native way to handle this customization, making it easier to implement in your applications.


Comments

2 responses to “How to Add Child Controls to the Context Menu in Autodesk Inventor”

  1. Limin Ma Avatar
    Limin Ma

    Can this code be convert to VB.NET and work using VB.NET?

  2. Chandra shekar Gopal Avatar
    Chandra shekar Gopal

    Hi Limin,
    Below VB.net may be helpful for your question.
    Imports Inventor
    Imports System.Runtime.InteropServices
    Public Class ContextMenuAddin
    ‘ Declare the UserInputEvents object
    Private WithEvents UIE As UserInputEvents
    Private ThisApplication As Inventor.Application
    Public Sub Init(app As Inventor.Application)
    ‘ Initialize the Inventor application object
    ThisApplication = app
    ‘ Initialize the UserInputEvents
    UIE = ThisApplication.CommandManager.UserInputEvents
    End Sub
    ‘ Event handler for context menu
    Private Sub UIE_OnContextMenu(ByVal SelectionDevice As SelectionDeviceEnum, ByVal AdditionalInfo As NameValueMap, ByVal CommandBar As CommandBar)
    Dim oBar As CommandBar = Nothing
    Try
    ‘ Attempt to get the existing “CustomBar”
    oBar = ThisApplication.UserInterfaceManager.CommandBars.Item(“CustomBar”)
    oBar.Delete() ‘ Delete the existing “CustomBar” if it exists
    ‘ Create a new CommandBar for the context menu
    oBar = ThisApplication.UserInterfaceManager.CommandBars.Add(“CustomBar”, “CustomBar”, CommandBarTypeEnum.kPopUpCommandBar)
    ‘ Add buttons to the custom bar (child controls)
    oBar.Controls.AddButton(ThisApplication.CommandManager.ControlDefinitions.Item(“AppLocalUpdateCmd”))
    oBar.Controls.AddButton(ThisApplication.CommandManager.ControlDefinitions.Item(“AppDeleteCmd”))
    oBar.Controls.AddButton(ThisApplication.CommandManager.ControlDefinitions.Item(“AppCopyCmd”))
    ‘ Add the custom bar as a popup to the context menu
    CommandBar.Controls.AddPopup(oBar)
    Catch ex As Exception
    ‘ Handle any errors
    MsgBox(“Error occurred: ” & ex.Message)
    End Try
    End Sub
    End Class

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading