找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1097|回复: 0

[分享] Retrieving nested entities under cursor aperture using .Net API

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-2 13:07:10 来自手机 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 csharp 于 2014-5-2 15:23 编辑

http://adndevblog.typepad.com/autocad/2012/04/retrieving-nested-entities-under-cursor-aperture-using-net-api.html
Retrieving nested entities under cursor aperture using .Net API



By Philippe Leefsma



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.  

  1. class ArxImports

  2. {

  3.     public struct ads_name

  4.     {

  5.         public IntPtr a;

  6.         public IntPtr b;

  7.     };



  8.     [StructLayout(LayoutKind.Sequential, Size = 32)]

  9.     public struct resbuf { }



  10.     [DllImport("acad.exe",

  11.         CallingConvention = CallingConvention.Cdecl,

  12.         CharSet = CharSet.Unicode,

  13.         ExactSpelling = true)]

  14.     public static extern PromptStatus acedSSGet(

  15.         string str, IntPtr pt1, IntPtr pt2,

  16.         IntPtr filter, out ads_name ss);



  17.     [DllImport("acad.exe",

  18.         CallingConvention = CallingConvention.Cdecl,

  19.         CharSet = CharSet.Unicode,

  20.         ExactSpelling = true)]

  21.     public static extern PromptStatus acedSSFree(ref ads_name ss);



  22.     [DllImport("acad.exe",

  23.         CallingConvention = CallingConvention.Cdecl,

  24.         CharSet = CharSet.Unicode,

  25.         ExactSpelling = true)]

  26.     public static extern PromptStatus acedSSLength(

  27.         ref ads_name ss, out int len);



  28.     [DllImport("acad.exe",

  29.         CallingConvention = CallingConvention.Cdecl,

  30.         CharSet = CharSet.Unicode,

  31.         ExactSpelling = true)]

  32.     public unsafe static extern PromptStatus acedSSNameX(

  33.         resbuf** rbpp, ref ads_name ss, int i);

  34. }



  35. class CursorDetectCls

  36. {

  37.     Editor _ed;



  38.     [CommandMethod("UnderCursorNested")]

  39.     public void UnderCursorNested()

  40.     {

  41.         _ed = Application.DocumentManager.MdiActiveDocument.Editor;



  42.         //Set up PointMonitor event

  43.         _ed.PointMonitor +=

  44.             new PointMonitorEventHandler(PointMonitorMulti);

  45.     }



  46.     void PointMonitorMulti(object sender, PointMonitorEventArgs e)

  47.     {

  48.         //Filters only block references (INSERT)

  49.         //that are on layer "0"

  50.         ResultBuffer resbuf = new ResultBuffer(

  51.             new TypedValue(-4, "<and"),

  52.             new TypedValue(0, "INSERT"),

  53.             new TypedValue(8, "0"),

  54.             new TypedValue(-4, "and>"));



  55.         ObjectId[] ids = FindAtPointNested(

  56.             _ed.Document,

  57.             e.Context.RawPoint,

  58.             true,

  59.             resbuf.UnmanagedObject);



  60.         //Dump result to commandline

  61.         foreach (ObjectId id in ids)

  62.         {

  63.             _ed.WriteMessage("\n - Entity: {0} [Id:{1}]",

  64.                 id.ObjectClass.Name,

  65.                 id.ToString());

  66.         }

  67.     }



  68.     //Retruns ObjectIds of entities at a specific position

  69.     //Including nested entities in block references

  70.     static public ObjectId[] FindAtPointNested(

  71.         Document doc,

  72.         Point3d worldPoint,

  73.         bool selectAll,

  74.         IntPtr filter)

  75.     {

  76.         System.Collections.Generic.List<ObjectId> ids =

  77.             new System.Collections.Generic.List<ObjectId>();



  78.         Matrix3d wcs2ucs =

  79.             doc.Editor.CurrentUserCoordinateSystem.Inverse();



  80.         Point3d ucsPoint = worldPoint.TransformBy(wcs2ucs);



  81.         string arg = selectAll ? "_:E:N" : "_:N";



  82.         IntPtr ptrPoint = Marshal.UnsafeAddrOfPinnedArrayElement(

  83.             worldPoint.ToArray(), 0);



  84.         ArxImports.ads_name sset;



  85.         PromptStatus prGetResult = ArxImports.acedSSGet(

  86.             arg, ptrPoint, IntPtr.Zero, filter, out sset);



  87.         int len;

  88.         ArxImports.acedSSLength(ref sset, out len);



  89.         //Need to rely on unsafe code in order to use pointers *

  90.         unsafe

  91.         {

  92.             for (int i = 0; i < len; ++i)

  93.             {

  94.                 ArxImports.resbuf rb = new ArxImports.resbuf();



  95.                 ArxImports.resbuf* pRb = &rb;



  96.                 if (ArxImports.acedSSNameX(&pRb, ref sset, i) !=

  97.                     PromptStatus.OK)

  98.                     continue;



  99.                 //Create managed ResultBuffer from our resbuf struct

  100.                 using (ResultBuffer rbMng = DisposableWrapper.Create(

  101.                     typeof(ResultBuffer),

  102.                     (IntPtr)pRb,

  103.                     true) as ResultBuffer)

  104.                 {

  105.                     foreach (TypedValue tpVal in rbMng)

  106.                     {

  107.                         //Not interested if it isn't an ObjectId

  108.                         if (tpVal.TypeCode != 5006) //RTENAME

  109.                             continue;



  110.                         ObjectId id = (ObjectId)tpVal.Value;



  111.                         if (id != null)

  112.                             ids.Add(id);

  113.                     }

  114.                 }

  115.             }

  116.         }



  117.         ArxImports.acedSSFree(ref sset);



  118.         return ids.ToArray();

  119.     }

  120. }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:20 , Processed in 0.181720 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表