Skip to content

Commit e672d29

Browse files
committed
Added example for reporting progress
1 parent bee65e9 commit e672d29

10 files changed

+256
-1
lines changed

ReportProgress/Example.cs

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
namespace ReportProgress
11+
{
12+
public class Example
13+
{
14+
15+
public static void Run()
16+
{
17+
18+
// Launch SOLIDWORKS Instance
19+
var solidworksManager = new SOLIDWORKSInstanceManager();
20+
var swApp = solidworksManager.GetNewInstance("/m", SOLIDWORKSInstanceManager.Year_e.Year2023, 160);
21+
swApp.Visible = true;
22+
23+
// Locate and open the test file
24+
var currentLocation = new FileInfo(typeof(Example).Assembly.Location).Directory;
25+
var testFile = currentLocation.GetFiles()
26+
.FirstOrDefault(x => x.Name.Equals("tank_20lb_propane_&.SLDPRT", StringComparison.OrdinalIgnoreCase));
27+
28+
if (testFile == null)
29+
throw new FileNotFoundException("Could not find 'tank_20lb_propane_&.SLDPRT' in the current directory.");
30+
31+
string filePath = testFile.FullName;
32+
33+
int errors = 0, warnings = 0;
34+
var swModel = swApp.OpenDoc6(filePath,
35+
(int)swDocumentTypes_e.swDocPART,
36+
(int)swOpenDocOptions_e.swOpenDocOptions_Silent,
37+
"", ref errors, ref warnings);
38+
39+
if (swModel == null)
40+
throw new InvalidOperationException("Failed to open the document.");
41+
42+
bool repeat = true;
43+
44+
while (repeat)
45+
{
46+
// Perform the long operation with progress bar
47+
PerformOperationWithProgress(swApp, swModel);
48+
49+
// Cleanup
50+
swApp.CloseDoc(swModel.GetTitle());
51+
52+
Console.WriteLine("\nOperation completed.");
53+
54+
// Ask the user if they want to repeat
55+
Console.Write("Do you want to repeat the process? (Y/N): ");
56+
var input = Console.ReadLine();
57+
58+
repeat = input?.Trim().Equals("Y", StringComparison.OrdinalIgnoreCase) == true;
59+
60+
if (repeat)
61+
{
62+
// Reopen or reinitialize your document/model if necessary
63+
swModel = swApp.ActiveDoc as ModelDoc2; // or reopen as needed
64+
65+
}
66+
}
67+
68+
// Exit SOLIDWORKS
69+
swApp.ExitApp();
70+
71+
Console.WriteLine("\nExiting the program. Press any key to close...");
72+
Console.ReadKey();
73+
}
74+
75+
private static void PerformOperationWithProgress(SldWorks swApp, ModelDoc2 swModel)
76+
{
77+
78+
swApp.GetUserProgressBar(out UserProgressBar swPrgBar);
79+
80+
int maxProgress = CalculateMaxProgress();
81+
82+
swPrgBar.Start(0, maxProgress, "Performing face operations...");
83+
84+
int progressCounter = 0;
85+
86+
87+
for (int i = 0; i < maxProgress; i++)
88+
{
89+
progressCounter++;
90+
91+
swPrgBar.UpdateTitle($"Counting {progressCounter}...");
92+
93+
94+
if (swPrgBar.UpdateProgress(progressCounter) == (int)swUpdateProgressError_e.swUpdateProgressError_UserCancel)
95+
{
96+
int userResponse = swApp.SendMsgToUser2(
97+
"Would you like to cancel this time consuming operation?",
98+
(int)swMessageBoxIcon_e.swMbQuestion,
99+
(int)swMessageBoxBtn_e.swMbYesNo);
100+
101+
if (userResponse == (int)swMessageBoxResult_e.swMbHitYes)
102+
{
103+
swPrgBar.End();
104+
Console.WriteLine("Operation canceled by user.");
105+
return;
106+
}
107+
}
108+
109+
}
110+
111+
112+
swPrgBar.End();
113+
Console.WriteLine("Operation completed successfully.");
114+
}
115+
116+
private static int CalculateMaxProgress()
117+
{
118+
return 100000;
119+
}
120+
}
121+
}
+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("ReportProgress")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ReportProgress")]
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("227a9700-e83d-4a61-94ac-21e492c0c55f")]
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")]

ReportProgress/ReportProgress.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>{227A9700-E83D-4A61-94AC-21E492C0C55F}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>ReportProgress</RootNamespace>
11+
<AssemblyName>ReportProgress</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>

ReportProgress/ReportProgress.png

150 KB
Loading

ReportProgress/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>

ReportProgress/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Report Progress
2+
3+
![Report Progress](reportprogress.png)
4+

SW_API_Tips_Tricks.sln

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TraverseTree", "TraverseTre
3333
EndProject
3434
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TraverseTree", "TraverseTree\TraverseTree.csproj", "{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}"
3535
EndProject
36+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ReportProgress", "ReportProgress", "{3A68C664-0F35-4C8D-B3C4-0B603F005DE9}"
37+
ProjectSection(SolutionItems) = preProject
38+
readme.md = readme.md
39+
EndProjectSection
40+
EndProject
41+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReportProgress", "ReportProgress\ReportProgress.csproj", "{227A9700-E83D-4A61-94AC-21E492C0C55F}"
42+
EndProject
3643
Global
3744
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3845
Debug|Any CPU = Debug|Any CPU
@@ -67,6 +74,10 @@ Global
6774
{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
6875
{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
6976
{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1}.Release|Any CPU.Build.0 = Release|Any CPU
77+
{227A9700-E83D-4A61-94AC-21E492C0C55F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
78+
{227A9700-E83D-4A61-94AC-21E492C0C55F}.Debug|Any CPU.Build.0 = Debug|Any CPU
79+
{227A9700-E83D-4A61-94AC-21E492C0C55F}.Release|Any CPU.ActiveCfg = Release|Any CPU
80+
{227A9700-E83D-4A61-94AC-21E492C0C55F}.Release|Any CPU.Build.0 = Release|Any CPU
7081
EndGlobalSection
7182
GlobalSection(SolutionProperties) = preSolution
7283
HideSolutionNode = FALSE
@@ -78,6 +89,7 @@ Global
7889
{487A83A3-2316-4CD1-9BE0-0C2D4F7F14B5} = {F998CA2B-0FFC-4F1C-868A-10F200BAD221}
7990
{439DA6FD-F105-4ECC-9B93-3B411773989B} = {ABCAE877-B8C8-493C-806E-616F5EE476EF}
8091
{BC3808EE-2F10-4AAF-A26B-BAAE0EDF36A1} = {046927B8-0D68-4241-B492-8E0DABBA223C}
92+
{227A9700-E83D-4A61-94AC-21E492C0C55F} = {3A68C664-0F35-4C8D-B3C4-0B603F005DE9}
8193
EndGlobalSection
8294
GlobalSection(ExtensibilityGlobals) = postSolution
8395
SolutionGuid = {2F04F7D1-18E7-4F1A-8F24-73ECE7EF8277}

SW_API_Tips_Tricks/Program.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ static void Main(string[] args)
1616
//EnableFeatureTree.Example.Run();
1717
//UserControlBackground.Example.Run();
1818
//ReleaseCOMObject.Example.Run();
19-
TraverseTree.Example.Run();
19+
//TraverseTree.Example.Run();
20+
21+
ReportProgress.Example.Run();
22+
2023
Console.ReadLine();
2124
}
2225
}

SW_API_Tips_Tricks/SW_API_Tips_Tricks.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@
123123
<Project>{439da6fd-f105-4ecc-9b93-3b411773989b}</Project>
124124
<Name>ReleaseCOMObject</Name>
125125
</ProjectReference>
126+
<ProjectReference Include="..\ReportProgress\ReportProgress.csproj">
127+
<Project>{227a9700-e83d-4a61-94ac-21e492c0c55f}</Project>
128+
<Name>ReportProgress</Name>
129+
</ProjectReference>
126130
<ProjectReference Include="..\TraverseTree\TraverseTree.csproj">
127131
<Project>{bc3808ee-2f10-4aaf-a26b-baae0edf36a1}</Project>
128132
<Name>TraverseTree</Name>

readme.md

Whitespace-only changes.

0 commit comments

Comments
 (0)