Solidworks is a popular computer-aided-design software developed by the French company Dassault Systèmes. At every mechanical design job I’ve ever held, we used CAD packages like Solidworks to model and validate our designs before proceeding with manufacturing. Like any good CAD package, it has tools to model, draft, and simulate at all stages of a design process, and even boasts a very capable API and macro feature to allow you to script/automate these processes. I’ve always struggled to approach the Solidworks API, however, as it only officially supports C++, C#, and VB languages. I am a big fan of using Python, especially for workflow automation, so my aim with this article is to discuss a few ways to interact with Solidworks using Python.

Why use Python with Solidworks?

Python is a popular programming language due to its human-readability, approachableness, and immense wealth of support and resources on the internet. I’ve used Python for a number of projects in the past, so it usually my first choice of programming language. Whenever I need to quickly move or rename a number of files, or extract information from a large number of filenames, a quick Python script is the way to go.

For Solidworks in particular, here are a number of ways I have personally used Python to aid my work:

  • Renaming a large number of files
  • Re-organizing a Solidworks project folder
  • Building a BOM from a list of file names
  • Adding to or changing information in custom properties
  • Creating a basic drawing for a long list of parts

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.

Ways to Interact with Solidworks using Python

I will discuss below 2 ways to interact with Solidworks using Python

  1. Equation and Design Table Interaction
  2. Solidworks API Wrapper

Equation and Design Table Interaction

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.

image.png

You can build your equations and variables within Solidworks, then simply export these to an external file. I have done this for a basic cube with 3 variables and 2 equations.

https://gist.github.com/mason-landry/98ce326c8539ba667ec1363146a31713

Since this is a simple .txt file located within my SW project folder, it is simple to manipulate the dimensions of the part using Python as you would write to any other file.

https://gist.github.com/mason-landry/12213889642091193693918f295b13f5

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!

Solidworks API with Python — Basics

Solidworks offers a robust API that officially supports C++, C#, and VB. Unfortunately, at the time of writing this article, they don’t officially support Python integration. It is possible, however, to make use of Python’s pywin32 package to pass VB commands through to the Solidworks application. I have used this approach several times to script and automate my Solidworks workflow as an alternative to using Solidworks macros. The basic premise is outlined as follows:

  1. With Solidworks open, go to Tools > Macro > Record
  2. Perform the action which you’d like to automate/script
  3. Stop recording and save the macro
  4. Go to Tools > Macro > Edit and select your saved file

When this file opens, you can see the VB commands used by Solidworks to perform your action. It can feel a bit hacky at times, but it’s pretty easy to Pythonify these VB commands and work them into a Python script.

image.png

Here are some examples of using pywin32 to pass API commands through to Solidworks.

First, install the necessary Python package:

pip install pywin32

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.

https://gist.github.com/mason-landry/580995d0d34f33f7c8e4d575fef3cc4f

These basic commands should let you get started interacting with Solidworks for your own aims. If you’re curious, here is a real-world example from a recent project I worked on.

Solidworks API with Python — Example

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:

  • Open each Solidworks part
  • Update the custom properties of the part to include my name, the date, etc. (Necessary for the drawing Title block)
  • Create a drawing using a known template
  • Drag standard views onto the drawing
  • Save the drawing to a specific location

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.

https://gist.github.com/mason-landry/47ec489b85d055a1ed9dbc0f72f52a3e

Again, most of these lines are taken directly from the output of the macro-recording function built-in to Solidworks.

Lastly, I need a function that will create and save a drawing with standard views.

https://gist.github.com/mason-landry/d97c129b1677843afe2dd2fd2bbc19af

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.

https://gist.github.com/mason-landry/e8456d1abc9cea2ba040a25176f6384d

Conclusion

If you’re reading this, you probably have something very specific in mind that you’d like to accomplish with Solidworks and Python. You should be able to see how I used Solidworks macro tools to build a mini Python library to start interacting with Solidworks using Python. Hopefully you can apply this method to facilitate your own design processes!

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐