// (C) Copyright 2020 by
//
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Reflection;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(ZoomUtils.Zoom))]
namespace ZoomUtils
{
public class Zoom
{
public static void ZoomWindow(Point3d p1, Point3d p2)
{
object acad = Application.AcadApplication;
object[] pts = { p1.ToArray(), p2.ToArray() };
acad.GetType().InvokeMember("ZoomWindow", BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.Instance | BindingFlags.InvokeMethod, null, acad, pts);
}
public static void ZoomExtents()
{
object acad = Application.AcadApplication;
acad.GetType().InvokeMember("ZoomExtents", BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.Instance | BindingFlags.InvokeMethod, null, acad, null);
}
public static void ZoomScaled(double scale, int zoomScaleType)
{
// zoomScaleType :
// acZoomScaledAbsolute = 0
// acZoomScaledRelative = 1
// acZoomScaledRelativePSpace = 2
object acad = Application.AcadApplication;
acad.GetType().InvokeMember("ZoomScaled", BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.Instance | BindingFlags.InvokeMethod, null, acad, new object[] { scale, zoomScaleType });
}
public static void ZoomCenter(Point3d pt, double magnitude)
{
object acad = Application.AcadApplication;
acad.GetType().InvokeMember("ZoomCenter", BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.Instance | BindingFlags.InvokeMethod, null, acad, new object[] { pt.ToArray(), magnitude });
}
static public void ZoomObject(ObjectId id, double zoomFactor)
{
Database db = HostApplicationServices.WorkingDatabase;
Extents3d ext;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity en = (Entity)tr.GetObject(id, OpenMode.ForRead);
ext = en.GeometricExtents;
}
Vector3d v = new Vector3d((ext.MinPoint - ext.MaxPoint).ToArray());
ZoomWindow(ext.MinPoint - v * 100.0 / zoomFactor, ext.MaxPoint + v * 100.0 / zoomFactor);
}
}
}