using System;
using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsSystem;
using Autodesk.AutoCAD.Internal;
public static void ZoomToExtents()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = doc.Editor;
using (DocumentLock docloc = doc.LockDocument())
{
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
Point3d min = (Point3d)Application.GetSystemVariable("EXTMIN");
Point3d max = (Point3d)Application.GetSystemVariable("EXTMAX");
int CvId = Convert.ToInt32((Application.GetSystemVariable("CVPORT")));
using (Manager gm = doc.GraphicsManager)
using (View vw = gm.GetGsView(CvId, true))
{
vw.ZoomExtents(min, max);
vw.Zoom(0.95); //optional
gm.SetViewportFromView(CvId, vw, true, true, false);
}
trans.Commit();
}
}
}
Shhh!!!!!
Do not tell or show anyone this is but, uses COM and at least does not tie you down to year and can be reused
Code - C#: [Select]
public static void ZoomExtents(this Document doc) { dynamic acadapp = Application.AcadApplication; acadapp.ZoomExtents(); }
Not so much here but there are interop COM haters out there that do not like any COM but with dynamics your not tied to release and i should throw in a SendStringToExecute() example.Shhh!!!!!
Do not tell or show anyone this is but, uses COM and at least does not tie you down to year and can be reused
Code - C#: [Select]
public static void ZoomExtents(this Document doc) { dynamic acadapp = Application.AcadApplication; acadapp.ZoomExtents(); }
:) I should add: I haven't really kept up with things. Thanks Jeff!
private static void ZoomToPart(ObjectId GID, Transaction trans, Document doc)
{
using (trans)
{
Group EntGrpDic = (Group)trans.GetObject(GID, OpenMode.ForRead);
ObjectId[] Eids = EntGrpDic.GetAllEntityIds();
Extents3d exts =
Eids.Select(id => ((Entity)trans.GetObject(id, OpenMode.ForRead)).GeometricExtents).Aggregate(
(e1, e2) => { e1.AddExtents(e2); return e1; });
Point3d min = exts.MinPoint;
Point3d max = exts.MaxPoint;
int CvId = Convert.ToInt32((Application.GetSystemVariable("CVPORT")));
using (Manager gm = doc.GraphicsManager)
using (View vw = gm.GetGsView(CvId, true))
{
vw.ZoomExtents(min, max);
vw.Zoom(0.95);
gm.SetViewportFromView(CvId, vw, true, true, false);
}
trans.Commit();
}
}
Public Shared Sub ZoomExtents(ByVal ed As Editor, ByVal db As Database, ByVal doc As Document)
Using view As ViewTableRecord = ed.GetCurrentView()
Using doclock As DocumentLock = doc.LockDocument
Dim extents As New Extents3d(db.Extmin, db.Extmax)
Dim WCS2DCS As Matrix3d = Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) * Matrix3d.Displacement(view.Target - Point3d.Origin) * Matrix3d.PlaneToWorld(view.ViewDirection)
extents.TransformBy(WCS2DCS.Inverse())
Dim center As New Point2d((extents.MinPoint.X + extents.MaxPoint.X) / 2.0, (extents.MinPoint.Y + extents.MaxPoint.Y) / 2.0)
view.Height = (extents.MaxPoint.Y - extents.MinPoint.Y)
view.Width = (extents.MaxPoint.X - extents.MinPoint.X)
view.CenterPoint = center
ed.SetCurrentView(view)
db.UpdateExt(True)
End Using
End Using
End Sub
<CommandMethod("CreateImage")> _
Public Sub Main()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument()
Dim ed As Editor = doc.Editor
Dim docWindow As Window = doc.Window
Dim db As Database = doc.Database
docWindow.WindowState = Window.State.Normal
Dim newSize As System.Drawing.Size = New System.Drawing.Size(950, 573) 'was 608
docWindow.SetSize(newSize)
Using docLoc As DocumentLock = doc.LockDocument
db.UpdateExt(True)
Dim ViewTableRecord As New ViewTableRecord
Dim MaxPoint As Point3d = db.Extmax
Dim MinPoint As Point3d = db.Extmin
Dim MaxPoint2D As Point2d = New Point2d(MaxPoint.X, MaxPoint.Y)
Dim MinPoint2D As Point2d = New Point2d(MinPoint.X, MinPoint.Y)
ViewTableRecord.CenterPoint = MinPoint2D + (MaxPoint2D - MinPoint2D) * 0.5
ViewTableRecord.Height = MaxPoint2D.Y - MinPoint2D.Y
ViewTableRecord.Width = MaxPoint2D.X - MinPoint2D.X
ed.SetCurrentView(ViewTableRecord)
End Using
End Sub
/// <summary> manipulates the Zoom method to emulate the traditional Zoom Extents command </summary>
public static void ZoomExtents()
{
Application.ShowAlertDialog("Begin Command"); // for testing
// call Zoom method with undefined MinPt & MaxPt, origin as the center point, and a non-zero veiwport scale factor
Zoom(new Point3d(), new Point3d(), Point3d.Origin, 1.02);
Application.ShowAlertDialog("End Command"); // for testing
}
/// <summary> this method is used as the base method for all variations of the traditional Zoom command </summary>
/// <param name="p3dMin"> the preferred lower left corner of the view, if known </param>
/// <param name="p3dMax"> the preferred upper right corner of the view, if known </param>
/// <param name="p3dCenter"> the view center point, if known </param>
/// <param name="dblFactor"> the final scale factor of the view </param>
public static void Zoom(Point3d p3dMin, Point3d p3dMax, Point3d p3dCenter, double dblFactor)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
int intCurVport = Convert.ToInt32(Application.GetSystemVariable("CVPORT"));
// get the extents of the current space; no points or only a center point is provided; check to see if ModelSpace is current
if (db.TileMode)
if ( p3dMin.Equals(new Point3d()) && p3dMax.Equals(new Point3d()) ) // if all are true
{
p3dMin = db.Extmin;
p3dMax = db.Extmax;
}
else
// check to see if PaperSpace is current
if (intCurVport == 1)
// get the extents of PaperSpace
if ( p3dMin.Equals(new Point3d()) && p3dMax.Equals(new Point3d()) ) // if all are true
{
p3dMin = db.Pextmin;
p3dMax = db.Pextmax;
}
else
// get the extents of Model space
if ( p3dMin.Equals(new Point3d()) && p3dMax.Equals(new Point3d()) ) // if all are true
{
p3dMin = db.Extmin;
p3dMax = db.Extmax;
}
// start a transaction
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// get the current view
using (ViewTableRecord vtr = doc.Editor.GetCurrentView())
{
Extents3d ext3d;
// translate world coordinate system (WCS) to display coordinate system (DCS)
Matrix3d mat3dWcsToDcs;
mat3dWcsToDcs = Matrix3d.PlaneToWorld(vtr.ViewDirection);
mat3dWcsToDcs = Matrix3d.Displacement(vtr.Target - Point3d.Origin) * mat3dWcsToDcs;
mat3dWcsToDcs = Matrix3d.Rotation(-vtr.ViewTwist, vtr.ViewDirection, vtr.Target) * mat3dWcsToDcs;
// if a center point is specified, define the min and max point of the extents for Center and Scale modes
if (p3dCenter.DistanceTo(Point3d.Origin) != 0)
{
p3dMin = new Point3d(p3dCenter.X - (vtr.Width / 2), p3dCenter.Y - (vtr.Height / 2), 0);
p3dMax = new Point3d((vtr.Width / 2) + p3dCenter.X, (vtr.Height / 2) + p3dCenter.Y, 0);
}
ext3d = new Extents3d(p3dMin, p3dMax);
// calculate the ratio between the width and height of the current view
double dblViewRatio;
dblViewRatio = (vtr.Width / vtr.Height);
// tranform the extents of the view
mat3dWcsToDcs = mat3dWcsToDcs.Inverse();
ext3d.TransformBy(mat3dWcsToDcs);
double dblWidth;
double dblHeight;
Point2d p2dNewCentPt;
// check to see if a center point was provided (Center and Scale modes)
if (p3dCenter.DistanceTo(Point3d.Origin) != 0)
{
dblWidth = vtr.Width;
dblHeight = vtr.Height;
if (dblFactor == 0)
p3dCenter = p3dCenter.TransformBy(mat3dWcsToDcs);
p2dNewCentPt = new Point2d(p3dCenter.X, p3dCenter.Y);
}
else // working in Window, Extents and Limits mode
{
// calculate the new width and height of the current view
dblWidth = ext3d.MaxPoint.X - ext3d.MinPoint.X;
dblHeight = ext3d.MaxPoint.Y - ext3d.MinPoint.Y;
// get the center of the view
p2dNewCentPt = new Point2d(((ext3d.MaxPoint.X + ext3d.MinPoint.X) * 0.5), ((ext3d.MaxPoint.Y + ext3d.MinPoint.Y) * 0.5));
}
// check to see if the new width fits in current window
if (dblWidth > (dblHeight * dblViewRatio))
dblHeight = dblWidth / dblViewRatio;
// resize and scale the view
if (dblFactor != 0)
{
vtr.Height = dblHeight * dblFactor;
vtr.Width = dblWidth * dblFactor;
}
// set the center of the view
vtr.CenterPoint = p2dNewCentPt;
// set the current view
doc.Editor.SetCurrentView(vtr);
}
// commit the changes
trans.Commit();
}
}
... Alexander.Rivilis responded with some excellent info and mentioned that the drawing window doesn't actually resize until after the command executes...Note. I wrote "As far as I remember the actual window resizing occurs only after the command completes".