I was asked this useful question by an ADN member a while ago, being able to retrieve in real time the entities under the cursor aperture, as the user is moving the mouse pointer. If the question is not the most challenging, looking for retrieving as well nested entities, like for example the ones contained in an xref, is making the task more interesting…
Retrieving entities at a specified point is unfortunately not directly exposed through managed functions, so one needs to rely on P/Invoking, among others, acedSSGet and acedSSName with the “:N” option. However this later will not return entities being nested, typically in a block reference. In order to retrieve nested entities, invoking acedSSNameX is required, and due to the signature of this method, P/Invoking it from .Net is not very intuitive: declaration of a custom structure “resbuf” is required, as well as relying on a piece of unsafe code in order to map managed and unmanaged objects.
Running command “UnderCursorNested” will dump to commandline any entity under mouse cursor being a block reference placed on layer “0”, or any nested entity contained by that block reference, including entities from an xref. This filtering condition can of course be modified to suits your needs.
class ArxImports
{
public struct ads_name
{
public IntPtr a;
public IntPtr b;
};
[StructLayout(LayoutKind.Sequential, Size = 32)]
public struct resbuf { }
[DllImport("acad.exe",
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode,
ExactSpelling = true)]
public static extern PromptStatus acedSSGet(
string str, IntPtr pt1, IntPtr pt2,
IntPtr filter, out ads_name ss);
[DllImport("acad.exe",
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode,
ExactSpelling = true)]
public static extern PromptStatus acedSSFree(ref ads_name ss);
[DllImport("acad.exe",
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode,
ExactSpelling = true)]
public static extern PromptStatus acedSSLength(
ref ads_name ss, out int len);
[DllImport("acad.exe",
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode,
ExactSpelling = true)]
public unsafe static extern PromptStatus acedSSNameX(
resbuf** rbpp, ref ads_name ss, int i);
}
class CursorDetectCls
{
Editor _ed;
[CommandMethod("UnderCursorNested")]
public void UnderCursorNested()
{
_ed = Application.DocumentManager.MdiActiveDocument.Editor;
//Set up PointMonitor event
_ed.PointMonitor +=
new PointMonitorEventHandler(PointMonitorMulti);
}
void PointMonitorMulti(object sender, PointMonitorEventArgs e)
{
//Filters only block references (INSERT)
//that are on layer "0"
ResultBuffer resbuf = new ResultBuffer(
new TypedValue(-4, "<and"),
new TypedValue(0, "INSERT"),
new TypedValue(8, "0"),
new TypedValue(-4, "and>"));
ObjectId[] ids = FindAtPointNested(
_ed.Document,
e.Context.RawPoint,
true,
resbuf.UnmanagedObject);
//Dump result to commandline
foreach (ObjectId id in ids)
{
_ed.WriteMessage("\n - Entity: {0} [Id:{1}]",
id.ObjectClass.Name,
id.ToString());
}
}
//Retruns ObjectIds of entities at a specific position
//Including nested entities in block references
static public ObjectId[] FindAtPointNested(
Document doc,
Point3d worldPoint,
bool selectAll,
IntPtr filter)
{
System.Collections.Generic.List<ObjectId> ids =
new System.Collections.Generic.List<ObjectId>();
Matrix3d wcs2ucs =
doc.Editor.CurrentUserCoordinateSystem.Inverse();
Point3d ucsPoint = worldPoint.TransformBy(wcs2ucs);
string arg = selectAll ? "_:E:N" : "_:N";
IntPtr ptrPoint = Marshal.UnsafeAddrOfPinnedArrayElement(
worldPoint.ToArray(), 0);
ArxImports.ads_name sset;
PromptStatus prGetResult = ArxImports.acedSSGet(
arg, ptrPoint, IntPtr.Zero, filter, out sset);
int len;
ArxImports.acedSSLength(ref sset, out len);
//Need to rely on unsafe code in order to use pointers *
unsafe
{
for (int i = 0; i < len; ++i)
{
ArxImports.resbuf rb = new ArxImports.resbuf();
ArxImports.resbuf* pRb = &rb;
if (ArxImports.acedSSNameX(&pRb, ref sset, i) !=
PromptStatus.OK)
continue;
//Create managed ResultBuffer from our resbuf struct
using (ResultBuffer rbMng = DisposableWrapper.Create(
typeof(ResultBuffer),
(IntPtr)pRb,
true) as ResultBuffer)
{
foreach (TypedValue tpVal in rbMng)
{
//Not interested if it isn't an ObjectId
if (tpVal.TypeCode != 5006) //RTENAME
continue;
ObjectId id = (ObjectId)tpVal.Value;
if (id != null)
ids.Add(id);
}
}
}
}
ArxImports.acedSSFree(ref sset);
return ids.ToArray();
}
}