Many SOLIDWORKS users find themselves repeating the same or similar tasks as part of their modeling workflows. This article looks at a powerful tool available within SOLIDWORKS, the application programming interface(API), for automating some of those tasks.
The API enables users to create custom programs (macros) that can assist them in automating many of the tasks of the graphical user interface (GUI). This can be as simple as creating PDFs of several drawings to more complex tasks such as generating complete parts and assemblies from code. This article will walk through some of the API basics by creating a simple extrusion.
Getting Started
It will be helpful to have the macro toolbar visible to access the main macro/API features.
Figure 1. The Command Manager menu.
Right-clicking the menu bar area will bring up the menu shown in Figure 1. Selecting “Macro” from this menu will bring up the toolbar shown in Figure 2.
Below is a legend for the icons shown in the toolbar:
The quickest way to make a macro is to simply press the record buttonand start some action, such as creating a sketch. However, in this example, we will first create a simple macro from scratch to ensure that the software’s installation is properly configured.
From the software’s menu, select Tools > Macro > New, which will create a blank macro template like the one shown in Figure 3. Tradition would have you print “Hello World” from the macro and the code in Figure 3 does that.
Figure 3. VBA code for Hello World.
While this code does not perform any drawing or modeling, it does ensure that all the support and reference files are in place to continue building more advanced macros. Also, to ensure that all the API components are installed, go to Tools > Reference and the appropriate references should appear as shown in Figure 4.
Figure 4.Visual Basic for Applications reference libraries.
The most common programming language for interfacing with the API is Visual Basic and in this example, we will create a simple macro to give you an overview of this process.
As mentioned, the quickest way to create macros is to record them. Begin by pressing the record button, drawing a centered rectangle and making an extrusion, which will produce the block shown in Figure 5.
Figure 5. Sample extruded part.
Stopping the recording will prompt you to save the macro to a file. Figure 6 shows the code that the API generated to mimic the actions used in creating the part.
Figure 6. Code from the macro recorder.
This code may appear cryptic because it contains much more information than is necessary to generate the block. This is a consequence of using the macro recorder and the accompanying machine-generated code.However, it is possible to perform some limited automation with this generated code. You can identify which lines are creating the major features of the extrusion and edit this code to create another version of the block. For example, the parameters in the CreateCenterRectangle method (see Figure 6) can be changed to different (XYZ) values.The macro can be rerun leaving all the other lines in the code as they are to generate the block with the new dimensions that were entered.
Creating a Macro from Scratch
We have seen that recording a macro is a quick way to generate code (Figure 6). However, the automatic code is overly complex, generating extra code that is difficult to modify and/or debug beyond minor edits. We will now create the code from scratch focusing on the minimal elements to create the extrusion shown in Figure 5.
From the menu bar, select Tools > Macro > New,which produces the following code stub:
Dim swApp As Object Sub main() Set swApp = Application.SldWorks End Sub
Below is the code I manually entered for creating the block, which is much more streamlined than the recorded macro approach.We will review the code by sections.
'(1) Dim swApp As SldWorks.SldWorks Dim Part As ModelDoc2 Dim sketchManager As sketchManager Dim vSkLines As Variant Dim myFeature As Feature Sub main() Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc '(2) Set sketchManager = Part.sketchManager '(3) vSkLines = sketchManager.CreateCenterRectangle(0, 0, 0, 1, 1, 1) Part.ClearSelection2 True '(4) Set myFeature = Part.FeatureManager.FeatureExtrusion3(True, False, False, 0, 0, 1, 1.5, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False) End Sub
Section (1)
This is where the variables are declared. One major difference between the recorded macro and what is shown above is that variable declarations are more explicit. For example, instead of Dim Part As Object we declare Dim Part As ModelDoc2. This gives us access to the object methods and properties in the Visual Basic for Applications Editor window. An example is shown in Figure 7 where all of the Part object’s methods and properties are presented in a drop-down menu, as we type, after pressing the period(.) key.Declaring Part as a ModelDoc2 object, instead of a generic object,provides access to this feature.
Figure 7. Drop-down menu for object-specific properties.
Section (2)
The sketching tools can be accessed through the SketchManager interface. The sketchManager object can be accessed through the ModelDoc2 interface, and since Part is of type ModelDoc2,it has SketchManager as a property. But how do we know this?
At this point, wewill make a brief introduction to the API help documents. Figure 8 shows an excerpt from the help file for the SketchManager object interface. In the Accessors section, we see that ModelDoc2 has access to the SketchManager interface.
Figure 8. Help file documentation for the SketchManager interface.
This leads us to the documentation for the ModeDoc2 interface, which is excerpted in Figure 9. It has a SketchManager property that provides access to the SketchManager interface. Along with a good grasp of object-oriented programming (OOP), the help file is the greatest resource for creating advanced macros.
Figure 9. Help file documentation for the ModelDoc2 interface.
Section (3)
Below is the signature, from the help documentation, for the CreateCenterRectangle method:
value = instance.CreateCenterRectangle(X1, Y1, Z1, X2, Y2, Z2)
It is important to note that the XYZ values are in meters. This method returns an array of sketch segments to value
The line in our code,
vSkLines = sketchManager.CreateCenterRectangle(0, 0, 0, 1, 1, 1)
creates the rectangle and stores the array of sketch segments in vSKLines.
Section (4)
This is where the actual block extrusion takes place. FeatureExtrusion3 is a member of the FeatureManager interface. We have not explicitly defined a FeatureManager object, but we can access it by chaining off of the Part object, which does have a FeatureManager property.
Conclusion
The SOLIDWORKS API provides access to the software’s modeling engine through programming methods. It is an efficient method for automating frequently used modeling functions.
In this article, we created a basic shape by recording a macro to generate code capturing mouse movements and keystrokes. We then created the code from scratch, which resulted in a more streamlined and readable program.
About the Author
Attilio Colangelo has more than 25 years of experience in engineering and project management in the chemical, process, ceramic and advanced-materials industries. His specialties include CAE, with an emphasis on FEA, high-temperature and heavy industrial design. His software skills include SOLIDWORKS Simulation, NASTRAN, Caesar II, ANSYS and iOS programming.