BlockTableRecord.Erase not deleting block

BlockTableRecord.Erase not deleting block

Bradleywalker12380
Advocate Advocate
1,402 Views
7 Replies
Message 1 of 8

BlockTableRecord.Erase not deleting block

Bradleywalker12380
Advocate
Advocate

I am trying to delete a specific block, I am using BlockTableRecord.Erase, and when I open the DWG the block is still there even though when I debug it specifically makes it past that line.

 

What am I doing wrong?

After BlockTableRecord.Erase, I did oDb.Purge for that BlockTableRecord.ObjectID

0 Likes
1,403 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

@Bradleywalker12380  a écrit :

I am trying to delete a specific block


What do you mean? BlockReference or BlockTableRecord?

Erasing a BlockTableRecord means purging the block definition and cannot be done if the BlockTableRecord is referenced (i.e. there's any BlockReference in the drawing).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 8

Bradleywalker12380
Advocate
Advocate

This is what I am doing, and the block is still there after I have deleted it.

 

Bradleywalker12380_0-1642584172372.png

 

0 Likes
Message 4 of 8

_gile
Consultant
Consultant

You are doing things the wrong way. You should read more attentively the documentation about the Database.Purge method.

First you have to check if the BlockTableRecord is not referenced with Database.Purge, and then, if it is not, delete it.

var ids = new ObjectIdCollection();
ids.Add(btr.ObjectId);
db.Purge(ids);
if (ids.Count == 1)
    btr.Erase();

 

PS: typically we use br as variable name for a BlockReference instance and btr for a BlockTableRecord instance.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 8

Bradleywalker12380
Advocate
Advocate

Thank you, I did try that and I see that the count is still = 0.

How can I delete the block before it is checked to be purged?  

0 Likes
Message 6 of 8

_gile
Consultant
Consultant

@Bradleywalker12380  a écrit :

Thank you, I did try that and I see that the count is still = 0.


That means the BlockTableRecord is referenced, i.e. there's one or more reference to this definition inserted in the drawing.

 


@Bradleywalker12380  a écrit :

How can I delete the block before it is checked to be purged?  


You cannot delete a BlockTableRecord (as any SymbolTableRecord) while it is referenced, which means, in AutoCAD drafter languauge : you cannot purge a block (as a layer, a linetype, ...) while it is used in the drawing.

 

To be able to erase the BlockTableRecord, you have to first erase all its BlockReferences.

You can get the existing references with the BlockTableRecord.GetBlockReferenceIds method.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 8

Bradleywalker12380
Advocate
Advocate

Thank you, I did that and was able to delete the block.... but since the block was nested it also deleted the entire block using .GetBlockReferenceIds(directOnly:=False, forceValidity:=True)

 

Bradleywalker12380_0-1642628319197.png

 

When I did .GetBlockReferenceIds(directOnly:=True, forceValidity:=True) it didnot delete the block.

 

I read the link about .GetBlockReferenceIds and I believe it t should be False, False.

Correct?

0 Likes
Message 8 of 8

Bradleywalker12380
Advocate
Advocate

I *think* I got it...?

 

So from the IDs returned in (GetBlockReferenceIds,False, False) I check and get the owner BTR from the ID as a BlockReference, and then check if .IsLayout = False.

 

Bradleywalker12380_0-1642649447629.png

 

It works, but I have a feeling this isn't the best way to see if it is a nested block.

Can anyone confirm this please?

 

Thanks!

-B 

 

0 Likes