In the previous part, I have introduced the concept of objects and touched lightly on the SolidWorks object model. In this part, I will continue where I left off and showcase more in depth examples on how to utilize the object model. With the goal of helping you to get a good grasp on programming using SolidWorks API.
So in the previous part, we have learnt that objects have members and properties, and how we can access them using the “dot” operator. Based on this concept, I will show you a few more examples on this topic to really ensure you have a good grasp of it. I will show an example for each type of SolidWorks document, namely part, assembly and drawing.
Example 1: Creating a circular extruded cut in a SolidWorks Part.
In this scenario, let’s say you want to create a macro that will create a circular extruded cut on a selected face when you run the macro. This is a simple task when done in the usual SolidWorks UI, but much more consideration needs to be taken into account when using SolidWorks API.
So before we dive into the code, let’s have a run down on what are the things we will need in our program. As usual, we need our SolidWorks application object to be able to access any other object. Then, we will also need the active document for the current SolidWorks Part. So let’s code it out.
This segment of code would be where we left off in the previous part of this topic. So now let’s proceed further. As mentioned, we want to create an extruded cut on a selected face. However, if you are familiar with SolidWorks, sketches can only be created on planar faces. So we will need write code to check if the selected face is planar.
So the next thing we need in our code is to declare and assign the selected face to a variable, and then check if the face is planar. If it is, the program will proceed, if not, then the program would show a warning.
Before we proceed, I will explain a bit about the SolidWorks selection manager. The SolidWorks selection manager is an interface within the SolidWorks handles all the selections. So whenever anything is selected, be it a body, a face, an edge, and etc., it will be registered within the selection manager. So to access which objects have been selected with the selection manager, we will need to assign the selection manager object to a variable before we can call it.
Now, as someone who isn’t familiar with this interface, how would you be able to know how to get the selection manager object? Let’s go through the usual process together. Whenever you are looking for how to get a certain object, we head over to the SolidWorks API Help interfaces page. From here it is convenient to use the keyboard shortcut “CTRL + F” to quickly locate the interface you are looking for. In this case, type in “selection” and locate the “ISelectionMgr” interface and click it’s link. Note that this method works for any interface.
Now that you are in the help page for the selection manager, we can find how to get the selection manager object. Under the subsection “Accessors” as shown in the image above, we can see the link with text “IModelDoc2::SelectionManager”.
In every interface help page exists the “Accessors” subsection that shows all the ways we can “access” these objects. The parent object will be stated before the “::” symbol and the object you wish to access will be after.
In this case, the selection manager object can be accessed through the parent object “IModelDoc2”. So based on this alone you should be able to deduce the code needed. However, if you are unsure, clicking the link will bring you to the help page that will show the sample code on how to do it.
As usual, you will need a variable to store the selection manager object, as well as another line to assign the object to the variable. Sample code below.
Now that you have the selection manager object, we would like to get the face that is selected through the selection manager. Based on what we want to do, we can probably already guess how the code may look like and which SolidWorks API objects and methods to use. Firstly, we would need to declare a variable with the type of a face. Then we can assign the selected face object to the declared variable.
However, we do not know which interface relates to the type face, nor do we know which property or method returns the selected face to us. In this case, we will search the members of the selection manager object. Back at the help page for “ISelectionMgr Interface” we can see a section with the link to all the members of this interface under the “See Also” subsection. Clicking the link brings us to the page with all the members listed. From here we can try to find the method or property that returns the currently selected objects.
Now in the list of members, we look for a method that may do what our program needs.
From the list of member methods, we should be able to locate the method “GetSelectedObject6”. From its name and description, it seems to do exactly what we need. We will click on the methods link to learn more.
Also note that there are many methods whose description states “Obsolete. Superseded by …”. What this means is that these methods are from an older release. These methods can still be used despite being called obsolete, however we need to be careful as they may not be compatible with the newer features in SolidWorks. For example, if SolidWorks has a new type of object that can be selected, an older version of the method may not be able to return said newer object type. Hence, it is always best practice to use the latest version of any method, albeit not compulsory.
Another thing to note is that there are methods that start with “I”, these methods usually return pointers instead of the object itself. These methods are usually used in C++ so if you are not using C++ you generally do not need to use these methods.
Hopefully you should be familiar enough with these help pages and how to read them. Go through the help page and understand what kind of inputs are required for the method and what kind of objects are returned. After that, try to create a continuation of the macro yourself and check back with the example below.
Now if you have typed the lines of code to declare a variable as well as the assign the selected object to the variable similar to the example above, you should probably have a good understanding already.
I have went slightly ahead and included some additional type checking to ensure that what is selected is a face, before assigning it to a variable. It is a good practice to ensure that the selected object type is the same as the variable type to prevent any errors. If there is no check, then if a different object is selected and the program attempts to assign it to the “Face2” type, then the program would crash.
Now we will add some more checking if the selected face is planar, and we will write code to exit the program if the face is not planar and show the user a warning. Going to the list of members in the “Face2” interface, we will find the “Normal” property. This property will return an array of (0, 0, 0) if the face is not planar. We will use this property to do the checking.
In the example above, after the selected face has been assigned to the variable “swFace”, another variable has been declared as an array with the type Double. Arrays are declared in VBA by adding a bracket after the variable name with the size of the array in the bracket. The type for the values in the array is typed the same way as all declarations.
Each element in the array is assigned value of 0. We will declare another variable “faceNormal” to store the return value for the “Normal” of the selected face.
Finally we compare each element in both “faceNormal” and “nonPlanar” variables. If all three conditions for the If loop matches then a message box will notify the user that the selected face is not planar. We then end the program using “Exit Sub” which ends the immediate function that it is written in.
Notice that the type for the variable “faceNormal” is variant. The “variant” type is also a flexible data type similar to “object”. While “object” stores references to objects, the “variant” stores the value. The actual type stored in a variant variable is determined during run time. This is useful in cases where a method may return multiple types of values. In SolidWorks API, if a method returns an array of some data type, the variant type is generally used. This is because determining the actual type returned may be overly troublesome. The individual values in each element of the array can then be evaluated individually and assigned to new variables. We will see examples of this in future articles.
Now that we have some form of checking and validations within our program, we will finally create the sketch and perform the extrude cut. As users of SolidWorks, we know that there has to be some method of inserting sketch somewhere within SolidWorks API. This is because we can perform this action within SolidWorks. However, as we do not know where this method may reside, we can use the search box in the API Help. The search box is located at the top right on any API Help page.
We type in “insert sketch” for the search box and we can find results similar to the image above. From the first two link, we can already see that “insert sketch” is a member and method of the “IModelDoc2” interface. We will select either one of the links and look for the “insert sketch” method. From there, we can find out how to use the method. After locating the “insert sketch” method, we find that it is actually obsolete.
So instead, we will click on the link for the method that supersedes the obsolete method. We can see that the “InsertSketch” method is a child of “ISketchManager”. From there we get another link to the “ISketchManager” interface page, where we can find from the accessors that “ISketchManager” is a child of “IModelDoc2”.
By going through SolidWorks API Help, we are able to locate all the required objects and methods to write our macro. We get the code as shown in the image below.
We now have the code that inserts sketches on a selected face. For the final part, I leave to you to complete the entire macro. Add additional code such that an extruded cut of a circle with a radius of 5 mm and depth of 5 mm will be created when the macro is run while a planar face of a body has been selected.
Use this exercise to test your understanding of SolidWorks API as well as your capabilities in using the SolidWorks API Help. I will leave the final code below for you to check.
Note that some methods in SolidWorks API takes in many variables and may cause the lines to be exceedingly long. In this case for VBA, you can split these long lines into multiple lines by creating a space ” ” followed by the underscore “_” at the point where you would want to split the lines and press enter.
Summary
In this example, I have showcased in greater detail about SolidWorks API Object Model as well as how to use the Help Page to locate the interfaces, properties and methods to use. In the example given, the object model can be summarized in the chart below. It can be said that every object in SolidWorks API can be “linked” through their accessing methods or properties. Being able to understand this relationship is of utmost importance to properly utilize SolidWorks API. In the next part, I will show you more examples as well as tips and tricks when creating macros for assemblies.