鼠标悬停监视——待调试

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using System;

using System.Text;



public class AttributeHoverMonitor

{

    private static Editor _ed;

    private static object _lastHoveredId = null;



    [CommandMethod("StartHoverMonitor")]

    public static void StartHoverMonitor()

    {

        _ed = Application.DocumentManager.MdiActiveDocument.Editor;

        _ed.PointMonitor += OnPointMonitor;

        _ed.WriteMessage("\n属性悬停监视已启用");

    }



    [CommandMethod("StopHoverMonitor")]

    public static void StopHoverMonitor()

    {

        _ed.PointMonitor -= OnPointMonitor;

        _ed.WriteMessage("\n属性悬停监视已禁用");

    }



    private static void OnPointMonitor(object sender, PointMonitorEventArgs e)

    {

        try

        {

            using (var tr = _ed.Document.TransactionManager.StartOpenCloseTransaction())

            {

                var objId = e.Context.GetPickedEntities()?.FirstOrDefault().ObjectId;

                if (objId == null || !objId.IsValid) return;



                // 性能优化:仅当悬停新对象时处理

                if (_lastHoveredId != null && _lastHoveredId.Equals(objId)) return;

                _lastHoveredId = objId;



                var entity = tr.GetObject(objId, OpenMode.ForRead) as Entity;

                if (entity is BlockReference br)

                {

                    var tooltip = GetAttributeTooltip(br, tr);

                    if (!string.IsNullOrEmpty(tooltip))

                    {

                        // 显示自定义工具提示

                        e.Context.SetToolTip(tooltip, new Point2d(e.Context.RawPoint.X + 10, e.Context.RawPoint.Y));

                    }

                }

                tr.Commit();

            }

        }

        catch { /* 错误处理 */ }

    }



    private static string GetAttributeTooltip(BlockReference br, Transaction tr)

    {

        var sb = new StringBuilder();

        sb.AppendLine($"块名称:{br.GetBlockName(tr)}");

        sb.AppendLine($"位置:{br.Position.ToString()}");



        foreach (ObjectId attId in br.AttributeCollection)

        {

            if (!attId.IsValid) continue;

            var attRef = tr.GetObject(attId, OpenMode.ForRead) as AttributeReference;

            sb.AppendLine($"{attRef?.Tag}:{attRef?.TextString}");

        }



        return sb.ToString();

    }

}



// BlockReference扩展方法

public static class BlockExtensions

{

    public static string GetBlockName(this BlockReference br, Transaction tr)

    {

        return ((BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead)).Name;

    }

}

// 在原有代码基础上添加以下功能

private static void OnPointMonitor(object sender, PointMonitorEventArgs e)

{

    // ...原有代码...



    // 添加高亮显示

    if (entity is BlockReference)

    {

        e.Context.HighlightEntities(new ObjectId[] { objId });

    }



    // 添加动态预览窗口

    var previewPos = new Point3d(e.Context.RawPoint.X + 20, e.Context.RawPoint.Y - 50, 0);

    e.Context.DrawText(previewPos, tooltip, 12, TextAlignment.Left);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山水CAD插件定制

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值