Traverse Assembly at Component and Feature Levels Using Recursion Example
(C#)
This example shows how to traverse an assembly at the component and
feature levels using recursion.
//--------------------------------------------------------------------------
// Preconditions: Open:
// <SolidWorks_install_dir>\samples\tutorial\PDMWorks\speaker.sldasm
// or any assembly document
//
// Postconditions:
// 1. Output of the assembly traversal is
shown in the Immediate Window.
// 2. The
elapsed time to traverse the assembly is also shown
// in
the Immediate Window.
//---------------------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
namespace RecursiveTraverseAssemblyCSharp.csproj
{
public
partial class SolidWorksMacro
{
public
void TraverseFeatures(Feature swFeat, long nLevel)
{
Feature
swSubFeat;
string
sPadStr = " ";
long
i = 0;
for
(i = 0; i <= nLevel; i++)
{
sPadStr
= sPadStr + " ";
}
while
((swFeat != null))
{
Debug.Print(sPadStr
+ swFeat.Name + " ["
+ swFeat.GetTypeName2() + "]");
swSubFeat
= (Feature)swFeat.GetFirstSubFeature();
if
((swSubFeat != null))
{
TraverseFeatures(swSubFeat,
nLevel + 1);
}
if
(nLevel == 1)
{
swFeat
= (Feature)swFeat.GetNextFeature();
}
else
{
swFeat
= (Feature)swFeat.GetNextSubFeature();
}
}
}
public
void TraverseComponentFeatures(Component2 swComp, long nLevel)
{
Feature
swFeat;
swFeat
= (Feature)swComp.FirstFeature();
TraverseFeatures(swFeat,
nLevel);
}
public
void TraverseComponent(Component2 swComp, long nLevel)
{
object[]
vChildComp;
Component2
swChildComp;
string
sPadStr = " ";
long
i = 0;
for
(i = 0; i <= nLevel - 1; i++)
{
sPadStr
= sPadStr + " ";
}
vChildComp
= (object[])swComp.GetChildren();
for
(i = 0; i < vChildComp.Length; i++)
{
swChildComp
= (Component2)vChildComp[i];
Debug.Print(sPadStr
+ "+" + swChildComp.Name2
+ " <" + swChildComp.ReferencedConfiguration
+ ">");
TraverseComponentFeatures(swChildComp,
nLevel);
TraverseComponent(swChildComp,
nLevel + 1);
}
}
public
void TraverseModelFeatures(ModelDoc2 swModel, long nLevel)
{
Feature
swFeat;
swFeat
= (Feature)swModel.FirstFeature();
TraverseFeatures(swFeat,
nLevel);
}
public
void Main()
{
ModelDoc2
swModel;
ConfigurationManager
swConfMgr;
Configuration
swConf;
Component2
swRootComp;
swModel
= (ModelDoc2)swApp.ActiveDoc;
swConfMgr
= (ConfigurationManager)swModel.ConfigurationManager;
swConf
= (Configuration)swConfMgr.ActiveConfiguration;
swRootComp
= (Component2)swConf.GetRootComponent3(true);
System.Diagnostics.Stopwatch
myStopwatch = new Stopwatch();
myStopwatch.Start();
Debug.Print("File
= " + swModel.GetPathName());
TraverseModelFeatures(swModel,
1);
if
(swModel.GetType() == (int)swDocumentTypes_e.swDocASSEMBLY)
{
TraverseComponent(swRootComp,
1);
}
myStopwatch.Stop();
TimeSpan
myTimespan = myStopwatch.Elapsed;
Debug.Print("Time
= " + myTimespan.TotalSeconds + " sec");
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}