找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2306|回复: 0

[分享] Detecting entities under cursor while selection is running

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-9 00:59:11 | 显示全部楼层 |阅读模式

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

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

×
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());
    }
}


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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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