Solidworks Api Python

3 views
Skip to first unread message

Reuquen Lanier

unread,
Aug 4, 2024, 8:57:08 PM8/4/24
to indreamidac
Ive 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:

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.


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.


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.


Solidworks does have features to help automate your workflow, such as recording and running Macros and using the Solidworks Task Scheduler, but I tend not to use these beyond very basic applications like exporting drawings as PDFs.


One indirect way to interact with Solidworks using Python is by making use of the Solidworks equation and design table features. Solidworks allows you to drive model dimensions using global variables and equations. These values are usually stored and edited within the Solidworks application, but you can choose to link it to an external file.


A useful application for this is to create a large number of similar parts that have slightly different dimensions. You create copies of the initial part and its equation file, then loop through each copy and update it as needed. Using equations effectively in Solidworks is a great way to elevate your designs, and Python can help you take it one step further!


Now we can look at some basic functions. These will let you start and kill Solidworks, connect to the application (necessary for all additional functions), open Solidworks files, and rebuild/save them.


I had a recent project that required me to make dozens of drawings of fairly simple 3D models. The models already existed, but no drawings had yet been created. I knew that for most, if not all the parts, a simple 3-view drawing with a handful of dimensions would be enough. I wanted to script something to make my life a bit easier, and settled on the following plan. The script would need to:


Most or all of these steps could probably be achieved using the built-in Solidworks macro tools but at the cost of a steep learning curve. I decided to use Python instead to loop through all my files and wrap API commands to achieve the above.


Building on all the basic functions discussed earlier, I wrote something to list and update the custom properties of a part. I used custom properties to drive the title blocks of the drawings, and needed to add my name and date to each drawing.


One important note here is something in lines 25, 29, 33, and 37. The API command SelectByID2 requires the input of a variable of type Nothing . This is something relevant to the VB language, and does not directly translate to Python. I had to change the API command to use the pythoncom.Nothing object instead.


With all that out of the way, it was easy to loop through my list of parts and create drawings for them. I still had to open each drawing and move the views around, add some dimensions, etc., but this saved me some of the tedious work and I learned something along the way.


How many of you are familiar with Python? Those of you who are, did you know it is pretty much a no-brainer to use it

to script Solidworks through COM with the win32 extensions from Mark Hammond? This means super-easy scripting of

the API, and with the use of wxPython (the windowing toolkit) you can do full-on GUI stuff just as easy.Why, you may ask? Well, first of all - it is totally free (Open Source). Secondly, it is 100% interpreted, so you can make

a change to your GUI application, with no need to re-compile, re-install, whatever, just hit Go! (Actually, you can even

compile Python into .exe's if you want to as well). I have a few programs I wrote in VB a long time ago (the obligatory

Custom Properties, and a batch file converter) and some macros I am porting over to Python. I just started tonight, and

I must admit I don't know much about COM. I have a couple small issues yet to figure out, but I am already talking to

Solidworks with Python. I think this would be a awesome outlet for people who may not be "programmers" to do some

pretty powerful stuff.If there is interest, I will try and help out as many people as possible in the days to come - provided they can help me out

as well. I would like to think I am pretty darn handy with Python, but when it comes to COM I am not. Luckily, once this

is working, the COM part of it just goes away, and you never see it.Anyway, post if you are interested (especially if you are currently using Python with Solidworks), and let's have some

fun! If you don't know what Python is - check out www.python.org. Download it, and go through the tutorial. It is the

easiest (and best) programming language on the planet (well, most of us think so...).Markus.




Well, I think the biggest reason (of many) is simply the ease and elegance of the language. I have tried to explain it

before, but the best thing is to try it for yourself (aw shit....I sound like JB...sorry). I DO have examples I could send

people, but you need to install Python first. We use it at work (pure software development - nothing to do with CAD or Solidworks) for rapid prototyping, but are

finding that we are writing complete apps in it with ease. I talked to one developer at a games company who said they

are using Python for everything but the most complex graphics in their games now, and their codesize is sitting around

65% of the size of an equivalent game written in C++.Anyway, simply put it is an elegant language, with beautiful syntax that most people find the best out there. There are

others who don't like it for exactly the reasons some people do like it. The biggest "preferential" barrier is that it is a

dynamically typed language. This means no declaring of variables. And you can reassign a variable to something

totally different later. It is similar to Java in that there is no memory management - garbage collection is done for you.

You don't have to destroy objects, maintain reference counts, etc. You just need to worry about programming. If you

aren't careful, you can pass the wrong type to a function however, and then if you try to iterate over a single integer

when you were expecting a list, and you don't catch the exception....well - then that's not good progrsmming anyway in

my opinion.Finally - everyone I have spoken to (including myself....yes, I talk to myself) - when they tried it for the first time were

absolutely astounded that that the first *real* program they wrote "worked" the first try. The language is so natural and

easy. I'll try to get some time to post some meaningful examples. Using CAD is my evening job, so I'll have to do what I can in

the evenings. I'll try to keep up with the newsgroup during the day.And yes, it is completely cross-platform (much better then Java in this respect in my experience), so when Solidworks

does move to Linux.....hehe.--

Markus.






I downloaded it last night and tried the tutorial a little bit. I have been

wanting to get into doing programs for SW but I have no experience at it. VB

was greek to me but so far Python seems pretty easy to learn. I would really

appreciate some examples too.I only have one question. Where are the movies on their site? ;-)Dave




> Hi All.

>

> How many of you are familiar with Python? Those of you who are, did you

> know it is pretty much a no-brainer to use it

> to script Solidworks through COM with the win32 extensions from Mark

> Hammond? This means super-easy scripting of the API, and with the use of

> wxPython (the windowing toolkit) you can do full-on GUI stuff just as

> easy.


Well, last night I figured things out (with some help - Open Source is great...), and it all seems to be working properly

now. Quoting the guru who helped me out:"Solidworks kinda sucks, in the same way notes does. Their object's provide

a type lib, but don't repsond to requests for that typelib at runtime."Anyway, now I need to get some useful examples together. What are some things you guys do that are a HUGE pain

in the ass? Or maybe something you think would be cool to do that requires a lot of scripting or repetetive stuff? At

some point I will rewrite my Custom Properties GUI but that won't happen this wwek... ;o)Markus.



3a8082e126
Reply all
Reply to author
Forward
0 new messages