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(ZoomViewport.Utils))]
namespace ZoomViewport
{
public class Utils
{
[CommandMethod("TestZoom")]
public void TestZoom()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptSelectionResult rs = ed.GetSelection();
if (rs.Status != PromptStatus.OK) return;
ObjectIdCollection ids = new ObjectIdCollection(rs.Value.GetObjectIds());
test(ref ids);
}
public static void test(ref ObjectIdCollection ids)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
Extents3d ext = new Extents3d();
foreach (ObjectId id in ids)
{
var ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
if (ent != null)
{
ext.AddExtents(ent.GeometricExtents);
}
}
LayoutManager layman = LayoutManager.Current;
DBDictionary layoutDic = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
double mScrRatio;
// Проходимся по всем листам (модель пропускаем)
foreach (DBDictionaryEntry entry in layoutDic)
{
Layout layout = tr.GetObject(entry.Value, OpenMode.ForRead) as Layout;
if (!layout.ModelType) // Это не модель
{
layman.CurrentLayout = layout.LayoutName;
ObjectIdCollection idsVports = layout.GetViewports();
// Проходимся по всем видовым экранам (кроме основного видового экрана листа)
for (int i = 1; i < idsVports.Count; i++)
{
Viewport vp = (Viewport)tr.GetObject(idsVports[i], OpenMode.ForRead);
mScrRatio = (vp.Width / vp.Height);
// prepare Matrix for DCS to WCS transformation
Matrix3d matWCS2DCS;
matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection);
matWCS2DCS = Matrix3d.Displacement(vp.ViewTarget - Point3d.Origin) * matWCS2DCS;
matWCS2DCS = Matrix3d.Rotation(-vp.TwistAngle, vp.ViewDirection, vp.ViewTarget) * matWCS2DCS;
matWCS2DCS = matWCS2DCS.Inverse();
ext.TransformBy(matWCS2DCS);
// width of the extents in current view
double mWidth;
mWidth = (ext.MaxPoint.X - ext.MinPoint.X);
// height of the extents in current view
double mHeight;
mHeight = (ext.MaxPoint.Y - ext.MinPoint.Y);
// get the view center point
Point2d mCentPt = new Point2d(
((ext.MaxPoint.X + ext.MinPoint.X) * 0.5),
((ext.MaxPoint.Y + ext.MinPoint.Y) * 0.5));
vp.UpgradeOpen();
if (mWidth > (mHeight * mScrRatio)) mHeight = mWidth / mScrRatio;
vp.ViewHeight = mHeight * 1.01;
// set the view center
vp.ViewCenter = mCentPt;
vp.Visible = true;
vp.On = true;
vp.UpdateDisplay();
doc.Editor.SwitchToModelSpace();
Application.SetSystemVariable("CVPORT", vp.Number);
}
}
}
tr.Commit();
}
}
}
}