By Barbara Han
Issue
In our application, we have command that are context sensitive. These commands can run only if, e.g. a drawing view is selected. Unfortunately Inventor add my command to the repeat last command menu, so my command can be called when no drawing view is selected. Is there a way to avoid the context command from repeating?
Solution
If adding the context sensitive command to the command category "Context Menu", this will prevent the command from repeating. The API help document has the following statement:
Remarks
Since Autodesk Inventor now has a "repeat last command" context menu feature, care should be exercised when adding custom commands that should execute only in very specific circumstances. …Commands which are in the Context Menu category will not participate in command repeat or customization.
The following VB.NET sample code snippet demonstrates this way:
' Inventor application object.
Private m_inApp As Inventor.Application
'ButtonDefinitions
Private myButtonDef As ButtonDefinition
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.
' Initialize AddIn members.
m_inApp = addInSiteObject.Application
Dim UIEvts As UserInputEvents = m_inApp.CommandManager.UserInputEvents
AddHandler UIEvts.OnContextMenu, AddressOf oUserInput_OnContextMenu
' Retrieve the GUID of this class for later use
Dim addInCLS As GuidAttribute
addInCLS = CType(System.Attribute.GetCustomAttribute(GetType(StandardAddInServer), _
GetType(GuidAttribute)), GuidAttribute)
Dim addInCLSIDString As String = "{" & addInCLS.Value & "}"
' Create the button definition objects
myButtonDef = m_inApp.CommandManager.ControlDefinitions.AddButtonDefinition( _
"Test", "vbButton:cloneBtn", _
CommandTypesEnum.kShapeEditCmdType, _
addInCLSIDString, "test", _
"")
' Link the button objects with the event
AddHandler myButtonDef.OnExecute, AddressOf MyButtonDefinition_OnExecute
Dim pCategory As CommandCategory
pCategory = m_inApp.CommandManager.CommandCategories.Item("fwxcontextmenucategory")
pCategory.Add(myButtonDef)
' End UI customization.
End Sub
Private Sub oUserInput_OnContextMenu(ByVal SelectionDevice As SelectionDeviceEnum, _
ByVal AdditionalInfo As NameValueMap, _
ByVal CommandBar As CommandBar)
Dim obj As Object
If m_inApp.ActiveDocument.SelectSet.Count = 1 Then
obj = m_inApp.ActiveDocument.SelectSet.Item(1)
If obj.Type = ObjectTypeEnum.kDrawingViewObject Then
'add this menu to the context menu
Call CommandBar.Controls.AddButton(myButtonDef, 0)
End If
End If
End Sub