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