Results 1 to 3 of 3

Thread: Blockreference and attributes

  1. #1
    Member
    Join Date
    2017-04
    Posts
    10


    Default Blockreference and attributes

    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 .
    Last edited by jrauden748232; 2017-05-27 at 07:45 PM. Reason: Mistake

  2. #2
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    201


    Default Re: Blockreference and attributes

    Hi,

    To list all block definitions (and their attribute definitions):

    Code:
            [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):

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

  3. #3
    Member
    Join Date
    2017-04
    Posts
    10


    Default Re: Blockreference and attributes

    Thank you very much gile (yet again)...i was soooo close! Making alot of beginner mistakes right now, i truly appreciate your help.

Similar Threads

  1. How i can sweep an BlockReference and find entities without explode
    By MarceloRocha777 in forum VBA/COM Interop
    Replies: 0
    Last Post: 2013-06-05, 02:55 PM
  2. DynamicBlock a look inside a BlockReference
    By Marcio Cartacho in forum Dot Net API
    Replies: 1
    Last Post: 2011-07-20, 09:40 PM
  3. How to Delete Multiple Instances of a BlockReference?
    By brad.moon in forum VBA/COM Interop
    Replies: 2
    Last Post: 2007-10-02, 02:36 PM
  4. Replies: 21
    Last Post: 2007-03-20, 02:03 PM
  5. Count BlockReference entities
    By axa2001ro in forum VBA/COM Interop
    Replies: 4
    Last Post: 2006-04-21, 01:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •