By HawkRidge Systems Engineering Team
In the previous post, we discussed the SolidWorks Object Model and how its hierarchical structure impacts the way you access API functionality. But how do you find what you need within the hundreds of interfaces and functions within the API? Fortunately, you don’t need to know all – or even most – of the API in order to start doing useful things. Here are some commonly used interfaces and functions that will get you up and running:
The top-level SldWorks interface is accessed automatically when a new macro is created. The auto-generated “Set swApp = Application.SldWorks” line makes this initial connection and gives the macro access to doing things like opening, closing, creating, and saving documents, turning toolbars on and off, and loading and unloading add-ins. Almost anything you want to do within the SolidWorks application requires first connecting to the ISldWorks interface.
The IModelDoc2 interface is exposed by the ISldWorks interface. This interface (also referred to as the ModelDoc2 object) gives the macro access to things like rebuilding a document and adding configurations. ModelDoc2 also exposes several other interfaces (see below) that can be used create and modify features and sketches.
The following is an example of an implementation of the ModelDoc2 object:
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Dim swModel As ModelDoc2
Set swModel = swApp.ActiveDoc
In this example, the variable swModel is used to store a reference to the active SolidWorks document’s ModelDoc2 interface. This step isn’t always strictly necessary, but typing swModel.function is easier than having to type out swApp.ActiveDoc.function every time.
The ModelDocExtension object is, as it sounds, an extension of the ModelDoc2 object. ModelDocExtension was created because objects have a limited number of methods and properties that are allowed. The ModelDoc2 object reached its capacity, so the SolidWorks software engineering team created ModelDocExtension to define more things that would otherwise belong to ModelDoc2. ModelDocExtension is exposed by the IModelDoc2 interface, meaning that it is accessed in the following manner:
Dim swDocExt As ModelDocExtension
Set swDocExt = swModel.Extension
These statements assume that swModel has been declared as a ModelDoc2 object and assigned to a valid SolidWorks document (both of which are done in the previous example). ModelDocExtension controls functionality like adding custom properties, getting mass properties, and adding BOM and revision tables.
The IFeatureManager interface is exposed by the ModelDoc2 object, just like the IModelDocExtension interface. This means that its implementation is almost identical: Dim swFeatMgr As FeatureManager Set swFeatMgr = swModel.FeatureManager The FeatureManager object controls things normally done in the Feature Manager Tree when using the graphical user interface. Examples include editing and creating features, hiding and showing solid bodies, and changing the rollback state of the Feature Manager Tree.
As the name suggests, the SketchManager object allows your macro to access sketch tools for creating sketches and sketch entities. It is also exposed by the ModelDoc2 object, so its implementation looks something like this:
Dim swSketchMgr As SketchManager
Set swSketchMgr = swModel.SketchManager Hopefully, this short list of commonly used interfaces gives you a better sense of how the SolidWorks Object Model is organized and what terms you might need to search for in the API Help File to find more functions to use. We’ll discuss the API Help File in greater detail in the next post, but for now you can search the API Help File for “_______ interface” (fill in the blank with one of the interfaces above) and see the first search result for a full list of properties and methods that you can access through each of the above objects.
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