Advanced Assembly Tree Traversal with Filtering
Production-grade assembly tree extraction that handles suppressed components, envelope exclusions, virtual parts, and configuration-aware traversal. Goes beyond basic recursion to build a filterable tree structure.
Beyond Basic Traversal
The basic assembly traversal pattern (see our Traverse SolidWorks Assembly Tree snippet) uses a simple recursive loop. That works for tutorials, but real assemblies need filtering, type detection, and progress reporting.
A production assembly might have:
- Suppressed components that should be excluded from BOMs
- Envelope components (reference geometry like building outlines) that aren't real parts
- Virtual components (in-context parts) with no external file path
- 500+ components requiring progress feedback to the user
GetComponents: Two Modes
The IComponent2.GetChildren() method has an important parameter that changes behavior:
GetComponents(true)— returns top-level children only (use for recursive traversal)GetComponents(false)— returns all components flattened (use for counting and statistics)
For building a tree, always use GetComponents(true) and recurse manually. Using false gives you a flat list with no parent-child relationships.
Filtering Rules
Suppressed Components
Check before processing:
var suppState = (swComponentSuppressionState_e)
component.GetSuppression();
if (suppState == swComponentSuppressionState_e
.swComponentSuppressed)
continue; // Skip this componentSuppressed components have no loaded geometry and should be excluded from most operations (BOM export, mass calculations, interference checks).
Envelope Components
Envelopes represent reference geometry — building outlines, clearance volumes, or fixtures. They exist in the assembly for visual reference but are not manufactured parts:
if (component.IsEnvelope())
continue; // Not a real partVirtual Components
Virtual (in-context) parts are stored inside the assembly file, not as separate files:
string path = component.GetPathName();
if (string.IsNullOrEmpty(path))
{
// Virtual component — no external file
node.IsVirtual = true;
}Building the Tree Model
Define a node class that captures the full context:
public class AssemblyTreeNode
{
public string Name { get; set; }
public string FullPath { get; set; }
public string Configuration { get; set; }
public int Level { get; set; }
public ComponentType Type { get; set; }
public bool IsVirtual { get; set; }
public List<AssemblyTreeNode> Children { get; set; }
}Component Type Detection
Determining whether a component is a part, assembly, or drawing requires a two-step approach:
// Strategy 1: Use the loaded model (preferred)
var modelDoc = component.GetModelDoc2();
if (modelDoc != null)
{
int docType = modelDoc.GetType();
// 1 = Part, 2 = Assembly, 3 = Drawing
}
// Strategy 2: Fallback to file extension
string ext = Path.GetExtension(path).ToUpper();
// .SLDPRT = Part, .SLDASM = AssemblyThe model might not be loaded (lightweight mode, suppressed sub-assemblies), so always have the extension-based fallback.
Progress Reporting
For large assemblies, use IProgress<T> to provide feedback:
public void ExtractTree(IComponent2 root,
IProgress<(int percent, string message)>? progress)
{
var allComponents = root.GetComponents(false);
int total = allComponents?.Length ?? 0;
int processed = 0;
// Recursive traversal with progress
void Traverse(IComponent2 comp, int level)
{
processed++;
if (processed % 50 == 0)
progress?.Report((
(int)(100.0 * processed / total),
$"Processing {comp.Name2}..."));
// ... recursion
}
}Report every 50 components to avoid UI thread flooding. The total count from GetComponents(false) gives an accurate denominator for percentage calculation.
When to Use This vs. Basic Traversal
| Scenario | Basic | Advanced |
|---|---|---|
| Learning the API | Yes | Overkill |
| Quick macro / script | Yes | Overkill |
| BOM export tool | No | Yes |
| PDM / file management | No | Yes |
| Production application | No | Yes |
If you are building a tool that will be used by others, invest in the filtering and progress reporting. Users will encounter assemblies you never anticipated.