This post is an enhanced version of that previous one:
Replacing a native Inventor button by a custom one
It illustrates how you can replace an Inventor button by a custom one that looks the same. There can be several reasons you may want to do that, for example the button you are replacing invokes a command that do not fire the UserInputEvents.OnActivateCommand / OnTerminateCommand, you want to replace the native functionality by your own, and so on…
I achieved that by using a custom class that handles the button replacement:
Class AdnCustomReplacementCmd
Private _app As Application
Private _nativeButtonDef As ButtonDefinition
Private _customButtonDefinition As ButtonDefinition
Public Event OnExecute(
ByVal Context As NameValueMap,
nativeButtonDef As ButtonDefinition)
Public Sub New(
ByVal app As Inventor.Application,
ByVal nativeCmdInternalName As String)
_app = app
Dim controlDefs As ControlDefinitions
controlDefs = app.CommandManager.ControlDefinitions
Dim controlDef As ControlDefinition
For Each controlDef In app.CommandManager.ControlDefinitions
If (controlDef.InternalName = nativeCmdInternalName) Then
_nativeButtonDef = controlDef
Exit For
End If
Next
If Not _nativeButtonDef Is Nothing Then
_customButtonDefinition = controlDefs.AddButtonDefinition(
_nativeButtonDef.DisplayName,
"Custom_" + nativeCmdInternalName,
CommandTypesEnum.kFileOperationsCmdType,
_nativeButtonDef.ClientId,
_nativeButtonDef.DescriptionText,
_nativeButtonDef.ToolTipText,
_nativeButtonDef.StandardIcon,
_nativeButtonDef.LargeIcon)
AddHandler _customButtonDefinition.OnExecute,
AddressOf OnExecuteButton
End If
ReplaceNativeControls()
End Sub
Private Sub OnExecuteButton(ByVal Context As NameValueMap)
RaiseEvent OnExecute(Context, _nativeButtonDef)
End Sub
Private Sub ReplaceNativeControls()
Dim ctrls As CommandControlsEnumerator =
_app.UserInterfaceManager.AllReferencedControls(_nativeButtonDef)
For Each ctrl As CommandControl In ctrls
Try
' Assumes we deal with a control
' from RibbonPanel
ctrl.Parent.CommandControls.AddButton(
_customButtonDefinition,
ctrl.UseLargeIcon,
ctrl.ShowText,
_nativeButtonDef.InternalName,
True)
' Hides native control
ctrl.Visible = False
Catch ex As Exception
End Try
Try
' Assumes we deal with a control
' from App menu
ctrl.Parent.ChildControls.AddButton(
_customButtonDefinition,
ctrl.UseLargeIcon,
ctrl.ShowText,
_nativeButtonDef.InternalName,
True)
' Hides native control
ctrl.Visible = False
Catch ex As Exception
End Try
Next
End Sub
End Class
It is pretty easy to use it from your add-in code, in the example below I replace two native commands: Make Part and Make Component:
-
Public Class StandardAddInServer
Implements Inventor.ApplicationAddInServer
' Inventor application object.
Private m_inventorApplication As Inventor.Application
Private _customControls As New List(Of AdnCustomReplacementCmd)
Public Sub Activate(
ByVal addInSiteObject As Inventor.ApplicationAddInSite,
ByVal firstTime As Boolean) Implements
Inventor.ApplicationAddInServer.Activate
m_inventorApplication = addInSiteObject.Application
Dim nativeCtrls As New List(Of String)
nativeCtrls.Add("PartMakePartCmd")
nativeCtrls.Add("PartMakeComponentsCmd")
For Each ctrl As String In nativeCtrls
Dim customCtrl As New AdnCustomReplacementCmd(
m_inventorApplication,
ctrl)
AddHandler customCtrl.OnExecute, AddressOf OnExecuteButton
_customControls.Add(customCtrl)
Next
End Sub
Private Sub OnExecuteButton(
ByVal Context As NameValueMap,
nativeDef As ButtonDefinition)
System.Windows.Forms.MessageBox.Show(
"Native button definition: " + nativeDef.InternalName)
nativeDef.Execute()
End Sub