Author Topic: Get the layout an entity belongs to  (Read 663 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Get the layout an entity belongs to
« on: June 22, 2012, 07:26:01 PM »
Code: [Select]
  // based on code from:
  // http://adndevblog.typepad.com/autocad/2012/06/get-the-layout-an-entity-belongs-to.html
  [CommandMethod("EntityLayoutName","enl", CommandFlags.Modal)]
        static public void GetEntityLayout()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptEntityOptions peo = new PromptEntityOptions("\nSelect object >>");

                PromptEntityResult res;

                res = ed.GetEntity(peo);

                if (res.Status != PromptStatus.OK)
                    return;

                Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);

                if (ent == null)
                    return;

                BlockTableRecord btr = tr.GetObject(ent.OwnerId, OpenMode.ForRead, true) as BlockTableRecord;

                Layout lt = tr.GetObject(btr.LayoutId, OpenMode.ForRead, true) as Layout;

                string layoutName = lt.LayoutName;

                ed.WriteMessage("\nThe layout the selected entity belongs to is {0}", layoutName);

            }
        }