This post is about how to create, edit and debug SOLIDWORKS macros when you are completely new to building software.
We’ll record a simple macro, explain the code and clean it up. Then we explain how you can debug your own macros.
You don’t need to be a software engineer by profession to be able to create macros, but a basic understanding of programming will help. I myself am a mechanical engineer by trade. I only started coding professionally when I started CAD Booster in 2017.
SOLIDWORKS has two flavors for automation: macros and add-ins. Macros are the most accessible, so we’ll start with those.
We’ll be using VBA or Visual Basic for Applications. VBA is an old Microsft programming language for creating macros for applications like Excel.
A VBA macro is a single .SWP file that can sit anywhere on your computer. You can run the macro by browsing for it or by adding a shortcut button to your command manager in SOLIDWORKS..
You can even record and play back macros, although not all actions can be recorded.
If you want to make a professional product that integrates with SOLIDWORKS, you need to build an add-in.
With add-ins, you can create Property Manager Pages (PMPs, those menus on the left, that let you edit Extrude properties, for example) and task panes (the menu tabs on the right).
Add-ins are usually installed on your computer with a Windows installer. After that, your new add-in appears in the list of available add-ins:
Add-ins can be built by SOLIDWORKS (like SOLIDWORKS Routing) or by third parties like CAD Booster. We develop and sell the products Drew (for drawing automation) and Lightning (the fast fastener filter) and we also build custom add-ins for our customers.
Overall, we love add-ins and we rarely build macros for our customers.
The first step in learning how to create macros is by recording your own actions using the macro recorder.
Prepare your model file, hit record and only perform the actions that you want to record. Hit stop to save the file.
This great video by GoEngineer shows you how to record your actions and let SOLIDWORKS convert them into a macro. You can then create a button in your toolbar to run the macro whenever you need it again.
SOLIDWORKS does not record all of your actions, though. This is confusing and annoying for people that are just getting started. So don’t be surprised if your action does not show up in the macro. Just be disappointed 😉
The macro recorder does record most actions that create geometry or features. It does not record many of the clicks in the user interface.
I have recreated the actions in the video above and saved the result. Clicking the rightmost button in the macro toolbar opens up the Microsoft Visual Basic for Applications editor. In my case, it looks like this.
The window has five sections:
One window that is not visible here is the Locals window. The Locals window shows you a list of the variables in your code and their current values.
If you don’t see a certain window, click View at the top to enable the missing window.
I added numbers in red to the previous image to explain the code.
I’ve written down everything that is happening in this short macro, everything that does not make sense and everything that I think you should know.
(I repeated the image to make the post more readable)
The output from the macro recorder is far from great.
It does not even use all variables and some lines are in there way too many times. Time to clean it up. This is what it can look like afterwards.
As you can see I have:
The functionality hasn’t changed a bit though. It still creates a sketch and a cut-extrude on a previously selected face.
And now that I am refreshing this blog post in 2020, I would make even more changes:
It is good practice to be ready for all possible situations.
Your code should not crash or give an error when the user does something unexpected. It is your fault that you did not expect that, so don’t blame the user.
So we’re adding some basic checks, which include, in this order:
After we get swApp, we can get the active model.
1 2 3 4 5 6 7 8 9 10 |
dim swApp as SldWorks.SldWorks set swApp = Applcation.Sldworks Dim swModel as ModelDoc2 set swModel = swApp.ActiveDoc if swModel is nothing then Msgbox "Please open a model first" End End if |
Getting swApp never goes wrong. But if not model is open, swModel will be nothing. In that case, we show a message box to the user. End stops execution of the macro entirely.
If you only want to work with the ModelDoc2 object, you don’t need these checks. But when you want to call methods that are only available for part, assembly or drawing objects, you need to verify the active model type. To make sure the model is a part:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
dim swApp as SldWorks.SldWorks set swApp = Applcation.Sldworks Dim swModel as ModelDoc2 set swModel = swApp.ActiveDoc if swModel is nothing then Msgbox "Please open a model first" End End if if swModel.GetType <> swDocumentTypes_e.swDocPART then Msgbox "Please open a part first" End End if 'Perform the actual work here |
For this particular macro, we should also check if the user selected a face beforehand. If not, you can give the user a message box with an error and stop the execution of the macro.
There are a few options that you should know when you want to properly test en debug your bits of code:
You can execute your code until a certain point by setting your cursor where it needs to pause and click Debug > Step into or press F8. The code runs until it reaches this point. From there you can run it line by line by pressing F8, this is a great way to debug your own code or to get to know another macro.
Pressing F5 calls the run command and runs the macro until the end or until you reach a breakpoint.
By left-clicking before a line, you can add Break Points. The execution of code stops at these points.
Now you can assert the values of variables by hovering over them or by checking those that you’ve assigned watches to.
Pressing the play button will make it hop until the next breakpoint, hitting stop will make it stop immediately.
This line adds the contents of one or multiple variables to the Immediate Window, as long as they contain text or numbers. You can combine multiple strings or values with an ampersand (&).
Example: Debug.Print “The value of boolstatus is ” & boolstatus
The line Debug.Assert x > 10 will assert whether x is indeed greater than 10 and it will pause the program if it isn’t.
The last tool for debugging that I want to mention is the Call Stack option.
When the code execution is started and paused, you can press View > Call Stack or Ctrl + L to see how the current function has been called by other functions. This is a really powerful tool, although the VBA implementation isn’t that great.
You can write macros in three Microsoft-based programming languages. The first one is Visual Basic for Applications (VBA), of which you have seen a lot above.
It is easiest to start with VBA because it is the most accessible option. The help website by SOLIDWORKS is also extensive and sorted by version.
VB (which used to be called VB.NET) and C# are the other two options. These are the more modern and more advanced options. They also allow you to create add-ins for SOLIDWORKS, this isn’t possible with VBA.
Here are some useful sites when you want to get started with building macros for SOLIDWORKS:
Doing a Google search in the form of SOLIDWORKS API [your search term] will also do miracles. I do that a dozen times per day.
If you want some professional guidance in your endeavors, you can enroll in this course by SOLIDWORKS:
Your current Value Added Reseller (VAR) can help you with official SOLIDWORKS courses.
I started CAD Booster four years ago to automate the boring bits of my engineering job. Since then, I have created add-ins like Drew and Lightning, plus a dozen others for our customers.
Because of all that experience, I know my way around the SOLIDWORKS API. So a macro that might take you days can sometimes take me only 30 minutes.
I will usually advise you to develop an add-in because they are more stable and can be maintained properly, but I also just finished a macro for a customer.
Check out the Contact page for our email address and phone number.
Are you excited to build a simple program that can take over some of your daily tasks? I’m pretty sure there are some great ideas out there.
Now let’s get those little robots to do our chores!