xarial / codestack

Library of SOLIDWORKS API, SOLIDWORKS PDM API, VBA and Desktop code examples, applications macros and tutorials.
https://www.codestack.net
MIT License
186 stars 69 forks source link

Macro for deleting ALL hanging dimensions in Solidworks #221

Open DaVegas opened 1 year ago

DaVegas commented 1 year ago

Is there a Macro for deleting all hanging dimensions from a drawing in Solidworks?

Snake-60 commented 1 year ago

Option Explicit

Sub main() Dim swApp As SldWorks.SldWorks Dim swModel As ModelDoc2 Dim swDraw As DrawingDoc Dim swSheet As Sheet Dim swView As View Dim boolstatus As Boolean Dim swAnn As Annotation Dim vSheetNames As Variant Dim i As Integer

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

If swModel.GetType <> SwConst.swDocumentTypes_e.swDocDRAWING Then Exit Sub
Set swDraw = swModel

swModel.ClearSelection2 (True)
vSheetNames = swDraw.GetSheetNames
For i = 0 To UBound(vSheetNames)
    swDraw.ActivateSheet vSheetNames(i)
    Set swSheet = swDraw.Sheet(vSheetNames(i))
    Set swView = swDraw.GetFirstView
    Do While Not swView Is Nothing
        Set swAnn = swView.GetFirstAnnotation3
        Do While Not swAnn Is Nothing
            If swAnn.IsDangling Then
                boolstatus = swAnn.Select3(True, Nothing)
            End If
            Set swAnn = swAnn.GetNext3
        Loop
        Set swView = swView.GetNextView
    Loop
    boolstatus = swModel.DeleteSelection(True)
    swModel.ClearSelection2 (True)
Next i
swModel.ClearSelection2 (True)

End Sub