By HawkRidge Systems Engineering Team
The Help File for the SolidWorks API (found under Help -> API Help) is a very convenient and useful resource when it comes to working your way through writing a macro. Not only does the Help File provide helpful information about specific components within the API, its organization also provides us with a lot of insight into the SolidWorks Object Model that we've been discussing.
There are two main ways to search for information on what you’re trying to do in the API:
Specific interface/method search terms – This is, of course, only possible if you already know which specific interfaces or methods you’re looking for. Either you've used that specific item before, or you've run across something captured by the macro recorder (for more on the macro recorder, see the eLearning API webinar) that you want more information on.
Descriptive search terms – This means searching based on how you would describe what you’re trying to do in normal English. For example, you might search “save file as dxf” or “get material properties.” The search will usually return some examples that contain interfaces or methods that you can then specifically search for, or in some cases will take you more directly to an article for an interface or method that addresses that topic.
Once you find an article that seems like it might be useful, it’s important to note which category the article falls into: 1. Interface
If the subject of the article is described as an interface, think of it as an umbrella object that must be implemented in order to gain access to the functions that it contains. In every “Interface” article, there will be a section called “Accessors.” This tells you how to create an instance of this interface so that its functionality can be used within your macro.
For example, the IModelDoc2 interface lists “ISldWorks::ActiveDoc” as one of its accessors. This means that in the top level SldWorks interface, there is an “ActiveDoc” method that returns a ModelDoc2 object. Thus, in order to “reach” ModelDoc2 and use its functions, you have to go through swApp.ActiveDoc or one of the other accessors. The implementation of the code looks like this (something we've seen a few times in previous posts already):
Dim swApp As SldWorks.SldWorks Set swApp = Application.SldWorks
Dim swModel As ModelDoc2 Set swModel = swApp.ActiveDoc
Each interface article will also have a link to “(Interface name) Members.” This is a complete listing of the interface’s properties and methods that you can call to actually do useful things.
2. Method
These are the items that will actually perform useful actions. Methods (also called functions) may contain one or more parameters that must be defined as input in order for the method to execute properly. For example, the FeatureExtrusion2 method has a list of 22 parameters (see picture below), ALL of which must be defined in order for it to execute properly. The Help File’s listing and description of the different inputs, what they mean, and what data type they require (Integer, String, Boolean, etc.) is a much more reliable resource than your memory.
One tip on using parameters in VBA: When you’re calling a method and assigning its return value to a variable, the parameters must be enclosed in parentheses. Otherwise, no parentheses are necessary.
For example, the CustomPropertyManager interface contains an .Add2 method that adds a new custom property to a SolidWorks document. The return value for .Add2 is the integer 1 if the property was successfully added and 0 if it was not. If you want to use this return value later (as in, execute or skip portions of the code based on whether that property was added successfully), you would want to store the return information from .Add2, and its parameters would have to be enclosed in parentheses as follows:
AddStatus = CusPropMgr.Add2("Property Name", swCustomInfoText, "PropertyValue")
However, if you don’t care about keeping track of whether the property was added successfully, you could just use the following line of code:
CusPropMgr.Add2 "Property Name", swCustomInfoText, "PropertyValue"
Note that the parameters are simply added after a space following the method call. Using parentheses in this case would return an error.
3. Example
The Help File contains many useful examples in various languages (not just VBA) that can help guide you in the right direction. These examples will generally use one or two key functions that you can specifically search for in order to understand how to put them into some new context that’s more useful for what you’re trying to accomplish.
When searching through the Help File, make sure you pay attention to any messages about methods being “Obsolete. Superseded by ___________.” Methods that are obsolete may still work, but they might be buggy or more limited than newer versions. Using the latest version is generally a good idea.
If you’re interested in the SolidWorks API, make sure to check out the recorded webinar: “Introduction to the SolidWorks API”.
See the previous posts from this series: Getting Started with the SolidWorks API - Part 1
SolidWorks API Building Blocks - Part 2
SolidWorks API Building Blocks - Part 3
Comments
Please sign in to leave a comment.