Several developers have been asking this question recently through ADN support, on how to retrieve the extents of the AutoCAD model window in world coordinates.
There are several approaches to achieve that, but the easiest according to me, is to rely on the “SCREENSIZE” system variable.
Here is the C# code:
[CommandMethod("ScreenExtents")]
public void ScreenExtents()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Point2d screenSize = (Point2d)
Application.GetSystemVariable("SCREENSIZE");
System.Drawing.Point upperLeft = new System.Drawing.Point(0, 0);
System.Drawing.Point lowerRight = new System.Drawing.Point(
(int)screenSize.X,
(int)screenSize.Y);
Point3d upperLeftWorld = ed.PointToWorld(upperLeft, 0);
Point3d lowerRightWorld = ed.PointToWorld(lowerRight, 0);
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
//Draws a line to visualize result...
Line line = new Line(upperLeftWorld, lowerRightWorld);
BlockTableRecord btr = Tx.GetObject(
db.CurrentSpaceId, OpenMode.ForWrite)
as BlockTableRecord;
btr.AppendEntity(line);
Tx.AddNewlyCreatedDBObject(line, true);
Tx.Commit();
}
}