How to remove a block by name?

How to remove a block by name?

m09366023695
Enthusiast Enthusiast
569 Views
4 Replies
Message 1 of 5

How to remove a block by name?

m09366023695
Enthusiast
Enthusiast

I want to remove a block by name :

 

ThisDrawing.Blocks.Item("1|3137.xxx-xxxxxxxx|22059.6018301436|13369.4589738571|18917.7435065279||line").Delete

 

yes, my block name is long ! 🙂

But I got this error:

 

error.JPG

0 Likes
570 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

A block definition, which you are trying to delete, cannot be deleted if it is referenced (that is, an AcadBlockReference of this block, a AcadBlock, exists somewhere in the drawing). IN your case, if you call AcadDocument.PurgeAll(), this block definition would still remain in the drawing.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

m09366023695
Enthusiast
Enthusiast

Tnx.

I didn't understand.

I should call PurgeAll function? how to do?

0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor

I assume you do know how to run the "PURGE" command in AutoCAD. If you run that command, you would not see that particular block definition is listed as purge-able, BECAUSE it is/are referenced, that is, there is/are block reference (block, as user sees) of this block definition somewhere in the drawing, thus, it CANNOT BE purged/deleted.

 

If you run code 

 

ThisDrawing.PurgeAll

 

it does the same thing as if you execute command "Purge" with "all" option. It is possible to call PurgeAll()/execute command "Purge" multiple time to purge/delete nested block definitions.

 

Anyway, in your case, if you do not see this actual block reference of this odd block definition anywhere in the drawing, it could be a nested block in another block definition. So, you can try run "Purge" command/ call ThisDrawing.PurgeAll multiple times (2 or 3 times, say) to see if this block definition is gone or not. 

 

Or, since you are doing programming, you can actually search the drawing, (modelSpace/layout/all block definitions...) to see if you can find an AcadBlockReference with this block name. If yes, then you CANNOT delete the block definition!

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 5

m09366023695
Enthusiast
Enthusiast

I found this solution (link😞  

 

Function DelBlock(strBnm As String)
    Dim objDef As AcadBlock
    Dim objCmp As AcadObject
    
    For Each objDef In ThisDrawing.Blocks
        If Not objDef.IsXRef Then
            For Each objCmp In objDef
                If objCmp.ObjectName = "AcDbBlockReference" Then
                    If objCmp.Name = strBnm Then
                        objCmp.Delete
                    End If
                End If
            Next objCmp
        End If
    Next objDef
    
    ThisDrawing.Blocks(strBnm).Delete
End Function

 

0 Likes