Any ideas on compacting this, or making it simpler . . . I'm a little brain dead.
// (C) Copyright 2022 CodeHimBelonga: kdub 2022/05/10
//
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Kdub.Common;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
[assembly
: CommandClass
(typeof(LayoutClass
.LayoutClass))]
namespace LayoutClass
{
public class LayoutClass
{
[CommandMethod("MakeLayout")]
public static void MakeLayout()
{
Layout layout = CreateLayout("Layout3");
Active.WriteMessage("\nTab Order: "
+ layout.TabOrder
+ "\nTab Selected: "
+ layout.TabSelected
+ "\nBlock Table Record ID: "
+ layout.BlockTableRecordId.ToString());
}
private static Layout CreateLayout(string layoutName)
{
Layout layout
= new Layout
();
using (Transaction transaction = Active.Database
.TransactionManager.StartTransaction())
{
LayoutManager layoutManager = LayoutManager.Current;
// Check if the Layout doesn't exists : create or get !
if (!layoutManager.LayoutExists(layoutName))
{
layout = (Layout)transaction.GetObject(
layoutManager.CreateLayout(layoutName), OpenMode.ForRead);
}
else
{
layout = (Layout)transaction.GetObject(
layoutManager.GetLayoutId(layoutName), OpenMode.ForRead);
}
// ensure the layout is current
if (!layout.TabSelected)
{
layoutManager.CurrentLayout = layout.LayoutName;
}
transaction.Commit();
}
return layout;
}
}
}
Common Stuff: . . . showing the Active Class ; to save explaining it.
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
namespace Kdub.Common
{
public class Active
{
public static Document Document => Application.DocumentManager.MdiActiveDocument;
public static Editor Editor => Document.Editor;
public static Database Database => Document.Database;
public static void UsingTransaction(Action<Transaction> action)
{
using (var transaction = Active.Database.TransactionManager.StartTransaction())
{
action(transaction);
transaction.Commit();
}
}
/// <summary>
/// Sends a string to the command line in the active Editor
/// </summary>
/// <param name="message">The message to send.</param>
public static void WriteMessage(string message)
{
Editor.WriteMessage(message);
}
/// <summary>
/// Sends a string to the command line in the active Editor using String.Format.
/// </summary>
/// <param name="message">The message containing format specifications.</param>
/// <param name="parameter">The variables to substitute into the format string.</param>
public static void WriteMessage(string message, params object[] parameter)
{
Editor.WriteMessage(message, parameter);
}
}
}