View Full Version : Blockreference and attributes
jrauden748232
2017-05-27, 07:45 PM
I am really struggling here, simply trying to print to command line every block and its corresponding attributes. I can get all the blocks to print, but i cant seem to get to the attributes. I get a block table record, print its name (the block name) and check using HasAttributes, and it does. But how can i get those attributes? I cant do it from by using the block table record, i think i need a block reference. But i dont know how to get a block reference, keep getting casting errors. I would post code but im on a trip and have no wifi, posting this from my phone. Thanks .
_gile
2017-05-28, 06:52 AM
Hi,
To list all block definitions (and their attribute definitions):
[CommandMethod("ListBlockDefinitions")]
public void ListBlockDefinitions()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
using (var tr = db.TransactionManager.StartTransaction())
{
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.IsAnonymous || btr.IsFromExternalReference || btr.IsFromOverlayReference))
{
ed.WriteMessage($"\n{btr.Name}");
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId attId in btr)
{
if (attId.ObjectClass.Name == "AcDbAttributeDefinition")
{
var attDef = (AttributeDefinition)tr.GetObject(attId, OpenMode.ForRead);
ed.WriteMessage($"\n\t{attDef.Tag}");
}
}
}
}
}
tr.Commit();
}
}
To list all block references (an their attribute references):
[CommandMethod("ListBlockReferences")]
public void ListBlockReferences()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
using (var tr = db.TransactionManager.StartTransaction())
{
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.IsAnonymous || btr.IsFromExternalReference || btr.IsFromOverlayReference))
{
foreach (ObjectId brId in btr.GetBlockReferenceIds(true, true))
{
var br = (BlockReference)tr.GetObject(brId, OpenMode.ForRead);
ed.WriteMessage($"\n{br.Name}");
foreach (ObjectId attId in br.AttributeCollection)
{
var attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
ed.WriteMessage($"\n\t{attRef.Tag} = {attRef.TextString}");
}
}
if (btr.IsDynamicBlock)
{
foreach (ObjectId btrId in btr.GetAnonymousBlockIds())
{
var anonymousBtr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
foreach (ObjectId brId in anonymousBtr.GetBlockReferenceIds(true, true))
{
var br = (BlockReference)tr.GetObject(brId, OpenMode.ForRead);
ed.WriteMessage($"\n{btr.Name} ({anonymousBtr.Name})");
foreach (ObjectId attId in br.AttributeCollection)
{
var attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
ed.WriteMessage($"\n\t{attRef.Tag} = {attRef.TextString}");
}
}
}
}
}
}
tr.Commit();
}
}
jrauden748232
2017-05-28, 11:08 PM
Thank you very much gile (yet again)...i was soooo close! Making alot of beginner mistakes right now, i truly appreciate your help.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.