2. 'preconditions: Exit all sketches and select a sketch from the Feature Manager Design Tree (Left Pane).
  4. Option Explicit
  5. Sub main()
  6. Dim swApp As SldWorks.SldWorks
  7. Dim swModel As SldWorks.ModelDoc2
  8. Dim swSelMgr As SldWorks.SelectionMgr
  9. Dim swFeat As SldWorks.Feature
  10. Dim swSketch As SldWorks.Sketch
  11. Dim vSketchSeg As Variant
  12. Dim swSketchSeg As SldWorks.SketchSegment
  13. Dim vSketchPt As Variant
  14. Dim swSketchPt As SldWorks.SketchPoint
  15. Dim swSelData As SldWorks.SelectData
  16. Dim i As Long
  17. Dim bRet As Boolean
  18. Set swApp = Application.SldWorks
  19. Set swModel = swApp.ActiveDoc
  20. Set swSelMgr = swModel.SelectionManager
  21. Set swSelData = swSelMgr.CreateSelectData
  22. Set swFeat = swSelMgr.GetSelectedObject6(1, 0)
  23. Set swSketch = swFeat.GetSpecificFeature2
  24. swModel.EditSketch
  25. vSketchSeg = swSketch.GetSketchSegments
  26. For i = 0 To UBound(vSketchSeg)
  27. Set swSketchSeg = vSketchSeg(i)
  28. bRet = swSketchSeg.Select4(False, swSelData): Debug.Assert bRet
  29. swModel.SketchConstraintsDelAll
  30. Next i
  31. vSketchPt = swSketch.GetSketchPoints2
  32. For i = 0 To UBound(vSketchPt)
  33. Set swSketchPt = vSketchPt(i)
  34. bRet = swSketchPt.Select4(False, swSelData): Debug.Assert bRet
  35. swModel.SketchConstraintsDelAll
  36. Next i
  37. swModel.InsertSketch2 True
  38. End Sub

    You must exit all sketches to run this macro and you must have a sketch selected in the Feature Manager Design Tree (left pane). This macro removes all restrictions in a sketch by cycling through each sketch segment in the currently selected sketch and removing all constraints with the .SketchConstraintsDelAll function. Then the macro cycles through each sketch point and removes all constraints, again using the .SketchConstraintsDelAll function. This macro removes all dimensions in the sketch. It removes all horizontal and vertical restraints, all coincidence and anchor constraints.

    This macro requires you to be outside a sketch in order to delete all constraints, relations, and dimensions. For a macro that requires you to be inside a sketch to delete all constraints, check out DeleteRelations. Both DeleteRelations and DeleteRelations2 are modified versions of the Delete All Constraints in Selected Sketch Example (VBA) found in the Solidworks API Online help.

Return to the SolidWorks VBA Macros page.