This beauty is awesome
Found on Augi but deserves a place here (it's so freakin simple!)
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace GetAttributesSample
{
public class Commands
{
[CommandMethod("Test")]
public void Test()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// Build a selection filter
TypedValue[] filter = { new TypedValue(0, "INSERT"), new TypedValue(66, 1) };
// Prompt the user for a selection
PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
if (psr.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Iterate the selection set
foreach (ObjectId brId in psr.Value.GetObjectIds())
{
// Open the block reference
BlockReference br = (BlockReference)tr.GetObject(brId, OpenMode.ForRead);
ed.WriteMessage("\n\nBlock: {0}", br.Name);
// Iterate the attribute collection
foreach (ObjectId attId in br.AttributeCollection)
{
// Open the attribute reference
AttributeReference att = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
ed.WriteMessage("\n\t{0} = {1} ", att.Tag, att.TextString);
}
}
tr.Commit();
}
}
}
}