Skip to content

Commit bee65e9

Browse files
committed
Added example on how to traverse feature tree and component display order and flat
1 parent 2ccaeaa commit bee65e9

19 files changed

+310
-2
lines changed

SW_API_Tips_Tricks.sln

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ReleaseCOMObject", "Release
2929
EndProject
3030
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReleaseCOMObject", "ReleaseCOMObject\ReleaseCOMObject.csproj", "{439DA6FD-F105-4ECC-9B93-3B411773989B}"
3131
EndProject
32+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TraverseTree", "TraverseTree", "{046927B8-0D68-4241-B492-8E0DABBA223C}"
33+
EndProject
34+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TraverseTree", "TraverseTree\TraverseTree.csproj", "{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}"
35+
EndProject
3236
Global
3337
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3438
Debug|Any CPU = Debug|Any CPU
@@ -59,6 +63,10 @@ Global
5963
{439DA6FD-F105-4ECC-9B93-3B411773989B}.Debug|Any CPU.Build.0 = Debug|Any CPU
6064
{439DA6FD-F105-4ECC-9B93-3B411773989B}.Release|Any CPU.ActiveCfg = Release|Any CPU
6165
{439DA6FD-F105-4ECC-9B93-3B411773989B}.Release|Any CPU.Build.0 = Release|Any CPU
66+
{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67+
{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
68+
{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
69+
{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}.Release|Any CPU.Build.0 = Release|Any CPU
6270
EndGlobalSection
6371
GlobalSection(SolutionProperties) = preSolution
6472
HideSolutionNode = FALSE
@@ -69,6 +77,7 @@ Global
6977
{12941B8B-3E1F-4A13-840A-914C81317ECC} = {238F2A04-B8F7-432B-A3D7-2ED9DC25D55F}
7078
{487A83A3-2316-4CD1-9BE0-0C2D4F7F14B5} = {F998CA2B-0FFC-4F1C-868A-10F200BAD221}
7179
{439DA6FD-F105-4ECC-9B93-3B411773989B} = {ABCAE877-B8C8-493C-806E-616F5EE476EF}
80+
{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1} = {046927B8-0D68-4241-B492-8E0DABBA223C}
7281
EndGlobalSection
7382
GlobalSection(ExtensibilityGlobals) = postSolution
7483
SolutionGuid = {2F04F7D1-18E7-4F1A-8F24-73ECE7EF8277}
436 KB
Binary file not shown.
153 KB
Binary file not shown.
168 KB
Binary file not shown.
918 KB
Binary file not shown.
154 KB
Binary file not shown.
1020 KB
Binary file not shown.
869 KB
Binary file not shown.
543 KB
Binary file not shown.
114 KB
Binary file not shown.
126 KB
Binary file not shown.
Binary file not shown.

SW_API_Tips_Tricks/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ static void Main(string[] args)
1515
// EnableGraphicsUpdate.Example.Run();
1616
//EnableFeatureTree.Example.Run();
1717
//UserControlBackground.Example.Run();
18-
19-
ReleaseCOMObject.Example.Run();
18+
//ReleaseCOMObject.Example.Run();
19+
TraverseTree.Example.Run();
2020
Console.ReadLine();
2121
}
2222
}

SW_API_Tips_Tricks/SW_API_Tips_Tricks.csproj

+37
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,39 @@
6868
</ItemGroup>
6969
<ItemGroup>
7070
<None Include="App.config" />
71+
<None Include="Assembly\_fidget spinner.SLDASM">
72+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
73+
</None>
74+
<None Include="Assembly\___108543.SLDPRT">
75+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
76+
</None>
77+
<None Include="Assembly\___108544.SLDPRT">
78+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
79+
</None>
80+
<None Include="Assembly\___108545.SLDPRT">
81+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
82+
</None>
83+
<None Include="Assembly\___108546.SLDPRT">
84+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
85+
</None>
86+
<None Include="Assembly\___108547.SLDPRT">
87+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
88+
</None>
89+
<None Include="Assembly\___108548.SLDASM">
90+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
91+
</None>
92+
<None Include="Assembly\___108549.SLDPRT">
93+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
94+
</None>
95+
<None Include="Assembly\___108550.SLDPRT">
96+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
97+
</None>
98+
<None Include="Assembly\___108551.SLDPRT">
99+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
100+
</None>
101+
<None Include="Assembly\___108552.SLDPRT">
102+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
103+
</None>
71104
<None Include="packages.config" />
72105
<Content Include="tank_20lb_propane_&amp;.SLDPRT">
73106
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@@ -90,6 +123,10 @@
90123
<Project>{439da6fd-f105-4ecc-9b93-3b411773989b}</Project>
91124
<Name>ReleaseCOMObject</Name>
92125
</ProjectReference>
126+
<ProjectReference Include="..\TraverseTree\TraverseTree.csproj">
127+
<Project>{bc3808ee-2f10-4aaf-a26b-baae0edf36a1}</Project>
128+
<Name>TraverseTree</Name>
129+
</ProjectReference>
93130
<ProjectReference Include="..\UserControlBackground\UserControlBackground.csproj">
94131
<Project>{487a83a3-2316-4cd1-9be0-0c2d4f7f14b5}</Project>
95132
<Name>UserControlBackground</Name>

TraverseTree/Example.cs

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using BlueByte.SOLIDWORKS.Extensions;
2+
using SolidWorks.Interop.sldworks;
3+
using SolidWorks.Interop.swconst;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace TraverseTree
12+
{
13+
public class Example
14+
{
15+
public static void Run()
16+
{
17+
var solidworksManager = new SOLIDWORKSInstanceManager();
18+
var swApp = solidworksManager.GetNewInstance("", SOLIDWORKSInstanceManager.Year_e.Year2023, 160);
19+
swApp.Visible = true;
20+
21+
var currentLocation = (new FileInfo(typeof(Example).Assembly.Location)).Directory;
22+
23+
var assemblyFile = currentLocation.GetDirectories().First(x=> x.Name.Equals("Assembly")).GetFiles()
24+
.FirstOrDefault(x => x.Name.Equals("_fidget spinner.SLDASM", StringComparison.OrdinalIgnoreCase));
25+
26+
27+
if (assemblyFile == null)
28+
throw new FileNotFoundException($"Could not find '{assemblyFile.Name}' in the assembly directory.");
29+
30+
string filePath = assemblyFile.FullName;
31+
32+
int errors = 0, warnings = 0;
33+
var swModel = swApp.OpenDoc6(filePath,
34+
(int)swDocumentTypes_e.swDocASSEMBLY,
35+
(int)swOpenDocOptions_e.swOpenDocOptions_Silent,
36+
"", ref errors, ref warnings);
37+
38+
39+
40+
if (swModel == null)
41+
throw new InvalidOperationException("Failed to open the document.");
42+
43+
44+
45+
46+
47+
Console.WriteLine("\n--- Method 1: Flat List using GetComponents ---");
48+
PrintComponentsFlat(swModel);
49+
50+
Console.WriteLine("\n--- Method 2: Traverse Assembly Components In Displayed Order---");
51+
PrintComponentsInDisplayedOrder(swModel);
52+
53+
Console.WriteLine("\n--- Method 3: Recursive Hierarchical Traversal ---");
54+
var rootItem = swModel.FeatureManager.GetFeatureTreeRootItem2((int)swFeatMgrPane_e.swFeatMgrPaneBottom);
55+
TraverseTreeItems(rootItem);
56+
57+
58+
swApp.CloseDoc(swModel.GetTitle());
59+
swApp.ExitApp();
60+
61+
Console.WriteLine("\nPress any key to exit...");
62+
Console.ReadKey();
63+
}
64+
65+
private static void PrintComponentsInDisplayedOrder(ModelDoc2 swRootAssemblyModelDoc)
66+
{
67+
Action<Component2> action = LogComponentName;
68+
69+
var swFeature = swRootAssemblyModelDoc.FirstFeature() as Feature;
70+
while (swFeature != null)
71+
{
72+
TraverseFeatureForComponents(swFeature, action);
73+
swFeature = swFeature.GetNextFeature() as Feature;
74+
75+
}
76+
77+
78+
79+
}
80+
81+
82+
// Method 1: Flat List using GetComponents
83+
public static void PrintComponentsFlat(ModelDoc2 swModel)
84+
{
85+
Console.WriteLine("Method 1: Flat List using GetComponents");
86+
var swAssembly = swModel as AssemblyDoc;
87+
88+
if (swAssembly == null)
89+
return;
90+
var comps = swAssembly.GetComponents(false) as object[];
91+
foreach (Component2 comp in comps)
92+
Console.WriteLine(comp.Name2);
93+
}
94+
// Method 2: Recursive Hierarchical Traversal using GetFeatureTreeRootItem
95+
public static void TraverseTreeItems(TreeControlItem item, string indent = "")
96+
{
97+
while (item != null)
98+
{
99+
Console.WriteLine($"{indent}{item.Text}");
100+
101+
var child = item.GetFirstChild();
102+
if (child != null)
103+
TraverseTreeItems(child, indent + " ");
104+
105+
item = item.GetNext();
106+
}
107+
}
108+
109+
110+
111+
112+
113+
static void TraverseFeatureForComponents(Feature swFeature, Action<Component2> performAction)
114+
{
115+
var swSubFeature = default(Feature);
116+
117+
var swComponent = swFeature.GetSpecificFeature2() as Component2;
118+
if (swComponent != null)
119+
{
120+
performAction(swComponent);
121+
122+
swSubFeature = swComponent.FirstFeature();
123+
while (swSubFeature != null)
124+
{
125+
TraverseFeatureForComponents(swSubFeature, performAction);
126+
swSubFeature = swSubFeature.GetNextFeature() as Feature;
127+
}
128+
}
129+
130+
131+
}
132+
static void LogComponentName(Component2 swComponent)
133+
{
134+
// this code is not performant
135+
int parentCount = 0;
136+
Component2 swParentComponent;
137+
swParentComponent = swComponent.GetParent();
138+
while (swParentComponent != null)
139+
{
140+
parentCount++;
141+
swParentComponent = swParentComponent.GetParent();
142+
}
143+
string indentation = string.Join(string.Empty, Enumerable.Repeat(" ", parentCount));
144+
Console.WriteLine($"{indentation}{swComponent.Name}");
145+
}
146+
147+
148+
}
149+
150+
151+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("TraverseTree")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("TraverseTree")]
13+
[assembly: AssemblyCopyright("Copyright © 2025")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("bc3808ee-2f10-4aaf-a26b-baae0edf36a1")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

TraverseTree/TraverseTree.csproj

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>TraverseTree</RootNamespace>
11+
<AssemblyName>TraverseTree</AssemblyName>
12+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="BlueByte.SOLIDWORKS.Extensions, Version=2021.43.0.0, Culture=neutral, PublicKeyToken=55f393faf77448d4, processorArchitecture=MSIL">
35+
<HintPath>..\packages\BlueByte.SOLIDWORKS.Extensions.2021.43.0\lib\net472\BlueByte.SOLIDWORKS.Extensions.dll</HintPath>
36+
</Reference>
37+
<Reference Include="SolidWorks.Interop.sldworks, Version=29.1.0.61, Culture=neutral, PublicKeyToken=7c4797c3e4eeac03, processorArchitecture=MSIL">
38+
<HintPath>..\packages\BlueByte.SOLIDWORKS.Interops.2021.3.0\lib\SolidWorks.Interop.sldworks.dll</HintPath>
39+
<EmbedInteropTypes>False</EmbedInteropTypes>
40+
</Reference>
41+
<Reference Include="SolidWorks.Interop.swcommands, Version=29.1.0.61, Culture=neutral, PublicKeyToken=0fda92720ba7919f, processorArchitecture=MSIL">
42+
<HintPath>..\packages\BlueByte.SOLIDWORKS.Interops.2021.3.0\lib\SolidWorks.Interop.swcommands.dll</HintPath>
43+
<EmbedInteropTypes>False</EmbedInteropTypes>
44+
</Reference>
45+
<Reference Include="SolidWorks.Interop.swconst, Version=29.1.0.61, Culture=neutral, PublicKeyToken=19f43e188e4269d8, processorArchitecture=MSIL">
46+
<HintPath>..\packages\BlueByte.SOLIDWORKS.Interops.2021.3.0\lib\SolidWorks.Interop.swconst.dll</HintPath>
47+
<EmbedInteropTypes>False</EmbedInteropTypes>
48+
</Reference>
49+
<Reference Include="SolidWorks.Interop.swpublished, Version=29.1.0.61, Culture=neutral, PublicKeyToken=89a97bdc5284e6d8, processorArchitecture=MSIL">
50+
<HintPath>..\packages\BlueByte.SOLIDWORKS.Interops.2021.3.0\lib\SolidWorks.Interop.swpublished.dll</HintPath>
51+
<EmbedInteropTypes>False</EmbedInteropTypes>
52+
</Reference>
53+
<Reference Include="System" />
54+
<Reference Include="System.Core" />
55+
<Reference Include="System.Xml.Linq" />
56+
<Reference Include="System.Data.DataSetExtensions" />
57+
<Reference Include="Microsoft.CSharp" />
58+
<Reference Include="System.Data" />
59+
<Reference Include="System.Net.Http" />
60+
<Reference Include="System.Xml" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<Compile Include="Example.cs" />
64+
<Compile Include="Properties\AssemblyInfo.cs" />
65+
</ItemGroup>
66+
<ItemGroup>
67+
<None Include="packages.config" />
68+
</ItemGroup>
69+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
70+
</Project>
Loading

TraverseTree/packages.config

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="BlueByte.SOLIDWORKS.Extensions" version="2021.43.0" targetFramework="net472" />
4+
<package id="BlueByte.SOLIDWORKS.Interops" version="2021.3.0" targetFramework="net472" />
5+
</packages>

0 commit comments

Comments
 (0)