Author Topic: How to iterate entities in a block reference  (Read 2775 times)

0 Members and 1 Guest are viewing this topic.

sling blade

  • Guest
How to iterate entities in a block reference
« on: October 19, 2011, 12:27:00 AM »
Hi,

How can I iterate through an objects nested entities (if that's the right phase)?

In particular I have a block reference made up of AttributeDefinition's.

I need the values from those AttributeDefinition's but I don't know how to get at them without exploding the block, which is no good for what I need.

Can someone help?


Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: How to iterate entities in a block reference
« Reply #1 on: October 19, 2011, 12:44:22 AM »
Who hinders you to read this topic?

sling blade

  • Guest
Re: How to iterate entities in a block reference
« Reply #2 on: October 19, 2011, 02:47:46 AM »
Thanks Andrey, there is a lot of good information there and it was a big help!

Here is my solution
Code: [Select]
// Get the current document
                Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;

                // Get the PickFirst selection set
                //  PromptSelectionResult acSSPrompt;

                TypedValue[] tvs =
                new TypedValue[2]
                {
                    new TypedValue((int)DxfCode.LayerName, "TITL-BLOK"),
                    new TypedValue((int)DxfCode.Start, "INSERT")
                 };

                SelectionFilter selctfltr =
                  new SelectionFilter(tvs);

                PromptSelectionResult psr =
                  acDocEd.SelectAll(selctfltr);

                if (psr.Value.Count == 0)
                {
                    System.Windows.Forms.MessageBox.Show("Missing Title block - I cannot read the Name Attribute Definition");

                    return null;
                }

                Autodesk.AutoCAD.EditorInput.SelectedObject titleblock = (Autodesk.AutoCAD.EditorInput.SelectedObject)psr.Value[0];
                               
                Autodesk.AutoCAD.ApplicationServices.DocumentCollection docCol;
                Autodesk.AutoCAD.ApplicationServices.Document doc;
                Autodesk.AutoCAD.DatabaseServices.TransactionManager tm;

                docCol = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
                doc = docCol.MdiActiveDocument;
                 
                tm = doc.Database.TransactionManager;

                Transaction tr = tm.StartTransaction();

                using (tr)
                {
                    for (int i = 0; i < psr.Value.Count; i++)
                    {
                        SelectedObject o = psr.Value[i];

                        if (o.ObjectId.ObjectClass == RXClass.GetClass(typeof(BlockReference)))
                        {
                            BlockReference obj =
                                (BlockReference)tm.GetObject(o.ObjectId, OpenMode.ForRead);

                            // get its attribute collection
                            // iterate through the attributes
                            foreach (ObjectId attRefID in obj.AttributeCollection)
                            {
                                AttributeReference attref = (AttributeReference)tm.GetObject(attRefID, OpenMode.ForRead, false);
                                   
                                if (attref.Tag == "NAME")
                                {
                                    return  attref.TextString;
                                }
                            }
           
                        }           

                    }
                }

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: How to iterate entities in a block reference
« Reply #3 on: October 19, 2011, 02:59:48 AM »
Expression "RXClass. GetClass (typeof (BlockReference)" pull out from a cycle (save to string variable).