Fundamental to writing macros and add-ins with the SolidWorks API is knowing how to get the pointer to a particular object.  In some cases, this is a simple.  If you need a selected object’s pointer then you just use ISelectionManager::GetSelectedObject6.  If you need the name of a feature called “CutExtrude1” then you would just use IPartDoc::FeatureByName.  These methods, however, assume that you know the name of that object.  This isn’t always the case.  What to do then?

First, you need to programmatically locate the object.  The most common way is to traverse the FeatureManager tree (in the case of a feature or component) or traverse the geometry or topology itself (in the case of a face, edge, vertex, etc.).  Once you have the object, however, how does your program “remember” it for later use?  You could store its name or perhaps its location, but depending on the object this could be very tedious.

Instead, a much easier option is to use “persistent IDs”.  Persistent IDs are a unique identifier for any selectable object.  According to the API Help, “The reference IDs persist for the objects in the model document across SolidWorks sessions. Other applications can use persistent reference IDs to locate objects at runtime.”  This means that, unless you remove and then recreate that object, you can always locate that object so long as you have its ID.

Obtaining an object’s ID is very simple.  Just use IModelDocExtension::GetPersistReference3, which returns an object containing the persistent ID.  (Fun fact: Think your password is unique?  A persistent ID has 297 digits.)  Later, even if its a different SolidWorks session with a different program, you can re-obtain that object’s pointer using IModelDocExtension::GetObjectByPersistReference3.

Want to see how easy they are to use?  Open up any existing part or create a new one and add at least one feature.  Paste the following code in a new VBA macro and set a breakpoint on the line right after IModelDoc2::ClearSelection2 is used.  Now, select a feature in the FeatureManager tree and run the macro.  When your code breaks, look at your part and notice that nothing is selected.  Click Run to continue the code, and then look at the part again.  Why is the feature re-selected?  The persistent ID for that feature was obtained right before the selection was cleared.  On the line following IModelDoc2::ClearSelection2, the pointer was re-obtained using the persistent ID and then re-selected using appropriate Select method.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swObj As SldWorks.Feature
Dim swRef As Variant
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swObj = swSelMgr.GetSelectedObject6(1, -1)
    swRef = swModel.Extension.GetPersistReference3(swObj)
    swModel.ClearSelection2 True
    Set swObj = swModel.Extension.GetObjectByPersistReference3(swRef, Empty)
    swObj.Select2 False, Empty
End Sub

You can make this code work with any selectable object.  Just change the data type for swObj to something else—a face, a note, a component, etc.—and then make sure that you use the correct Select method for that object. A version that allows you to re-select multiple entities using persistent IDs is found here.

As you can see, this technique for locating objects can be far simpler than using entity names or attributes, because you don’t even need to assign the ID—it’s already there.  Indeed, many people wrongly use attributes when persistent IDs should be used.  Attributes will be discussed in part two of this series.

Persisting,
Keith

Want to keep up with new blog posts, videos, and macros we create each month? Sign up for our newsletter!