Lesson 4 – Managing Groups: Removing Components and Deleting Group

In this lesson, you will extend the functionality created in Lesson 3 by adding group maintenance commands.

Specifically, you will learn how to:

  • Remove individual components from an existing group
  • Completely delete a group from the assembly
  • Safely clean up attributes stored on component occurrences
  • Build user-friendly commands for managing grouped data

Lesson Objective

By the end of this lesson, you will be able to:

• Remove selected components from a group
• Delete an entire group from an assembly
• Work safely with Inventor AttributeSets
• Use AttributeManager.FindObjects for cleanup operations
• Add multiple maintenance commands to the Ribbon
• Apply defensive checks for document state and selection


Prerequisites

Before starting this lesson, ensure you have completed the foundational lessons and are familiar with key concepts required for this exercise.

Completed Lessons

You should have successfully completed the following:

  • Lesson 1: Creating and Registering an Add-In
  • Lesson 2: Adding a Button and Hiding a Component
  • Lesson 3: Grouping Components and Controlling Visibility

Required Knowledge

A basic understanding of the following concepts is recommended:

  • Assembly documents
  • Component occurrences
  • Inventor attributes and attribute sets

Software Requirements

Visual Studio 2022

Autodesk Inventor 2026


Lesson Description

In Lesson 3, you learned how to add components to a group and toggle their visibility using attributes.

In real-world applications, users often require additional flexibility to manage these groups effectively. This includes the ability to:

  • Remove individual components from an existing group
  • Completely delete a group when it is no longer needed

To address these requirements, this lesson introduces two new commands:

  • Remove from Group – Removes attributes from selected components
  • Delete Group – Removes the group attribute from all grouped components

Both commands rely on attribute deletion, which permanently removes the grouping metadata.


Step 1 – Add-In Server and GUID

As with previous lessons, the Add-In implements:

public class StandardAddInServer : Inventor.ApplicationAddInServer

The Add-In is uniquely identified using a GUID:

[Guid("6d63459b-3c99-4d26-8a8b-b65316abd967")]

⚠️ This GUID must match the registry entry and should not be changed after deployment.


Step 2 – Storing the Inventor Application Object

private Inventor.Application m_inventorApplication;

This object:
• Is assigned during activation
• Provides access to documents, UI, and commands
• Is released during deactivation


Step 3 – Ribbon and Command Identifiers

The lesson defines separate client IDs for:

  • Ribbon tab
  • Ribbon panel
  • Each button
private string tab_client_ID;

private string panel_client_ID;

private string button1_client_ID;

private string button2_client_ID;

Using unique IDs ensures:
• Proper registration
• No UI conflicts
• Stable command behavior


Step 4 – Button Definitions

Button 1 – Remove from Group

This command removes only the selected components from the group.

“Remove from Group”

Purpose:
• Deletes the group attribute from selected components
• Leaves the group intact for other components


Button 2 – Delete Group

This command deletes the entire group from the assembly.

“Delete Group”

Purpose:
• Finds all components belonging to the group
• Deletes the attribute set from each one
• Restores component visibility


Step 5 – Add-In Activation Logic

During activation:

  • The Inventor application reference is stored
  • Button icons are loaded from resources
  • Button definitions are created
  • OnExecute handlers are registered
  • The Ribbon UI is created (first-time only)
button1Def.OnExecute += Button1Def_OnExecute;

button2Def.OnExecute += Button2Def_OnExecute;

Step 6 – Creating the Ribbon UI

The UI is added to the Assembly environment only.

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

The Add-In:
• Finds or creates the MyFirstPlugin tab
• Adds a panel named Lesson 4 and places both maintenance buttons on the panel.


Step 7 – Removing a Component from a Group

Command: Remove from Group

Execution flow:

  1. Verify an Assembly document is active
  2. Ensure at least one component is selected
  3. Iterate through the selected components
  4. Check for the group attribute
  5. Delete the attribute set
if (attbSets.NameIsUsed["myPartGroup"])
{
    attbSets["myPartGroup"].Delete();
}

This operation:
• Removes only the selected components
• Does not affect other grouped components


Step 8 – Defensive Programming and Validation

The command includes safeguards to:
• Ensure an Assembly document is active
• Ensure components are selected
• Prevent invalid casts

MessageBox.Show("Need to select a Part or Sub Assembly");

This protects the Add-In from runtime errors and improves usability.


Step 9 – Deleting an Entire Group

Command: Delete Group

This command operates without user selection.

Execution flow:

  1. Verify an Assembly document is active
  2. Use AttributeManager to find all grouped components
  3. Delete the attribute set from each component
  4. Restore visibility
ObjectCollection objsCol =
    attbMan.FindObjects("myPartGroup", "PartGroup1", "Group1");

Step 10 – Cleaning Up Attributes

For each grouped component:

attbSets["myPartGroup"].Delete();

obj.Visible = true;

This ensures:
• Group metadata is completely removed
• Components return to a visible state
• No orphaned attributes remain

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:

✅ Remove individual components from a group
✅ Delete an entire group from an assembly
✅ Safely delete Inventor attribute sets
✅ Use AttributeManager.FindObjects for cleanup
✅ Complete the full group lifecycle


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading