TheSwamp

Code Red => .NET => Topic started by: Coder on April 13, 2016, 03:34:20 AM

Title: Get layout name while in its BlockTableRecord object
Post by: Coder 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.             }
Title: Re: Get layout name while in its BlockTableRecord object
Post by: gile 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);
Title: Re: Get layout name while in its BlockTableRecord object
Post by: Coder 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
Title: Re: Get layout name while in its BlockTableRecord object
Post by: gile on April 13, 2016, 05:38:56 AM
Nothing wrong, each layout has its own viewport which Number property = 1 (CVPORT sysvar).
Title: Re: Get layout name while in its BlockTableRecord object
Post by: Coder on April 13, 2016, 06:47:47 AM
Many thanks gile for your help.