In this final part of understanding the SolidWorks Object Model, I will be showing an example on how to get the names of all the components within a drawing view of a SolidWorks Drawing. This part will be relatively shorter than the others as most of the important topics has been covered. Here we will only be going slightly deeper in the context of a SolidWorks Drawing. Be sure to go through the previous parts if you have not already done so.
Example 3: Listing out all components in a drawing view.
Although this may seem as a trivial task, as the names of each component are already listed in the feature manager, this is still a useful lesson as being able to obtain the names of each component also means that you are able to access any component within the drawing view. Hence, you can also perform any operation on any component directly from a SolidWorks Drawing.
Now let us go through the hierarchy of objects within a SolidWorks Drawing before we proceed to the code. In a SolidWorks Drawing, we can have any number of views. And in each view, we can have either an assembly or a single part. Notice how that we can only have one root component (an assembly or a part) in each drawing view. From here, SolidWorks API has provided us with a method to obtain the root component within any view.
So from this information, I have created a chart that shows all the steps needed to go from a SolidWorks Drawing file all the way to each individual SolidWorks Part. Now this chart will seem excessively long just to get some parts from a drawing view, and I agree, but that’s just the way it works. However, we can always create our own methods to do this for us, such that every time we need a list of all components in a drawing view, we can do so in a single line of code. I will be teaching you that in a future topic.
Now that you have gone through the chart to get an understanding of each object you need in order to get the final lowest level component in a SolidWorks Drawing, try and create your own implementation of such a program yourself before proceeding.
Refer to the methods shown in the chart above if you get stuck or need some hints. Refer to parts 1 and 2 if you need a refresher on how to locate methods and properties.
After you have completed, return to this article and compare with the example code below.
Example Solution
Did you manage to do it?
Let’s take a look at my implementation of the code. Keep in mind that I declare the variables throughout the code because it helps with the process of explaining the code to you. It is still best practice to declare the variables together so that it will be easier to maintain the code.
As usual, we assign the SolidWorks application and documents to variables as shown in lines 1 to 9 in the example above.
Next we need to have the drawing view object. There are more than one way to get a drawing view from the drawing doc. Refer to the “View” help page to see the list of accessors that gives you the “View” object. The approach I have chosen to use is by looping through all the “View” objects and setting the first view that is not a drawing view of type “swDrawingSheet”. This is because a drawing sheet does not actually contain the components. Hence from lines 16 to 22 I use a “Do While” loop and exit the loop when I find the correct view. The variable “firstView” will be set to the first view that isn’t a drawing sheet.
Next, I get the root drawing component of the drawing view through the property “View::RootDrawingComponent” in line 26. Then I get the “Component2” object of the root component using the property “DrawingComponent::Component” in line 30. From here on, the methods are similar to the methods described in the previous part, where I will obtain all the component from the root component, then from there obtain the “ModelDoc2” objects from each component and lastly the path names from each “ModelDoc2” object.
Summary
With this we conclude the topic of understanding the SolidWorks API Object Model. I hope that the information presented here is useful in your SolidWorks API journey. I will continue in the next article with the topic of creating and using methods in VBA.