Using Iron Python with Solidworks API

1

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 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 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
python
.net
ironpython
asked on Stack Overflow Sep 20, 2013 by Steinbex • edited Sep 24, 2013 by Steinbex

2 Answers

3

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.

answered on Stack Overflow Sep 20, 2013 by abarnert
1

I am wondering if this question was settled, but still post sth here. Like other apps using COM, simply type:

swApp = Dispatch("sldworks.Application")

By doing this, you get access to Solidworks, and you can use APIs to do what you want. Hope this will help, whoever needs it.

answered on Stack Overflow Apr 17, 2017 by Jiang Yu • edited Apr 17, 2017 by Tom Fuller

User contributions licensed under CC BY-SA 3.0