<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Extensible By Design &#187; Extensible By Design</title>
	<atom:link href="http://extensiblecad.com/words/category/macros/feed/" rel="self" type="application/rss+xml" />
	<link>http://extensiblecad.com/words</link>
	<description>SolidWorks and PDMWorks Enterprise Development Blog</description>
	<lastBuildDate>Mon, 02 Feb 2009 09:02:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>SolidWorks API Presentation &#8211; Beginner&#8217;s Guide To The API</title>
		<link>http://extensiblecad.com/words/2008/12/17/solidworks-api-presentation-beginners-guide-to-the-api/</link>
		<comments>http://extensiblecad.com/words/2008/12/17/solidworks-api-presentation-beginners-guide-to-the-api/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 17:40:11 +0000</pubDate>
		<dc:creator>Jeff Cope</dc:creator>
				<category><![CDATA[CAD]]></category>
		<category><![CDATA[Macros]]></category>
		<category><![CDATA[SolidWorks]]></category>
		<category><![CDATA[SolidWorks API]]></category>

		<guid isPermaLink="false">http://extensiblecad.com/words/?p=150</guid>
		<description><![CDATA[From time to time I do presentations on the SolidWorks API and one that gets requested fairly often is the Beginner&#8217;s Guide To The API.  I just presented this in Roanoke, VA at the Blue Ridge SolidWorks User&#8217;s Group headed up by Jeremy Marks.  They were a good bunch of guys (although there was a [...]]]></description>
			<content:encoded><![CDATA[<p>From time to time I do presentations on the SolidWorks API and one that gets requested fairly often is the Beginner&#8217;s Guide To The API.  I just presented this in Roanoke, VA at the Blue Ridge SolidWorks User&#8217;s Group headed up by Jeremy Marks.  They were a good bunch of guys (although there was a University of North Carolina fan in attendance) and it seems like a well-run group.  However, after risking my life to get there by driving through what seemed the rain-storm of the century on unfamiliar mountain highways with almost no visibility I have decided that in the future it might be safer for me to simply make the presentation available on my blog : )</p>
<p><a href="http://extensiblecad.com/words/wp-content/uploads/2008/12/beginners-guide-to-the-api.pdf">beginners-guide-to-the-api</a></p>
<p>Also, the SolidWorks Autonumbering Macro (w/ MS Access Database) that I usually show has been written up for ConnectPress with a link to download the Macro and Database.  You can view that article here:</p>
<p><a href="http://www.solidworkscommunity.com/feature_full.php?cpfeatureid=32314">http://www.solidworkscommunity.com/feature_full.php?cpfeatureid=32314</a></p>
	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<small><p>&copy; Jeff Cope for <a href="http://extensiblecad.com/words">Extensible By Design</a>, 2008. |
	<a href="http://extensiblecad.com/words/2008/12/17/solidworks-api-presentation-beginners-guide-to-the-api/">Permalink</a> |
	<a href="http://extensiblecad.com/words/2008/12/17/solidworks-api-presentation-beginners-guide-to-the-api/#comments">One comment</a> |
	Add to
	<a href="http://del.icio.us/post?url=http://extensiblecad.com/words/2008/12/17/solidworks-api-presentation-beginners-guide-to-the-api/&amp;title=SolidWorks API Presentation &#8211; Beginner&#8217;s Guide To The API"><img src="http://yoursite.com/images/icons/delicious.gif" border="0"> del.icio.us</a>
	<a href="http://www.digg.com/submit"><img src="http://yoursite.com/images/digg.gif" border="0"> digg</a><br/>
	Who's linking ?
	<a href="http://www.technorati.com/search/http://extensiblecad.com/words/2008/12/17/solidworks-api-presentation-beginners-guide-to-the-api/" title="Search on Technorati"><img src="http://yoursite.com/images/icons/technorati.gif" border="0">Technorati</a>
	<a href="http://www.blogpulse.com/search?query=http://extensiblecad.com/words/2008/12/17/solidworks-api-presentation-beginners-guide-to-the-api/" title="Search on Blogpulse"><img src="http://yoursite.com/images/icons/blogpulse.gif" border="0">BlogPulse</a>
	<a href="http://google.com/blogsearch?q=http://extensiblecad.com/words/2008/12/17/solidworks-api-presentation-beginners-guide-to-the-api/" title="Search on Google Blog Search"><img src="http://yoursite.com/images/icons/google.gif" border="0">Google</a><br/>
	Want more on these topics ? Browse the archive of posts filed under <a href="http://extensiblecad.com/words/category/cad/" title="View all posts in CAD" rel="category tag">CAD</a>, <a href="http://extensiblecad.com/words/category/macros/" title="View all posts in Macros" rel="category tag">Macros</a>, <a href="http://extensiblecad.com/words/category/solidworks/" title="View all posts in SolidWorks" rel="category tag">SolidWorks</a>, <a href="http://extensiblecad.com/words/category/solidworks/solidworks-api/" title="View all posts in SolidWorks API" rel="category tag">SolidWorks API</a>.</small></p>
<p>Copyright 2007 <a href=http://extensiblecad.com/words>Extensible By Design.</a>  All rights reserved.</p>]]></content:encoded>
			<wfw:commentRss>http://extensiblecad.com/words/2008/12/17/solidworks-api-presentation-beginners-guide-to-the-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Macros 201 &#8211; How To Register Your SolidWorks Macro To Receive Notifications For SolidWorks Events</title>
		<link>http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/</link>
		<comments>http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/#comments</comments>
		<pubDate>Thu, 01 Nov 2007 20:56:43 +0000</pubDate>
		<dc:creator>Jeff Cope</dc:creator>
				<category><![CDATA[Macros]]></category>
		<category><![CDATA[SolidWorks]]></category>
		<category><![CDATA[SolidWorks API]]></category>

		<guid isPermaLink="false">http://extensiblecad.com/words/?p=25</guid>
		<description><![CDATA[Why Should I Learn How To Register My Macros To Register For Notifications From SolidWorks?  The common Macros that you see floating around the SolidWorks community are many and most of them are useful.  However, most of those Macros run only when explicitly commanded to do so by the user.  This is great for most Macros but there are [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Why Should I Learn How To Register My Macros To Register For Notifications From SolidWorks?</strong> </p>
<p>The common Macros that you see floating around the <a target="_blank" href="http://www.lennyworks.com/solidworks/default.asp?ID=20">SolidWorks community</a> are many and most of them are useful.  However, most of those Macros run only when explicitly commanded to do so by the user.  This is great for most Macros but there are times when it is nice to have some of your code run every time a certain event happens in SolidWorks.  For example, what if you want to have <a target="_blank" href="http://www.solidworks.com/">SolidWorks </a>filenames determined based on your company&#8217;s part-numbering scheme, which is pulled from a database?  Wouldn&#8217;t it be nice to have the situation where every time SolidWorks prompts you to save a file, the filename box was pre-populated with the next number in the sequence? </p>
<p>You might be thinking, why not just <span id="more-25"></span>write a regular Macro that will retrieve a part number from a database and then save the active file with that number as the filename?  Well, that works great if and only if your users remember to always use the Macro to save the files.  In this case it is better to have the user be &#8220;automagically reminded&#8221; when they attempt to save the file.  To do something like this, you need to register your Macro to receive notifications from SolidWorks.  This is not extremely hard to do if you know how but if you don&#8217;t know how, then the SolidWorks API Help isn&#8217;t very helpful on this topic; hence the post. </p>
<p>Note:  This particular example may be a moot point for you if you have a PLM tool that takes care of this for you.  If that is your situation, then be thankful but don&#8217;t rub it in.  The general concept will still apply though albeit with a different concrete example. </p>
<p> For the Macro to accompany this post, I was going to do the scenario above but I saw a <a href="http://www.solidworkscommunity.com/connectqna_submitanswer.php?cpfeatureid=24031&amp;id=32697">post</a> on <a href="http://www.solidworkscommunity.com/">SolidWorks Community&#8217;s </a>Q&amp;A page that made me decide to do the example below.  However, if there appears to be much interest in the auto-numbering Macro then I might post that one also.</p>
<p><strong>What Does This Macro Do?</strong></p>
<p> This Macro will &#8220;listen&#8221; for SolidWorks to open SolidWorks files.  When this happens, the Macro will populate the document with a custom property (&#8220;sw_version&#8221;) that contains the SolidWorks version (e.g. 2007 SP4.0) used to open the file.  If the user saves the file after opening it, you know without opening the file what version of SolidWorks last saved the file.  If the user doesn&#8217;t save the file after opening it, then the custom property isn&#8217;t added or modified.  You will need to run the Macro exactly once, each time you open SolidWorks.  From then until you close SolidWorks, the Macro will monitor all SolidWorks files being opened. </p>
<p> <strong>How Hard Is This To Do?</strong></p>
<p>This article assumes you know how to write simple Macros.  You should know how to create and initialize objects and call functions.  As the title suggests, this is not a freshman class on SolidWorks Macros but rather a sophomore class.</p>
<p> <strong>I Don&#8217;t Want To Learn Anything, Can I Just Have The Finished Macro?</strong></p>
<p>Yes.  <a href="http://extensiblecad.com/words/wp-content/uploads/2007/11/versionwritermacro.zip" title="Download Version Writer Macro">You may download the Macro from here. </a> <em>Warning:  No warranty is either expressed or implied for this Macro and the Macro is not in any way supported.  By downloading the Macro, you assume all responsibility for running the Macro.</em></p>
<p><strong>What Version Of SolidWorks Was This Macro Written For?</strong></p>
<p>This Macro was written for SolidWorks 2007 but should run on 2008 as well.</p>
<p> <strong>How Do You Do It (High-Level)?</strong></p>
<ol>
<li> Open a new Macro.  </li>
<li>Add a new class to &#8220;listen for events&#8221;</li>
<li>Declare a class-level sldworks.sldworks variable for the new class like this:  &#8220;Private WithEvents sldworks.sldworks&#8221;</li>
<li>Register methods in the new class to be notified by SolidWorks when those events occur.</li>
<li>Register a method in the new class to be notified by VBA when the class is initialized.  Make sure that class initializes a sldworks.sldworks variable that is declared at the class level.</li>
<li>Add a method to perform the desired work and add a call to it from the method registered in step 4, above.</li>
<li>Add a public (i.e. global) variable to your main module that represents the event listener class.</li>
<li>Add code to the main procedure in your main module to initialize the public event listener class.</li>
<li>Debug endlessly until it &#8220;works&#8221;</li>
</ol>
<p><strong> How Do You Do It (Detailed)?</strong></p>
<ul>
<li> Open a new Macro.  
<ul>
<li>You should already know how to do this step.</li>
</ul>
</li>
<li>Add a new class to &#8220;listen for events&#8221;
<ul>
<li><img vspace="10" align="top" width="473" src="http://extensiblecad.com/words/wp-content/uploads/2007/11/createnewclass.PNG" hspace="10" alt="Insert Class" height="293" style="width: 473px; height: 293px" title="Insert Class" /></li>
<li>Call the class &#8220;clsEvents&#8221;</li>
</ul>
</li>
<li>Declare a class-level sldworks.sldworks variable for the new class like this:  &#8220;Private WithEvents swApp as sldworks.sldworks&#8221;
<ul>
<li><img vspace="10" align="top" width="680" src="http://extensiblecad.com/words/wp-content/uploads/2007/11/withevents.PNG" hspace="10" alt="WithEvents" height="547" style="width: 680px; height: 547px" title="WithEvents" /></li>
</ul>
</li>
<li>Register methods in the new class to be notified by SolidWorks when those events occur.   The drop down box on the right with events only appears after you declare your swApp variable &#8220;WithEvents.&#8221;  Also, you must have the left drop-down box set to swApp to see the events for swApp.  If you have the left drop-down box set to Class, then you will see only the class events in the right drop-down box. 
<ul>
<li><img vspace="10" width="790" src="http://extensiblecad.com/words/wp-content/uploads/2007/11/notifications-available.PNG" hspace="10" alt="Available Notifications" height="488" style="width: 790px; height: 488px" title="Available Notifications" /></li>
</ul>
</li>
<li>Register a method in the new class to be notified by VBA when the class is initialized.  Make sure that class initializes a sldworks.sldworks variable that is declared at the class level.  NOTE:  In this case, you must have the left drop-down box set to Class in order to have the right drop-down box show Class events.
<ul>
<li><img vspace="10" width="885" src="http://extensiblecad.com/words/wp-content/uploads/2007/11/class_initialize.PNG" hspace="10" height="511" style="width: 885px; height: 511px" /></li>
</ul>
</li>
<li>Add a method to perform the deisred work and add a call to it from the method registered in step 3, above.
<ul>
<li><img vspace="10" align="top" width="903" src="http://extensiblecad.com/words/wp-content/uploads/2007/11/class_initialize2.PNG" hspace="10" height="526" style="width: 903px; height: 526px" /></li>
<li>I like to put in a simple msgbox() and verify that it is getting called appropriately before I complicate things by adding my worker method.</li>
</ul>
</li>
<li>Add a public (i.e. global) variable to your main module that represents the event listener class.
<ul>
<li>This class will be loaded into SolidWorks memory for the duration of the SolidWorks session.</li>
</ul>
</li>
<li>Add code to the main procedure in your main module to initialize the public event listener class.
<ul>
<li><img vspace="10" width="903" src="http://extensiblecad.com/words/wp-content/uploads/2007/11/initializeeventlistener.PNG" hspace="10" alt="Initialize Event Listener" height="526" style="width: 903px; height: 526px" title="Initialize Event Listener" /></li>
</ul>
</li>
<li>Debug endlessly until it &#8220;works&#8221;
<ul>
<li>I would start out by making sure that my msgbox is called first and foremost.</li>
<li><img vspace="10" width="140" src="http://extensiblecad.com/words/wp-content/uploads/2007/11/messageboxcalled.PNG" hspace="10" alt="msgbox called" height="107" style="width: 140px; height: 107px" title="msgbox called" /></li>
<li>Once the msgbox pops up consistently, then I would add the worker method.</li>
<li>Here is the worker method that I employed:</li>
</ul>
<ul>Sub writeVersion()</ul>
<ul>Dim swDoc As SldWorks.ModelDoc2<br />
Dim bSetProp As Boolean<br />
Dim longstatus As Long, longwarnings As Long<br />
Dim swxFormattedRevision As String<br />
Dim swxRevisionVar As Variant<br />
Dim versions(8 To 16) As swxVersion<br />
Dim swDocExt As SldWorks.ModelDocExtension<br />
Dim swPropMgr As SldWorks.CustomPropertyManager</ul>
<ul>Dim i As Integer<br />
For i = 8 To 16<br />
    versions(i).id = i<br />
    Select Case i<br />
        Case 8<br />
            versions(i).majorVersion = &#8220;2000&#8243;<br />
        Case 9<br />
            versions(i).majorVersion = &#8220;2001&#8243;<br />
        Case 10<br />
            versions(i).majorVersion = &#8220;2001plus&#8221;<br />
        Case 11<br />
            versions(i).majorVersion = &#8220;2003&#8243;<br />
        Case 12<br />
            versions(i).majorVersion = &#8220;2004&#8243;<br />
        Case 13<br />
            versions(i).majorVersion = &#8220;2005&#8243;<br />
        Case 14<br />
            versions(i).majorVersion = &#8220;2006&#8243;<br />
        Case 15<br />
            versions(i).majorVersion = &#8220;2007&#8243;<br />
        Case 16<br />
            versions(i).majorVersion = &#8220;2008&#8243;<br />
        Case Else<br />
            MsgBox (&#8220;Version not recognized&#8221;)<br />
            Exit Sub<br />
    End Select<br />
Next i<br />
    Set swApp = Application.SldWorks<br />
   <br />
    swxFormattedRevision = swApp.RevisionNumber<br />
   <br />
    If Len(swxFormattedRevision) &gt; 0 Then<br />
        swxRevisionVar = Split(swxFormattedRevision, &#8220;.&#8221;)<br />
        For i = 8 To 16<br />
            If versions(i).id = swxRevisionVar(0) Then<br />
                Set swDoc = swApp.ActiveDoc<br />
                Set swDocExt = swDoc.Extension<br />
                Set swPropMgr = swDocExt.CustomPropertyManager(&#8220;&#8221;)<br />
                swPropMgr.Delete (&#8220;sw_version&#8221;)<br />
                bSetProp = swPropMgr.Add2(&#8220;sw_version&#8221;, SwConst.swCustomInfoText, versions(i).majorVersion &amp; &#8221; SP&#8221; &amp; swxRevisionVar(1) &amp; &#8220;.&#8221; &amp; swxRevisionVar(2))<br />
                Exit Sub<br />
            End If<br />
        Next i<br />
    Else<br />
        MsgBox (&#8220;Version not recognized&#8221;)<br />
        Exit Sub<br />
    End If<br />
End Sub</ul>
</li>
</ul>
<ul>
<li>Good luck.  <a href="http://extensiblecad.com/words/wp-content/uploads/2007/11/versionwritermacro.zip" title="Download Version Writer macro">You can &#8221;cheat&#8221; by downloading the finished Macro and checking it out if you need to do so.</a></li>
</ul>
	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<small><p>&copy; Jeff Cope for <a href="http://extensiblecad.com/words">Extensible By Design</a>, 2007. |
	<a href="http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/">Permalink</a> |
	<a href="http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/#comments">10 comments</a> |
	Add to
	<a href="http://del.icio.us/post?url=http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/&amp;title=Macros 201 &#8211; How To Register Your SolidWorks Macro To Receive Notifications For SolidWorks Events"><img src="http://yoursite.com/images/icons/delicious.gif" border="0"> del.icio.us</a>
	<a href="http://www.digg.com/submit"><img src="http://yoursite.com/images/digg.gif" border="0"> digg</a><br/>
	Who's linking ?
	<a href="http://www.technorati.com/search/http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/" title="Search on Technorati"><img src="http://yoursite.com/images/icons/technorati.gif" border="0">Technorati</a>
	<a href="http://www.blogpulse.com/search?query=http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/" title="Search on Blogpulse"><img src="http://yoursite.com/images/icons/blogpulse.gif" border="0">BlogPulse</a>
	<a href="http://google.com/blogsearch?q=http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/" title="Search on Google Blog Search"><img src="http://yoursite.com/images/icons/google.gif" border="0">Google</a><br/>
	Want more on these topics ? Browse the archive of posts filed under <a href="http://extensiblecad.com/words/category/macros/" title="View all posts in Macros" rel="category tag">Macros</a>, <a href="http://extensiblecad.com/words/category/solidworks/" title="View all posts in SolidWorks" rel="category tag">SolidWorks</a>, <a href="http://extensiblecad.com/words/category/solidworks/solidworks-api/" title="View all posts in SolidWorks API" rel="category tag">SolidWorks API</a>.</small></p>
<p>Copyright 2007 <a href=http://extensiblecad.com/words>Extensible By Design.</a>  All rights reserved.</p>]]></content:encoded>
			<wfw:commentRss>http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>From King of Prussia, PA With Love</title>
		<link>http://extensiblecad.com/words/2007/10/26/from-king-of-prussia-with-love/</link>
		<comments>http://extensiblecad.com/words/2007/10/26/from-king-of-prussia-with-love/#comments</comments>
		<pubDate>Fri, 26 Oct 2007 05:17:12 +0000</pubDate>
		<dc:creator>Jeff Cope</dc:creator>
				<category><![CDATA[Macros]]></category>
		<category><![CDATA[SolidWorks]]></category>
		<category><![CDATA[SolidWorks API]]></category>

		<guid isPermaLink="false">http://extensiblecad.com/words/?p=23</guid>
		<description><![CDATA[So here is what you missed out on if you were too lazy to weren&#8217;t able to attend the SWUGN Technical Summit &#8211; Philadelphia, which was held in King of Prussia, PA (right down the road from the city of brotherly love) : A chance to win a free pass to SolidWorks World 2008 in [...]]]></description>
			<content:encoded><![CDATA[<p><img vspace="5" align="left" width="290" src="http://extensiblecad.com/words/wp-content/uploads/2007/10/enlarge_waatvafo.jpg" hspace="35" alt="Washington At Valley Forge" height="210" style="width: 290px; height: 210px" title="Washington At Valley Forge" />So here is what you missed out on if you <strike>were too lazy to</strike> weren&#8217;t able to attend the SWUGN Technical Summit &#8211; Philadelphia, which was held in King of Prussia, PA (right down the road from the city of brotherly love) :</p>
<ul>
<li>A chance to win a free pass to <a href="http://www.solidworks.com/pages/swworld08/index.html">SolidWorks World 2008</a> in San Diego, CA</li>
<li>A chance to win a <a href="http://www.3dconnexion.com/">3D Connexion Motion Controller</a></li>
<li>A chance to meet Dennis Miller (Susquehanna Valley SolidWorks User Group Leader) in person</li>
</ul>
<p><span id="more-23"></span>Seriously, it was a lot of fun. In addition to the networking, the great food and the little technical bits of information, we were right across the street from <a href="http://www.ushistory.org/valleyforge/">Valley Forge</a>. The highlights of my trip were #1-Trying to find a gas station anywhere near the airport (this is difficult in any city but Philadelphia takes it to a new level) and #2-Learning PhotoWorks tips from the master, Rob Rodriguez.  I have to give a big thanks to TriMech for sending me up there.  It is nice when your employer believes in giving back to the SolidWorks community.</p>
<p>Some of the folks who attended my session asked me for the PPT so I am posting it <a href="http://extensiblecad.com/words/wp-content/uploads/2007/10/beginners-guide-to-the-api.zip">here</a>. If you didn&#8217;t attend, then just make sure you smack yourself on the hands or something before you download the presentation. Note: I also included the before and after versions of the PDF-making Macro that we recorded.</p>
<p>One last thing, did anyone else find it interesting that all of the street signs that say &#8220;Phila&#8221; instead of Philadelphia? It seems odd that the cream cheese guys can fit the entire word on a little box of cream cheese but the DOT can&#8217;t fit the entire word on a huge highway sign. It didn&#8217;t really throw me off or anything though so we&#8217;re cool DOT. The highway-sign maker&#8217;s union is putting you on notice though.</p>
	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<small><p>&copy; Jeff Cope for <a href="http://extensiblecad.com/words">Extensible By Design</a>, 2007. |
	<a href="http://extensiblecad.com/words/2007/10/26/from-king-of-prussia-with-love/">Permalink</a> |
	<a href="http://extensiblecad.com/words/2007/10/26/from-king-of-prussia-with-love/#comments">3 comments</a> |
	Add to
	<a href="http://del.icio.us/post?url=http://extensiblecad.com/words/2007/10/26/from-king-of-prussia-with-love/&amp;title=From King of Prussia, PA With Love"><img src="http://yoursite.com/images/icons/delicious.gif" border="0"> del.icio.us</a>
	<a href="http://www.digg.com/submit"><img src="http://yoursite.com/images/digg.gif" border="0"> digg</a><br/>
	Who's linking ?
	<a href="http://www.technorati.com/search/http://extensiblecad.com/words/2007/10/26/from-king-of-prussia-with-love/" title="Search on Technorati"><img src="http://yoursite.com/images/icons/technorati.gif" border="0">Technorati</a>
	<a href="http://www.blogpulse.com/search?query=http://extensiblecad.com/words/2007/10/26/from-king-of-prussia-with-love/" title="Search on Blogpulse"><img src="http://yoursite.com/images/icons/blogpulse.gif" border="0">BlogPulse</a>
	<a href="http://google.com/blogsearch?q=http://extensiblecad.com/words/2007/10/26/from-king-of-prussia-with-love/" title="Search on Google Blog Search"><img src="http://yoursite.com/images/icons/google.gif" border="0">Google</a><br/>
	Want more on these topics ? Browse the archive of posts filed under <a href="http://extensiblecad.com/words/category/macros/" title="View all posts in Macros" rel="category tag">Macros</a>, <a href="http://extensiblecad.com/words/category/solidworks/" title="View all posts in SolidWorks" rel="category tag">SolidWorks</a>, <a href="http://extensiblecad.com/words/category/solidworks/solidworks-api/" title="View all posts in SolidWorks API" rel="category tag">SolidWorks API</a>.</small></p>
<p>Copyright 2007 <a href=http://extensiblecad.com/words>Extensible By Design.</a>  All rights reserved.</p>]]></content:encoded>
			<wfw:commentRss>http://extensiblecad.com/words/2007/10/26/from-king-of-prussia-with-love/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>GetObject( ) or CreateObject( )? Is there a difference? &#8211; Part III</title>
		<link>http://extensiblecad.com/words/2007/10/11/getobject-or-createobject-is-there-a-difference-part-iii/</link>
		<comments>http://extensiblecad.com/words/2007/10/11/getobject-or-createobject-is-there-a-difference-part-iii/#comments</comments>
		<pubDate>Thu, 11 Oct 2007 05:24:59 +0000</pubDate>
		<dc:creator>Jeff Cope</dc:creator>
				<category><![CDATA[CAD]]></category>
		<category><![CDATA[Macros]]></category>
		<category><![CDATA[MS Office]]></category>
		<category><![CDATA[SolidWorks]]></category>
		<category><![CDATA[SolidWorks API]]></category>

		<guid isPermaLink="false">http://extensiblecad.com/words/?p=17</guid>
		<description><![CDATA[At the end of my last post on this topic, I promised you that I would share a &#8220;dirty secret&#8221; so I guess I now have to come clean. FYI &#8211; &#8220;Dirty secret&#8221; may have been a bit strong but it sounded cool (at least to me). Anyways, there is a difference between how SolidWorks [...]]]></description>
			<content:encoded><![CDATA[<p>At the end of my last post on this topic, I promised you that I would share a &#8220;dirty secret&#8221; so I guess I now have to come clean.  FYI &#8211; &#8220;Dirty secret&#8221; may have been a bit strong but it sounded cool (at least to me).  Anyways, there is a difference between how SolidWorks handles CreateObject( ) and how MS Office products handle CreateObject( ).  This is something to be aware of when automating across applications.</p>
<p>What is the difference?  In a nutshell, calling CreateObject( ) on an MS Office application interface will always create a new instance of the application and then return a handle to that instance.  Calling CreateObject( )  on the SldWorks.Application interface will only create a new instance of SolidWorks if one isn&#8217;t already running.   This basically ensures that only one instance of SolidWorks at a time is ever automated by your Macro.  This is a <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank" title="The Singleton Design Pattern">basic coding pattern called &#8220;the Singleton&#8221;</a> and it prevents untold suffering and countless tears.  SolidWorks is so kind to us and many of us didn&#8217;t even know it.</p>
<p>Below are two Macros <span id="more-17"></span>to illustrate the difference between SolidWorks and MS Office applications with regard to CreateObject( ).  The first is an Excel Macro that automates SolidWorks and the second is a SolidWorks Macro that automates Excel.  If you copy/paste the code, be sure you add references in each Macro project to the object library of the application you are automating.  If you don&#8217;t know what the heck I am talking about, then just download the complete files <a href="http://extensiblecad.com/words/wp-content/uploads/2007/10/createobjectxlvsswx.zip" title="CreateObjectXLvsSWX">from here. </a></p>
<ul>
<li>Excel Macro to automate SolidWorks:</li>
</ul>
<blockquote><p><font color="#009933">&#8216;Copyright 2007 by Extensible CAD Technologies, LLC &#8211; All rights reserved<br />
&#8216;Written 10/10/2007 by Jeff Cope<br />
&#8216;User assumes all risk for using the code below.</font></p>
<p>Sub AutomateSolidWorks()</p>
<p><font color="#009933">&#8216;Declare three handles to SolidWorks instances.</font><br />
<font color="#330099">Dim</font> swx1 <font color="#330099">As</font> SldWorks.SldWorks<br />
<font color="#330099">Dim</font> swx2 <font color="#330099">As</font> SldWorks.SldWorks<br />
<font color="#330099">Dim</font> swx3 <font color="#330099">As</font> SldWorks.SldWorks</p>
<p><font color="#009933">&#8216;Use CreateObject( ) for the first instance.  A new instance of SolidWorks IS NOT created.<br />
&#8216;Note:  As we showed previously, using GetObject( ) without a filepath argument here would<br />
&#8216;return an error since no instances of SolidWorks are available to attach to yet.</font><br />
<font color="#330099">Set</font> swx1 = CreateObject(&#8220;SldWorks.Application&#8221;)<br />
<font color="#009933">&#8216;Use CreateObject( ) for the first instance.  A new instance of SolidWorks IS NOT created.</font><br />
<font color="#330099">Set</font> swx2 = CreateObject(&#8220;SldWorks.Application&#8221;)<br />
<font color="#009933">&#8216;Use GetObject( ) for the first instance.  A new instance of SolidWorks IS NOT created.</font><br />
<font color="#330099">Set</font> swx3 = GetObject(, ”SldWorks.Application”)<br />
<font color="#009933">&#8216;At this point, we should only have one SolidWorks window open and one SolidWorks process running.<br />
&#8216;Add new parts using each of the three handles</font></p>
<p>swx1.NewPart<br />
swx2.NewPart<br />
swx3.NewPart<br />
<font color="#009933">&#8216;We should now have one SolidWorks window open with three new SolidWorks parts loaded.</font></p>
<p><font color="#009933"><font color="#330099">End Sub</font></font></p></blockquote>
<ul>
<li>SolidWorks Macro to automate Excel:</li>
</ul>
<blockquote><p><font color="#009933">&#8216;Copyright 2007 by Extensible CAD Technologies, LLC &#8211; All rights reserved<br />
&#8216;Written 10/10/2007 by Jeff Cope<br />
&#8216;User assumes all risk for using the code below.</font></p>
<p><font color="#009933">&#8216;Declare three instances of the Excel.Application interface</font><br />
<font color="#330099">Dim</font> xl1 <font color="#330099">As</font> Excel.Application<br />
<font color="#330099">Dim</font> xl2 <font color="#330099">As</font> Excel.Application<br />
<font color="#330099">Dim</font> xl3 <font color="#330099">As</font> Excel.Application</p>
<p><font color="#330099">Sub</font> main()</p>
<p><font color="#009933">&#8216;Use CreateObject( ) for the first instance.  A new instance of Excel is created.</font><br />
<font color="#330099">Set </font>xl1 = CreateObject(&#8220;Excel.Application&#8221;)<br />
<font color="#009933">&#8216;A new Workbook gets added to this instance</font><br />
xl1.Workbooks.Add<br />
<font color="#009933">&#8216;Make the instance visible so we can see what is happening</font><br />
xl1.Visible = True</p>
<p><font color="#009933">&#8216;Use CreateObject( )for the first instance.  A new instance of Excel is created.</font><br />
<font color="#330099">Set</font> xl2 = CreateObject(&#8220;Excel.Application&#8221;)<br />
<font color="#009933">&#8216;A new Workbook gets added to this instance</font><br />
xl2.Workbooks.Add<br />
<font color="#009933">&#8216;Make the instance visible so we can see what is happening</font><br />
xl2.Visible = True<br />
<font color="#009933">&#8216;At this point, we should be able to see two Excel windows running and each should have a</font><br />
<font color="#009933">&#8216;new Workbook added.</font></p>
<p><font color="#009933">&#8216;Use GetObject( ) for the first instance.  A new instance of Excel IS NOT created.  A handle<br />
&#8216;is simply returned to one of the existing instances (usually xl1).</font><br />
<font color="#330099">Set</font> xl3 = GetObject(, &#8220;Excel.Application&#8221;)<br />
<font color="#009933">&#8216;A new Workbook gets added to this instance</font><br />
xl3.Workbooks.Add<br />
<font color="#009933">&#8216;Make the instance visible so we can see what is happening</font><br />
xl3.Visible = True<br />
<font color="#009933">&#8216;At this point in the Macro&#8217;s execution, we should see two Excel windows (and two Excel processes)<br />
&#8216;running.  However, we have three handles to Excel instances so two of the handles are pointing<br />
&#8216;to the same Excel instance.</font></p>
<p><font color="#009933">&#8216;Release handles to COM objects</font><br />
<font color="#330099">Set</font> xl1 = Nothing<br />
<font color="#330099">Set</font> xl2 = Nothing<br />
<font color="#330099">Set</font> xl3 = Nothing</p>
<p><font color="#009933">&#8216;One of the Excel windows will have two Workbooks in it instead of one.  Note:  If you<br />
&#8216;want to know which one, then just leave one of the instances invisible.<br />
</font><font color="#330099">End Sub</font></p></blockquote>
	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<small><p>&copy; Jeff Cope for <a href="http://extensiblecad.com/words">Extensible By Design</a>, 2007. |
	<a href="http://extensiblecad.com/words/2007/10/11/getobject-or-createobject-is-there-a-difference-part-iii/">Permalink</a> |
	<a href="http://extensiblecad.com/words/2007/10/11/getobject-or-createobject-is-there-a-difference-part-iii/#comments">2 comments</a> |
	Add to
	<a href="http://del.icio.us/post?url=http://extensiblecad.com/words/2007/10/11/getobject-or-createobject-is-there-a-difference-part-iii/&amp;title=GetObject( ) or CreateObject( )? Is there a difference? &#8211; Part III"><img src="http://yoursite.com/images/icons/delicious.gif" border="0"> del.icio.us</a>
	<a href="http://www.digg.com/submit"><img src="http://yoursite.com/images/digg.gif" border="0"> digg</a><br/>
	Who's linking ?
	<a href="http://www.technorati.com/search/http://extensiblecad.com/words/2007/10/11/getobject-or-createobject-is-there-a-difference-part-iii/" title="Search on Technorati"><img src="http://yoursite.com/images/icons/technorati.gif" border="0">Technorati</a>
	<a href="http://www.blogpulse.com/search?query=http://extensiblecad.com/words/2007/10/11/getobject-or-createobject-is-there-a-difference-part-iii/" title="Search on Blogpulse"><img src="http://yoursite.com/images/icons/blogpulse.gif" border="0">BlogPulse</a>
	<a href="http://google.com/blogsearch?q=http://extensiblecad.com/words/2007/10/11/getobject-or-createobject-is-there-a-difference-part-iii/" title="Search on Google Blog Search"><img src="http://yoursite.com/images/icons/google.gif" border="0">Google</a><br/>
	Want more on these topics ? Browse the archive of posts filed under <a href="http://extensiblecad.com/words/category/cad/" title="View all posts in CAD" rel="category tag">CAD</a>, <a href="http://extensiblecad.com/words/category/macros/" title="View all posts in Macros" rel="category tag">Macros</a>, <a href="http://extensiblecad.com/words/category/ms-office/" title="View all posts in MS Office" rel="category tag">MS Office</a>, <a href="http://extensiblecad.com/words/category/solidworks/" title="View all posts in SolidWorks" rel="category tag">SolidWorks</a>, <a href="http://extensiblecad.com/words/category/solidworks/solidworks-api/" title="View all posts in SolidWorks API" rel="category tag">SolidWorks API</a>.</small></p>
<p>Copyright 2007 <a href=http://extensiblecad.com/words>Extensible By Design.</a>  All rights reserved.</p>]]></content:encoded>
			<wfw:commentRss>http://extensiblecad.com/words/2007/10/11/getobject-or-createobject-is-there-a-difference-part-iii/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>GetObject( ) or CreateObject( )? Is there a difference? &#8211; Part II</title>
		<link>http://extensiblecad.com/words/2007/10/08/getobject-or-createobject-is-there-a-difference-part-ii/</link>
		<comments>http://extensiblecad.com/words/2007/10/08/getobject-or-createobject-is-there-a-difference-part-ii/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 18:52:30 +0000</pubDate>
		<dc:creator>Jeff Cope</dc:creator>
				<category><![CDATA[CAD]]></category>
		<category><![CDATA[Macros]]></category>
		<category><![CDATA[MS Office]]></category>
		<category><![CDATA[SolidWorks]]></category>
		<category><![CDATA[SolidWorks API]]></category>

		<guid isPermaLink="false">http://extensiblecad.com/words/?p=13</guid>
		<description><![CDATA[Last time I explained how the SolidWorks application object behaves differently depending on whether it is instantiated via CreateObject( ) or GetObject( ). So now you are probably thinking, &#8220;That&#8217;s it? That was too easy.&#8221; Well, I don&#8217;t want to be one to disappoint so I will let you in on the rest of the [...]]]></description>
			<content:encoded><![CDATA[<p>Last time I explained how the SolidWorks application object behaves differently depending on whether it is instantiated via CreateObject( ) or GetObject( ). So now you are probably thinking, &#8220;That&#8217;s it? That was too easy.&#8221; Well, I don&#8217;t want to be one to disappoint so I will let you in on the rest of the story including a dirty little secret.</p>
<p>So what else is there? The GetObject( ) call has another trick up its sleeve that I didn&#8217;t discuss last time. Taking a look at its Intellisense signature provides a clue as to what this is.</p>
<p><img vspace="5" align="bottom" width="509" src="http://extensiblecad.com/words/wp-content/uploads/2007/10/intellisense.JPG" hspace="10" alt="GetObject signature" height="292" style="width: 509px; height: 292px" title="GetObject signature" /></p>
<p>In the method signature you actually see two parameters,<span id="more-13"></span> PathName and Class. We know the Class is &#8220;SldWorks.SldWorks&#8221; but what is this PathName and to where does this path lead? Well, it turns out that this is the path to a SolidWorks file and that GetObject is so cool that not only can it return a reference to a SolidWorks application interface (e.g. SldWorks.SldWorks), it can also return a reference to a SolidWorks file as well.</p>
<p>Below are some examples on how you can use GetObject( ).</p>
<ul>
<li>Example 1 &#8211; The first example is basically review from the last post.  This code will usually get you a handle to an already running session of SolidWorks (i.e. a handle to an instance of the SldWorks.SldWorks class)</li>
</ul>
<blockquote>
<blockquote><p><font color="#009933"><font color="#330099">Dim<font color="#009933"> </font><font color="#000000">swapp</font><font color="#009933"> </font><font color="#330099">As </font><font color="#009933">SldWorks.ModelDoc2</font></font><br />
</font><font color="#330099">Set<font color="#000000"> swapp = GetObject(, “SldWorks.Application”)</font></font></p></blockquote>
</blockquote>
<ul>
<li>Example 2 &#8211; Iff (if and only if) you have &#8220;SLDPRT&#8221; associated with SolidWorks on your machine, then the code below will usually get you a handle to a SolidWorks document (i.e. a handle to an instance of the ModelDoc2 class) that resides at the address of &#8220;C:\myFiles\myPart.SLDPRT.&#8221; Note: In this particular case, an instance of SolidWorks will be created even if one doesn&#8217;t already exist. This was glossed over last time.</li>
</ul>
<blockquote>
<blockquote><p><font color="#330099">Dim</font><font color="#009933"> </font><font color="#000000">swDoc</font><font color="#009933"> </font><font color="#330099">As </font><font color="#009933">SldWorks.SldWorks<br />
</font><font color="#330099">Set</font> swDoc = GetObject(&#8220;C:\MyFiles\myPart.SLDPRT&#8221;)</p></blockquote>
</blockquote>
<ul>
<li>Example 3 &#8211; The code below will usually get you a handle to a SolidWorks document (i.e. a handle to an instance of the ModelDoc2 class) that resides at the address of &#8220;C:\myFiles\myPart.SLDPRT.&#8221; This is irregardless of whether or not you have &#8220;SLDPRT&#8221; associated with SolidWorks on your machine. Again, in this particular case, an instance of SolidWorks will be created even if one doesn&#8217;t already exist.</li>
</ul>
<blockquote>
<blockquote><p><font color="#330099">Dim</font><font color="#009933"> </font><font color="#000000">swDoc</font><font color="#009933"> </font><font color="#330099">As </font><font color="#009933">SldWorks.SldWorks<br />
</font><font color="#330099">Set</font> swDoc = GetObject(&#8220;C:\MyFiles\myPart.SLDPRT&#8221;<font color="#330099"><font color="#000000">, _ “SldWorks.Application”</font></font>)</p></blockquote>
</blockquote>
<p>In a nutshell, GetObject can also be used to return a handle to a specific file.  To do this you simply include the path to the file as the first argument.  If you do this, then the second argument becomes optional unless you don&#8217;t have the particular file&#8217;s extension associated to the application in which you desire to open it.</p>
<p>&#8220;Ok Jeff. That&#8217;s pretty cool but what about that dirty little secret you promised us?&#8221;</p>
<p>We&#8217;ll have to save that for Part III and the final installment. I&#8217;ll give you a little hint, though. SolidWorks and Microsoft don&#8217;t make ALL of the same mistakes. Some but not ALL.</p>
	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<small><p>&copy; Jeff Cope for <a href="http://extensiblecad.com/words">Extensible By Design</a>, 2007. |
	<a href="http://extensiblecad.com/words/2007/10/08/getobject-or-createobject-is-there-a-difference-part-ii/">Permalink</a> |
	<a href="http://extensiblecad.com/words/2007/10/08/getobject-or-createobject-is-there-a-difference-part-ii/#comments">No comment</a> |
	Add to
	<a href="http://del.icio.us/post?url=http://extensiblecad.com/words/2007/10/08/getobject-or-createobject-is-there-a-difference-part-ii/&amp;title=GetObject( ) or CreateObject( )? Is there a difference? &#8211; Part II"><img src="http://yoursite.com/images/icons/delicious.gif" border="0"> del.icio.us</a>
	<a href="http://www.digg.com/submit"><img src="http://yoursite.com/images/digg.gif" border="0"> digg</a><br/>
	Who's linking ?
	<a href="http://www.technorati.com/search/http://extensiblecad.com/words/2007/10/08/getobject-or-createobject-is-there-a-difference-part-ii/" title="Search on Technorati"><img src="http://yoursite.com/images/icons/technorati.gif" border="0">Technorati</a>
	<a href="http://www.blogpulse.com/search?query=http://extensiblecad.com/words/2007/10/08/getobject-or-createobject-is-there-a-difference-part-ii/" title="Search on Blogpulse"><img src="http://yoursite.com/images/icons/blogpulse.gif" border="0">BlogPulse</a>
	<a href="http://google.com/blogsearch?q=http://extensiblecad.com/words/2007/10/08/getobject-or-createobject-is-there-a-difference-part-ii/" title="Search on Google Blog Search"><img src="http://yoursite.com/images/icons/google.gif" border="0">Google</a><br/>
	Want more on these topics ? Browse the archive of posts filed under <a href="http://extensiblecad.com/words/category/cad/" title="View all posts in CAD" rel="category tag">CAD</a>, <a href="http://extensiblecad.com/words/category/macros/" title="View all posts in Macros" rel="category tag">Macros</a>, <a href="http://extensiblecad.com/words/category/ms-office/" title="View all posts in MS Office" rel="category tag">MS Office</a>, <a href="http://extensiblecad.com/words/category/solidworks/" title="View all posts in SolidWorks" rel="category tag">SolidWorks</a>, <a href="http://extensiblecad.com/words/category/solidworks/solidworks-api/" title="View all posts in SolidWorks API" rel="category tag">SolidWorks API</a>.</small></p>
<p>Copyright 2007 <a href=http://extensiblecad.com/words>Extensible By Design.</a>  All rights reserved.</p>]]></content:encoded>
			<wfw:commentRss>http://extensiblecad.com/words/2007/10/08/getobject-or-createobject-is-there-a-difference-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

