
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I feel like this is more of a sanity check question. I slightly struggle to understand a certain aspect of how AutoCAD manages its blocks in a drawing given the problem I am facing.
I am trying to find an inserted block in the drawing with a specific name - if I am not very technical, I would refer to that as a Block Reference, where as the Block/Block Record being the definition of the block itself - please correct me if otherwise.
I am using the following function to dig into the Database and retrieve the reference I am seeking:
// Searches the drawing for a block with the specified name. // Returns either the block, or null - check accordingly. private BlockReference GetBorderReference(string _BorderName) { BlockReference blkRef = null; using (Transaction _trans = acDb.TransactionManager.StartTransaction()) { BlockTable blkTable = _trans.GetObject(acDb.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord blkRecord; if (blkTable.Has(_BorderName)) { ObjectId BlkRecId = blkTable[_BorderName]; if (BlkRecId != null) { blkRecord = _trans.GetObject(BlkRecId, OpenMode.ForRead) as BlockTableRecord; ObjectIdCollection blockRefIds = blkRecord.GetBlockReferenceIds(false, false); foreach (ObjectId blockRefId in blockRefIds) { if ((_trans.GetObject(blockRefId, OpenMode.ForRead) as BlockReference).Name == _BorderName && (_trans.GetObject(blockRefId,OpenMode.ForRead) != null)) { blkRef = _trans.GetObject(blockRefId, OpenMode.ForRead) as BlockReference; } } } } _trans.Commit(); } return blkRef; }
(P.S, there might be unnecessary checks/case handling)
Good news is, the above method does work in some instances.
Bad news - in certain drawings, the GetBlockReferenceIds method returns empty. From my understanding, if the block is inserted in the drawing, that collection cannot be empty.
Upon further inspection using the MgdDbg tool, the successful drawing happens to have a reference whose Block Table Record match the name I was looking for. However the failed drawing has an different one (even though the Block Table Record I was expecting also existed - but had an empty Reference ID collection). Does that simply mean that the definition for the border I was looking for exist, but was not used? And perhaps whoever made that drawing used a different border whose block definition included the name I wasn't expecting?
I am terribly sorry for the confusion, I am fundamentally misunderstanding the way blocks are handled.
If the above code should work for ideal cases, is there a smarter way to find the reference I need without addressing it by name? I could say that the block in question does have distinct attributes I am looking for...
Thanks for the clarification!
Solved! Go to Solution.