Author Topic: Get layout name while in its BlockTableRecord object  (Read 1380 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Get layout name while in its BlockTableRecord object
« on: April 13, 2016, 03:34:20 AM »
Hello,

I am trying to print all object names in Model space and layouts (object name + layer + layout name) to command line but I am facing a problem with the layout name.
Can anyone tell me how to get the layout name while I am in its space ?.

Thanks in advance.

Code - C#: [Select]
  1.             using (Transaction tr = db.TransactionManager.StartTransaction())
  2.             {
  3.                 BlockTable blks = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  4.                 foreach (var item in blks)
  5.                 {
  6.                     BlockTableRecord rec = (BlockTableRecord)tr.GetObject(item, OpenMode.ForRead);
  7.                     if (rec.IsLayout)
  8.                     {
  9.                         Layout lay = (Layout)tr.GetObject(rec.ObjectId, OpenMode.ForRead);                        
  10.                         foreach (var obj in rec)
  11.                         {
  12.                             ent = (Entity)tr.GetObject(obj, OpenMode.ForRead);
  13.                             ed.WriteMessage("\nName: (" + ent.GetType().Name + ") Layer: (" + ent.Layer + ") Space: (" + lay.LayoutName + ").");
  14.                             ed.WriteMessage("\n");
  15.                         }
  16.                     }
  17.                 }
  18.             }

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Get layout name while in its BlockTableRecord object
« Reply #1 on: April 13, 2016, 03:54:04 AM »
Hi,

Juste replace:
Code - C#: [Select]
  1. Layout lay = (Layout)tr.GetObject(rec.ObjectId, OpenMode.ForRead);

with:
Code - C#: [Select]
  1. Layout lay = (Layout)tr.GetObject(rec.LayoutId, OpenMode.ForRead);
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Get layout name while in its BlockTableRecord object
« Reply #2 on: April 13, 2016, 04:13:10 AM »
Thank you gile , that works great.

After running the program successfully the program printed (viewport) two times and I am sure I have only one viewport in each layout, What is wrong with that?

Thanks

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Get layout name while in its BlockTableRecord object
« Reply #3 on: April 13, 2016, 05:38:56 AM »
Nothing wrong, each layout has its own viewport which Number property = 1 (CVPORT sysvar).
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Get layout name while in its BlockTableRecord object
« Reply #4 on: April 13, 2016, 06:47:47 AM »
Many thanks gile for your help.