Replacing a native Inventor button by a custom one

By Philippe Leefsma

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:

button_thumb5

    <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.


Comments

4 responses to “Replacing a native Inventor button by a custom one”

  1. please help me
    1. I can’t compiler your file
    2. where put my code to replace AppFilePrintCmd
    I want put some text before printer .
    thank you
    my email changthales@gmail.com

  2. Philippe Avatar
    Philippe

    Hi,
    Why can’t you compile the code, can you be more specific? You have compilation errors?
    If you want to completely replace the command place your code inside the OnExecute but do not call the _nativeButtonDef.Execute()

  3. Why can’t you compile the code, can you be more specific? You have compilation errors? ===> No compilation error code only bulid error on left buttom
    I creat a new subject in Visual Studio , then copy your code in StandardAddInServer.vb
    finaly can Run.
    thank your answer
    but I have a new problem
    Private _invApp As Inventor.Application
    ….
    MsgBox(“Custom code…”) ‘this can run
    Dim oDocType As DocumentTypeEnum
    oDocType = _invApp.ActiveDocument.DocumentType
    MsgBox(oDocType)
    it’s can’t run
    And another problem
    in the file menu
    new
    :
    :
    :
    print > print
    ^Not Replease ^Can replease
    can replease all print command ?

  4. I found the code
    Dim _invApp As Inventor.Application
    _invApp = System.Runtime.InteropServices.Marshal.GetActiveObject(“Inventor.Application”)
    It’s can run
    But can replease all print button command ?

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading