All Resources
Code SnippetSolidWorks API2026-03-10

Traverse SolidWorks Assembly Tree

Recursively walk a SolidWorks assembly tree to collect all component references, paths, and suppression states. Essential for BOM extraction and batch operations.

csharp
public void TraverseAssembly(Component2 comp, int level)
{
    var children = (object[])comp.GetChildren();
    foreach (Component2 child in children)
    {
        string indent = new string(' ', level * 2);
        Debug.Print($"{indent}{child.Name2} [{child.GetSuppression()}]");
        TraverseAssembly(child, level + 1);
    }
}

How It Works

This recursive method walks the SolidWorks assembly tree starting from any Component2 reference. At each level, it:

  1. Gets child components using GetChildren(), which returns all immediate sub-components
  2. Prints each child's name and suppression state with indentation to visualize the tree hierarchy
  3. Recurses into each child, incrementing the level for deeper indentation

When to Use This

  • BOM extraction: Collecting all parts and sub-assemblies for bill of materials generation
  • Batch operations: Applying changes (materials, custom properties, configurations) across an entire assembly
  • Validation scripts: Checking that all components are resolved, not suppressed, or meet naming conventions

Tips

  • Cast the result of GetChildren() to object[] — the SolidWorks API returns a COM variant array
  • Check GetSuppression() to skip suppressed or lightweight components when needed
  • For large assemblies (1000+ components), consider collecting results into a List instead of printing to debug output
  • Start the traversal from the root component: (Component2)assemblyDoc.GetRootComponent3(true)
SolidWorks APIC#Assembly