Find list of blocks in drawing but no instance visable

Find list of blocks in drawing but no instance visable

conveyor1
Enthusiast Enthusiast
1,278 Views
4 Replies
Message 1 of 5

Find list of blocks in drawing but no instance visable

conveyor1
Enthusiast
Enthusiast
I am looking for a VB.net way to search for a blocks name in a drawing where the block current has no visable instance. For example, if I have a block named "temp" that has previously been inserted into the drawing, but has been deleted, but not purged, how can I find this block by its name or other means?

Please let me know your thoughts.
0 Likes
Accepted solutions (2)
1,279 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

Hi,

 

You can use the Database.Purge() method to ge unreferenced blocks.

 

        public List<string> GetUnreferencedBlockNames(Database db)
        {
            var result = new List<string>();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                ObjectIdCollection blockIds = new ObjectIdCollection();
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId id in bt)
                {
                    blockIds.Add(id);
                }
                db.Purge(blockIds);
                foreach (ObjectId id in blockIds)
                {
                    var block = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                    result.Add(block.Name);
                }
            }
            return result;
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

_gile
Consultant
Consultant

Another way using the BlockTableRecord.GetBlockReferenceIds() method.

This one does not care if the block definition is hard referenced by some other object than a block reference.

 

        public List<string> GetUnreferencedblockNames(Database db)
        {
            var result = new List<string>();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId id in bt)
                {
                    var btr = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
                    if (!btr.IsLayout && btr.GetBlockReferenceIds(true, true).Count == 0)
                    {
                        result.Add(btr.Name);
                    }
                }
            }
            return result;
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 5

ActivistInvestor
Mentor
Mentor
Accepted solution

You can use the BlockTable's indexer to find the ObjectId of a BlockTableRecord given the block's name.

 

You can use the BlockTable's Has() method to find out if a block with the given name is defined in a drawing.

 


@conveyor1wrote:
I am looking for a VB.net way to search for a blocks name in a drawing where the block current has no visable instance. For example, if I have a block named "temp" that has previously been inserted into the drawing, but has been deleted, but not purged, how can I find this block by its name or other means?

Please let me know your thoughts.

 

0 Likes
Message 5 of 5

conveyor1
Enthusiast
Enthusiast
Accepted solution

Here is what I used to get the required result. (TextBoxForm32 has the name of the Block).

 

' Check if Block Exists
Dim mydwg As Document = Application.DocumentManager.MdiActiveDocument
Dim mytransMan As Database = mydwg.Database
' Get the current document and database, and start a transaction
Using mytrans As Transaction = mytransMan.TransactionManager.StartTransaction()
' Open the Block table record for read
Dim mybt As BlockTable
mybt = mytrans.GetObject(mytransMan.BlockTableId, OpenMode.ForRead)
If (mybt.Has(TextBoxForm32)) Then
Else
Form_Error = True
MsgBox("Load the Drawing Template for dot Net")
End If
' Dispose of the transaction
mytrans.Dispose()
End Using

0 Likes