Author Topic: Kudos to Gile in Augi - Get attributes for selection of blocks  (Read 1525 times)

0 Members and 1 Guest are viewing this topic.

bchapman

  • Guest
Kudos to Gile in Augi - Get attributes for selection of blocks
« on: February 11, 2013, 02:05:08 AM »
This beauty is awesome :) Found on Augi but deserves a place here (it's so freakin simple!)

Code: [Select]
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();
            }
        }
    }
}