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.

8 responses to “Testing whether an AutoCAD drawing is 2D or 3D using .NET”

  1. You mentioned "potential situations where this might not be true". I'm not sure what these situations are exactly and would love to know more. I am using RealDWG and I often have to iterate geometry to get extmax, which causes other problems (GeometricExtents being null?). So, what are these potential situations?

    Thanks in advance!

  2. Kean Walmsley Avatar

    Ah, I knew I'd called on that one... :-S

    I've checked in with Engineering to get some specifics. Some situations are related to planar entities (about which I'm waiting for information), but there is another related to entity erasure/display:

    "When entities that define the extents are erased, or their layer is frozen, then the EXTMIN and EXTMAX sysvars won't automatically move, at least in the 2D GS; that won’t happen until a ZOOM ALL/EXTENTS."

    It seems there are also issues with EXTMAX being updated when using the 3D GS - and this is more significant. If you set the Visual Style to conceptual (for instance) and create 3D geometry, EXTMIN/EXTMAX will not be updated to reflect the new extents.

    Regards,

    Kean

  3. Kean Walmsley Avatar

    On the subject of planar entities: it's just possible that someone is working in 2D, but on a 3D plane that is not aligned with the standard axes. EXTMAX would give a false positive at this point (although it seems a very rare situation, in my opinion).

    Regards,

    Kean

  4. outsourcing autocad Avatar
    outsourcing autocad

    well by looking into the code to see to it if the drawing is 2d or 3d is really a smart idea

  5. Hi Kean,

    How about checking the difference between the Z-Value of EXTMIN and EXTMAX? This might help in some situations.

    Regards,
    Marco

  6. Kean Walmsley Avatar

    Hi Marco,

    Yes, it might, although in the rare cases that the 2D plane is not aligned with the axes, this wouldn't help. But again - that's got to be a very rare case...

    Regards,

    Kean

  7. Hi kean I realise this is an old posting hope your still reading.

    How would you right this as a boolean statement for a contextual ribbon tab rule?

    Regards

    Rod

  8. Kean Walmsley Avatar

    Hi Rod,

    That's an interestng question.

    I'll add it to my list of possible topics - you might try posting to the AutoCAD .NET Discussion Group, in the meantime.

    Regards,

    Kean

Leave a Reply to Rod Cancel reply

Your email address will not be published. Required fields are marked *