It’s Not Magic! It’s a Macro: Huge Time Savings in SOLIDWORKS
Here’s a crazy thought – why not do more with less? But it’s not crazy, it’s possible with macros. You can take your SOLIDWORKS design work to the next level by doing even less work—just let SOLIDWORKS do the work for you by automating with macros and some VBA Programming.
In this article, we’ll show you how you can get started saving time and automating any process in SOLIDWORKS using macros. The time saved may start with instants, but these instants add up to incredible time savings. Here’s how you get started.
What Are Macros?
When you hear the word macro, think automation. Think of macros as custom programs you create to perform a task in SOLIDWORKS. Instead of you physically moving your mouse around the screen to create geometry or launch commands, a macro can do this for you.
Macros are powered by the SOLIDWORKS application programming interface, or API. The SOLIDWORKS API is incredibly powerful and incredibly open. Macros are like the land of opportunity; if you can dream it, you can do it. All it takes is a little bit of knowledge on how macros work and you can create all sorts of amazing mini-applications to automate your design process.
Using Macros in Two Easy Step!
In an extremely oversimplified context, macros really only take two steps: one, creating it and two, running or using it. With each of these steps—both creating and running—you have many choices. As is typical with much of SOLIDWORKS, with great power comes flexibility. If you are already a power user, then you can save time and fast forward to Step 1, Option 2.
Step 1: Creating a Macro
Creating a macro is the first step. You need to create the thing which will be run. The process begins by first thinking of what it is you want to automate. What is the specific task in SOLIDWORKS that you want to execute? It could be something as simple as a modeling operation like creating a hole, or as complex as creating entire CAD models based on some user input.
You have the task you want to automate, now let’s create a macro for it. You can create a macro directly inside of SOLIDWORKS, or outside using a basic coding interface. Here is where two roads diverge; but unlike Robert Frost, we can take both. In this case, one road can be thought of as a beginner’s road to using macros, while the other is wide open and flexible–yet more advanced.
Option 1: Creating a Macro for Beginners
Let’s work on creating your first macro within SOLIDWORKS. But don’t think of this as creating a macro; rather, think of it as teaching SOLIDWORKS what you want to automate by showing SOLIDWORKS the series of steps that will be converted into a macro by recording your mouse movements and key-strokes as you execute the commands or modeling operations. Check out the example below, where we will record a macro to create a hole. In this example, you should imagine creating countless holes each day. By creating a macro, we can leverage a hotkey to create these holes in an instance. Over the course of a week or month, these instances add up to significant time savings.
Record a Macro
Launch the record function from the tool macro bar or menu in the macro area of the tools drop down menu. Once the record button is pressed, SOLIDWORKS is recording every click of your mouse. You will immediately be in teaching mode, where SOLIDOWRKS is memorizing the sequence of clicks.
Teach SOILDWORKS the Operation
Now you are the teacher and SOLIDWORKS is your student. The macro learns by mimicking exactly what you do. Therefore, you have to streamline the process and clicking only what is necessary in the exact order, because SOLIDWORKS is literally capturing the clicks or commands. In this example, we are teaching the macro how to create a hole, so we will complete all the steps necessary to add this hole. After SOLIDWORKS has learned the lesson, click Stop on the macro tool bar to capture your macro and save it to a location on the disk. From here you will run it–and that is where the magic happens.
Run the Macro
It’s not magic, it’s a macro! Running a macro will execute the commands you recorded. When you click run on the macro tool bar, SOLIDWORKS will prompt you for the .swp file. Opening this file will execute the set of recorded steps in the macro.
Pro tip: For some macros, you will need to preselect the face for the operation. Remember that SOLIDWORKS doesn’t require preselection of entities but with macros, you have to.
Run the Macro, But Better!
Now, let’s start to take it up a notch and run the macro—but this time, we will go one better. You can add the macro to a hotkey, shortcut or mouse gesture to run it, instead of opening the macro through the macro toolbar. To do this, you will first enter the customize interface like you do to customize any area of the SOLIDWORKS UI. Then, add the Run Macro option linked to the .swp file to a hotkey, command button or mouse gesture. You can even assign your own 16 x 16-pixel thumbnail picture for the command. With these custom commands, you will seamlessly and instantly be able to run SOLIDWORKS operations—leading to great time savings multiplied by an ease-of-use factor of 100.
(For Power Users) Option 2: Creating a Macro, But Better
Here’s how to take your macros to the next level and unleash the full power of macros using the SOLIDWORKS API and VBA programming.
Instead of teaching SOLIDWORKS what to do, you can leverage the SOLIDWORKS API and program your own macros. Think of this as greenfield macro, where the possibilities are endless and limited only by your imagination and programming skills.
But don’t think you need to be a programming whiz or expert to make the most of this. In fact, only basic familiarity with programming concepts is required to get started creating macros in the VB macro editor.
Begin by going to the macro area of the tools drop down menu. Instead of hitting record to capture the steps, click “new” on the macro toolbar to open the VBA interface. Here you will be able to create the code for your macro from scratch. Below is an example of automating the process of creating a PDF from a file.
As you can see, the interface isn’t all that scary. It is quite simple and easy to understand as long as you know a few calls.
All the Resources You Need
The best resource for working with the SOLIDWORKS API is the SOLIDWORKS API web help found here. This is more than just a help page, it’s your go-to reference for coding in SOLIDWORKS where you can find step by step help as well as examples of some of some API code snippets to get started.
Another resource is the SOLIDWORKS forums. Just keep in mind that the forums have been transitioned to the 3DEXPERIENCE platform. (One more reason why you should create a 3DEXPERIENCE login, if you haven’t already.)
If you want to get started with the web help for the SOLIDWORKS API, I recommend bookmarking this page to keep as a handy reference to the API nomenclature, including some very detailed and robust examples. Check out the example below for deleting all constraints in a sketch.
If you want an even deeper understanding, I recommend taking a look at this epic flow chart that is nearly 500 pages long. This is the SOLIDWORKS Access diagram, which is a graphical representation depicting access to SOLIDWORKS API objects.
As you can see, there is an incredible amount of reference material available to you for working with the SOLIDWORKS API. There’s a lot of information and a lot of power.
Macros & API: What Do I Really Need to Know?
Since space in your brain comes at a premium, let’s talk about the minimum amount of information you will need to know to get started with programming a macro in SOLIDWORKS.
- If you remember one thing, remember to keep the help file handy. This is your go-to reference guide for the code (bookmark this link).
- In case you already forgot I’ll say it again, keep the API help link handy.
- Macros are snippets of code. You create a new macro by accessing the VBA editor through the SOLIDWORKS interface. Click Tools > Macro > and New to create a new one, or Edit to edit an existing file.
- When it comes to writing a macro, I recommend you leverage the code examples found in the API help.
- Assign a hotkey, shortcut or mouse gesture to easily run the macro and make it part of your modeling workflow.
Learn More About Macros and the API
You’ve made it to the end of this article, and you want to know more. What’s next? Did you know there’s an official SOLIDWORKS eCourse available on MySolidWorks for the SOLIDWORKS API? Take a look at it here. This is a paid course but check your SOLIDWORKS licensing—you might already have access to the training through your company.
If not, I recommend getting started for free. Lesson 1, the introduction to API fundamentals, is a half hour of great content. It’s more than enough to get you started and I recommend this as your next step in working with the SOLIDWORKS API.
Once you have become a master of the SOLIDWORKS API, why not put your knowledge to the test and get certified? There’s a CSWP specialist exam for the API.
Take a look at the sample exam questions for the API certification. It even advises you to refer to the help file to find the answer.
So, now you’ve bookmarked the link to the API help file and you’re armed with all the information you need to begin taking advantage of the SOLIDWORKS API and macros. When you think API, your brain should immediately think of automation and time savings. You have flexibility when it comes to creating these macros by recording your mouse and keystrokes or from scratch through code. You are only limited by your imagination.
With all this power to automate tasks, don’t simply think about what you’ll automate—instead, imagine what you can do with all the time you save. The SOLIDWORKS API gives you the gift of time; what will you do with it?