November 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            










« Displaying a progress meter during long operations in AutoCAD using .NET | Main | Showing a splash-screen from your AutoCAD .NET application »

May 29, 2007

Getting the type of an AutoCAD solid using .NET

Now this may seem like a very trivial post, but this was actually a significant problem for my team when preparing the material for the Autodesk Component Technologies presentation we delivered at last year's DevDays tour.

Basically the "type" (or really "sub-type") of a Solid3d (an AcDb3DSolid in ObjectARX) is not exposed through ObjectARX and therefore neither is it exposed through the managed interface. It is possible to get at the information from C++ using the Brep API, but this is currently not available from .NET (and yes, we are aware that many of you would like to see this point addressed, although I can't commit to when it will happen).

But there is some good news: the property is exposed through COM, so we can simply get a COM interface for our solid and query the data through that. Thanks again for Fenton Webb, a member of DevTech Americas, for the tip of using COM rather than worrying about the Brep API.

Here's some C# code demonstrating the technique:

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Interop.Common;


namespace SolidTest

{

  public class Cmds

  {

    [CommandMethod("GST")]

    static public void GetSolidType()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;

      Database db = doc.Database;


      PromptEntityResult per =

        ed.GetEntity("\nSelect a 3D solid: ");


      if (per.Status == PromptStatus.OK)

      {

        Transaction tr =

          db.TransactionManager.StartTransaction();

        using (tr)

        {

          DBObject obj =

            tr.GetObject(per.ObjectId, OpenMode.ForRead);


          Solid3d sol = obj as Solid3d;

          if (sol != null)

          {

            Acad3DSolid oSol =

              (Acad3DSolid)sol.AcadObject;

            ed.WriteMessage(

              "\nSolid is of type \"{0}\"",

              oSol.SolidType

            );

          }

          tr.Commit();

        }

      }

    }

  }

}

And here's what we see when we run the GST command, selecting a number of Solid3d objects, one by one:

Command: GST

Select a 3D solid:

Solid is of type "Box"

Command:  GST

Select a 3D solid:

Solid is of type "Sphere"

Command:  GST

Select a 3D solid:

Solid is of type "Wedge"

Command:  GST

Select a 3D solid:

Solid is of type "Cylinder"

Command:  GST

Select a 3D solid:

Solid is of type "Cone"

Command:  GST

Select a 3D solid:

Solid is of type "Torus"

TrackBack

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

Listed below are links to weblogs that reference Getting the type of an AutoCAD solid using .NET:

blog comments powered by Disqus

Feed/Share

10 Random Posts