Using Iron Python with Solidworks API
Question: I've been working on scripting some repetitive behaviors in Solidworks using
python. I spent a while trying to go through the win32com library and managed
to get a lot to work, but ran into a roadblock. So I'm now trying to control
the API via Iron Python. Just trying to get rolling and have run into an
issue. I've tried to run the code below:

    
    
    import clr
    clr.AddReferenceToFileAndPath('..\\Redist\\SolidWorks.Interop.sldworks.dll')
    clr.AddReference('SolidWorks.Interop.swconst')
    from SolidWorks.Interop import sldworks
    from SolidWorks.Interop import swconst
    
    print sldworks
    swApp = sldworks.ISldWorks()
    swApp.Visible = True
    

On running this code, I get "TypeError: Cannot create instances of ISldWorks
because it is abstract"

Upon looking at the solidworks documentation
[here](http://help.solidworks.com/2013/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.ISldWorks.html?id=11f79586cf2348529713b8667e1fdba3#Pg0)
I see this information: "This interface is the highest-level object in the
SolidWorks API. This interface provides a general set of functions that allow
application-level operations such as create, open, close, and quit documents,
arrange icons and windows, change the active document, and create attribute
definitions.

Use CreateObject, GetObject, New, or similar functions to obtain the ISldWorks
object from a Dispatch application (Visual Basic or C++ Dispatch). Standalone
.exe C++ COM applications can use CoCreateInstance. All of the SolidWorks API
add-in wizards automatically create the ISldWorks object for you.

Events are implemented with delegates in the Microsoft .NET Framework. See the
Overview topic for a list of delegates for this interface."

Now, while I'm quite familiar with python programming this whole .net thing is
a new animal for me so I'm sure I'm doing something simple wrong, but I'm
certainly struggling to figure out exactly what that is. Thanks for your help.

\--UPDATE So I have gone through and looked into how the .net system works,
and I feel like I have a better handle on it. So if I understand correctly my
goal is to try to create an instance of the Solidworks application object, or
ISldWorks, and then I should be able to access all of the members. In my
research I've come across these two articles: [Solidworks standalone
app](http://www.linkedin.com/groups/Create-simple-SolidWorks-StandAlone-
Application-4961405.S.235214502) and iron python documentation from these, and
your very helpful response, it seems like the code below should work. Though
when run, I get an error that says "EnvironmentError:
System.Runtime.InteropServices.COMException (0x8002802B): Element not found.
(Exception from HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND))" which would
lead me to believe that the object still is not instatiating correctly.

    
    
    import System
    t = System.Type.GetTypeFromProgID('SldWorks.Application')
    swApp = System.Activator.CreateInstance(t)
    swApp.Visible = True
    


Answer: Simplifying a bit:

In .NET, and in COM, you don't normally create instance by directly calling
the constructor of a class. In fact, most services don't even expose the
actual class. Instead, they expose an interface—that is, an abstract type
which is a supertype of the actual class, and just defines the public methods
they want you to have—and then either (a) a factory function that generates an
instance of some concrete subclass of that interface, or (b) a concrete class
that COM can use in "automatic factory" functions like `CreateObject`.

That's what the docs mean when they say:

> Use CreateObject, GetObject, New, or similar functions to obtain the
> ISldWorks object from a Dispatch application (Visual Basic or C++ Dispatch).
> Standalone .exe C++ COM applications can use CoCreateInstance.

`CreateObject` takes a "ProgID", a string representing a concrete type name,
and does all the magic needed to get a concrete type from that name, pass it
through .NET/COM interop, ask the service to create an object of that concrete
type, verify that it matches the appropriate interface, and wrap it up. If
there's an example of using SolidWorks from VB.NET, it will probably use
`CreateObject`, and you can do the same thing from IronPython.

However, really, at some point you're going to need to read some documentation
on .NET and understand what all of it means.

