Extensible CAD Technologies
SolidWorks and PDMWorks Enterprise Development Blog

GetObject( ) or CreateObject( )? Is there a difference? – Part II

Posted by Jeff Cope at 8 October, 2007, 2:52 pm

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, “That’s it? That was too easy.” Well, I don’t want to be one to disappoint so I will let you in on the rest of the story including a dirty little secret.

So what else is there? The GetObject( ) call has another trick up its sleeve that I didn’t discuss last time. Taking a look at its Intellisense signature provides a clue as to what this is.

GetObject signature

In the method signature you actually see two parameters, PathName and Class. We know the Class is “SldWorks.SldWorks” 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.

Below are some examples on how you can use GetObject( ).

  • Example 1 – 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)

Dim swapp As SldWorks.ModelDoc2
Set swapp = GetObject(, “SldWorks.Application”)

  • Example 2 – Iff (if and only if) you have “SLDPRT” 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 “C:\myFiles\myPart.SLDPRT.” Note: In this particular case, an instance of SolidWorks will be created even if one doesn’t already exist. This was glossed over last time.

Dim swDoc As SldWorks.SldWorks
Set swDoc = GetObject(“C:\MyFiles\myPart.SLDPRT”)

  • Example 3 – 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 “C:\myFiles\myPart.SLDPRT.” This is irregardless of whether or not you have “SLDPRT” associated with SolidWorks on your machine. Again, in this particular case, an instance of SolidWorks will be created even if one doesn’t already exist.

Dim swDoc As SldWorks.SldWorks
Set swDoc = GetObject(“C:\MyFiles\myPart.SLDPRT”, _ “SldWorks.Application”)

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’t have the particular file’s extension associated to the application in which you desire to open it.

“Ok Jeff. That’s pretty cool but what about that dirty little secret you promised us?”

We’ll have to save that for Part III and the final installment. I’ll give you a little hint, though. SolidWorks and Microsoft don’t make ALL of the same mistakes. Some but not ALL.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Category : CAD | MS Office | Macros | SolidWorks | SolidWorks API

Generate First-Article Inspection Reports From TIF, PDF and CAD Drawing Files With InspectionXpert. Generate First-Article Inspection Reports From SolidWorks Drawing Files With InspectionXpert.