using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(Rivilis.ZoomExtDb))]
namespace Rivilis
{
public class ZoomExtDb
{
[CommandMethod("ZoomExtDB")]
public void zoomdb()
{
Microsoft.Win32.OpenFileDialog openFileDialog =
new Microsoft.Win32.OpenFileDialog();
openFileDialog.Title =
"Выберите dwg-файл";
openFileDialog.Filter = "dwg-файлы|*.dwg";
bool? bClickedOK = openFileDialog.ShowDialog();
if (!bClickedOK.HasValue || !bClickedOK.Value) return;
using (Database db = new Database(false, false)) {
db.ReadDwgFile(openFileDialog.FileName, FileOpenMode.OpenForReadAndReadShare, true, null);
Database prevDb = HostApplicationServices.WorkingDatabase;
HostApplicationServices.WorkingDatabase = db;
db.UpdateExt(true);
using (ViewportTable vTab = db.ViewportTableId.Open(OpenMode.ForRead) as ViewportTable) {
ObjectId acVptId = vTab["*Active"];
using (ViewportTableRecord vpTabRec = acVptId.Open(OpenMode.ForWrite) as ViewportTableRecord) {
double scrRatio = (vpTabRec.Width / vpTabRec.Height);
Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(vpTabRec.ViewDirection);
matWCS2DCS = Matrix3d.Displacement(vpTabRec.Target - Point3d.Origin) * matWCS2DCS;
matWCS2DCS = Matrix3d.Rotation(-vpTabRec.ViewTwist,
vpTabRec.ViewDirection,
vpTabRec.Target)
* matWCS2DCS;
matWCS2DCS = matWCS2DCS.Inverse();
Extents3d extents = new Extents3d(db.Extmin, db.Extmax);
extents.TransformBy(matWCS2DCS);
double width = (extents.MaxPoint.X - extents.MinPoint.X);
double height = (extents.MaxPoint.Y - extents.MinPoint.Y);
Point2d center = new Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5,
(extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5);
if (width > (height * scrRatio))
height = width / scrRatio;
vpTabRec.Height = height;
vpTabRec.Width = height * scrRatio;
vpTabRec.CenterPoint = center;
}
}
HostApplicationServices.WorkingDatabase = prevDb;
db.SaveAs(openFileDialog.FileName.Substring(0,openFileDialog.FileName.Length-4) +
"_zoomExts.dwg", DwgVersion.Current);
}
}
}
}