马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Detecting entities under cursor while selection is running By Philippe Leefsma
The PointMonitor callback in .Net lets you access the entities that are under the cursor aperture as the user is hovering the mouse, however this feature gets disabled while running an Editor.GetEntity.
There is a workaround for that behavior that involves P/Invoking a bunch of ObjectARX methods, as we like to do. Below is a C# sample for that.
Nested entities can also be detected as exposed in this previous post:
Retrieving nested entities under cursor aperture using .Net API
class ArxImports { public struct ads_name { public IntPtr a; public IntPtr b; }; [StructLayout(LayoutKind public struct resbuf { } [DllImport("accore.dll", 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("accore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true public static extern PromptStatus acedSSFree( ref ads_name ss); [DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true public static extern PromptStatus acedSSLength( ref ads_name ss, out int len); [DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true public static extern PromptStatus acedSSName( ref ads_name ss, int i, out ads_name name); [DllImport("acdb19.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true public static extern ErrorStatus acdbGetObjectId( out ObjectId id, ref ads_name name); } static System.Collections.Generic.List<ObjectId> FindAtPoint(Point3d worldPoint, bool selectAll = true) { System.Collections.Generic.List<ObjectId> ids = new System.Collections.Generic.List<ObjectId>(); Document doc = Application.DocumentManager.MdiActiveDocument; Matrix3d wcs2ucs = doc.Editor.CurrentUserCoordinateSystem.Inverse(); Point3d ucsPoint = worldPoint.TransformBy(wcs2ucs); string arg = selectAll ? ":E" : string.Empty; IntPtr ptrPoint = Marshal.UnsafeAddrOfPinnedArrayElement( worldPoint.ToArray(), 0); ArxImports.ads_name sset; PromptStatus prGetResult = ArxImports.acedSSGet( arg, ptrPoint, IntPtr.Zero, IntPtr.Zero, out sset); int len; ArxImports.acedSSLength(ref sset, out len); if (len <= 0) return ids; for (int i = 0; i < len; ++i) { ArxImports.ads_name name; if (ArxImports.acedSSName( ref sset, i, out name) != PromptStatus.OK) continue; ObjectId id; if (ArxImports.acdbGetObjectId( out id, ref name) != ErrorStatus.OK) continue; ids.Add(id); } ArxImports.acedSSFree(ref sset); return ids; } Editor AdnEditor; [CommandMethod("PointMonitorSelection" public void PointMonitorSelection() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; try { AdnEditor = doc.Editor; AdnEditor.PointMonitor += FindUsingPointMonitor; PromptEntityOptions peo = new PromptEntityOptions( "Select an entity..."); PromptEntityResult per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) return; ObjectId id = per.ObjectId; ed.WriteMessage("\n - Selected " + " Entity: " + id.ObjectClass.Name + " Id: " + id.ToString()); } catch(System.Exception ex) { ed.WriteMessage("\nException: " + ex.Message); } finally { AdnEditor.PointMonitor -= FindUsingPointMonitor; } } void FindUsingPointMonitor(object sender, PointMonitorEventArgs e) { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; // Not working when running editor selection //foreach (var subId in e.Context.GetPickedEntities()) //{ // foreach (var id in subId.GetObjectIds()) // { // ed.WriteMessage("\n - " + // " Entity: " + id.ObjectClass.Name + // " Id: " + id.ToString()); // } //} var ids = FindAtPoint(e.Context.RawPoint); foreach (var id in ids) { ed.WriteMessage("\n + " + " Entity: " + id.ObjectClass.Name + " Id: " + id.ToString()); } }
|