I would like to use tracking IDs to track edges and other entities across SolidWorks features, however tracking IDs disappear for me any time I add a feature to the part. For example, the macro below just assigns tracking IDs to every edge in a solid body, copies that solid body, and checks if the copy has any tracking IDs set.
Dim swApp As Object Dim Part As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Sub main() Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc Dim Component As Object Dim FeatureData As Object Dim varFeature As Object Dim bodies As Variant Dim body As Object Dim Cookies As Long Dim edges As Variant Dim edge As Object Dim trackingId As Long Dim trackingIdCount As Long bodies = Part.GetBodies2(0, True) Set body = bodies(0) Cookies = swApp.RegisterTrackingDefinition("Tracking_Test") If (Cookies = -1) Then swApp.SendMsgToUser2 "No tracking cookie", 4, 2 End End If edges = body.GetEdges() trackingId = 1 For i = LBound(edges) To UBound(edges) Set edge = edges(i) edge.SetTrackingID Cookies, trackingId trackingIdCount = edge.GetTrackingIDsCount(Cookies) If trackingIdCount = 0 Then swApp.SendMsgToUser2 "Setting tracking ID failed", 4, 2 End End If trackingId = trackingId + 1 Next i Part.ClearSelection2 True Set SelectData = Part.SelectionManager.CreateSelectData SelectData.Mark = 1 boolstatus = body.Select2(True, SelectData) Set varFeature = Part.FeatureManager.InsertMoveCopyBody2(0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, False, 0) bodies = Part.GetBodies2(0, True) For IBody = LBound(bodies) To UBound(bodies) Set body = bodies(IBody) edges = body.GetEdges() For IEdge = LBound(edges) To UBound(edges) Set edge = edges(IEdge) trackingIdCount = edge.GetTrackingIDsCount(Cookies) If trackingIdCount <> 0 Then swApp.SendMsgToUser2 "Tracking ID preserved after Move/Copy!", 2, 2 End End If Next IEdge Next IBody swApp.SendMsgToUser2 "Tracking ID lost after Move/Copy :(", 4, 2 End Sub
Every time I run this macro on any part it shows "Tracking ID lost after Move/Copy :("
. For example, here is another macro that creates a very simple part (run it on an empty part)
Dim swApp As Object Dim Part As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Sub main() Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", -3.69205019122774E-02, 6.84815534726226E-02, 0.118749125463296, False, 0, Nothing, 0) Part.SketchManager.InsertSketch True Part.ClearSelection2 True Dim skSegment As Object Set skSegment = Part.SketchManager.CreateCircle(0#, 0#, 0#, -0.011839, 0.004472, 0#) boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0) Dim myFeature As Object Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.01, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False) Part.SelectionManager.EnableContourSelection = False End Sub
What am I doing wrong?