August 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            








« Getting access to an entity from its handle in AutoCAD .NET | Main | Allowing users to escape from long operations in AutoCAD .NET »

February 21, 2007

Using the COM interface for AutoCAD objects from .NET

This question came in a couple of weeks ago from csharpbird, and I promised myself to turn it into a post:

The Hatch class of the current AutoCAD version does not provide the OriginPoint property.How to get/set the OriginPoint of the Hatch object using P/Invoke?

The reason I wanted to delay making an official post is that I have good news (that I couldn't talk about until the product was announced): the Autodesk.AutoCAD.DatabaseServices.Hatch object now has an Origin property in AutoCAD 2008. :-)

But this does raise an interesting topic - how to get at information that isn't available through the managed interface? P/Invoke is an option for access certain ObjectARX functions, but as stated in this previous post, it cannot be used with instance members, such as AcDbHatch methods.

This particular property (a Hatch's Origin) has been exposed for a few releases through COM; Property Palette support for object properties sometimes drives their COM exposure more quickly than their "managed" exposure (although increasingly the .NET API is there right from the beginning).

Anyway, here's some C# code for AutoCAD 2007 that accesses the COM interface for a hatch object, returning the Origin property. The code first gets the object as a managed hatch, simply to show a technique for going from a managed object to a COM object:

[Update: this is Version 1 of the code, posted before Albert's comment, below. Scroll down for the more elegant Version 2...]


using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Interop;

using System.Runtime.InteropServices;


namespace HatchQuery

{

  public class HatchQueryCommands

  {

    [CommandMethod("QH")

    ]

    public void QueryHatchOrigin()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;


      try

      {

        PromptEntityResult per =

          ed.GetEntity("\nSelect hatch object: ");


        if (per.Status == PromptStatus.OK)

        {

          Transaction tr =

            doc.TransactionManager.StartTransaction();

          using (tr)

          {

            // Open up the object

            DBObject obj =

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


            // Make sure we have a managed hatch object

            Hatch hat = obj as Hatch;

            if (hat != null)

            {

              // Now let's pretend we had our managed hatch,

              // and need to use COM to get at its Origin

              Autodesk.AutoCAD.Interop.AcadApplication oAcad =

                (AcadApplication)Marshal.GetActiveObject(

                  "AutoCAD.Application.17"

                );


              // Let's use the COM API to get a COM reference

              // to the hatch (as a base object)

              object obj2 =

                oAcad.ActiveDocument.ObjectIdToObject(

                  obj.ObjectId.OldId

                );


              // Let's convert that object reference to

              // a COM hatch

              Autodesk.AutoCAD.Interop.Common.AcadHatch oHat =

                obj2 as

                Autodesk.AutoCAD.Interop.Common.AcadHatch;


              if (oHat != null)

              {

                // Now let's get the Origin as an array

                // of doubles, and print the contents

                double[] orig = (double[])oHat.Origin;

                ed.WriteMessage(

                  "\nHere's the hatch origin: X: " +

                  orig[0] + ", Y: " + orig[1]

                );

              }

            }

          }

        }

      }

      catch (System.Exception ex)

      {

        ed.WriteMessage(

          "Exception: " + ex

        );

      }

    }

  }

}

Version 2:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Interop;

using System.Runtime.InteropServices;


namespace HatchQuery

{

  public class HatchQueryCommands

  {

    [CommandMethod("QH")

    ]

    public void QueryHatchOrigin()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;


      try

      {

        PromptEntityResult per =

          ed.GetEntity("\nSelect hatch object: ");


        if (per.Status == PromptStatus.OK)

        {

          Transaction tr =

            doc.TransactionManager.StartTransaction();

          using (tr)

          {

            // Open up the object

            DBObject obj =

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


            // Make sure we have a managed hatch object

            Hatch hat = obj as Hatch;

            if (hat != null)

            {

              // a COM hatch

              Autodesk.AutoCAD.Interop.Common.AcadHatch oHat =

                obj.AcadObject as

                Autodesk.AutoCAD.Interop.Common.AcadHatch;


              if (oHat != null)

              {

                // Now let's get the Origin as an array

                // of doubles, and print the contents

                double[] orig = (double[])oHat.Origin;

                ed.WriteMessage(

                  "\nHere's the hatch origin: X: " +

                  orig[0] + ", Y: " + orig[1]

                );

              }

            }

          }

        }

      }

      catch (System.Exception ex)

      {

        ed.WriteMessage(

          "Exception: " + ex

        );

      }

    }

  }

}

And here's what happens when you run the code and select a hatch with an origin of 50,30:

Command: QH

Select hatch object:

Here's the hatch origin: X: 50, Y: 30

TrackBack

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

Listed below are links to weblogs that reference Using the COM interface for AutoCAD objects from .NET:

blog comments powered by Disqus

Feed/Share

10 Random Posts