using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Autodesk namespaces ***************
using acad = Autodesk.AutoCAD.ApplicationServices.Application;
using AcApp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcRtm = Autodesk.AutoCAD.Runtime;
using AcPub = Autodesk.AutoCAD.Publishing;
using AcPlt = Autodesk.AutoCAD.PlottingServices;
using AcGem = Autodesk.AutoCAD.Geometry;
using AcCol = Autodesk.AutoCAD.Colors;
using AcCMod = Autodesk.AutoCAD.ComponentModel;
using AcGInt = Autodesk.AutoCAD.GraphicsInterface;
using AcGSys = Autodesk.AutoCAD.GraphicsSystem;
using AcLayMng = Autodesk.AutoCAD.LayerManager;
using AcWin = Autodesk.AutoCAD.Windows;
using AcComp = ACSMCOMPONENTS17Lib;
//************************************
[assembly
: AcRtm
.CommandClass(typeof(Xany
.AutoCAD.SheetSets.Tools))]
namespace Xany.AutoCAD.SheetSets {
public class Tools {
[AcRtm.CommandMethod("CreateNamedViewsByLayoutViewports")]
public void CreateNamedViewsByLayoutViewports() {
AcApp.Document doc = acad.DocumentManager.MdiActiveDocument;
AcDb.Database db = doc.Database;
AcEd.Editor ed = doc.Editor;
using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) {
AcDb.BlockTable bt = (AcDb.BlockTable)tr.GetObject(db.BlockTableId, AcDb.OpenMode.ForRead);
AcDb.ObjectId msId = bt[AcDb.BlockTableRecord.ModelSpace];
AcDb.ObjectId psId = bt[AcDb.BlockTableRecord.PaperSpace];
Boolean modalSpaceIsCurrent = ((Int16) acad.GetSystemVariable("TILEMODE")) == 1 ? true : false;
if (modalSpaceIsCurrent) {
ed.WriteMessage("Command canceled. Reason: MODEL tab is current.\n");
return;
}
//Boolean viewPortIsCurrent = !modalSpaceIsCurrent && ((Int16)acad.GetSystemVariable("CVPORT")) != 1 ? true : false;
//if (viewPortIsCurrent) {
// ed.WriteMessage("Coming back to paper from viewport...\n");
// ed.SwitchToPaperSpace();
//}
AcDb.LayoutManager layMng = AcDb.LayoutManager.Current;
AcDb.ObjectId layoutId = layMng.GetLayoutId(layMng.CurrentLayout);
//Current Layout
AcDb.Layout layout = (AcDb.Layout)tr.GetObject(layoutId, AcDb.OpenMode.ForRead);
AcDb.ObjectIdCollection ids = layout.GetViewports();
AcDb.ViewTable vt = (AcDb.ViewTable) tr.GetObject(db.ViewTableId, AcDb.OpenMode.ForWrite);
foreach (AcDb.ObjectId id in ids) {
AcDb.Viewport viewport = (AcDb.Viewport) tr.GetObject(id, AcDb.OpenMode.ForRead);
if (viewport.Number != 1) {
//Get viewport data:
Double width = viewport.Width;
Double height = viewport.Height;
Double scale = viewport.CustomScale;
AcGem.Point2d point = viewport.ViewCenter;
//Creating the new ViewTableRecord based on Viewport data, or opening existing.
String vtrName = "view-" + viewport.Number;
Boolean vtHasView = vt.Has(vtrName);
AcDb.ViewTableRecord vtr = vtHasView ? (AcDb.ViewTableRecord)tr.GetObject(vt[vtrName],
AcDb
.OpenMode.ForWrite) : new AcDb
.ViewTableRecord();
//How can I create Category Name? Next row ain't working (I get error);
//vtr.CategoryName = "Auto generated view";
//I get correct CenterPoint
vtr.CenterPoint = point;
//But I get incorrect width and height. Why?
vtr.Width = width * scale;
vtr.Height = height * scale;
//How can I use it?
//vtr.ViewAssociatedToViewport = true;
if (!vtHasView) {
vtr.Name = vtrName;
vt.Add(vtr);
tr.AddNewlyCreatedDBObject(vtr, true);
}
}
}
tr.Commit();//Saving all changes.
}
}
}
}