using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
// This line is not mandatory, but improves loading performances
[assembly
: CommandClass
(typeof(AutoCAD_CSharp_plug_in1
.MyCommands))]
namespace AutoCAD_CSharp_plug_in1
{
public class MyCommands
{
[CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
public void MyCommand() // This method can have any name
{
//layout to be skipped variables
int pageNumber = 1;
string layouttobeskipped = "";
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
ObjectIdCollection ids
= new ObjectIdCollection
(); using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//Get ID of all layers for later freezing
LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (ObjectId item in acLyrTbl)
{
ids.Add(item);
}
//Get layout dictionary
DBDictionary dLayouts = acTrans.GetObject(acCurDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
//Add New layouts for example testing
LayoutManager LM = LayoutManager.Current;
if (!dLayouts.Contains("New Layout"))
{
LM.CreateLayout("New Layout");
LM.CreateLayout("New Layout2");
}
else
{
if (dLayouts.Contains("New Layout2"))
{
LM.CreateLayout("New Layout3");
LM.CreateLayout("New Layout4");
}
}
//Add a viewport to each layout
foreach (DBDictionaryEntry item in dLayouts)
{
Layout la = acTrans.GetObject(item.Value, OpenMode.ForRead) as Layout;
//Gets name of layout to be skipped for later layer freezing
if (la.TabOrder == pageNumber)
{
layouttobeskipped = la.LayoutName;
}
if (la.LayoutName != "Model")
{
//Add New Viewport
acTrans.addviewport(la, acCurDb);
la.Dispose();
}
}
//Sets layout to be skipped
Layout laSKIP = acTrans.GetObject(LM.GetLayoutId(layouttobeskipped), OpenMode.ForRead) as Layout;
ObjectId BTRIDLayoutskipped = laSKIP.BlockTableRecordId;
//Cycle through database.viewport collection
ObjectIdCollection IDsVP = acCurDb.GetViewports(false);
foreach (ObjectId id in IDsVP)
{
Viewport vp = acTrans.GetObject(id, OpenMode.ForWrite) as Viewport;
//Ignore layoutskipped
if (vp.BlockId != BTRIDLayoutskipped)
{
vp.FreezeLayersInViewport(ids.GetEnumerator());
vp.UpdateDisplay();
}
vp.Dispose();
}
acDoc.Editor.Regen();
LM.Dispose();
acTrans.Commit();
}
}
}
public static class myExtensions
{
public static void addviewport(this Transaction acTrans, Layout la, Database acCurDb)
{
using (LayoutManager LM = LayoutManager.Current)
{
//Get database.viewports
ObjectIdCollection DBVPids = acCurDb.GetViewports(true);
if (DBVPids.Count == 0)
{
//No initialised layouts - Make first layout current
//viewport will be created - though need to check variable setting
LM.CurrentLayout = la.LayoutName;
}
//Check layout for viewports and clone if exists
ObjectIdCollection IDsVP = la.GetViewports();
Viewport VpNew
= new Viewport
(); if (IDsVP.Count != 0 && IDsVP.Count != 1)
{
Viewport vp = acTrans.GetObject(IDsVP[1], OpenMode.ForRead) as Viewport;
ObjectIdCollection ids
= new ObjectIdCollection
(); ids.Add(vp.ObjectId);
IdMapping IDMAP
= new IdMapping
(); acCurDb.DeepCloneObjects(ids, la.BlockTableRecordId, IDMAP, false);
ObjectIdCollection IDsVP2 = la.GetViewports();
VpNew = acTrans.GetObject(IDsVP2[IDsVP.Count], OpenMode.ForWrite) as Viewport;
}
if (IDsVP.Count <= 1)
{
if (IDsVP.Count == 0)
{
//Layout not initialized
la.UpgradeOpen();
la.Initialize();
la.DowngradeOpen();
}
//No viewports but layout initialized
//Find a viewport and clone it in
DBVPids = acCurDb.GetViewports(false);
ObjectIdCollection ids
= new ObjectIdCollection
(); ids.Add(DBVPids[1]);
IdMapping IDMAP
= new IdMapping
(); acCurDb.DeepCloneObjects(ids, la.BlockTableRecordId, IDMAP, false);
IDsVP = la.GetViewports();
VpNew = acTrans.GetObject(IDsVP[1], OpenMode.ForWrite) as Viewport;
}
//Example Viewport Settings
VpNew
.CenterPoint = new Point3d
(100,
100,
0); VpNew.Width = 500;
VpNew.Height = 100;
VpNew.UpdateDisplay();
VpNew.Dispose();
}
}
}
}