MACRO LIBRARY

Assign and find tracking IDs

Free public access. No account or subscription required.

Files

Assign and find tracking IDs

Code

'Precondition: Any number of bodies, faces, edges, or vertices are selected in a part or _
    assy. (Note: Since loops technically are edges, they are not included in this example.)
'Result: All selected objects are given tracking IDs and re-selected.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swBody As SldWorks.Body2
Dim swFace As SldWorks.Face2
Dim swEdge As SldWorks.Edge
Dim swVertex As SldWorks.Vertex
Dim swEnt As SldWorks.Entity
    
Dim lngCookie As Long
Dim vObjects As Variant
Dim i As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    
    'register tracking definition
    lngCookie = swApp.RegisterTrackingDefinition("tracking")
    
    'assign arbitrary tracking IDs to every selected object
    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
        Select Case swSelMgr.GetSelectedObjectType3(1, -1)
            Case swSelectType_e.swSelSOLIDBODIES
                Set swBody = swSelMgr.GetSelectedObject6(1, -1)
                swBody.SetTrackingID lngCookie, i
            Case swSelectType_e.swSelFACES
                Set swFace = swSelMgr.GetSelectedObject6(1, -1)
                swFace.SetTrackingID lngCookie, i
            Case swSelectType_e.swSelEDGES
                Set swEdge = swSelMgr.GetSelectedObject6(1, -1)
                swEdge.SetTrackingID lngCookie, i
            Case swSelectType_e.swSelVERTICES
                Set swVertex = swSelMgr.GetSelectedObject6(1, -1)
                swVertex.SetTrackingID lngCookie, i
            Case Else
                Debug.Print "Tracking ID not assigned."
        End Select
    Next i
    
    'clear the selections
    swModel.ClearSelection2 True
    
    're-select the objects with tracking IDs
    vObjects = swModel.Extension.FindTrackedObjects(lngCookie, Nothing, 0, Nothing)
    For i = 0 To UBound(vObjects)
        If TypeOf vObjects(i) Is SldWorks.Body2 Then
            Set swBody = vObjects(i)
            swBody.Select2 True, Nothing
            Debug.Print "body"
        ElseIf TypeOf vObjects(i) Is SldWorks.Face2 Then
            Set swFace = vObjects(i)
            Set swEnt = swFace
            swEnt.Select4 True, Nothing
            Debug.Print "face"
        ElseIf TypeOf vObjects(i) Is SldWorks.Edge Then
            Set swEdge = vObjects(i)
            Set swEnt = swEdge
            swEnt.Select4 True, Nothing
            Debug.Print "edge"
        ElseIf TypeOf vObjects(i) Is SldWorks.Vertex Then
            Set swVertex = vObjects(i)
            Set swEnt = swVertex
            swEnt.Select4 True, Nothing
            Debug.Print "vertex"
        End If
    Next i
    End Sub
    
'Notes:
'Tracking IDs disappear after each rebuild. To make them persistent one must use macro features. _
    While using this macro, if you do not rebuild the part after each run, you will see results _
    from previously selections.
'To determine the type of object from IModelDocExtension::FindTrackedObjects, either use TypeOf _
    or use the selection filter argument to only return a particular type, so you know what will _
    be contained in the resulting array.
    
'Results are inconsistent when selecting multiple items at once.