Author Topic: Setting attribute value in nested block  (Read 1451 times)

0 Members and 1 Guest are viewing this topic.

cannorth

  • Guest
Setting attribute value in nested block
« on: September 11, 2012, 06:22:17 PM »
Hello,

  Could someone please send code for setting an attribute value in a nested block?

Thanks,

cannorth

gile

  • Gator
  • Posts: 2522
  • Marseille, France
Re: Setting attribute value in nested block
« Reply #1 on: September 12, 2012, 02:23:48 AM »
Hi,

You have to scan the owner BlockTableRecord looking for the BlockReference which contains the attribute and then iterate the BlockReference AttributeCollection to find the AttributeReference you want to change the value.

Doing this the change will affect all references of the owner block.
Speaking English as a French Frog

cannorth

  • Guest
Re: Setting attribute value in nested block
« Reply #2 on: September 12, 2012, 10:08:47 AM »
Can someone show me a code snippet?

Thanks,

cannorth

gile

  • Gator
  • Posts: 2522
  • Marseille, France
Re: Setting attribute value in nested block
« Reply #3 on: September 12, 2012, 10:32:24 AM »
Here's a quick and dirty.

Replace the parent and nested block names, the attribute tag and the attribute value as you need.

Code - C#: [Select]
  1.         [CommandMethod("Test")]
  2.         public void Test()
  3.         {
  4.             Document doc = AcAp.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.             using (Transaction tr = db.TransactionManager.StartTransaction())
  8.             {
  9.                 BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  10.                 if (!bt.Has("parent")) return; // Replace "parent" by the parent block name
  11.                 BlockTableRecord btr =
  12.                     (BlockTableRecord)tr.GetObject(bt["parent"], OpenMode.ForRead);
  13.                 foreach (ObjectId id in btr)
  14.                 {
  15.                     BlockReference br = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
  16.                     if (br != null && br.Name == "nested") // replace "nested" withe the nested block name
  17.                     {
  18.                         foreach (ObjectId attId in br.AttributeCollection)
  19.                         {
  20.                             AttributeReference att =
  21.                                 (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
  22.                             if (att.Tag == "TAG") // replace "TAG" with the attribute tag
  23.                             {
  24.                                 att.UpgradeOpen();
  25.                                 att.TextString = "foo"; // repace "foo" with the new attribute value
  26.                                 break;
  27.                             }
  28.                         }
  29.                         break;
  30.                     }
  31.                 }
  32.                 tr.Commit();
  33.             }
  34.             ed.Regen();
  35.         }
Speaking English as a French Frog