Lesson 3 – Grouping Components and Controlling Visibility with Attributes

In this lesson, you will extend the Add-In created in Lesson 2 by introducing component grouping using attributes and adding a second command that toggles the visibility of all components in a group.

This lesson demonstrates how to:

  • Persist metadata on component occurrences
  • Retrieve objects using the AttributeManager
  • Control multiple components with a single command
  • Design more scalable and maintainable Inventor Add-Ins

Lesson Objective

In this lesson, you will learn how to:

• Store custom metadata on ComponentOccurrence objects using attributes
• Create multiple ribbon buttons in a single panel
• Use AttributeManager.FindObjects to retrieve grouped components
• Toggle visibility of components programmatically
• Implement multi-command Add-In patterns
• Follow Inventor Add-In lifecycle and cleanup best practices


Prerequisites

Before starting this lesson, ensure that you meet the following requirements:

Completed Lessons

You should have completed:

  • Lesson 1: Creating and Registering an Add-In
  • Lesson 2: Adding a Button and Hiding a Component

Required Knowledge

You should be familiar with:

  • C# and Visual Studio 2022
  • Inventor assemblies and component occurrences
  • Ribbon UI customization

Software Requirement

Autodesk Inventor installed and running


Lesson Description

This lesson introduces the concept of logical grouping of components using Inventor Attributes.

Instead of operating solely on the current selection, the Add-In enables users to:

  • Add selected components to a named group
  • Hide or show all components belonging to that group with a single click

This approach is widely used in:

  • Automation and rule-based modeling
  • Assembly management tools
  • Visualization workflows

Step 1 – Add-In Server Class

As in previous lessons, the Add-In implements the ApplicationAddInServer interface.

public class StandardAddInServer : Inventor.ApplicationAddInServer

This class remains the primary entry point for:
• UI creation
• Command registration
• Event handling
• API access


Step 2 – Add-In Identification (GUID)

[Guid("435f2d68-c329-4b1a-b6cc-fb95aeb479b4")]

Each Add-In requires a unique GUID that:
• Identifies the Add-In in the Windows Registry
• Must match the registry entry
• Should never be changed after deployment


Step 3 – Storing the Inventor Application Object

private Inventor.Application m_inventorApplication;

The Inventor Application object is:
• Provided during activation
• Stored for later use
• The gateway to all Inventor API functionality


Step 4 – Activating the Add-In

public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)

During activation, the Add-In:

  • Stores the Inventor application reference
  • Loads button icons
  • Creates two command definitions
  • Registers execution handlers
  • Creates the UI (first-time only)

Step 5 – Creating Multiple Button Definitions

This lesson introduces two buttons:

Button 1 – Add To Group

Adds selected components to a logical group using attributes.

button1Def = m_inventorApplication.CommandManager
    .ControlDefinitions.AddButtonDefinition(
        "Add To Group",
        "AddToGroupButtonName",
        CommandTypesEnum.kNonShapeEditCmdType,
        button1_client_ID,
        "Button to add the Component to a group",
        "Add component to group",
        smallPicture1,
        largePicture1);

Button 2 – Hide or Show Group

Toggles visibility of all components belonging to the group.

button2Def = m_inventorApplication.CommandManager
    .ControlDefinitions.AddButtonDefinition(
        "Hide or Show Group",
        "HideOrShowGroupButtonName",
        CommandTypesEnum.kNonShapeEditCmdType,
        button2_client_ID,
        "Button to hide or show the group",
        "Hide or show the group",
        smallPicture2,
        largePicture2);

Step 6 – Creating the Ribbon User Interface

The UI is added only to the Assembly environment.

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

The Add-In:
• Reuses the existing tab if found
• Creates a new tab if necessary
• Adds a Lesson 3 panel
• Places both buttons on the panel


Step 7 – Adding the Ribbon Tab and Panel

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

A panel named Lesson 3 is then added, and both buttons are placed inside it.


Step 8 – Adding Components to a Group (Attributes)

When Add To Group is clicked:

Execution Flow:

Validate user selection

Loop through selected components

Access AttributeSets on each occurrence

Add a named attribute set and value

AttributeSet attbSet = attbSets.Add("myPartGroup");

attbSet.Add("PartGroup1", ValueTypeEnum.kStringType, "Group1");

Attributes act as persistent metadata stored directly on the component occurrence.


Step 9 – Finding Grouped Components

When Hide or Show Group is clicked, the Add-In uses:

AttributeManager attbMan = asmDoc.AttributeManager;

ObjectCollection objsCol =
    attbMan.FindObjects("myPartGroup", "PartGroup1", "Group1");

This allows the Add-In to retrieve all components that belong to the group—without relying on selection.


Step 10 – Toggling Component Visibility

Each retrieved object is cast to a ComponentOccurrence, and visibility is toggled:

compOcc.Visible = false;

This provides a clean and efficient way to control multiple components with a single command.

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:

✅ Group components using Inventor attributes
✅ Retrieve objects using AttributeManager
✅ Add multiple commands to a ribbon panel
✅ Toggle visibility for grouped components
✅ Build scalable, metadata-driven Add-Ins


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading