autocad

Delete All Drawing Objects

This is useful when you are creating drawings from a template that has inserted blocks or pre-drawn objects. It is also helps when creating many CNC drawings. Instead of creating a new drawing each time, just create one drawing, create objects, save as, delete all, create objects, save as, and repeat until all the CNC drawings are created. This is a much faster process.

There are two methods that can be used. The first will create a selection set and delete all the objects in that selection set. The second will create the selection set and delete each entity in the selection with a loop. The first method is much faster but will not always work and will sometimes result in an ‘invalid entity name’ error.

Note these modules are written and run from inside an excel file. They can also be written in an AutoCAD file vba module. Just replace acadDoc with ThisDrawing. Also there is no need to get acadApp object when running from inside AutoCAD drawing.

Sub delete_all()

    Dim acadDoc As Object
    Dim acadApp As Object
    Dim objss As AcadSelectionSet

    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    acadApp.Visible = True
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
    If acadApp Is Nothing Then
        MsgBox "Sorry, it was impossible to get AutoCAD!", vbCritical, "AutoCAD Error"
        Exit Sub
    End If
   
    acadApp.WindowState = acMax
    Set acadDoc = acadApp.ActiveDocument
    
    acadDoc.ActiveSpace = acModelSpace

    Set objss = acadDoc.SelectionSets.Add("ToErase")
    objss.Select acSelectionSetAll 'select all entities (no filters)
    objss.Erase 'erase the selection
    objss.Delete 'delete the selection set object (not the entities)

    acadDoc.Regen acAllViewports

    On Error GoTo 0

End Sub
Sub delete_all_loop()

    Dim acadDoc As Object
    Dim acadApp As Object
    Dim objdrawingobject As AcadEntity
    Dim objss As AcadSelectionSet

    On Error Resume Next
    
    Set acadApp = GetObject(, "AutoCAD.Application")
    acadApp.Visible = True
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
    If acadApp Is Nothing Then
        MsgBox "Sorry, it was impossible to get AutoCAD!", vbCritical, "AutoCAD Error"
        Exit Sub
    End If
   
    acadApp.WindowState = acMax
    Set acadDoc = acadApp.ActiveDocument
    
    acadDoc.ActiveSpace = acModelSpace
   
    Set objss = acadDoc.SelectionSets.Add("ToErase")
    objss.Select acSelectionSetAll
    For Each objdrawingobject In objss
        objdrawingobject.Delete
    Next
    objss.Delete
    
    acadDoc.Regen acAllViewports
    
    On Error GoTo 0

End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s