Author Topic: Make or Get and Set Layout  (Read 1789 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • class keyThumper<T>:ILazy<T>
Make or Get and Set Layout
« on: May 10, 2022, 01:45:22 AM »
Any ideas on compacting this, or making it simpler . . . I'm a little brain dead.

Code - C#: [Select]
  1. // (C) Copyright 2022 CodeHimBelonga: kdub 2022/05/10
  2. //
  3. using Autodesk.AutoCAD.ApplicationServices;
  4. using Autodesk.AutoCAD.DatabaseServices;
  5. using Autodesk.AutoCAD.Runtime;
  6.  
  7. using Kdub.Common;
  8. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  9.  
  10. [assembly: CommandClass(typeof(LayoutClass.LayoutClass))]
  11.  
  12. namespace LayoutClass
  13. {
  14.     public class LayoutClass
  15.     {
  16.         [CommandMethod("MakeLayout")]
  17.         public static void MakeLayout()
  18.         {
  19.             Layout layout = CreateLayout("Layout3");
  20.  
  21.             Active.WriteMessage("\nTab Order: "
  22.                 + layout.TabOrder
  23.                 + "\nTab Selected: "
  24.                 + layout.TabSelected
  25.                 + "\nBlock Table Record ID: "
  26.                 + layout.BlockTableRecordId.ToString());
  27.         }
  28.  
  29.  
  30.         private static Layout CreateLayout(string layoutName)
  31.         {
  32.             Layout layout = new Layout();
  33.  
  34.             using (Transaction transaction = Active.Database
  35.                                             .TransactionManager.StartTransaction())
  36.             {
  37.                 LayoutManager layoutManager = LayoutManager.Current;
  38.  
  39.                 // Check if the Layout doesn't exists : create or get !
  40.                 if (!layoutManager.LayoutExists(layoutName))
  41.                 {
  42.                     layout = (Layout)transaction.GetObject(
  43.                                              layoutManager.CreateLayout(layoutName), OpenMode.ForRead);
  44.                 }
  45.                 else
  46.                 {
  47.                     layout = (Layout)transaction.GetObject(
  48.                                             layoutManager.GetLayoutId(layoutName), OpenMode.ForRead);
  49.                 }
  50.  
  51.                 // ensure the layout is current
  52.                 if (!layout.TabSelected)
  53.                 {
  54.                     layoutManager.CurrentLayout = layout.LayoutName;
  55.                 }
  56.                 transaction.Commit();
  57.             }
  58.             return layout;
  59.         }
  60.     }
  61. }
  62.  


Common Stuff:  . . . showing the Active Class  ; to save explaining it.

Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.EditorInput;
  7.  
  8.  
  9. namespace Kdub.Common
  10. {
  11.     public class Active
  12.     {
  13.         public static Document Document => Application.DocumentManager.MdiActiveDocument;
  14.         public static Editor Editor => Document.Editor;
  15.         public static Database Database => Document.Database;
  16.  
  17.         public static void UsingTransaction(Action<Transaction> action)
  18.         {
  19.             using (var transaction = Active.Database.TransactionManager.StartTransaction())
  20.             {
  21.                 action(transaction);
  22.  
  23.                 transaction.Commit();
  24.             }
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Sends a string to the command line in the active Editor
  29.         /// </summary>
  30.         /// <param name="message">The message to send.</param>
  31.         public static void WriteMessage(string message)
  32.         {
  33.             Editor.WriteMessage(message);
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Sends a string to the command line in the active Editor using String.Format.
  38.         /// </summary>
  39.         /// <param name="message">The message containing format specifications.</param>
  40.         /// <param name="parameter">The variables to substitute into the format string.</param>
  41.         public static void WriteMessage(string message, params object[] parameter)
  42.         {
  43.             Editor.WriteMessage(message, parameter);
  44.         }
  45.     }
  46. }
  47.  
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Make or Get and Set Layout
« Reply #1 on: May 10, 2022, 02:03:16 AM »
Hi,

There's no need of a transaction to create a layout and make it current.
Code - C#: [Select]
  1.         private static ObjectId CreateLayout(string layoutName)
  2.         {
  3.             ObjectId layoutId;
  4.             var layoutMgr = LayoutManager.Current;
  5.             if (layoutMgr.LayoutExists(layoutName))
  6.             {
  7.                 layoutId = layoutMgr.GetLayoutId(layoutName);
  8.             }
  9.             else
  10.             {
  11.                 layoutId = layoutMgr.CreateLayout(layoutName);
  12.             }
  13.             layoutMgr.CurrentLayout = layoutName;
  14.             return layoutId;
  15.         }
Speaking English as a French Frog

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • class keyThumper<T>:ILazy<T>
Re: Make or Get and Set Layout
« Reply #2 on: May 10, 2022, 02:06:30 AM »
Thanks Gile.
That will certainly clean it up :)

appreciated !!

added: I started playing with this because of a question at the other place where the Layout1 data was removed . . which I couldn't understand :)
« Last Edit: May 10, 2022, 02:09:42 AM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • class keyThumper<T>:ILazy<T>
Re: Make or Get and Set Layout
« Reply #3 on: May 10, 2022, 02:49:39 AM »
Updated :

Code - C#: [Select]
  1.  
  2. // (C) Copyright 2022 CodeHimBelonga: kdub 2022/05/10
  3. //
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.Runtime;
  7.  
  8. using Kdub.Common;
  9. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  10.  
  11. [assembly: CommandClass(typeof(LayoutClass.LayoutClass))]
  12.  
  13. namespace LayoutClass
  14. {
  15.     public class LayoutClass
  16.     {
  17.         [CommandMethod("MakeLayout")]
  18.         public static void MakeLayout()
  19.         {
  20.             ObjectId layoutId = CreateLayout("Layout3");            
  21.         }
  22.  
  23.         private static ObjectId CreateLayout(string layoutName)
  24.         {
  25.             ObjectId layoutId;
  26.             var layoutMgr = LayoutManager.Current;
  27.             if (layoutMgr.LayoutExists(layoutName))
  28.             {
  29.                 layoutId = layoutMgr.GetLayoutId(layoutName);
  30.             }
  31.             else
  32.             {
  33.                 layoutId = layoutMgr.CreateLayout(layoutName);
  34.             }
  35.             layoutMgr.CurrentLayout = layoutName;
  36.             return layoutId;
  37.         }
  38.     }
  39. }
  40.  
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.