tag:blogger.com,1999:blog-13017039396311679512024-03-19T04:00:02.759-07:00SolidAPIMike Spenshttp://www.blogger.com/profile/00365628417064674695noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-1301703939631167951.post-68008743932539692062017-02-28T11:21:00.001-08:002017-02-28T11:23:14.454-08:00From Macros to Add-Ins<!--<div class="content_1">-->
<div class="MsoNormal">
At some point after creating and using macros regularly, you
may begin to realize that deploying and sharing them with many users isn’t
always easy. You can share them across
the network, or through your SOLIDWORKS PDM, but how do you make sure all users
get the same layout for new buttons in the user interface?<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
Building SOLIDWORKS add-ins can make macro deployment much
simpler across your teams, but it does mean making some slight modifications to
your existing macros. The great news is
that you don’t have to start from scratch or import your existing code into an
add-in. SOLIDWORKS also provides a
comprehensive add-in template through the SOLIDWORKS API SDK. This can make the whole process pretty simple
really.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
The first assumption is that you’ll be using a Visual Studio
development environment. I will use
Visual Studio 2015 for this example, but older versions will work as well. There are free Express and Community editions
of Visual Studio available for personal and educational use if you don’t have
access to a full edition. <o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
This example will cover three essential tasks: tweaking your existing macros to work from
the add-in, creating the add-in itself, referencing your macros, and finally,
creating a deployment package (install files).<o:p></o:p><br />
<br /></div>
<h3>
Tweak your Macro</h3>
<h1>
<o:p></o:p></h1>
<div class="MsoNormal">
A quick discussion about macros. Most SOLIDWORKS macros out there are VBA
macros written in Visual Basic and are saved in the SWP file format (SOLIDWORKS
Project). These shouldn’t need any
adjustment to reference in an add-in.
However, the less-common, but more interesting VSTA macros (Visual
Studio for Applications) will need some preparation work. These are compiled as DLL files and can be
written in either C# or VB.NET languages.
We’ll focus on how to prepare VSTA macros for add-ins first.<o:p></o:p><br />
<br /></div>
<h3>
.NET Macro Adjustments</h3>
<h2>
<o:p></o:p></h2>
<div class="MsoNormal">
To make DLL macros with an external entry point, we need a
new class with a public procedure that calls your desired code. With VSTA macros, that can include arguments
that come from the add-in. In these
examples, the SOLIDWORKS application interface is passed to the Run procedure
as an argument.<o:p></o:p><br />
<br /></div>
<h4>
VB</h4>
<h2>
<o:p></o:p></h2>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">Imports</span><span style="font-family: "consolas"; font-size: 11pt;"> SolidWorks.Interop.sldworks</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">Imports</span><span style="font-family: consolas; font-size: 11pt;"> SolidWorks.Interop.swconst</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">Public</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Class</span><span style="font-family: consolas; font-size: 11pt;"> App</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Public</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Sub</span><span style="font-family: consolas; font-size: 11pt;"> Run(</span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">ByVal</span><span style="font-family: consolas; font-size: 11pt;"> swApp </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">As</span><span style="font-family: consolas; font-size: 11pt;"> SldWorks)</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: green; font-family: consolas; font-size: 11.0pt;">'processing code starts here<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">End</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Sub</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">End</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Class</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h4>
C#</h4>
<h2>
<o:p></o:p></h2>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">using</span><span style="font-family: consolas; font-size: 11pt;"> System.Collections.Generic;</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">using</span><span style="font-family: consolas; font-size: 11pt;"> System.Text;</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">using</span><span style="font-family: consolas; font-size: 11pt;"> SolidWorks.Interop.sldworks;</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">using</span><span style="font-family: consolas; font-size: 11pt;"> SolidWorks.Interop.swconst;</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">namespace</span><span style="font-family: consolas; font-size: 11pt;"> MakePart.csproj</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;">{</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">public</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">class</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: teal; font-family: consolas; font-size: 11.0pt;">App</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> {</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">public</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">void</span><span style="font-family: consolas; font-size: 11pt;"> Run(</span><span style="color: teal; font-family: consolas; font-size: 11.0pt;">ISldWorks</span><span style="font-family: consolas; font-size: 11pt;"> swApp)</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> {</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: green; font-family: consolas; font-size: 11.0pt;">//process code starts here<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> }</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> }</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Copy everything you had in your original main procedure to
the new Run procedure. Don’t forget
necessary Imports or using statements. The
following are just examples of format, not any real macro code.<o:p></o:p><br />
<br /></div>
<h4>
VB</h4>
<h2>
<o:p></o:p></h2>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">Public</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Sub</span><span style="font-family: consolas; font-size: 11pt;"> Run(</span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">ByVal</span><span style="font-family: consolas; font-size: 11pt;"> swApp </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">As</span><span style="font-family: consolas; font-size: 11pt;"> SldWorks)</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span lang="NO-NYN" style="font-family: consolas; font-size: 11pt;"> </span><span lang="NO-NYN" style="color: blue; font-family: consolas; font-size: 11.0pt;">Dim</span><span lang="NO-NYN" style="font-family: consolas; font-size: 11pt;"> MyDialog </span><span lang="NO-NYN" style="color: blue; font-family: consolas; font-size: 11.0pt;">As</span><span lang="NO-NYN" style="font-family: consolas; font-size: 11pt;"> </span><span lang="NO-NYN" style="color: blue; font-family: consolas; font-size: 11.0pt;">New</span><span lang="NO-NYN" style="font-family: consolas; font-size: 11pt;">
Dialog1</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> AddComponentsMod.m_swApp = swApp</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> MyDialog.Show()</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">End</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Sub</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span><br />
<span style="color: blue; font-family: consolas; font-size: 11.0pt;"><br /></span></div>
<h4>
C#</h4>
<h2>
<o:p></o:p></h2>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">public</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">void</span><span style="font-family: consolas; font-size: 11pt;"> Run(</span><span style="color: teal; font-family: consolas; font-size: 11.0pt;">ISldWorks</span><span style="font-family: consolas; font-size: 11pt;"> swApp)</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;">{</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: teal; font-family: consolas; font-size: 11.0pt;">ModelDoc2</span><span style="font-family: consolas; font-size: 11pt;"> swDoc = </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">null</span><span style="font-family: consolas; font-size: 11pt;">;</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;"> bool</span><span style="font-family: consolas; font-size: 11pt;">
boolstatus = </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">false</span><span style="font-family: consolas; font-size: 11pt;">;<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> ...</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: green; font-family: consolas; font-size: 11.0pt;"> // Zoom To Fit</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> swDoc.ViewZoomtofit2();</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;">}</span><span style="font-size: 11.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
If you want your macro to still work stand-alone in
SOLIDWORKS (and who wouldn’t), you need to call the Run procedure from your
original main procedure. Declare a variable
as a new instance of your application class and call the new Run procedure in
your main procedure. This will be the
same format you use to call your macro from your add-in later in the example.<o:p></o:p><br />
<br /></div>
<h4>
VB</h4>
<h2>
<o:p></o:p></h2>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">Public</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Sub</span><span style="font-family: consolas; font-size: 11pt;"> main()</span><span style="font-size: 11.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Dim</span><span style="font-family: consolas; font-size: 11pt;"> myMacro </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">As</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">New</span><span style="font-family: consolas; font-size: 11pt;"> App</span><span style="font-size: 11.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> myMacro.Run(swApp)</span><span style="font-size: 11.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">End</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">Sub</span><span style="font-size: 11.0pt;"><o:p></o:p></span><br />
<span style="color: blue; font-family: consolas; font-size: 11.0pt;"><br /></span></div>
<h4>
C#</h4>
<h2>
<o:p></o:p></h2>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="color: blue; font-family: consolas; font-size: 11.0pt;">public</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">void</span><span style="font-family: consolas; font-size: 11pt;"> Main()</span><span style="font-size: 11.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;">{</span><span style="font-size: 11.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: teal; font-family: consolas; font-size: 11.0pt;">App</span><span style="font-family: consolas; font-size: 11pt;"> MyMacro = </span><span style="color: blue; font-family: consolas; font-size: 11.0pt;">new</span><span style="font-family: consolas; font-size: 11pt;"> </span><span style="color: teal; font-family: consolas; font-size: 11.0pt;">App</span><span style="font-family: consolas; font-size: 11pt;">();</span><span style="font-size: 11.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;"> MyMacro.Run(swApp); </span><span style="font-size: 11.0pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: consolas; font-size: 11pt;">}</span><span style="font-size: 11.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The last thing you need to do is make the .NET macro
COM-Visible by enabling the setting in Project Properties, Application, Assembly
Information.<o:p></o:p><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgf4m6SvzJm48X7zoNHtztCJ5NVX60to1-qph1w5s-kVudL1DqIvLpOrLmUVROIseLiazKIm4XWAGWc5XFTd_KtD8xC_jODWdxdlrZWpQAbrwt0yz1xhalPUMpdAJtKTJz5lqDuWnh963I/s1600/make_com_visible.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="396" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgf4m6SvzJm48X7zoNHtztCJ5NVX60to1-qph1w5s-kVudL1DqIvLpOrLmUVROIseLiazKIm4XWAGWc5XFTd_KtD8xC_jODWdxdlrZWpQAbrwt0yz1xhalPUMpdAJtKTJz5lqDuWnh963I/s400/make_com_visible.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a></div>
<a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"></a><a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"></a><a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"></a><a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"></a><a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"></a><a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"></a></div>
<div class="MsoNormal">
<br />
After making adjustments, build and run your macro as usual
to make sure it still works stand-alone.<o:p></o:p><br />
<br /></div>
<h3>
VBA Macro Information</h3>
<h2>
<o:p></o:p></h2>
<div class="MsoNormal">
Now, a quick discussion about VBA macros and how to call
them.<o:p></o:p></div>
<div class="MsoNormal">
For VBA macros, you need to know the macro name, the module
name and the procedure, typically “main”.
The following macro is named <i>MakePart.swp</i>,
its module is <i>MakePart1</i> and the
procedure name is main (not shown).<o:p></o:p><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqE7prq811eh5YjjQthKm2Op-lSQF3Ug7alDhkC9MaioVCV-EB2K1caa95hQ8sWEpldkIfJLLlXk4Z154GeOLJfad-SYMdPz2EHQXWHDjoF-nEj0YMUeDa04qUlKycLLSTDRGKMc6pnf4/s1600/vba_structure.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqE7prq811eh5YjjQthKm2Op-lSQF3Ug7alDhkC9MaioVCV-EB2K1caa95hQ8sWEpldkIfJLLlXk4Z154GeOLJfad-SYMdPz2EHQXWHDjoF-nEj0YMUeDa04qUlKycLLSTDRGKMc6pnf4/s1600/vba_structure.png" /></a></div>
<div style="text-align: center;">
<br /></div>
</div>
<!--
</div>
<div class="content_2" style="display: none;">
-->
<h3>
Create your Add-In</h3>
<h1>
<o:p></o:p></h1>
<div class="MsoNormal">
Visual Studio 2008 through 2015 will work. This example will use 2015. There are free express versions available or
full versions for purchase.</div>
<div class="MsoNormal">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://www.blogger.com/blogger.g?blogID=1301703939631167951" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a></div>
Install the SOLIDWORKS API SDK – found here on your
SOLIDWORKS DVD or in a full media kit download.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-left: .75in;">
<i>Install_media</i>\api\SolidWorks API SDK.msi<o:p></o:p></div>
<div class="MsoNormal" style="margin-left: .75in;">
<br /></div>
<span style="font-family: "calibri" , sans-serif; font-size: 11.0pt; line-height: 107%;">If the SDK has been installed successfully, when
you create a new project and select Visual Basic or C#, you will see the
SwVBAddin (or SwCSharpAddin) available as a template. Give it a good name to begin.</span><br />
<span style="font-family: "calibri" , sans-serif; font-size: 11.0pt; line-height: 107%;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfJ_Qpz3ejP_dtLF9nvfSZEvYjzy62Huiv390U6tChoWNzWXnNJH-sN_fIGAVPTtkbkhj9u3HbuyhVFPGV6icZuuvyRrLGz-HmZ28aditYkbtqlNyXI2Nai5XZL8uipABNi9T4XBKYWmY/s1600/addin_template.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="186" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfJ_Qpz3ejP_dtLF9nvfSZEvYjzy62Huiv390U6tChoWNzWXnNJH-sN_fIGAVPTtkbkhj9u3HbuyhVFPGV6icZuuvyRrLGz-HmZ28aditYkbtqlNyXI2Nai5XZL8uipABNi9T4XBKYWmY/s400/addin_template.png" width="400" /></a></div>
<div style="text-align: center;">
<span style="font-family: "calibri" , sans-serif; font-size: 11.0pt; line-height: 107%;"><br /></span></div>
<div style="text-align: left;">
<span style="font-family: "calibri" , sans-serif; font-size: 11.0pt; line-height: 107%;"><br /></span></div>
<h3>
Code Structure of an Add-In</h3>
<h2>
<o:p></o:p></h2>
<span style="font-family: "calibri" , sans-serif; font-size: 11.0pt; line-height: 107%;">
</span>
<br />
<div class="MsoNormal">
There are several code regions in the primary class SwAddin
(same in VB and C#). The most important
are ISwAddin Implementation, UI Methods and UI Callbacks.<o:p></o:p><br />
<br /></div>
<div class="MsoNormal">
<div class="MsoNormal">
The two functions in the ISwAddin Implementation code
section run when the add-in is turned on: ConnectToSW and when it is turned off
or SOLIDWORKS is closed: DisconnectFromSW.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In this example, the event handlers and property manager
pages are not needed, so it is just two procedures to focus on: AddCommandMgr
and RemoveCommandMgr. Nothing needs to
be modified in RemoveCommandMgr.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
AddCommandMgr is found within the UI Methods code
section. The procedure sets up the user
interface by adding menu items, toolbars and a Command Manager tab. It also initializes Icons and establishes
“callback” functions. This procedure also
establishes which document types the command group will display under. We will come back to this section shortly.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The UI Callbacks section is where you add your processing code. To reference an existing .NET macro, add the
macro as a reference to the project first, then call it. Add a reference to the macro by selecting Project,
Add Reference. Browse to the compiled
.NET macro dll. After adding the
reference, based on our example, call the Run procedure as shown.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h4>
VB</h4>
<h2>
<o:p></o:p></h2>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; color: blue; font-family: "consolas";">Sub</span><span style="background: white; font-family: "consolas";"> MakePartMacro()</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">Dim</span><span style="background: white; font-family: "consolas";"> myMacro </span><span style="background: white; color: blue; font-family: "consolas";">As</span><span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">New</span><span style="background: white; font-family: "consolas";"> MakePart.vbproj.</span><span style="background: white; color: #2b91af; font-family: "consolas";">App</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> myMacro.Run(SwApp)</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; color: blue; font-family: "consolas";">End</span><span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">Sub</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<h4>
C#</h4>
<h2>
<o:p></o:p></h2>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas";">public</span><span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">void</span><span style="background: white; font-family: "consolas";">
MakePartMacro()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";"> MakePart.csproj.</span><span style="background: white; color: #2b91af; font-family: "consolas";">App</span><span style="background: white; font-family: "consolas";">
myMacro = </span><span style="background: white; color: blue; font-family: "consolas";">new</span><span style="background: white; font-family: "consolas";"> MakePart.csproj.</span><span style="background: white; color: #2b91af; font-family: "consolas";">App</span><span style="background: white; font-family: "consolas";">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";"> myMacro.Run(SwApp);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "consolas";">}</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
To reference a VBA macro, first add it to the add-in project
by selecting Project, Add Existing Item.
Browse to the macro SWP file.
After adding it to the project, find it in the Project Explorer and set
its properties as follows. Build Action
should be Content and Copy to Output Directory should be Copy if newer.<o:p></o:p></div>
</div>
<div class="MsoNormal">
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieTDTq8db05AJIe873uE4P3fDdsOUgekVvsScBZvelVgNmh85DTWAIYI_evZOjRwCOi95ajfOD5w3SlR4u72OeWycAVERzT2g4gpejR5yquG8Xe9mWsxv8Pksu6J-mu2_pyxZNTGqXHJY/s1600/swp_properties.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="191" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieTDTq8db05AJIe873uE4P3fDdsOUgekVvsScBZvelVgNmh85DTWAIYI_evZOjRwCOi95ajfOD5w3SlR4u72OeWycAVERzT2g4gpejR5yquG8Xe9mWsxv8Pksu6J-mu2_pyxZNTGqXHJY/s400/swp_properties.png" width="400" /></a></div>
<div style="text-align: left;">
<br /></div>
<div class="MsoNormal">
Call the VBA macro from the add-ins runtime folder by using
the following. This assumes the macro
name, module name and procedure name explained above.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h4>
VB</h4>
<h2>
<o:p></o:p></h2>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; color: blue; font-family: "consolas";">Sub</span><span style="background: white; font-family: "consolas";"> MakePartVBA()</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">Dim</span><span style="background: white; font-family: "consolas";"> myDir </span><span style="background: white; color: blue; font-family: "consolas";">As</span><span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">String</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> myDir = IO.</span><span style="background: white; color: #2b91af; font-family: "consolas";">Path</span><span style="background: white; font-family: "consolas";">.GetDirectoryName _</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> (</span><span style="background: white; color: #2b91af; font-family: "consolas";">Assembly</span><span style="background: white; font-family: "consolas";">.GetExecutingAssembly.Location)</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">Dim</span><span style="background: white; font-family: "consolas";"> macroPath </span><span style="background: white; color: blue; font-family: "consolas";">As</span><span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">String</span><span style="color: blue; font-family: "consolas"; mso-bidi-font-family: +mn-cs; mso-bidi-font-weight: bold; mso-fareast-font-family: +mn-ea; mso-font-kerning: 12.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: blue; font-family: "consolas"; mso-bidi-font-family: +mn-cs; mso-bidi-font-weight: bold; mso-fareast-font-family: +mn-ea; mso-font-kerning: 12.0pt;"> Dim </span><span style="font-family: "consolas"; mso-bidi-font-family: +mn-cs; mso-bidi-font-weight: bold; mso-fareast-font-family: +mn-ea; mso-font-kerning: 12.0pt;">errors <span style="color: blue;">As String</span></span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> macroPath = myDir & </span><span style="background: white; color: #a31515; font-family: "consolas";">"\MakePart.swp"</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> SwApp.RunMacro2(macroPath, </span><span style="background: white; color: #a31515; font-family: "consolas";">"MakePart1"</span><span style="background: white; font-family: "consolas";">, _</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: #a31515; font-family: "consolas";">"main"</span><span style="background: white; font-family: "consolas";">, 1, </span><span style="background: white; font-family: "consolas";">errors)</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; color: blue; font-family: "consolas";">End</span><span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">Sub</span><span style="font-family: "times new roman" , serif; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<h4>
C#</h4>
<h2>
<o:p></o:p></h2>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas";">public</span><span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">void</span><span style="background: white; font-family: "consolas";">
MakePartVBA()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">string</span><span style="background: white; font-family: "consolas";">
myDir;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";"> myDir = System.IO.</span><span style="background: white; color: #2b91af; font-family: "consolas";">Path</span><span style="background: white; font-family: "consolas";">.GetDirectoryName<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";"> (</span><span style="background: white; color: #2b91af; font-family: "consolas";">Assembly</span><span style="background: white; font-family: "consolas";">.GetExecutingAssembly().Location);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">string</span><span style="background: white; font-family: "consolas";">
macroPath;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";"> macroPath = myDir + </span><span style="background: white; color: #a31515; font-family: "consolas";">"\\MakePart.swp"</span><span style="background: white; font-family: "consolas";">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: blue; font-family: "consolas";">int</span><span style="background: white; font-family: "consolas";">
errors;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: "consolas";"> SwApp.RunMacro2(macroPath, </span><span style="background: white; color: #a31515; font-family: "consolas";">"MakePart1"</span><span style="background: white; font-family: "consolas";">,
</span><span style="background: white; color: #a31515; font-family: "consolas";">"main"</span><span style="background: white; font-family: "consolas";">, 1, </span><span style="background: white; color: blue; font-family: "consolas";">out</span><span style="background: white; font-family: "consolas";">
errors);<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; font-family: "consolas";">}</span><span style="font-family: "consolas";"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The RunMacro2 method of SldWorks needs the following
arguments: 1) the full path to the macro file, 2) the module name, 3) the
procedure name, 4) run options, and 5) an output variable for error
handling. The example above assumes you
have added the VBA macro to the add-in project in Visual Studio and that it
will be copied to the installation location.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<!--
</div>
<div class="content_3" style="display: none;">
-->
<div class="MsoNormal">
Now that you have your callback function for your macro, it
is time to connect that function to the buttons in the user interface. Within the “UI Methods” code region, inside
the AddCommandMgr procedure, look for the two lines that create Command Items,
or buttons. The lines will start with
the following code in both VB and C#.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">cmdIndex0 =
cmdGroup.AddCommandItem2</span><span style="font-family: "consolas"; font-size: 9.5pt; line-height: 107%;">(...)</span><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The AddCommandItem2 method creates a command and associates
it to a callback function. It requires
nine arguments, most of which relate to button labels, tooltips and icon. For our example, the sixth argument is the
most important. Even though it is a
string, it must be the exact name of the callback function that runs your
macro. For example, if the callback function
name were MakePartVBA, the AddCommandItem2 call might look like the following.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";">cmdIndex0
= cmdGroup.AddCommandItem2(</span><span style="background: white; color: #a31515; font-family: "consolas";">"MakePart"</span><span style="background: white; font-family: "consolas";">,
-1, </span><span style="background: white; color: #a31515; font-family: "consolas";">"Make a Part"</span><span style="background: white; font-family: "consolas";">, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> </span><span style="background: white; color: #a31515; font-family: "consolas";">"Make
a part from a VBA macro"</span><span style="background: white; font-family: "consolas";">, 0, </span><span style="background: white; color: #a31515; font-family: "consolas";">"MakePartVBA"</span><span style="background: white; font-family: "consolas";">,
</span><span style="background: white; color: #a31515; font-family: "consolas";">""</span><span style="background: white; font-family: "consolas";">, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="background: white; font-family: "consolas";"> mainItemID1, </span><span style="background-color: white; font-family: "consolas";">menuToolbarOption)</span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Edit the two existing command items to run two different
macros. If you need more than two, you
will need to also declare additional variables related to command items listed
below.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-left: .5in;">
Public Class variables: mainItemID1,
mainItemID2, ... , mainItemID<i>n</i><o:p></o:p></div>
<div class="MsoNormal" style="margin-left: .5in;">
<br /></div>
<div class="MsoNormal" style="margin-left: .5in;">
AddCommandMgr procedure variables:
cmdIndex0, cmdIndex1, ... , cmdIndex<i>n</i><o:p></o:p></div>
<div class="MsoNormal" style="margin-left: .5in;">
<br /></div>
<div class="MsoNormal" style="margin-left: .5in;">
AddCommandMgr arrays: cmdIDs and
TextType <i>(you will need to modify the
array size as well as continue to populate the arrays with your additional
command items)</i><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h3>
Test your Add-In</h3>
<h1>
<o:p></o:p></h1>
<div class="MsoNormal">
Now that you’ve made edits and included your macros, try out
the new add-in. You can run in Debug
mode in Visual Studio and it will automatically register the add-in with
SOLIDWORKS. Hit Debug, Start Debugging
to automatically launch a session of SOLIDWORKS with your new add-in.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
For more information on installation and deployment options,
check out my book, Automating SOLIDWORKS Using Macros 2017 for an in-depth
chapter on the subject.<o:p></o:p></div>
</div>
<div align="center" class="MsoNormal" style="text-align: center;">
<o:p></o:p></div>
<!--
</div>
<div class="post-pagination">
<a class="button_1" href="#">1</a>
<a class="button_2" href="#">2</a>
<a class="button_3" href="#">3</a>
</div>
-->Mike Spenshttp://www.blogger.com/profile/00365628417064674695noreply@blogger.com0tag:blogger.com,1999:blog-1301703939631167951.post-58007406828937831712016-02-16T15:41:00.001-08:002016-02-16T15:41:39.860-08:00Extending SOLIDWORKS Interference Detection<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIWcltqp22hLag0A2LHYdlF3chSALBF018_gsddVbRsjaAg7LYfp9mL2-HOqQ6TqI3i63XJkcdi9Kl4ntsk_OSeXyMHLvOgUhjQVwauaQPd_C41zdEmHmP4_D_YahmNCe_dtdLQQs73zs/s1600/interference.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="270" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIWcltqp22hLag0A2LHYdlF3chSALBF018_gsddVbRsjaAg7LYfp9mL2-HOqQ6TqI3i63XJkcdi9Kl4ntsk_OSeXyMHLvOgUhjQVwauaQPd_C41zdEmHmP4_D_YahmNCe_dtdLQQs73zs/s400/interference.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Model c/o Malcom Ma</td></tr>
</tbody></table>
<div style="text-align: center;">
<br /></div>
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_lCCMT3NhbuCXiufBbh2Jp2OcpufvU-uEtxEA_PZwr9OmtsgjHElHs6gZDc2K82l84GUKiebyPHR0tkQnOkurqNq7lzowMPqZR7_ZcEYNPqqKneXrqe-O_YBej_XzSYSrlZQF4a1hCXY/s1600/interferencetool.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_lCCMT3NhbuCXiufBbh2Jp2OcpufvU-uEtxEA_PZwr9OmtsgjHElHs6gZDc2K82l84GUKiebyPHR0tkQnOkurqNq7lzowMPqZR7_ZcEYNPqqKneXrqe-O_YBej_XzSYSrlZQF4a1hCXY/s320/interferencetool.png" width="264" /></a>Have you ever run the SOLIDWORKS Interference Detection tool and thought, "If I could only save this list of interfering components?" <br />
<br />
I was talking with a colleague who recently hit this problem square-on. He wanted to work through hundreds of interference problems resulting from the lackluster design of a printed circuit board. Here comes the SOLIDWORKS API to the rescue! <br />
<br />
The fundamentals come from an API help example <a href="http://help.solidworks.com/2016/English/api/sldworksapi/Run_Interference_Detection_Example_VBNET.htm" target="_blank">here</a>. Minor modifications were made to store the data being sent to the Debug window and save it to a text file. Finally, to give the macro some real flair, adding the option to turn the interfering volumes into bodies in a part.<br />
<br />
Rather than packing all of the code into the macro's main procedure, I've created new classes named InterferenceChecking and LocalInterferences. These two classes have all of the routines needed and can be called from a separate application if you don't want to just run it as a macro.<br />
<br />
<a href="https://drive.google.com/file/d/0B60ZWiGeHEXYMGIxLW81SXFSQms/view?usp=sharing" target="_blank">Download the macro source code here (VB.NET)</a><br />
<br />
Implement the new class like this.<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">Public</span><span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Sub</span> main()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span> model <span style="color: blue;">As</span> ModelDoc2 = swApp.ActiveDoc<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
intCheck <span style="color: blue;">As</span> <span style="color: blue;">New</span>
InterferenceChecking<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> intCheck.CheckInterference(model, swApp)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> intCheck = <span style="color: blue;">Nothing<o:p></o:p></span></span></div>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">End</span><span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Sub<o:p></o:p></span></span></div>
<br />
The following is all code for the project, not including the calling reference. <br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">Imports</span><span style="font-family: "courier new"; font-size: 10.0pt;">
SolidWorks.Interop.sldworks<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">Imports</span><span style="font-family: "courier new"; font-size: 10.0pt;">
SolidWorks.Interop.swconst<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">Imports</span><span style="font-family: "courier new"; font-size: 10.0pt;">
System.Runtime.InteropServices<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">Imports</span><span style="font-family: "courier new"; font-size: 10.0pt;"> System<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">Imports</span><span style="font-family: "courier new"; font-size: 10.0pt;">
System.Diagnostics<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">Imports</span><span style="font-family: "courier new"; font-size: 10.0pt;">
System.Windows.Forms<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">Public</span><span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Class</span> InterferenceChecking<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> CheckInterference(<span style="color: blue;">ByVal</span>
model <span style="color: blue;">As</span> ModelDoc2, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">ByRef</span> swApp
<span style="color: blue;">As</span> SldWorks)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
swModelDoc <span style="color: blue;">As</span> ModelDoc2 = model<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
swAssemblyDoc <span style="color: blue;">As</span> AssemblyDoc<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
pIntMgr <span style="color: blue;">As</span> InterferenceDetectionMgr<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
vInts <span style="color: blue;">As</span> <span style="color: blue;">Object<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span> i <span style="color: blue;">As</span> <span style="color: blue;">Long<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span> j <span style="color: blue;">As</span> <span style="color: blue;">Long<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
interference <span style="color: blue;">As</span> IInterference<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
vComps <span style="color: blue;">As</span> <span style="color: blue;">Object</span>
= <span style="color: blue;">Nothing<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
comp <span style="color: blue;">As</span> Component2<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span> vol
<span style="color: blue;">As</span> <span style="color: blue;">Double<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
vTrans <span style="color: blue;">As</span> <span style="color: blue;">Object</span>
= <span style="color: blue;">Nothing<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span> ret
<span style="color: blue;">As</span> <span style="color: blue;">Boolean<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
repMessage <span style="color: blue;">As</span> <span style="color: blue;">String</span>
= <span style="color: maroon;">"Interference report for: "</span> _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> & model.GetTitle & vbCrLf<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> swAssemblyDoc = swModelDoc<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr =
swAssemblyDoc.InterferenceDetectionManager<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: green;">' Specify the
interference detection settings and options<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr.TreatCoincidenceAsInterference
= <span style="color: blue;">False<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr.TreatSubAssembliesAsComponents
= <span style="color: blue;">True<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;">
pIntMgr.IncludeMultibodyPartInterferences = <span style="color: blue;">True<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr.MakeInterferingPartsTransparent
= <span style="color: blue;">False<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr.CreateFastenersFolder = <span style="color: blue;">True<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr.IgnoreHiddenBodies = <span style="color: blue;">True<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr.ShowIgnoredInterferences = <span style="color: blue;">False<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr.UseTransform = <span style="color: blue;">True<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: green;">' Specify how
to display non-interfering components<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr.NonInterferingComponentDisplay
= _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> swNonInterferingComponentDisplay_e. _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;">
swNonInterferingComponentDisplay_Wireframe<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: green;">' Run
interference detection<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> vInts = pIntMgr.GetInterferences<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> Debug.Print(<span style="color: maroon;">"Total
number of interferences: "</span> _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> & pIntMgr.GetInterferenceCount)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> repMessage = repMessage _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt; text-indent: 0.5in;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> & <span style="color: maroon;">"Total
number of interferences: "</span> _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> & pIntMgr.GetInterferenceCount
& vbCrLf<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: green;">' Get
interfering components and transforms<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> ret =
pIntMgr.GetComponentsAndTransforms(vComps, vTrans)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
bodies(0) <span style="color: blue;">As</span> <span style="color: blue;">Object<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: green;">' Get
interference information<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">For</span> i =
0 <span style="color: blue;">To</span> UBound(vInts)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> Debug.Print(<span style="color: maroon;">"Interference "</span> & (i + 1))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> repMessage = repMessage & <span style="color: maroon;">"Interference "</span> & (i + 1) _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in; text-indent: 0.5in;">
<span style="font-family: "courier new"; font-size: 10.0pt;">& vbCrLf<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> interference = vInts(i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">ReDim</span>
<span style="color: blue;">Preserve</span> bodies(i)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> bodies(i) =
interference.GetInterferenceBody()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> Debug.Print( _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in; text-indent: 0.5in;">
<span style="color: maroon; font-family: "courier new"; font-size: 10.0pt;">" Number of components in this interference:
"</span><span style="font-family: "courier new"; font-size: 10.0pt;"> _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> & interference.GetComponentCount)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> repMessage = repMessage _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> & <span style="color: maroon;">" Number of components in this interference:
"</span> _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> & interference.GetComponentCount
& vbCrLf<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> vComps = interference.Components<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">For</span>
j = 0 <span style="color: blue;">To</span> UBound(vComps)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> comp = vComps(j)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
cName <span style="color: blue;">As</span> <span style="color: blue;">String</span>
= comp.ComponentReference<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">If</span>
cName = <span style="color: maroon;">""</span> <span style="color: blue;">Then</span>
cName = comp.Name2<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> Debug.Print(<span style="color: maroon;">" "</span>
& cName)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> repMessage = repMessage & <span style="color: maroon;">" "</span>
& cName & vbCrLf<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Next</span>
j<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> vol = interference.Volume<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
volMessage <span style="color: blue;">As</span> <span style="color: blue;">String</span>
= _</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"><span style="color: maroon;"> "
Interference volume: "</span> _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> & Format((vol * 1000000000), <span style="color: maroon;">"#.###"</span>) & <span style="color: maroon;">"
mm^3"<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> Debug.Print(volMessage)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> repMessage = repMessage &
volMessage & vbCrLf<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Next</span> i<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: green;">' Stop
interference detection<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> pIntMgr.Done()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">If</span>
MsgBox(<span style="color: maroon;">"Save report to file?"</span>,
MsgBoxStyle.YesNo _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> + MsgBoxStyle.Question) =
MsgBoxResult.Yes <span style="color: blue;">Then<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: green;">'write
out the report<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> WriteReport(repMessage,
model.GetTitle & <span style="color: maroon;">".txt"</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">End</span> <span style="color: blue;">If<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">If</span>
MsgBox(<span style="color: maroon;">"Create intereference volume part?"</span>
& vbCrLf _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> & <span style="color: maroon;">"*Complex
interference can take time."</span>, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> MsgBoxStyle.YesNo + MsgBoxStyle.Question)
_<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt; text-indent: 0.5in;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> = MsgBoxResult.Yes <span style="color: blue;">Then<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: green;">'Create a
part with the bodies array<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
intf <span style="color: blue;">As</span> <span style="color: blue;">New</span>
LocalInterferences(bodies)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;">
intf.CreateInterferingBodiesPart(swApp)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> intf = <span style="color: blue;">Nothing<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">End</span> <span style="color: blue;">If<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">End</span> <span style="color: blue;">Sub<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Private</span> <span style="color: blue;">Sub</span> WriteReport(<span style="color: blue;">ByVal</span>
repMessage <span style="color: blue;">As</span> <span style="color: blue;">String</span>,
_<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Optional</span> <span style="color: blue;">ByVal</span> defaultName <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">""</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span> sfd
<span style="color: blue;">As</span> <span style="color: blue;">New</span>
SaveFileDialog<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> sfd.DefaultExt = <span style="color: maroon;">"txt"<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> sfd.Filter = <span style="color: maroon;">"Text
file(*.txt)|*.txt"<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> sfd.FileName = defaultName<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> sfd.OverwritePrompt = <span style="color: blue;">True<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
diaRes <span style="color: blue;">As</span> DialogResult = sfd.ShowDialog()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">If</span>
diaRes = DialogResult.OK <span style="color: blue;">Then<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
filePath <span style="color: blue;">As</span> <span style="color: blue;">String</span>
= sfd.FileName<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">My</span>.Computer.FileSystem.WriteAllText(sfd.FileName,
_<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> repMessage, <span style="color: blue;">False</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> Process.Start(sfd.FileName)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">End</span> <span style="color: blue;">If<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">End</span> <span style="color: blue;">Sub<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">End</span><span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Class<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">Class</span><span style="font-family: "courier new"; font-size: 10.0pt;">
LocalInterferences<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
intBodies <span style="color: blue;">As</span> <span style="color: blue;">Object<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Friend</span> <span style="color: blue;">Sub</span> CreateInterferingBodiesPart( _</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"><span style="color: blue;"> ByRef</span> swApp <span style="color: blue;">As</span>
SldWorks)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">If</span>
intBodies <span style="color: blue;">Is</span> <span style="color: blue;">Nothing</span>
<span style="color: blue;">Then<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Exit</span>
<span style="color: blue;">Sub<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">End</span> <span style="color: blue;">If<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
part <span style="color: blue;">As</span> PartDoc<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> part = swApp.NewPart()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">For</span> i <span style="color: blue;">As</span> <span style="color: blue;">Integer</span> = 0 <span style="color: blue;">To</span> UBound(intBodies)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Dim</span>
feat <span style="color: blue;">As</span> Feature</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10pt;"> feat = part.CreateFeatureFromBody3(intBodies(i), _</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">False</span>,
swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> + swCreateFeatureBodyOpts_e.swCreateFeatureBodySimplify)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Next</span> i<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">End</span> <span style="color: blue;">Sub<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> <span style="color: blue;">New</span>(<span style="color: blue;">ByVal</span> bodies <span style="color: blue;">As</span> <span style="color: blue;">Object</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> intBodies = bodies<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">End</span> <span style="color: blue;">Sub<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "courier new"; font-size: 10.0pt;">End</span><span style="font-family: "courier new"; font-size: 10.0pt;"> <span style="color: blue;">Class<o:p></o:p></span></span></div>
Mike Spenshttp://www.blogger.com/profile/00365628417064674695noreply@blogger.com1tag:blogger.com,1999:blog-1301703939631167951.post-63186816243872824432016-01-26T16:29:00.002-08:002016-02-09T13:06:28.489-08:00SOLIDWORKS Document Manager API Toolkit<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXT_8fk6EIlFT993mYtQHuyNqP0ZKRkUmZBH95QgF4yJcyqm2eF-Ioulb3jghgcpytlvqQggMgvy8l6skjoob_s5brP9OI0nA8mV7JOsSYoBlXXdckA8B-VtLoqHTd5yrpeDTEvNji95w/s1600/swtoolbox.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXT_8fk6EIlFT993mYtQHuyNqP0ZKRkUmZBH95QgF4yJcyqm2eF-Ioulb3jghgcpytlvqQggMgvy8l6skjoob_s5brP9OI0nA8mV7JOsSYoBlXXdckA8B-VtLoqHTd5yrpeDTEvNji95w/s1600/swtoolbox.png" /></a></div><br />
The SOLIDWORKS Document Manager is a powerful tool used for reporting content and manipulating SOLIDWORKS parts, assemblies and drawings. I’ve used it to export custom properties from thousands of SOLIDWORKS files in seconds! It can be used to read bill of materials and other tables from drawings as well as update references when files are renamed. <br />
<br />
But, just like any other project, it can take time to get started and get familiar with the calls needed. To make that all easier, I’ve put together a Visual Studio 2010 project in VB.NET I’m calling the Document Manager Toolkit. <br />
<br />
I’ll be presenting the toolkit along with a general discussion of the SOLIDWORKS Document Manager at SOLIDWORKS World 2016 in Dallas, TX next week. Hope to see you there!<br />
<br />
“What’s in the toolkit?” you ask? Download the project here for a sample implementation and read up on the general functionality below.<br />
<br />
<a href="https://drive.google.com/file/d/0B60ZWiGeHEXYNUt5TXRGamxrVmc/view?usp=sharing" target="_blank">Document Manager Toolkit Download</a><br />
<a href="https://drive.google.com/file/d/0B60ZWiGeHEXYVmtNREFVVENwZG8/view?usp=sharing" target="_blank">Document Manager Presentation PDF (SOLIDWORKS World 2016)</a><br />
<br />
<h2>
MyDocMan Class</h2>
<br />
The MyDocMan class is the basis for the toolkit. It takes care of license validation and a plethora of common operations shared below. The functions and procedures are documented in the project to make it easier to use in your own tools.<br />
<br />
Instantiating the class will connect to the SOLIDWORKS Document Manager and is built to read your Document Manager license from a text file named “DocMgrLicense.txt” from C:\. Edit the GetDocManApp procedure to insert and compile your license, redirect the license file location, or point to license information saved in My.Settings.<br />
<br />
<h3>
Common Operations</h3>
<div>
<br /></div>
<div>
<a href="#GetDocument">GetDocument</a><br />
<a href="#GetAllProperties">GetAllProperties</a><br />
<a href="#GetDelimitedTableText">GetDelimitedTableText</a><br />
<a href="#GetBOMTableNames">GetBOMTableNames</a><br />
<a href="#GetMissingReferences">GetMissingReferences</a></div>
<br />
<h4>
<a name="GetDocument">GetDocument</a></h4>
<b>Summary:</b><br />
From a full file path, return an opened SwDMDocument.<br />
<br />
<b>Parameters:</b><br />
<i>FilePath</i>: full path to the desired SOLIDWORKS file<br />
<i>forReadOnly</i>: True to open the file read-only, False to open with write access.<br />
<b><br /></b>
<b>Return Values:</b><br />
Returns an SwDMDocument interface if successful.<br />
<br />
<h4>
<a name="GetAllProperties">GetAllProperties</a></h4>
<b>Summary:</b><br />
Function to read all file and configuration specific custom properties from a document.<br />
<br />
<b>Parameters:</b><br />
<i>dmDoc</i>: Document Manager document<br />
<b><br /></b>
<b>Return Values:</b><br />
A tab-delimited string containing all configuration names, properties and values. An empty string in the configuration column indicates a file property.<br />
<br />
<b>Remarks:</b><br />
Each line in the string will terminate in a carriage return character.<br />
<br />
<h4>
<a name="GetDelimitedTableText">GetDelimitedTableText</a></h4>
<b>Summary:</b><br />
Get all text from a named table with the desired delimiter.<br />
<b><br /></b>
<b>Parameters:</b><br />
<i>tableName</i>: Name of the table in the SOLIDWORKS file<br />
<i>dmDoc</i>: the SwDMDocument10 interface containing the table<br />
delimiter: the desired column delimiter<br />
<br />
<b>Return Values:</b><br />
A string representing the text of the entire table, including headers. Return characters are applied between rows.<br />
<br />
<h4>
<a name="GetBOMTableNames">GetBOMTableNames</a></h4>
<b>Summary:</b><br />
Gets all table names from a document.<br />
<b><br /></b>
<b>Parameters:</b><br />
<i>dmDoc</i>: The desired document containing tables.<br />
<br />
<b>Return Values:</b><br />
An array of strings in an Object.<br />
<br />
<b>Remarks:</b><br />
Use this function before calling GetDelimitedTableText to retrieve a valid BOM table name.<br />
<br />
<h4>
<a name="GetMissingReferences">GetMissingReferences</a></h4>
<b>Summary:</b><br />
Recursive routine to report all missing file references.<br />
<b><br /></b>
<b>Parameters:</b><br />
<i>dmDoc</i>: Parent document of reported children, typically an assembly or drawing.<br />
<br />
<b>Remarks:</b><br />
Reports back a string of missing reference files separated by a return character.<br />
<br />
<h3>
File Operations</h3>
</br>
<a href="#BrowseForFile">BrowseForFile</a><br />
<a href="#GetFilesFromPath">GetFilesFromPath</a><br />
<br />
<h4>
<a name="BrowseForFile">BrowseForFile</a></h4>
<b>Summary:</b><br />
Browse for a SOLIDWORKS file using the Windows OpenFileDialog<br />
<br />
<b>Return Values:</b><br />
Returns the full path to the selected file.<br />
<br />
<h4>
<a name="GetFilesFromPath">GetFilesFromPath</a></h4>
<b>Summary:</b><br />
Get all file paths from a user-selected directory.<br />
<br />
<b>Parameters:</b><br />
<i>extensionFilter</i>: a file extension to filter. "*.sld*" would retrieve all SOLIDWORKS files.<br />
<i>includeSubFolders</i>: Set to True to get files from all sub folders<br />
<br />
<b>Return Values:</b><br />
Returns an array of full file paths.<br />
<br />
<b>Remarks:</b><br />
Temp files, ~$Part1.sldprt, should be filtered from your results before processing.<br />
<br />Mike Spenshttp://www.blogger.com/profile/00365628417064674695noreply@blogger.com8tag:blogger.com,1999:blog-1301703939631167951.post-64497589189107648372015-12-28T15:05:00.000-08:002015-12-28T15:05:18.099-08:00Common VBA Procedures for Batch Export<div class="MsoNormal">
Try as Microsoft may, it doesn’t look like VBA is going away
anytime soon! And really, it’s a great
place to start developing macros for SOLIDWORKS. The macros are single-file, self-contained,
and easy to develop. Not to mention that
there are thousands of existing VBA macros out there and lots of sample code online. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The downside? VBA
doesn’t have as many pre-built functions for common file operations available
to C# and VB.net. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In an effort to make your macro development easier, I’ve put
together a handful of common routines and functions you can add to your
SOLIDWORKS VBA macros. If there are
others you’d like to see, let me know!<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Batch processing of files is one of those places where you
need to manipulate file information.
Rather than packing all of the processing into one routine, I have broken
it out into common functions so you can save them into a code module to use in
all of your macros.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h2>
Macro Code<o:p></o:p></h2>
<div class="MsoNormal">
Start by adding the following procedures and functions to
your VBA macro.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Option
Explicit<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Dim swApp
As SldWorks.SldWorks<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'batch process using Dir - one
folder level only<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Sub
BatchConvert(folderPath As String, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileNameFilter As String, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> convertExtension As String, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Optional
outputFolderPath As String)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim file As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'use Dir function to get all files matching the filter <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'in the specific</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> folder<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> file
= Dir(folderPath & fileNameFilter)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Do While Not file = ""<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim filePath As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> filePath = folderPath & file<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim newFilePath As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> newFilePath = ConvertSWFile(filePath,
convertExtension, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> outputFolderPath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"> 'get the next file<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> file = Dir<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Loop<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End Sub<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'batch process using
FileSystemObject - all sub folders included<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'must add a reference to Microsoft
Scripting Runtime type library<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Sub
BatchConvert2(folderPath As String, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileNameFilter As String, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> convertExtension As String, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Optional doSubFolders As Boolean = False, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Optional outputFolderPath As String)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim file As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim fso As New FileSystemObject<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim fld As Scripting.Folder<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Set fld = fso.GetFolder(folderPath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim f As Scripting.file<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> For Each f In fld.files()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"> 'wildcards not allowed in
fileNameFilter when using <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"> 'FileSystemObject methods<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> If InStr(1, f.Name, fileNameFilter, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> vbTextCompare) <> 0 Then<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim newFilePath As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> newFilePath = ConvertSWFile(f.Path, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> convertExtension,
outputFolderPath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> End If<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Next<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'recursive routine if doSubFolders is True<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> If doSubFolders Then<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim subFolder As Scripting.Folder<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> For Each subFolder In fld.subFolders<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Call BatchConvert2(subFolder.Path,
fileNameFilter, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> convertExtension, doSubFolders,
outputFolderPath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Next<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> End If<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End Sub<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'open the file in SOLIDWORKS and
convert by changing <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'</span><span style="color: green; font-family: 'Courier New'; font-size: 13.3333px; line-height: 15.3333px;">the file extension. </span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; line-height: 115%;">new file will be created in the </span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: 'Courier New'; font-size: 13.3333px; line-height: 15.3333px;">'</span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; line-height: 115%;">same folder as the parent file</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'or, pass an outputFolderPath
string to send files to </span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: 'Courier New'; font-size: 13.3333px; line-height: 15.3333px;">'</span><span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">a specific folder<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Function
ConvertSWFile(filePath As String, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> convertExtension As String, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Optional outputFolderPath As String) As
String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim model As ModelDoc2<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim fileType As Long<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileType = GetSWFileType(filePath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim errors As Long, warnings As Long<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Set model = swApp.OpenDoc6(filePath, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileType, swOpenDocOptions_Silent, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> "", errors, warnings)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> If model Is Nothing Then<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> ConvertSWFile = ""<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Exit Function<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> End If<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim newFilePath As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> newFilePath = ChangeExtension(filePath,
convertExtension)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> If outputFolderPath <> ""
Then<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> newFilePath =
ChangeFolderPath(newFilePath, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> outputFolderPath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> End If<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Call model.Extension.SaveAs(newFilePath, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> swSaveAsCurrentVersion, </span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 115%;">swSaveAsOptions_Silent, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 115%;"> Nothing,
errors, warnings)</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Call swApp.CloseDoc(filePath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> ConvertSWFile = newFilePath<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End
Function<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'return the file type from a full
file path<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'helpful for opening various
SOLIDWORKS files<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Function
GetSWFileType(filePath As String) As Long<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim ext As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim fileType As Long<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> ext = Right(filePath, Len(filePath) -
InStrRev(filePath, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> ".") + 1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Select Case UCase(ext)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Case ".SLDPRT"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileType = swDocPART<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Case ".SLDASM"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileType = swDocASSEMBLY<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Case ".SLDDRW"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileType = swDocDRAWING<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Case Else<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileType = swDocNONE<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> End Select<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> GetSWFileType = fileType<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End
Function<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'============ File path
manipulation ==================<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'change the extension of a file
from a given file path<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Function
ChangeExtension(filePath As String, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">newExtension
As String) As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim filePathNoExt As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> filePathNoExt = Left(filePath,
InStrRev(filePath, ".") - 1)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> ChangeExtension = filePathNoExt &
newExtension<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End
Function<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'change the folder path of the file<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Function
ChangeFolderPath(filePath As String, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">newFolderPath As String) As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim fileName As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileName = Right(filePath, _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Len(filePath) -
InStrRev(filePath, </span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 115%;">"\"))</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> ChangeFolderPath = newFolderPath &
fileName<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End Function<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'verify the path ends with \<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Function
VerifyFolderPath(folderPath As String) As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> If InStrRev(folderPath, "\")
<> Len(folderPath) Then<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> VerifyFolderPath = folderPath &
"\"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Else<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> VerifyFolderPath = folderPath<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> End If<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End Function<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'verify folder exists<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Function
VerifyFileFolderExists(fullPath As String) As Boolean<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim res As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> res = Dir(fullPath, vbDirectory)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'result will be some value if the folder exists<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> If res = vbNullString Or fullPath =
"" Then<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> VerifyFileFolderExists = False<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Else<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> VerifyFileFolderExists = True<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> End If<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End
Function<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'============ Other utilities
===================<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'open any file in its associated
application<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Sub
OpenFile(ByVal filePath As String)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim WshShell As Object<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Set WshShell =
CreateObject("WScript.Shell")<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> 'append quotes in case of spaces in file
path<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> WshShell.Run Chr(34) & filePath &
Chr(34)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End Sub<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'show a folder browser dialog and
return the selected folder path <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'or empty</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> </span><span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">string if cancelled<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Function
BrowseFolder(message As String) As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim folderBrowser As Object<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim options As Integer<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> options = 40 </span><span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">'UI with New Folder button and right-click menus</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'options = 0 'UI without New Folder button </span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"> </span><span style="color: green; font-family: 'Courier New'; font-size: 13.3333px; line-height: 15.3333px;">'</span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; line-height: 115%;">and no right-click</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 115%;"> </span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; line-height: 115%;">menus</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Set folderBrowser =
CreateObject("Shell.Application"). _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> BrowseForFolder(0, message, options)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> On Error Resume Next<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> BrowseFolder = folderBrowser.Self.Path<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> On Error GoTo 0<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">End
Function</span><span style="font-family: "Courier New";"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"><br /></span></div>
<h2>
Usage<o:p></o:p></h2>
<div class="MsoNormal">
The following main procedure makes use of most of these
procedures and functions to publish SOLIDWORKS files to PDF in a selected
directory.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;">Sub main()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Set swApp = Application.SldWorks<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim folderPath As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'browse for folder<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> folderPath = BrowseFolder("Select
folder to process...")<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> folderPath = VerifyFolderPath(folderPath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> If Not VerifyFileFolderExists(folderPath)
Then Exit Sub<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim convertExtension As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> convertExtension = ".PDF"<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'if using Dir function to get files using wildcards<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim fileNameFilter As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> fileNameFilter = _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> InputBox("Enter the file name
filter (*.sldprt, *.sld*):")<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Dim outputFolderPath As String<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
''optionally, set an output folder path<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'outputFolderPath = _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"> </span><span style="color: green; font-family: 'Courier New'; font-size: 13.3333px; line-height: 15.3333px;">'</span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; line-height: 115%;">BrowseFolder("Select destination
folder...")</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'outputFolderPath = VerifyFolderPath(outputFolderPath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> Call BatchConvert(folderPath,
fileNameFilter, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> convertExtension, outputFolderPath)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
''or if using FileSystemObject to process all sub folders...<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'fileNameFilter = _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'InputBox("Enter the file name filter </span><span style="color: green; font-family: 'Courier New'; font-size: 13.3333px; line-height: 15.3333px;">" _</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: 'Courier New'; font-size: 13.3333px; line-height: 15.3333px;"> </span><span style="color: green; font-family: 'Courier New'; font-size: 13.3333px; line-height: 15.3333px;">'</span><span style="color: green; font-family: 'Courier New'; font-size: 13.3333px; line-height: 15.3333px;">& "</span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; line-height: 115%;">(sldprt or</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 115%;"> </span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; line-height: 115%;">1234.slddrw):")</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
'Call BatchConvert2(folderPath, fileNameFilter, _<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">
' convertExtension,</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> </span><span style="color: green; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">True)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%;"> MsgBox "Finished!"<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin;">End Sub</span></div>
<div class="MsoNormal">
<br /></div>
<h2>
Descriptions<o:p></o:p></h2>
<div class="MsoNormal">
<b>BatchConvert</b> uses
the Dir function to get all files in a given folder and allows wildcards in the
file name filter. This method doesn’t
support processing of sub-folders. </div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>BatchConvert2</b> allows processing of
sub-folders, but doesn’t allow wildcards because of its use of the
FileSystemObject. Either routine must be
passed the desired conversion extension, including the period (.PDF in this
example). Optionally, you can pass an
output folder path as a final argument.
If an output folder is specified, all published files go to that
folder. If no output folder path is
passed, the files are created in the same location as their source files.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>ConvertSWFile</b> is
called for each file path. This does all
the heavy lifting of opening the file in SOLIDWORKS, changing the file extension
and optional output folder path, saving the new output file and closing the
source file. It returns the newly
created file name if you need to log the results.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h2>
File Path Manipulation<o:p></o:p></h2>
<div class="MsoNormal">
There are a few functions to help with common operations
like changing file extensions, changing folder paths as well as verifying a
folder path string is complete and verifying if a file or folder exists.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h2>
Other Utilities<o:p></o:p></h2>
<div class="MsoNormal">
The last section includes <b>OpenFile</b>, a routine to open any file in its associated application. This is triggering the Windows shell, so it
is just like double-clicking on a file in Windows Explorer. It does not return an interface to the opened
file and does not wait until the operation is complete. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>BrowseFolder</b> is a
function that launches a folder browser and returns the user-selected folder
path or an empty string if cancelled. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
I hope you find these helpful in your next macro
project! Let me know if there are others
you’d like to see or share.<o:p></o:p></div>
Mike Spenshttp://www.blogger.com/profile/00365628417064674695noreply@blogger.com0tag:blogger.com,1999:blog-1301703939631167951.post-18568416963755486312012-02-10T16:22:00.000-08:002012-02-13T00:33:37.568-08:00SolidWorks World 2012 Presentation Files<div id="komoona_cboxOverlay" style="display: none; height: 100%; left: 0px; overflow: hidden; position: fixed; top: 0px; width: 100%; z-index: 9999;"></div><div id="komoona_colorbox" style="display: none; left: 0px; overflow: hidden; padding-bottom: 50px; padding-right: 50px; position: fixed; top: 0px; z-index: 9999;"><div id="komoona_cboxWrapper" style="left: 0px; overflow: hidden; position: absolute; top: 0px; z-index: 9999;"><div><div id="komoona_cboxTopLeft" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/border1.png") no-repeat 0px 0px; float: left; height: 25px; width: 25px;"></div><div id="komoona_cboxTopCenter" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/border1.png") repeat-x 0px -50px; float: left; height: 25px;"></div><div id="komoona_cboxTopRight" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/border1.png") no-repeat -25px 0px; float: left; height: 25px; width: 25px;"></div></div><div style="clear: left;"><div id="komoona_cboxMiddleLeft" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/border2.png") repeat-y 0px 0px; float: left; width: 25px;"></div><div id="komoona_cboxContent" style="background: rgb(255, 255, 255); float: left; overflow: hidden; position: relative;"><div id="komoona_cboxLoadedContent" style="height: 0px; overflow: hidden; width: 0px;"></div><div id="komoona_cboxLoadingOverlay" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/loading.gif") no-repeat 5px 5px rgb(255, 255, 255); left: 0px; position: absolute; top: 0px; width: 100%;"></div><div id="komoona_cboxLoadingGraphic" style="left: 0px; position: absolute; top: 0px; width: 100%;"></div><div id="komoona_cboxClose" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/controls.png") -50px 0px; cursor: pointer; height: 20px; position: absolute; right: 2px; text-indent: -9999px; top: 1px; width: 20px;"></div></div><div id="komoona_cboxMiddleRight" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/border2.png") repeat-y -25px 0px; float: left; width: 25px;"></div></div><div style="clear: left;"><div id="komoona_cboxBottomLeft" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/border1.png") no-repeat 0px -25px; float: left; height: 25px; width: 25px;"></div><div id="komoona_cboxBottomCenter" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/border1.png") repeat-x 0px -75px; float: left; height: 25px;"></div><div id="komoona_cboxBottomRight" style="background: url("http://dimrh7ecexr02.cloudfront.net/images/komoona_colorbox/border1.png") no-repeat -25px -25px; float: left; height: 25px; width: 25px;"></div></div></div><div style="display: none; position: absolute; visibility: hidden; width: 9999px;"></div></div>Use these links to download the files to my presentations at SolidWorks World. <br />
<br />
**After selecting the link below, select File, Download Original from the menus at the top of the page to download the entire zip file.<br />
<br />
<a href="https://docs.google.com/open?id=0B3lhos7k5bhoNDRlM2FkZTItMzZlZS00ZWU2LTkxNTMtNzNiZGFhZDk2ZGU0" target="_blank">Introduction to the EPDM API</a><br />
<a href="https://docs.google.com/open?id=0B3lhos7k5bhoNGQxODlmZTEtYjVhYi00NDgyLWFmNWUtMWIwMjMwMmY0MGRi" target="_blank">Tips and Strategies for Large Automation Projects</a>Mike Spenshttp://www.blogger.com/profile/00365628417064674695noreply@blogger.com4tag:blogger.com,1999:blog-1301703939631167951.post-37098204410321467292012-02-09T21:05:00.000-08:002012-02-09T21:05:00.462-08:00SolidWorks Macro Performance Enhancers<span style="font-family: Calibri;">No one likes a slow macro.<span style="mso-spacerun: yes;"> </span>What’s worse is to be embarrassed by your peers with lackluster speed resulting in macro envy!<span style="mso-spacerun: yes;"> </span>Don’t let your macros suffer from this unnecessary condition.<span style="mso-spacerun: yes;"> </span>Add a few of these tweaks to your code to re-invigorate their performance.</span><br />
<h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">SketchManager.AddToDB<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">If your macro is ever creating sketch geometry, this one is a must!<span style="mso-spacerun: yes;"> </span>Try the following VB.NET macro code.<span style="mso-spacerun: yes;"> </span>Start by opening a new part and inserting a sketch on any plane.<span style="mso-spacerun: yes;"> </span>The macro is supposed to create 5 circles in a very small pattern.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Sub</span> main()<o:p></o:p></span></div><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swApp <span style="color: blue;">As</span> SldWorks.SldWorks<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> Part <span style="color: blue;">As</span> ModelDoc2<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> skSegment <span style="color: blue;">As</span> SketchSegment<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> skMgr <span style="color: blue;">As</span> SketchManager<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p> </o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swApp = Application.SldWorks<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p> </o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>Part = swApp.ActiveDoc<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr = Part.SketchManager<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(0#, 0#, 0#, 0.001)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(0.0005, 0.0005, 0#, 0.0001)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(0.0005, -0.0005, 0#, 0.0001)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(-0.0005, 0.0005, 0#, 0.0001)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(-0.0005, -0.0005, 0#, 0.0001)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span><o:p></o:p><br />
<br />
<span style="font-family: Calibri;">There’s a good chance that when you run the code, there are no circles drawn as a result.<span style="mso-spacerun: yes;"> </span>If you zoom in to the origin close enough, it will run.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Now, try the following.<span style="mso-spacerun: yes;"> </span>AddToDB is set to True to cause the sketch geometry to be created and to avoid snapping to other nearby geometry.<span style="mso-spacerun: yes;"> </span>AddToDB should be set back to False when you’re done with the sketch.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Sub</span> main()<o:p></o:p></span></div><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swApp <span style="color: blue;">As</span> SldWorks.SldWorks<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> Part <span style="color: blue;">As</span> ModelDoc2<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> skSegment <span style="color: blue;">As</span> SketchSegment<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> skMgr <span style="color: blue;">As</span> SketchManager<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p> </o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swApp = Application.SldWorks<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p> </o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>Part = swApp.ActiveDoc<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr = Part.SketchManager<o:p></o:p></span><br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.AddToDB = <span style="color: blue;">True<o:p></o:p></span></span></b><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(0#, 0#, 0#, 0.001)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(0.0005, 0.0005, 0#, 0.0001)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(0.0005, -0.0005, 0#, 0.0001)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(-0.0005, 0.0005, 0#, 0.0001)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.CreateCircleByRadius(-0.0005, -0.0005, 0#, 0.0001)<o:p></o:p></span><br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>skMgr.AddToDB = <span style="color: blue;">False<o:p></o:p></span></span></b><br />
<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub<o:p></o:p></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">See the difference?<span style="mso-spacerun: yes;"> </span>Now, the enhancement doesn’t come without some side effects which may include missing automatic relationships and occasional headaches for other users who need to work with the sketch.</span></div><h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">ModelView.EnableGraphicsUpdate<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Sometimes a macro seems to go so fast that it’s over before you were ready.<span style="mso-spacerun: yes;"> </span>You never saw anything happen until, whammo, it’s on the screen.<span style="mso-spacerun: yes;"> </span>Well, there’s a good chance the programmer used this little magic trick.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">The ModelView is the current viewport for the file in SolidWorks.<span style="mso-spacerun: yes;"> </span>If a user regularly splits the viewport into multiple views, you would need to operate on each of them.<span style="mso-spacerun: yes;"> </span>Luckily most users don’t.<span style="mso-spacerun: yes;"> </span>You can get to the ModelView interface by calling ModelDoc2.ActiveView.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Record yourself a macro of whatever actions you’d like.<span style="mso-spacerun: yes;"> </span>But keep it to one model.<span style="mso-spacerun: yes;"> </span>The macro below is a subset of me zooming and rotating all over the screen.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> main()<o:p></o:p></span></div><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swDoc <span style="color: blue;">As</span> ModelDoc2 = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swPart <span style="color: blue;">As</span> PartDoc = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swDrawing <span style="color: blue;">As</span> DrawingDoc = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swAssembly <span style="color: blue;">As</span> AssemblyDoc = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> boolstatus <span style="color: blue;">As</span> <span style="color: blue;">Boolean</span> = <span style="color: blue;">False<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> longstatus <span style="color: blue;">As</span> <span style="color: blue;">Integer</span> = 0<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> longwarnings <span style="color: blue;">As</span> <span style="color: blue;">Integer</span> = 0<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc = <span style="color: blue;">CType</span>(swApp.ActiveDoc, ModelDoc2)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> myModelView <span style="color: blue;">As</span> ModelView = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView = <span style="color: blue;">CType</span>(swDoc.ActiveView, ModelView)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView.FrameState = _</span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> swWindowState_e.swWindowMaximized<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView = <span style="color: blue;">CType</span>(swDoc.ActiveView, ModelView)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView.RotateAboutCenter(0.10566, 0)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView = <span style="color: blue;">CType</span>(swDoc.ActiveView, ModelView)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView.RotateAboutCenter(0.05513, 0)<o:p></o:p></span><br />
<i style="mso-bidi-font-style: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>.... way too much stuff in between to post ....<o:p></o:p></span></i><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc.ViewZoomtofit2()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc.HideComponent2()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc.ClearSelection2(<span style="color: blue;">True</span>)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc.ShowNamedView2(<span style="color: maroon;">"*Top"</span>, 5)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span><span style="font-size: 10pt; line-height: 115%;"><o:p></o:p></span><br />
<br />
<span style="font-family: Calibri;">Playing back the macro gives you a dizzying display of nearly the exact steps you just recorded.<span style="mso-spacerun: yes;"> </span>Now, for the performance boost.<span style="mso-spacerun: yes;"> </span>Add the following call to set EnableGraphicsUpdate to False until everything is done.<span style="mso-spacerun: yes;"> </span>Then set it back to True to see a final graphics update.<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> main()<o:p></o:p></span></div><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swDoc <span style="color: blue;">As</span> ModelDoc2 = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swPart <span style="color: blue;">As</span> PartDoc = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swDrawing <span style="color: blue;">As</span> DrawingDoc = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swAssembly <span style="color: blue;">As</span> AssemblyDoc = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> boolstatus <span style="color: blue;">As</span> <span style="color: blue;">Boolean</span> = <span style="color: blue;">False<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> longstatus <span style="color: blue;">As</span> <span style="color: blue;">Integer</span> = 0<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> longwarnings <span style="color: blue;">As</span> <span style="color: blue;">Integer</span> = 0<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc = <span style="color: blue;">CType</span>(swApp.ActiveDoc, ModelDoc2)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> myModelView <span style="color: blue;">As</span> ModelView = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView = <span style="color: blue;">CType</span>(swDoc.ActiveView, ModelView)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView.FrameState = _</span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> swWindowState_e.swWindowMaximized<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView = <span style="color: blue;">CType</span>(swDoc.ActiveView, ModelView)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p> </o:p></span><br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView.EnableGraphicsUpdate = <span style="color: blue;">False</span><o:p></o:p></span></b><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p> </o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView.RotateAboutCenter(0.10566, 0)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView = <span style="color: blue;">CType</span>(swDoc.ActiveView, ModelView)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView.RotateAboutCenter(0.055131, 0)<o:p></o:p></span><br />
<i style="mso-bidi-font-style: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>.... way too much stuff in between to post ....<o:p></o:p></span></i><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc.ViewZoomtofit2()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc.HideComponent2()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc.ClearSelection2(<span style="color: blue;">True</span>)<o:p></o:p></span><br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p> </o:p></span></b><br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myModelView.EnableGraphicsUpdate = <span style="color: blue;">True</span><o:p></o:p></span></b><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p> </o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc.ShowNamedView2(<span style="color: maroon;">"*Top"</span>, 5)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span><span style="font-size: 10pt; line-height: 115%;"><o:p></o:p></span><br />
<br />
<span style="font-family: Calibri;">Now run the macro again and, presto change-o, all you’ll see is a brief pause and the end result.<span style="mso-spacerun: yes;"> </span>Add this to any existing macro that takes time to process and you’ll likely see improvements.<span style="mso-spacerun: yes;"> </span>Watch out though.<span style="mso-spacerun: yes;"> </span>This one can come with a nasty side effect if you don’t handle and trap errors.<span style="mso-spacerun: yes;"> </span>If your code stops before setting EnableGraphicsUpdate to true, your model view is dead in the water until you re-open the file.</span><br />
<h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">SldWorks.DocumentVisible<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">This one is perfect for those macros that need to open reference models into memory.<span style="mso-spacerun: yes;"> </span>Adding views to drawings and inserting components in assemblies are two obvious uses.<span style="mso-spacerun: yes;"> </span>The SolidWorks application has the DocumentVisible method that causes any files of a given type to be opened without a display window.<span style="mso-spacerun: yes;"> </span>Refreshing the display window is visually distracting as well as a performance drain.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">The following VB.NET macro code inserts a part into a new assembly.<span style="mso-spacerun: yes;"> </span>Check the path for the sample part to make sure it is correct for your system.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> main()<o:p></o:p></span></div><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swDoc <span style="color: blue;">As</span> ModelDoc2 = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swAssembly <span style="color: blue;">As</span> AssemblyDoc = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swComp <span style="color: blue;">As</span> Component2<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc = swApp.NewAssembly()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> assemName <span style="color: blue;">As</span> <span style="color: blue;">String<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>assemName = swDoc.GetTitle<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swAssembly = swDoc<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> FileName <span style="color: blue;">As</span> <span style="color: blue;">String<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>FileName = <span style="color: maroon;">"C:\Program Files\SolidWorks Corp"</span> _<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>& <span style="color: maroon;">"\SolidWorks\samples\tutorial\"</span> _<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>& <span style="color: maroon;">"advdrawings\base plate.sldprt"<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc = swApp.OpenDoc(FileName, </span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;">_<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>swDocumentTypes_e.swDocPART)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>swApp.ActivateDoc(assemName)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swComp = swAssembly.AddComponent2(FileName, 0, 0, 0)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swAssembly.EditRebuild()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub<o:p></o:p></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Before you can make the call to AddComponent2, you need to have the model open in memory.<span style="mso-spacerun: yes;"> </span>But notice the time required to open the base plate part.<span style="mso-spacerun: yes;"> </span>And when you’re done, the base plate part is still open with its own window.<span style="mso-spacerun: yes;"> </span>Imagine adding dozens or even hundreds of parts into an assembly.<span style="mso-spacerun: yes;"> </span>You could throw someone into a seizure with all the blinking!<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Add some smooth speed to the macro by calling DocumentVisible from the SolidWorks application interface.<span style="mso-spacerun: yes;"> </span>You need to pass it the document type too.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> main()<o:p></o:p></span></div><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swDoc <span style="color: blue;">As</span> ModelDoc2 = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swAssembly <span style="color: blue;">As</span> AssemblyDoc = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swComp <span style="color: blue;">As</span> Component2<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc = swApp.NewAssembly()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> assemName <span style="color: blue;">As</span> <span style="color: blue;">String<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>assemName = swDoc.GetTitle<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swAssembly = swDoc<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> FileName <span style="color: blue;">As</span> <span style="color: blue;">String<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>FileName = <span style="color: maroon;">"C:\Program Files\SolidWorks Corp"</span> _<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>& <span style="color: maroon;">"\SolidWorks\samples\tutorial\"</span> _<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>& <span style="color: maroon;">"advdrawings\base plate.sldprt"<o:p></o:p></span></span><br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'Open parts invisibly<o:p></o:p></span></span></b><br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swApp.DocumentVisible(<span style="color: blue;">False</span>, swDocumentTypes_e.swDocPART)<o:p></o:p></span></b><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><o:p> </o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc = swApp.OpenDoc(FileName, _<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>swDocumentTypes_e.swDocPART)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swApp.ActivateDoc(assemName)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swComp = swAssembly.AddComponent2(FileName, 0, 0, 0)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swAssembly.EditRebuild()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><o:p> </o:p></span><br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'reset part visibility<o:p></o:p></span></span></b><br />
<b style="mso-bidi-font-weight: normal;"><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swApp.DocumentVisible(<span style="color: blue;">True</span>, swDocumentTypes_e.swDocPART)<o:p></o:p></span></b><br />
<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-bidi-font-size: 18.0pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub<o:p></o:p></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Much nicer, no?<span style="mso-spacerun: yes;"> </span>If you’re adding multiple components, consider using DocumentVisible along with EnableGraphicsUpdate on the assembly ModelView and you’ll have some serious speed!<span style="mso-spacerun: yes;"> </span>Side effects include completely destroying a user’s ability to open models with any kind of user interface if you fail to trap errors and reset document visibility at the end of your code.<span style="mso-spacerun: yes;"> </span>Once you’ve called DocumentVisible during a SolidWorks session, all files opened of that type will be opened in memory, but will be invisible to the user unless you reset DocumentVisible to True.<o:p></o:p></span></div>Mike Spenshttp://www.blogger.com/profile/00365628417064674695noreply@blogger.com3tag:blogger.com,1999:blog-1301703939631167951.post-16435147303144516142012-01-30T22:09:00.002-08:002012-02-20T21:17:56.538-08:00Intro to the SolidWorks EPDM API<span style="font-family: Calibri;">SolidWorks World 2012 is less than two weeks away.<span style="mso-spacerun: yes;"> </span>That means I’m two weeks from sharing more tips on the SolidWorks and Enterprise PDM APIs.<span style="mso-spacerun: yes;"> </span>I’ll be presenting two sessions: one hands-on session introducing a lucky group to the guts of the Enterprise PDM API, and one sharing some of the tips and tricks I’ve learned over the years for building automation in SolidWorks.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">For now, let’s start with some API fundamentals for SolidWorks Enterprise PDM!<span style="mso-spacerun: yes;"> </span>I had someone ask about how to compare files that are outside of EPDM with files already in the system.<span style="mso-spacerun: yes;"> </span>For example, if a contract design group sends you a batch of files for a design, how do you know what to put back in EPDM?<span style="mso-spacerun: yes;"> </span>The file set may contain common parts as well as new parts, assemblies and drawings.<span style="mso-spacerun: yes;"> </span>Trying to sift through the file set to figure out what you should add to EPDM could be extremely tedious.<span style="mso-spacerun: yes;"> </span>I can think of four or five activities involving sticks, rocks and dirt that would be more invigorating.<span style="mso-spacerun: yes;"> </span>Yet another chance to automate!<o:p></o:p></span></div><br />
<h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">Connecting to EPDM<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">As a starting point, fire up a new SolidWorks VSTA VB.NET macro.<span style="mso-spacerun: yes;"> </span>Alternatively, you could begin with a new Visual Studio project.<span style="mso-spacerun: yes;"> </span>The sample code below assumes using a macro.<span style="mso-spacerun: yes;"> </span>Once you’ve started the project, add a reference to the SolidWorks Enterprise PDM library.<span style="mso-spacerun: yes;"> </span>Select Project, Add Reference.<span style="mso-spacerun: yes;"> </span>You should find it under the COM tab named <i style="mso-bidi-font-style: normal;">PDMWorks Enterprise 20XX Type Library</i>.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Open the <i style="mso-bidi-font-style: normal;">SolidWorksMacro.vb</i> code file if it isn’t open already and add an Imports statement to add <i style="mso-bidi-font-style: normal;">EdmLib</i> (the EPDM library).<span style="mso-spacerun: yes;"> </span>Add the following code under the main procedure.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> SolidWorks.Interop.sldworks<o:p></o:p></span></div><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> SolidWorks.Interop.swconst<o:p></o:p></span><br />
<span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> System.Runtime.InteropServices<o:p></o:p></span><br />
<span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> System<o:p></o:p></span><br />
<span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> EdmLib<o:p></o:p></span><br />
<br />
<span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Partial</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">Class</span> SolidWorksMacro<o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> main()<o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> vaultName <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">"EPDMVault"<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> CompareFolder <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">"C:\EPDM Test"<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'create a vault connection and log in<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> v <span style="color: blue;">As</span> <span style="color: blue;">New</span> EdmVault5<span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>v.LoginAuto(vaultName, 0)<o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">''' <summary><o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">''' The SldWorks swApp variable is pre-assigned for you.<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">''' </summary><o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> swApp <span style="color: blue;">As</span> SldWorks<o:p></o:p></span><br />
<br />
<span style="color: blue; font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;">End</span><span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: blue;">Class</span></span><o:p></o:p><br />
<br />
<h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">Logging into EPDM<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">There’s not much to creating a connection to a local vault view and logging in.<span style="mso-spacerun: yes;"> </span>The <b style="mso-bidi-font-weight: normal;">EdmVault5</b> interface just needs to be declared with the </span><span style="color: blue; font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;">New</span><span style="font-family: Calibri;"> keyword to connect.<span style="mso-spacerun: yes;"> </span>The <b style="mso-bidi-font-weight: normal;">LoginAuto</b> method is identical to a user opening the local view folder.<span style="mso-spacerun: yes;"> </span>If they’re not currently logged in, the system will attempt to log them in using their Windows login.<span style="mso-spacerun: yes;"> </span>If that fails, the login prompt is displayed.<span style="mso-spacerun: yes;"> </span>The first argument for LoginAuto is the name of the vault.<span style="mso-spacerun: yes;"> </span>The second argument is to help EPDM know what the calling application is (it’s window handle as an integer).<span style="mso-spacerun: yes;"> </span>Since I’ve used a SolidWorks macro, I’m simply passing 0 to essentially ignore the argument.<span style="mso-spacerun: yes;"> </span>It’s really a shortcut for something less than critical.</span></div><h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">Creating a Search Interface<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">To compare files between the vault and an outside folder, we’ll use the EPDM search capability.<span style="mso-spacerun: yes;"> </span>Using search through the API isn’t much different than using it through the client software.<span style="mso-spacerun: yes;"> </span>Add the following code to the procedure to include the search definition.<span style="mso-spacerun: yes;"> </span>We’ll fill out the details and run the search next.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> main()<o:p></o:p></span></div><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> vaultName <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">"EPDMVault"<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> CompareFolder <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">"C:\EPDM Test"<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'create a vault connection and log in<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> v <span style="color: blue;">As</span> <span style="color: blue;">New</span> EdmVault5<span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>v.LoginAuto(vaultName, 0)<o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'create the search interface and initial settings<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> search <span style="color: blue;">As</span> IEdmSearch5<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search = v.CreateSearch<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search.FindFiles = <span style="color: blue;">True<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search.FindFolders = <span style="color: blue;">False<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span><o:p><span style="font-family: Calibri;"> </span></o:p><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">The search interface type is IEdmSearch5.<span style="mso-spacerun: yes;"> </span>You create a search interface by calling EdmVault5.CreateSearch.<span style="mso-spacerun: yes;"> </span>It has a handful of different settings.<span style="mso-spacerun: yes;"> </span>Since we’re interested in finding files, we’ve set the FindFiles property to True and the FindFolders property to false.</span></div><h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">Get the List of Files to Compare<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Before we start utilizing the file and directory methods from VB.NET, it’s easiest to add a reference to the System.IO namespace.<span style="mso-spacerun: yes;"> </span>Add the following code to import System.IO by the rest of the Imports statements.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">...<o:p></o:p></span></div><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> EdmLib<o:p></o:p></span><br />
<span style="color: blue; font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;"> System.IO</span><o:p></o:p><br />
<br />
<span style="font-family: Calibri;">Once that’s been added, you can use FileInfo and DirectoryInfo classes from System.IO to get all files in a given folder path.<span style="mso-spacerun: yes;"> </span>The code is finally using the CompareFolder variable to get the compare folder.<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> main()<o:p></o:p></span></div><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> vaultName <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">"EPDMVault"<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> CompareFolder <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">"C:\Test Folder"<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'create a vault connection and log in<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> v <span style="color: blue;">As</span> <span style="color: blue;">New</span> EdmVault5<span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>v.LoginAuto(vaultName, 0)<o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'create the search interface and initial settings<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> search <span style="color: blue;">As</span> IEdmSearch5<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search = v.CreateSearch<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search.FindFiles = <span style="color: blue;">True<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search.FindFolders = <span style="color: blue;">False<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'get the file names from the folder to compare<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> searchDir <span style="color: blue;">As</span> <span style="color: blue;">New</span> DirectoryInfo(CompareFolder)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> files <span style="color: blue;">As</span> FileInfo()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>files = searchDir.GetFiles(<span style="color: maroon;">"*.*"</span>, _</span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> SearchOption.AllDirectories)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-tab-count: 1;"> </span><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Stop</span><o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub<o:p></o:p></span></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">If you run the macro to this point, you can review the array named <i style="mso-bidi-font-style: normal;">files</i> once you hit the Stop point.<span style="mso-spacerun: yes;"> </span>It should have an array of FileInfo objects that include the name of every file in the test folder. <o:p></o:p></span></div><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Now that we have an array of file names, we can run an EPDM search on each one.<span style="mso-spacerun: yes;"> </span>If we find a match in EPDM, we’ll add that into a collection to later report back to the user.</span></div><h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">Running a Search<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Depending on how you have your vault configured, a search for a file name in EPDM might possibly return multiple files.<span style="mso-spacerun: yes;"> </span>Since we’re interested in simply knowing if the file exists at all in the vault, we’ll only look for the first result.<span style="mso-spacerun: yes;"> </span>If there isn’t a first result, we can confidently assume the file doesn’t exist in the vault.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Add the rest of the macro shown below.<span style="mso-spacerun: yes;"> </span>If you’re looking for a shortcut, this is the entire macro.<o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> main()<o:p></o:p></span></div><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> vaultName <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">"EPDMVault"<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> CompareFolder <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">"C:\Test Folder"<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'create a vault connection and log in<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> v <span style="color: blue;">As</span> <span style="color: blue;">New</span> EdmVault5<span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>v.LoginAuto(vaultName, 0)<o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'create the search interface and initial settings<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> search <span style="color: blue;">As</span> IEdmSearch5<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search = v.CreateSearch<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search.FindFiles = <span style="color: blue;">True<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search.FindFolders = <span style="color: blue;">False<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'get the file names from the folder to compare<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> searchDir <span style="color: blue;">As</span> <span style="color: blue;">New</span> DirectoryInfo(CompareFolder)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> files <span style="color: blue;">As</span> FileInfo()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>files = searchDir.GetFiles(<span style="color: maroon;">"*.*"</span>, _</span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"> SearchOption.AllDirectories)<o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'collect any duplicates found in EPDM<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> duplicates <span style="color: blue;">As</span> <span style="color: blue;">New</span> Collection<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">For</span> <span style="color: blue;">Each</span> f <span style="color: blue;">As</span> FileInfo <span style="color: blue;">In</span> files<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'run the search for this file name<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>search.FileName = f.Name<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> sr <span style="color: blue;">As</span> IEdmSearchResult5<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>sr = search.GetFirstResult<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">If</span> <span style="color: blue;">Not</span> sr <span style="color: blue;">Is</span> <span style="color: blue;">Nothing</span> <span style="color: blue;">Then<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'found a file in the vault with the same name<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>duplicates.Add(f.Name)</span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="color: green; font-size: 10pt;"><span style="color: green; font-size: 10pt;"><span style="color: black;"> </span>'optionally, get a local copy of the file</span></span><br />
<span style="color: green; font-size: 10pt;"><span style="color: green; font-size: 10pt;"> </span></span><span style="color: blue; font-size: 10pt;"><span style="color: blue; font-size: 10pt;"><span style="color: black;"> </span>Dim</span></span><span style="font-size: 10pt;"> eFile </span><span style="color: blue; font-size: 10pt;"><span style="color: blue; font-size: 10pt;">As</span></span><span style="font-size: 10pt;"> IEdmFile5</span><br />
<span style="font-size: 10pt;"> eFile = sr </span><span style="color: green; font-size: 10pt;"><span style="color: green; font-size: 10pt;">'get the file from the search result</span></span><br />
<span style="color: green; font-size: 10pt;"><span style="color: green; font-size: 10pt;"> <span style="color: black;"> </span></span></span><span style="font-size: 10pt;">eFile.GetFileCopy(0) </span><span style="color: green; font-size: 10pt;"><span style="color: green; font-size: 10pt;">'get the latest file copy</span></span><br />
<span style="color: green; font-size: 10pt;"><span style="color: green; font-size: 10pt;"></span></span><o:p></o:p></span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">If<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Next<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'report back the files found<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> myMessage <span style="color: blue;">As</span> <span style="color: blue;">String</span> = <span style="color: maroon;">"Duplicate file list:"</span> & vbCrLf<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">For</span> <span style="color: blue;">Each</span> dup <span style="color: blue;">As</span> <span style="color: blue;">String</span> <span style="color: blue;">In</span> duplicates<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>myMessage = myMessage & dup & vbCrLf<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Next<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>MsgBox(myMessage & vbCrLf & duplicates.Count & _<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: maroon;">" duplicate files"</span>, MsgBoxStyle.Information)<o:p></o:p></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'reset the collection<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>duplicates = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span><o:p><span style="font-family: Calibri;"> </span></o:p><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">I’ve used a Collection to keep track of the files found during the search.<span style="mso-spacerun: yes;"> </span>It was easier than an array since I could simply add new elements and didn’t have to worry about resizing.</span></div><div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">The EPDM search is run by setting the FileName property and then calling the GetFirstResult method.<span style="mso-spacerun: yes;"> </span>The rest is simply formatting a message to return the list to the user.</span><br />
<br />
<span style="font-family: Calibri;">I've also added a few additional lines of code to get a local file copy in the vault. You need to first get the IEdmFile5 interface (directly from the IEdmSearchResult5 interface). There are several optional arguments to the GetFileCopy method, but if you're just getting the latest version, you can use the exact syntax posted.</span></div>Mike Spenshttp://www.blogger.com/profile/00365628417064674695noreply@blogger.com20tag:blogger.com,1999:blog-1301703939631167951.post-22346303055355798082012-01-23T20:29:00.000-08:002012-01-23T20:29:51.997-08:00One Button PDFs in SolidWorks<span style="font-family: Calibri;">Welcome to the beginning of SolidAPI - a blog dedicated to the SolidWorks API.<span style="mso-spacerun: yes;"> </span>What better place to start than one of my favorite macros - one button PDF publishing.<span style="mso-spacerun: yes;"> </span>I've had the chance to present this material several times at SolidWorks World.<span style="mso-spacerun: yes;"> </span>But if you're coming this year and were hoping to re-live the experience - this post will have to suffice.<span style="mso-spacerun: yes;"> </span>I'm presenting on the SolidWorks Enterprise PDM API instead.<span style="mso-spacerun: yes;"> </span>So here's your chance to brush up on macro basics using VSTA (Visual Studio Tools for Applications) and Visual Basic.<o:p></o:p></span><br />
<br />
<h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">Step #1 – Record it!<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Start by opening any SolidWorks file.<span style="mso-spacerun: yes;"> </span>Drawings are great, but models work fine too.<span style="mso-spacerun: yes;"> </span>Start recording the macro by going to Tools, Macro, Record.<o:p></o:p></span></div><span style="font-family: Calibri;">Select File, Save As.<span style="mso-spacerun: yes;"> </span>In the Save As dialog, change the save as type to <i style="mso-bidi-font-style: normal;">Adobe Acrobat PDF (*.pdf)</i> and save.<span style="mso-spacerun: yes;"> </span>That’s all we need to get started.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Stop and save the macro by selecting Tools, Macro, Stop.<span style="mso-spacerun: yes;"> </span>Change the save as type to <i style="mso-bidi-font-style: normal;">SW VSTA VB Macro (*.vbproj)</i> and save the macro as <i style="mso-bidi-font-style: normal;">AutoPDF.vbproj.</i><o:p></o:p></span></div><span style="font-family: Calibri;">Edit the newly saved macro by selecting Tools, Macro, Edit.<span style="mso-spacerun: yes;"> </span>Browse to <i style="mso-bidi-font-style: normal;">AutoPDF.vbproj</i> (found under the newly created folders <i style="mso-bidi-font-style: normal;">\AutoPDF\SwMacro\</i>).<span style="mso-spacerun: yes;"> </span>The important part of the macro is shown here.<span style="mso-spacerun: yes;"> </span>Anything else in your recorded macro is not needed.<o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-size: x-small;"><span style="color: blue; font-family: "Courier New"; mso-no-proof: yes;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span></span></span></div><span style="font-size: x-small;"><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="color: black;"> SolidWorks.Interop.sldworks<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="color: black;"> SolidWorks.Interop.swconst<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="color: black;"> System.Runtime.InteropServices<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Imports</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="color: black;"> System<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p><span style="color: black;"> </span></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;">Partial</span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="color: black;"> </span><span style="color: blue;">Class</span><span style="color: black;"> SolidWorksMacro<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p><span style="color: black;"> </span></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Public</span><span style="color: black;"> </span><span style="color: blue;">Sub</span><span style="color: black;"> main()<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Dim</span><span style="color: black;"> swDoc </span><span style="color: blue;">As</span><span style="color: black;"> ModelDoc2 = </span><span style="color: blue;">Nothing<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Dim</span><span style="color: black;"> longstatus </span><span style="color: blue;">As</span><span style="color: black;"> </span><span style="color: blue;">Integer</span><span style="color: black;"> = 0<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>swDoc = </span><span style="color: blue;">CType</span><span style="color: black;">(swApp.ActiveDoc, ModelDoc2)<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="color: black;"><span style="mso-spacerun: yes;"> </span>longstatus = swDoc.SaveAs3(</span><span style="color: maroon;">"C:\...\Drawing.PDF"</span><span style="color: black;">, 0, 0)<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">End</span><span style="color: black;"> </span><span style="color: blue;">Sub<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p> </o:p></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">''' <summary><o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">''' The SldWorks swApp variable is pre-assigned for you.<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: green;">''' </summary><o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"><span style="color: black;"> </span></span><span style="color: blue;">Public</span><span style="color: black;"> swApp </span><span style="color: blue;">As</span><span style="color: black;"> SldWorks<o:p></o:p></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><o:p><span style="color: black;"> </span></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: black; font-family: Times New Roman; font-size: small;"> </span><span style="color: blue; font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;">End</span><span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;"><span style="color: black;"> </span><span style="color: blue;">Class</span></span></div></span><div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"></div><h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">Description<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">There are really only two lines of important code in Sub main() that do all the work.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc = <span style="color: blue;">CType</span>(swApp.ActiveDoc, ModelDoc2)<o:p></o:p></span></div><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-no-proof: yes;"><span style="font-family: Calibri;">This line connects the variable named swDoc to the SolidWorks application’s active document using swApp.ActiveDoc.<o:p></o:p></span></span></div><span style="mso-no-proof: yes;"><span style="font-family: Calibri;">Next, a Save As operation is calle from the active document interface that we’ve captured in the variable swDoc.<span style="mso-spacerun: yes;"> </span>The main argument of interest is the first.<span style="mso-spacerun: yes;"> </span>It must be a string containing the full path to the desired PDF file.<o:p></o:p></span></span><br />
<br />
<h2 style="margin: 12pt 0in 0pt;"><span style="mso-no-proof: yes;"><span style="font-family: Calibri;">Code Changes<o:p></o:p></span></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">To make this macro worthwhile, we need to get a PDF path that makes sense.<span style="mso-spacerun: yes;"> </span>The macro above would just keep overwriting the same <i style="mso-bidi-font-style: normal;">Drawing.pdf</i> file with a new PDF each time the macro was run.<span style="mso-spacerun: yes;"> </span>Make the following changes to convert the active document’s current path to PDF.<o:p></o:p></span></div><span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Public</span> <span style="color: blue;">Sub</span> main()<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> swDoc <span style="color: blue;">As</span> ModelDoc2 = <span style="color: blue;">Nothing<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> longstatus <span style="color: blue;">As</span> <span style="color: blue;">Integer</span> = 0<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>swDoc = <span style="color: blue;">CType</span>(swApp.ActiveDoc, ModelDoc2)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'get the current model path<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> modelPath <span style="color: blue;">As</span> <span style="color: blue;">String</span> = swDoc.GetPathName<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">Dim</span> pdfName <span style="color: blue;">As</span> <span style="color: blue;">String<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'use IO.Path class to change the extension<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>pdfName = IO.Path.ChangeExtension(modelPath, <span style="color: maroon;">"pdf"</span>)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: green;">'save the PDF<o:p></o:p></span></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span>longstatus = swDoc.SaveAs3(pdfName, 0, 0)<o:p></o:p></span><br />
<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></span><o:p></o:p><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">I dig the IO.Path class in .NET!<span style="mso-spacerun: yes;"> </span>It makes it easy to manipulate file paths in all kinds of ways.<span style="mso-spacerun: yes;"> </span>Changing a file’s extension is as easy as that.<span style="mso-spacerun: yes;"> </span>Now the SaveAs3 method uses the newly modified path.<o:p></o:p></span></div><br />
<h2 style="margin: 12pt 0in 0pt;"><span style="font-family: Calibri;">Make a Button in SolidWorks<o:p></o:p></span></h2><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-family: Calibri;">Build your new macro (create the dlls) by selecting Build, Build AutoPDF.<span style="mso-spacerun: yes;"> </span>Once that’s done, close the macro and jump back to SolidWorks.<span style="mso-spacerun: yes;"> </span>To add a button to your toolbars or the Command Manager, select Tools, Customize.<span style="mso-spacerun: yes;"> </span>Select the Commands tab and choose Macro from the list of command groups.<span style="mso-spacerun: yes;"> </span>Drag and drop a new macro button onto any toolbar.<span style="mso-spacerun: yes;"> </span></span></div><div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-f2clOTlN_OI8gBCCCYqoi3Av5qOPh86BHfzY7nNoQEn6osyO5Os9tqtBpsmqyde028CkY6ZFMNRE74SHoRFXkBpRW2fpjWxsvlc_5IA8QFRohUpLhAJM_ABWhv9I7VIl4ExQfzT_QSQ/s1600/AutoPDF_Button.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-f2clOTlN_OI8gBCCCYqoi3Av5qOPh86BHfzY7nNoQEn6osyO5Os9tqtBpsmqyde028CkY6ZFMNRE74SHoRFXkBpRW2fpjWxsvlc_5IA8QFRohUpLhAJM_ABWhv9I7VIl4ExQfzT_QSQ/s320/AutoPDF_Button.png" width="313" /></a></div><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-f2clOTlN_OI8gBCCCYqoi3Av5qOPh86BHfzY7nNoQEn6osyO5Os9tqtBpsmqyde028CkY6ZFMNRE74SHoRFXkBpRW2fpjWxsvlc_5IA8QFRohUpLhAJM_ABWhv9I7VIl4ExQfzT_QSQ/s1600/AutoPDF_Button.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><div style="text-align: left;"> </div></a><br />
<div class="MsoNormal" style="margin: 0in 0in 10pt;"><br />
</div><span style="font-family: Calibri;">Fill out the form by adding a tool tip and a prompt.<span style="mso-spacerun: yes;"> </span>Then browse to the <i style="mso-bidi-font-style: normal;">bin</i> folder in your macro directory to find <i style="mso-bidi-font-style: normal;">AutoPDF.dll</i>.<span style="mso-spacerun: yes;"> </span>Click OK and you’re done!<span style="mso-spacerun: yes;"> </span>Now your PDFs are only one button away.<o:p></o:p></span>Mike Spenshttp://www.blogger.com/profile/00365628417064674695noreply@blogger.com2