Add Custom Button with UI Tools

  1. On the Windows Start menu, click All Programs Microsoft Visual Studio Microsoft Visual Studio.
  2. Create Visual C# Project:
    1. In the Visual Studio Development Environment, on the File menu, click New Project.
    2. In the left toolbar, expand the Visual C# Templates, and click InventorETO.
    3. In the list of templates, click Inventor ETO Addin.
    4. Enter a name for the project.
    5. Click OK to create the project.
  3. Build Project
    Several source files that generate automatically hook up to Inventor and Inventor ETO at the correct times. Build the project and verify that the build succeeds. As a part of the build process, it registers itself with Inventor. For help with registering the Add-in, see the ReadMe file that the Visual C# project generates.
  4. Run Inventor
    1. Open an inventor project, and an assembly.

      On the ribbon, a tab with the name of the Addin displays three buttons: Render, Export, and Import.

    2. Close Inventor without saving.
  5. Define custom button
    1. In the Addin Visual Studio project, expand the "UI" organizational folder, and open the file "UserInterfaceComponents.cs." This file defines the class UserInterfaceComponents, which handles creating the Ribbon and Buttons.
    2. Add a ButtonDefinitionWrapper object to this class to define the custom button.
    3. Below the constructor for the class, add the new button to the existing button definitions.
    4. To remove the existing buttons, delete or comment out their definitions. Add the following line below the other button definitions:
       internal ButtonDefinitionWrapper MyButton { get; privateset; }
  6. Create button instance:
    1. In the method "CreateButtonDefinitions," add the line:
       MyButton = CreateButtonDefinition(buttons, addInSite, "MyButton" , CommandTypesEnum .kNonShapeEditCmdType);

      An instance of a button displays with the name "MyButton", and the classification of "kNonShapeEditCmdType." Select the appropriate classification, depending on your intended use.

      The CreateButtonDefinition method calls a constructor in the ButtonDefinitionWrapper class to create the button. The constructor registers the resources necessary to display the button.

    2. To remove the existing buttons from the view, remove the instantiation of the other buttons.
  7. Add custom button to ribbon:
    1. In UserInterfaceComponents.cs, locate the CreateAssemblyRibbonTab method. This method adds each component to the Addin Ribbon to display. The buttons are added to the ribbon at the bottom of this method. Add our new button by adding the line:
    2. Add the new button by adding the line:
       controls.AddButton(MyButton.Target, true );
      Buttons add into the ribbon from left to right, so consider where you want your buttons to display.
  8. Create Button OnExecute Handler:
    1. Open the file IntentModel.cs in the root of the project.
    2. Add a method to handle clicks on the new button. The following snippet generates a MessageBox on each click to demonstrate when the OnExecute method is called.
       void MyButton_OnExecute( object sender, ButtonDefinitionWrapper . ExecuteEventArgs e)
       {
      // TODO Implement OnExecute Method
      MessageBox .Show( "MyButton's OnExecute Method" );
      }

      If removing the existing buttons, you can remove their respective OnExecute handlers.

  9. Register OnExecute handler:
    1. In the constructor for the IntentModel class, register the event handler with the GUI by adding the line:
       gui.MyButton.OnExecute += new EventHandler < ButtonDefinitionWrapper. ExecuteEventArgs >(MyButton_OnExecute);
  10. Build project:
    1. Build the project, cleaning up any errors.
    2. Open an inventor project, and choose any assembly. The add-in ribbon contains the custom button. Click the button to display the message box created in the OnExecute method handler.
    3. Close Inventor without saving.
  11. Add text to button:
    1. To add a custom icon and help text to the button, edit the Resources.resx design in the Resources organizational folder of the project.

      This file defines a set of strings and images for each button.

    2. To add the necessary strings for a button, follow the model of the other buttons closely. For the prefix of each resources name, use the same string you used to instantiate the button.

      The <name>_DisplayName string defines the name that displays below the icon for the custom button.

      <name>_DescriptionText defines the pause-over text of the custom button.

      <name>_ToolTip matches the DescriptionText string .

  12. Add custom icon:
    1. In the Resources.resx file, on the drop-down menu currently showing "Strings," select "Images."
    2. This view defines the icons for each button. Name your images <name>_LargeIcon and <name>_StandardIcon to ensure that the ButtonDefinitionWrapper class adds them. Size Large icons at 32x32 pixels, and standard icons at 16x16 pixels.

      As an example, reuse the Render icon set, and rename them to MyButton_LargeIcon, and MyButton.StandardIcon, or use any images.
  13. Build the solution:
    1. Save the resource file, and build the solution.
    2. Open your inventor project and assembly again. The Add-in ribbon contains an icon with the custom button. The new display text replaces the name given to the button.
    3. If any of the resources do not load, verify that you named each resource with the prefix you used to instantiate the button.