MACRO LIBRARY

Delete all blocks from current sheet

Free public access. No account or subscription required.

Files

Delete all blocks from current sheet

Code

'Deletes all blocks from the active drawing sheet.
    
'Preconditions: drawing is open
    
'Written by Artem Taturevych
'intercad.com.au
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    DeleteBlocksFromSheet swDraw.GetCurrentSheet, True
    swModel.ForceRebuild3 False
End Sub
    
'The easiest way to get block instances is from the active sheet sketch. Optionally, _
    blocks may also be deleted from the sheet format level.
Sub DeleteBlocksFromSheet(swSheet As SldWorks.Sheet, deleteFromSheetFormat As Boolean)
    Dim swSheetSketch As SldWorks.Sketch
    Set swSheetSketch = swModel.SketchManager.ActiveSketch
    DeleteBlocksFromSketch swSheetSketch
    If deleteFromSheetFormat Then
        Dim swFormatSketch As SldWorks.Sketch
        Set swFormatSketch = swSheet.GetTemplateSketch
        DeleteBlocksFromSketch swFormatSketch
    End If
End Sub
    
Sub DeleteBlocksFromSketch(swSketch As SldWorks.Sketch)
    Dim vBlockInstances As Variant
    Dim swSketchBlock As SldWorks.SketchBlockInstance
    vBlockInstances = swSketch.GetSketchBlockInstances
    If Not IsEmpty(vBlockInstances) Then
        Dim i As Integer
        For i = 0 To UBound(vBlockInstances)
            Set swSketchBlock = vBlockInstances(i)
            swSketchBlock.Select False, Nothing
            swModel.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed
        Next
    End If
End Sub