Lesson 2 – Adding a Button and Hiding a Component

In this lesson, it expands on the basic Add-In created in Lesson 1 by introducing user interaction. You will add a Ribbon button that hides selected component occurrences in an assembly.

The Add-In operates only in the Assembly Document environment and demonstrates safe handling of user selections.


Lesson Objective

In this lesson, you will learn how to:

  • Implement the ApplicationAddInServer interface
  • Create a custom Ribbon tab and panel in the Assembly environment
  • Add a command button with icons
  • Handle button execution events
  • Access the active Assembly document
  • Modify component visibility using the Inventor API

Prerequisites

Before starting this lesson, you should:

  • Have completed Lesson 1: Creating and Registering an Add-In
  • Be familiar with:
    • C# and Visual Studio 2022
    • Basic Inventor UI concepts
    • Assemblies and component occurrences
  • Have Autodesk Inventor installed and running

Lesson Description


Step 1 – The Add-In Server Class

Every Inventor Add-In must implement the ApplicationAddInServer interface.

public class StandardAddInServer : Inventor.ApplicationAddInServer

This class serves as the main entry point for communication between Inventor and your Add-In.

Key Responsibilities

  • React to Inventor loading and unloading the Add-In
  • Create UI elements
  • Handle command execution
  • Release resources properly

Step 2 – Add-In Identification

[Guid("fe90d295-8acd-48e7-9735-f9dc6736a982")]

Each Add-In requires a unique GUID:

  • Used by Inventor to identify the Add-In
  • Must match the Windows Registry entry
  • Should never be changed once deployed

Step 3 – Storing the Inventor Application Object

private Inventor.Application m_inventorApplication;

Inventor provides access to its API through the Application object.
This reference is stored during activation and used throughout the Add-In.


Step 4 – Activating the Add-In

public void Activate (ApplicationAddInSite addInSiteObject, bool firstTime)

This method is called when Inventor loads the Add-In.

What Happens During Activation

  • The Inventor application object is obtained
  • A button definition is created
  • Button icons are assigned
  • The button execution event is registered
  • The UI is created (only the first time)

Step 5 – Creating a Button Definition

buttonDef = m_inventorApplication.CommandManager
    .ControlDefinitions.AddButtonDefinition(
        "Hide Component",
        "LessonButtonName",
        CommandTypesEnum.kNonShapeEditCmdType,
        button_client_ID,
        "Button to hide the Component",
        "Hide Component",
        smallPicture,
        largePicture);

Step 6 – Creating the Ribbon User Interface

private void CreateUserInterface()

This method creates:

  • A custom Ribbon Tab
  • A Ribbon Panel
  • A Button placed on the panel

Target Environment

Ribbon ribbon = m_inventorApplication
    .UserInterfaceManager.Ribbons["Assembly"];

The UI is intentionally added only to the Assembly ribbon.


Step 7 – Adding the Ribbon Tab and Panel

If the tab already exists, it is reused.
Otherwise, a new one is created:

ribbonTab = ribbon.RibbonTabs.Add(
    "MyFirstPlugin",
    "AssemblyDocTab",
    tab_client_ID);

A ribbon panel named Lesson 2 is then added, and the button is placed inside it.


Step 8 – Handling Button Execution

buttonDef.OnExecute += ButtonDef_OnExecute;

When the button is clicked, Inventor calls the following method.


Step 9 – Hiding Selected Components

private void ButtonDef_OnExecute(NameValueMap Context)

Execution Flow

  • Access the active Assembly document
  • Check if a selection exists
  • Loop through selected objects
  • Cast each object to ComponentOccurrence
  • Set visibility to false
compOcc.Visible = false;

Download the sample assembly file to test this lesson here

Complete source code in both C# and VB.NET is provided and can be downloaded here.


Lesson Summary

In this lesson, you learned how to:

  • Create a custom ribbon tab and panel
  • Add a button to the Assembly ribbon
  • Handle button execution events
  • Work with selected component occurrences
  • Control component visibility

Comments

One response to “Lesson 2 – Adding a Button and Hiding a Component”

  1. […] Lesson 2: Adding a Button and Hiding a Component […]

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading