Have you ever needed to do a task in SolidWorks over and over again? Is there a repetitive sequence of actions that you happen to perform on many of your models? If so, macros are your new friend. They allow you to pre-program a set of commands that can then be executed automatically, at the press of a single button.
Macros exist in other applications (Microsoft Excel being a popular example), and they’re actually the product of APIs (Application Programming Interfaces). An API is basically an environment in which programmers can utilize the native features and functions of the base application to direct it to perform specific actions or services. For example, in SolidWorks, the environment would be all the features and tools available for the user. In Excel, it would be all the Excel functions, chart tools, and so forth.
So, by creating a macro in an application, you basically utilize the API of the specific application. SolidWorks’ API uses the Visual Basic programming language to write macros, but even if you don’t know how to program, you’ll probably be able to understand how your macro works (more on that in later sections).
SolidWorks macros allow almost infinite freedom in manipulating all of the program’s features and tools. They also allow for the implementation of classic programming along with the use of standard SolidWorks features. For example, you could write a macro that receives a design requirement from the user (say the diameter and height of a cylinder) and then automatically builds the model according to the requirement. Another use case would be the automatic creation of a cylinder and calculation of its required height based only on the user’s specification of its diameter and volume.
Those examples, however, are usually advanced topics for those who are more familiar with macros and programming. For users who are just getting into SolidWorks macros, they’re especially helpful for repetitive tasks that are composed of several steps. Once a macro is created, its execution time is incredibly fast (depending on your computer, of course) compared to the time it takes to perform the task manually.
In this article, we’ll discuss the three main ways to create a macro, followed by a step-by-step demonstration of how to create one. We’ll even show you how to assign it a button in SolidWorks. So, if saving time is important to you, read on.
There are three ways to create a SolidWorks macro. Let’s take a quick look at each.
The simplest way to create a macro is to record yourself performing the actions you would like the macro to repeat. In this case, you simply hit the record button, execute your sequence of commands, and once you’re done, stop recording. The macro will then be saved and ready for use.
This is a good solution for those repetitive tasks that are entirely contained within SolidWorks’ raw features and don’t require additional calculations or operations.
The biggest downside of a recorded macro is its limits. You’re basically limited to whatever you could do manually in SolidWorks. Instead of limiting yourself to only what you can record yourself doing, you could directly write the code for the macro.
This allows a much higher level of control of the application and, as mentioned, allows for the implementation of programming elements, calculations, and user interfaces, among other cool things. As you can see, this method requires some programming knowledge, so it’s not very friendly to beginners.
This is the preferred method, and it can suit both the experienced programmer as well as the everyday SolidWorks user. The idea is basically to record what you can, then program everything else. Features, document operation, and so on can be recorded, then you edit the recording’s code to suit your needs. Additionally, you can add code between the recorded sections to fine-tune the macro.
Though some programming is involved, even users who are new to macros would benefit from editing recorded macros. This is mainly because when recording a macro, SolidWorks is recording everything you do, including model rotations, zooming in and out, everything!
In the following sections, we’ll explain step by step how to create and use a macro. The example we’ll use for the macro will probably be familiar to anyone who uses SolidWorks alongside Cura. In Cura, the coordinate system is different from the global coordinate system used in SolidWorks: The vertical direction in Cura is along the Z-axis, while in SolidWorks, it’s along the Y-axis.
This results in the fact that what is the “top plane” in SolidWorks is actually the “front plane” in Cura. This means that if a part was designed in SolidWorks so that its vertical direction is perpendicular to the top plane (which is the intuitive way), it will need to be rotated in Cura in order to be printed in the correct orientation.
We’ll be creating a macro that’ll automatically rotate a part to make it fit the coordinate system of Cura, and then save it as an STL file in a determined folder. Although these are relatively simple tasks, multiply them by the number of parts you’ve exported to STL and had to rotate in Cura, and you’ll start to see why automation is great for cases just like this.
Before we jump into the steps, let’s quickly take a peek at SolidWorks’ Macro menu options. Found under the Tools menu, they include the following:
In this step, we’ll create the macro we discussed above by performing all of the necessary steps while SolidWorks records them.
Now, let’s review the recorded macro’s code. Select “Edit” in the Macro menu and browse for the macro you’ve just created. This will bring up the raw code.
If you have experience in programming with Visual Basic, you’ll probably understand what’s going on. For those of you who aren’t programmers, the following are comments (marked in bold) for every block of code lines:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.RotateAboutCenter 6.67588438887833E-02, 0.138244705827351
Set myModelView = Part.ActiveView
myModelView.RotateAboutCenter 0.153152641862503, 0.245768365915291
Set myModelView = Part.ActiveView
myModelView.RotateAboutCenter 1.17809724509618E-02, 7.68026143485283E-03
Set myModelView = Part.ActiveView
myModelView.RotateAboutCenter 0.11388273369263, 0
Dim swModelView As Object
Set swModelView = Part.ActiveView
swModelView.RollBy 0
boolstatus = Part.Extension.SelectByID2("Loft1", "SOLIDBODY", 3.07844476491823E-02, 2.99014192186746E-02, 2.21730995684766E-03, True, 0, Nothing, 0)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Loft1", "SOLIDBODY", 3.07844476491823E-02, 2.99014192186746E-02, 2.21730995684766E-03, False, 1, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.InsertMoveCopyBody2(0, 0, 0, 0, 0, 0, 0, 0, 0, 1.5707963267949, False, 1)
Part.ClearSelection2 True
Set myModelView = Part.ActiveView
myModelView.RotateAboutCenter -0.137444678594554, 0.184326274436468
Set myModelView = Part.ActiveView
myModelView.RotateAboutCenter -3.92699081698725E-03, 0
Set myModelView = Part.ActiveView
myModelView.RotateAboutCenter -0.15707963267949, 9.98433986530868E-02
Set swModelView = Part.ActiveView
swModelView.RollBy 0
longstatus = Part.SaveAs3("C:UsersAll3DPPart2.STL", 0, 0)
End Sub
There are several issues with the recorded macro, which you may have noticed:
RotateAboutCenter
) were generated because the model 3D view was changed around during the recording of the macro. This is obviously not something we want to happen on purpose, so we have to keep in mind not to change the view when recording a macro.By editing the code, we can overcome these issues.
Looking at our revisions, notice how short and relevant the code is in comparison to the raw recorded macro.
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
boolstatus = Part.Extension.SelectByID2("", "SOLIDBODY", 3.07844476491823E-02, 2.99014192186746E-02, 2.21730995684766E-03, False, 1, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.InsertMoveCopyBody2(0, 0, 0, 0, 0, 0, 0, 0, 0, 1.5707963267949, False, 1)
longstatus = Part.SaveAs3("C:UsersAll3DPPart2.STL", 0, 0)
End Sub
Now that we have a revised macro, we can access it in one of two ways. The first way, as mentioned earlier, is via the Macro menu where we have the Run option, which allows us to select and run the desired macro. The second way is to create a dedicated button that can be pinned to the SolidWorks’ Quick Command toolbar.
With macros, it’s all about accelerating your workflow, and ease of access to a macro can be very advantageous. So, here’s how to create a dedicated button:
That’s it! Now, the next time you need to export a SolidWorks model to Cura, just hit the button, and your macro will be executed.
If you want to get a deeper understanding of how macros work and how to use them, there are great resources on the web:
There are also great pre-made macros and libraries that you can download and use as is or with minor adjustments. Just download the macro, edit it if you wish (now that you know how), and start using it! The following are some sites to get you started:
Lead image source: Javelin
License: The text of "SolidWorks Macros: How to Get Started" by All3DP is licensed under a Creative Commons Attribution 4.0 International License.