This post 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…
Here is the VB.Net code that illustrates how to replace the Print button from the file menu pointed out in the picture below:
<ProgIdAttribute("ReplaceButtonDemo.StandardAddInServer"), _
GuidAttribute("0739b32a-9393-4463-ac9d-c5c162da0193")> _
Public Class StandardAddInServer
Implements Inventor.ApplicationAddInServer
' Inventor application object.
Private m_inventorApplication As Inventor.Application
Private _customButtonDefinition As ButtonDefinition
Private _nativeButtonDef As ButtonDefinition
Public Sub Activate(
ByVal addInSiteObject As Inventor.ApplicationAddInSite,
ByVal firstTime As Boolean)
Implements Inventor.ApplicationAddInServer.Activate
m_inventorApplication = addInSiteObject.Application
ReplaceButton(m_inventorApplication)
End Sub
Public Sub ReplaceButton(app As Inventor.Application)
Dim controlDefs As ControlDefinitions
controlDefs = app.CommandManager.ControlDefinitions
Dim controlDef As ControlDefinition
For Each controlDef In app.CommandManager.ControlDefinitions
If (controlDef.InternalName = "AppFilePrintCmd") Then
_nativeButtonDef = controlDef
Exit For
End If
Next
_customButtonDefinition = controlDefs.AddButtonDefinition(
_nativeButtonDef.DisplayName,
"CustomAppFilePrintCmd",
CommandTypesEnum.kFileOperationsCmdType,
_nativeButtonDef.ClientId,
_nativeButtonDef.DescriptionText,
_nativeButtonDef.ToolTipText,
_nativeButtonDef.StandardIcon,
_nativeButtonDef.LargeIcon)
AddHandler _customButtonDefinition.OnExecute, AddressOf OnExecute
Dim parentCtrl As CommandControl
parentCtrl =
app.UserInterfaceManager.FileBrowserControls("AppFilePrintCmd")
Dim targetCtrl As CommandControl
targetCtrl = parentCtrl.ChildControls("AppFilePrintCmd")
Dim cmdCtrl As CommandControl
cmdCtrl = parentCtrl.ChildControls.AddButton(
_customButtonDefinition,
False,
True,
_nativeButtonDef.InternalName,
True)
targetCtrl.Visible = False
End Sub
Private Sub OnExecute(ByVal Context As NameValueMap)
System.Windows.Forms.MessageBox.Show("Custom code...")
_nativeButtonDef.Execute()
End Sub
The full code is provide in the add-in project from the attachment below.