With the previous post on classes, we have learnt how to create a simple class and how it might come in handy for your program. In this part we will explore even more functionality in a class and how to use them in the context of SolidWorks API.
Example of a class using SolidWorks API
In this example, we will create a class that is used to store and retrieve information about all the open documents in SolidWorks.
In the previous example, we used “Public” properties to define the stored information in the class. However, in this example, we will be using “Private” properties to store information, and use another method to retrieve the information.
Having separate “Public” and “Private” properties may seem counter-intuitive at first but there are good reasons to use them. Let study the following example class to understand further.
We create a new class named “swComponent” with the following code.
'Private members of class Private pName As String Private pFileType As Integer Private pDoc2 As ModelDoc2 'Methods to get the values from the private members Public Property Get Name() As String Name = pName End Property Public Property Get FileType() As String If pFileType = swDocumentTypes_e.swDocPART Then FileType = "Part File" ElseIf pFileType = swDocumentTypes_e.swDocASSEMBLY Then FileType = "Assembly File" End If End Property Public Property Get Doc2() As String Doc2 = pDoc2 End Property 'Sub to initialize the values for the members of this class. Call once after 'a new instance of the class is created Public Sub Initial(ModDoc2 As ModelDoc2) Set pDoc2 = ModDoc2 pName = pDoc2.GetTitle pFileType = pDoc2.GetType End Sub
Private members
In the class above, we have private properties with names beginning with the letter ‘p’. The “pName”, “pFileType” as well as “pDoc2”.
Getting back to the reason for having separate “Public” and “Private” properties, not all properties within a class is used outside of the class. For example, the “pFileType” variable is an integer value. However for our class, we would like to get a string that describes the actual file type instead of just an integer. In this case, we can save the file type as a private property, while using the public property to “get” the actual string we want.
Using private properties or methods also help to reduce a bulk of clutter. This will be helpful when creating classes that will be shared with other programmers. They do not need to access every property or method of a class to use it. Often it is only the few properties and methods that are really used outside a class, while the rest will work in the background to fully define the class.
Property Get
This brings us to another feature of the class which is the “Public Property Get”. This allows us to create methods that run every time a property is being “get” from the class. For example, when we try to get the file type of this class, the class runs an “if else” to check the type of file stored in the “pFileType” variable, and returns the appropriate string to the program.
Using the “Public Property Get” allows for much more powerful customizability to the class. Conversely, we can also write values to private members through adding “Public Property Let” sections in our class. However, as we only want the class to be read-only, we will not be adding that to the class. But feel free to experiment with your own class.
Class Initialization
Finally in our class, we have a “Sub” named “Initial”. It is common practice to initialize a new instance of a class object with data. If you are familiar with other programming languages, initializing values in a class is usually simple as there are methods built-in to do so. However for VBA, we will need to write our own method.
For our class, we will want to take in a “ModelDoc2” object, and write the existing properties to our own class. Now that we have our class defined, we are ready to use the class.
Using the Example Class
We will use our class to create a simple program that lists out all of the documents currently opened, along with the file type, as well as an index to access this file.
Dim swApp As SldWorks.SldWorks Dim swDoc As SldWorks.ModelDoc2 Dim openDocs As Variant Dim numberOfDoc As Integer Dim swDocs As Collection Sub main() Set swApp = Application.SldWorks 'Get all of the currently open documents openDocs = swApp.GetDocuments 'Create a Colletion object to store all of our "swComponent" class object Set swDocs = New Collection 'Get number of documents open to range bound the for loop numberOfDoc = swApp.GetDocumentCount 'Add each of the open document to our collection of "swComponent" class objects Dim i As Integer For i = 0 To numberOfDoc - 1 'Declare new "swComponent" class object Dim comp As swComponent 'Allocate memory for the new "swComponent" class object Set comp = New swComponent 'Getting the modelDoc2 object to initialize our "swComponent" class object Set swDoc = openDocs(i) 'Initialize values for our "swComponent" class object comp.Initial swDoc 'Add the "swComponent" class object to our collection swDocs.Add comp Debug.Print(comp.Name + " | " + comp.FileType + " | " + CStr(i + 1)) Next i 'Accessing our "swComponent" class object from the collection Dim getComp As swComponent Set getComp = swDocs(5) Debug.Print getComp.Name End Sub
In the main program above, we create a new instance of our “swComponent” class for each open document in our SolidWorks session. We then add them into a collection, “swDocs”. We have not covered collections in this series yet, but for now you can think of a collection as a more dynamic version of an array. Using a collection, we are free to add or remove objects without worrying about the number of objects.
Finally, we can access these “swComponent” class objects to access their properties.
Summary
Through using classes, we are able to make our own custom objects. And within these objects, we can have custom properties and methods. All these allow for much more structured code, and also reusability of code, as there is no need to rewrite the same code over and over again.
Good classes should be written as modular as possible. This means that the class should be written to be as independent from the main program as possible, allowing the class to be reused in other programs simply by copy and pasting it.
The concept of classes goes much deeper than covered in this series, however I hope that the information provided will serve as a good starting point.
In the next lesson, we will be learning about UserForms in VBA to create user interfaces for our programs.