Kean Walmsley


  • About the Author
    Kean on Google+

July 2014

Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    








« Swapping identities of AutoCAD objects using .NET | Main | Adding and removing AutoCAD layer filters using .NET »

July 09, 2008

Testing whether an AutoCAD drawing is 2D or 3D using .NET

This post demonstrates a simple check for whether a drawing is two or three dimensional. The code is almost embarrassingly simple, but then the question is significant and in the absence of a "Is3D" property on the Database object this is likely to prove useful for people.

So how do we check whether a drawing is 3D? The quick answer is that in most circumstances the EXTMAX system variable will have a non-zero Z value for a 3D drawing. There are potential situations where this might not be true (and EXTMAX doesn't reflect the 3D nature of certain geometry), but given the likelihood that any real-world 3D model includes a variety of geometry, it's pretty safe to rely upon. The alternative is to iterate through and test geometry, but checking EXTMAX is quicker, by far, and the alternative should only by needed if you find a particular scenario that EXTMAX doesn't address.

Here's some C# code that tells us whether a drawing is 2D or 3D:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.Runtime;


namespace NumberOfDimensions

{

  public class Commands

  {

    [CommandMethod("IS3D")]

    static public void CheckWhether3D()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;


      ed.WriteMessage(

        "\nDrawing is {0}.",

        (IsDrawing3D(db) ? "3D" : "2D")

      );

    }


    private static bool IsDrawing3D(Database db)

    {

      return (db.Extmax.Z > Tolerance.Global.EqualPoint);

    }

  }

}

Here's what happens when we call the IS3D command on a fresh drawing, after we've drawn a 2D line and then after we've drawn a tiny sphere:

Command: IS3D

Drawing is 2D.

Command: LINE

Specify first point: 0,0,0

Specify next point or [Undo]: @10,10

Specify next point or [Undo]:

Command: IS3D

Drawing is 2D.

Command: SPHERE

Specify center point or [3P/2P/Ttr]: 0,0,0

Specify radius or [Diameter]: 0.0001

Command: IS3D

Drawing is 3D.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200e553ac41208834

Listed below are links to weblogs that reference Testing whether an AutoCAD drawing is 2D or 3D using .NET:

blog comments powered by Disqus

10 Random Posts