Getting the extents of AutoCAD model window

By Philippe Leefsma

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();

    }

<

p style=”line-height: normal;margin: 0in 0in 0pt” class=”MsoNormal”>}


Comments

2 responses to “Getting the extents of AutoCAD model window”

  1. Steven Taitinger Avatar
    Steven Taitinger

    How do you get the size of the extents of model space in vba? The limits property of the document seems more related to the paper space than the model space and the width and height properties seem to be relative to the application window size.

  2. Hi Steven,
    The easiest way is probably to use EXTMIN/EXTMAX system variables:
    Dim Extmin As Variant
    Dim Extmax As Variant
    Extmin = ThisDrawing.GetVariable(“EXTMIN”)
    Extmax = ThisDrawing.GetVariable(“EXTMAX”)
    Debug.Print “ExtMIN = [” & Extmin(0) & “, “; Extmin(1) & “, “; Extmin(2) & “]”
    Debug.Print “ExtMAX = [” & Extmax(0) & “, “; Extmax(1) & “, “; Extmax(2) & “]”

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading