Author Topic: List all blocknames in blocktable. Excluding layouts.  (Read 1502 times)

0 Members and 1 Guest are viewing this topic.

Irvin

  • Guest
List all blocknames in blocktable. Excluding layouts.
« on: November 28, 2011, 03:44:35 AM »
Hi there,

I need to create a function that lists all the block in the drawing. I need the same list of blocknames when you go to the insert command and drop the combobox down to display the block in the block table.

My code works fine but when i have an dynamicblock it displays the wrong name. I know how to solve this for the blockreferences but i don't know how i can get the corresponding names from blocktable. I need the block to be in the list when it has no blockreference in the drawing.

Here's the code i use:

Code: [Select]
private void ListBlocks()
        {
            Database db = AutoCAD.DocumentManager.MdiActiveDocument.Database;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId bId in bt)
                {
                    BlockTableRecord blkObj = (BlockTableRecord)trans.GetObject(bId, OpenMode.ForRead);
                    if (!blkObj.IsLayout)
                    {
                        cbbBlock.Items.Add(blkObj.Name);
                    }
                }
            }
        }

Hope you guys can help me.

Kind regards,

Irvin

Jeff H

  • Needs a day job
  • Posts: 6150
Re: List all blocknames in blocktable. Excluding layouts.
« Reply #1 on: November 28, 2011, 04:05:37 AM »
Hi Irvin,
 
One thing you can do is to skip over BlockTableRecords with IsAnonymous = true
 
Code: [Select]
[CommandMethod("ListBlocks")]
        public void ListBlocks()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor; 
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                ed.WriteMessage("\n****Start List*****");
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId bId in bt)
                {
                    BlockTableRecord blkObj = (BlockTableRecord)trans.GetObject(bId, OpenMode.ForRead);
                    if (!blkObj.IsLayout && !blkObj.IsAnonymous)
                    {
                        ed.WriteMessage("\n" + blkObj.Name);
                    }
                }
            }
            ed.WriteMessage("\n****End List*****");
        }
 

The only real change in your code would be
 
Code: [Select]
if (!blkObj.IsLayout)

to
Code: [Select]

   if (!blkObj.IsLayout && !blkObj.IsAnonymous)


Irvin

  • Guest
Re: List all blocknames in blocktable. Excluding layouts.
« Reply #2 on: November 28, 2011, 04:30:22 AM »
Hi Jeff,

Thanks, again.

Kind regards,

Irvin