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(LayoutAndViewports.MyCommands))]
namespace LayoutAndViewports
{
public class MyCommands
{
[CommandMethod("all")]
public void Create()
{
NewLayout();
CreateVieport();
}
[CommandMethod("createEmptyLayout")]
public void NewLayout()
{
string nameLayout = "MyLayout";
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
LayoutManager lm = LayoutManager.Current;
ObjectId layoutId = lm.CreateLayout(nameLayout);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Layout layout = tr.GetObject(layoutId, OpenMode.ForWrite) as Layout;
layout.Initialize();
tr.Commit();
}
lm.SetCurrentLayoutId(layoutId);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Layout layout = tr.GetObject(layoutId, OpenMode.ForRead) as Layout;
ObjectIdCollection vportIds = layout.GetViewports();
if (vportIds.Count > 1)
{
Viewport vp = tr.GetObject(vportIds[1], OpenMode.ForWrite) as Viewport;
vp.Erase();
}
tr.Commit();
}
}
[CommandMethod("createVP")]
public void CreateVieport()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
doc.Editor.SwitchToPaperSpace();
Application.SetSystemVariable("TILEMODE", 0);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId,
OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.PaperSpace],
OpenMode.ForWrite) as BlockTableRecord;
Viewport vport = new Viewport();
btr.AppendEntity(vport);
vport.On = true;
vport.GridIncrement = new Vector2d(100, 100);
vport.GridOn = true;
vport.GridMajor = 10;
vport.CenterPoint = new Point3d(100, 100, 0);
vport.Width = 200;
vport.Height = 200;
vport.CustomScale = 2;
vport.ViewTarget = new Point3d(1000, 1000, 0);
vport.ViewCenter = new Point2d(0, 0);
tr.AddNewlyCreatedDBObject(vport, true);
Circle circ = new Circle();
circ.Center = vport.CenterPoint;
circ.Radius = 100;
btr.AppendEntity(circ);
tr.AddNewlyCreatedDBObject(circ, true);
vport.NonRectClipEntityId = circ.ObjectId;
vport.NonRectClipOn = true;
tr.Commit();
}
}
}
}