找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2781|回复: 3

[分享] Highlight AutoCAD Entity in Your Own Way with Highlight Overrule

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-23 21:53:02 | 显示全部楼层 |阅读模式

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

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

×
Highlight AutoCAD Entity in Your Own Way with Highlight Overrule

Highlighting an AutoCAD entity is usually used as a visual hint that the entity is selected by user. Sometimes, when you develop an AutoCAD application, you may want to highlight some entities in interest in a way that is a bit different from AutoCAD does regularly, so that the entities would visually stand out to catch user's attention.

One way to do your own entity highlighting would be to use TransientGraphics object, which I have discussed previously here.

Now, I'd like show how to customize entity highlighting with Overrule, more precisely, HighlightOverrule. Do use Overrule, you need to use AutoCAD 2010 or later.

Scenario: there are polylines drawing in default color (white). When the mouse cursor hovers on it or clicks on it, AutoCAD highlights the polyline in the same color. I'd like the color to be different when highkighted.

Here is the code:


class MyHighlightOverrule.cs


using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

namespace HighlightOverruleSample
{
    public class MyHighlightOverrule : HighlightOverrule
    {
        private int _colorIndex = 1;
        private int _oldColorIndex;

        public MyHighlightOverrule()
        {
            AddOverrule(RXClass.GetClass(typeof(Polyline)), this, true);
        }

        public int ColorIndex
        {
            set { _colorIndex = value; }
            get { return _colorIndex; }
        }

        public override void Highlight(
            Entity entity, FullSubentityPath subId, bool highlightAll)
        {
            Polyline pline = entity as Polyline;
            if (pline == null) return;

            Database db= entity.Database;
            Document dwg=Application.DocumentManager.MdiDocument;

            using (DocumentLock dl = dwg.LockDocument())
            {
                using (Transaction tran = db.TransactionManager.StartTransaction())
                {
                    pline.UpgradeOpen();
                    _oldColorIndex = pline.ColorIndex;
                    pline.ColorIndex = _colorIndex;
                    pline.DowngradeOpen();
                }
            }

            base.Highlight(entity, subId, highlightAll);
        }

        public override void Unhighlight(
            Entity entity, FullSubentityPath subId, bool highlightAll)
        {
            Polyline pline = entity as Polyline;
            if (pline == null) return;

            Database db = entity.Database;
            Document dwg=Application.DocumentManager.MdiDocument;

            using (DocumentLock dl = _dwg.LockDocument())
            {
                using (Transaction tran = db.TransactionManager.StartTransaction())
                {
                    pline.UpgradeOpen();
                    pline.ColorIndex = _oldColorIndex;
                    pline.DowngradeOpen();
                }
            }

            base.Unhighlight(entity, subId, highlightAll);
        }
    }
}


class Commands.cs


using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;

namespace HighlightOverruleSample
{
    public class Commands
    {
        private static MyHighlightOverrule _hlOverRule = null;

        [CommandMethod("MyHLOn")]
        public void TurnOnMyHighlight()
        {
            if (_hlOverRule == null)
            {
                _hlOverRule = new MyHighlightOverrule();
            }

            MyHighlightOverrule.Overruling = true;
        }

        [CommandMethod("MyHLOff")]
        public void TurnOffMyHighlight()
        {
            if (_hlOverRule == null) return;

            MyHighlightOverrule.Overruling = false;
        }

        [CommandMethod("HLColor")]
        public void SetHighlightColor()
        {
            Document dwg = Autodesk.AutoCAD.ApplicationServices.
                Application.DocumentManager.MdiActiveDocument;

            Editor ed = dwg.Editor;

            PromptIntegerOptions opt = new PromptIntegerOptions(
                "\nEnter color index (a number form 0 to 7):");

            PromptIntegerResult res = ed.GetInteger(opt);

            if (res.Status == PromptStatus.OK)
            {
                _hlOverRule.ColorIndex = res.Value;
            }
        }
    }
}



论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2014-6-27 10:09:38 | 显示全部楼层
学习测试了一下,有2个问题:
1.加载后,先执行HLcolor命令会系统崩溃 ,原因 private static MyHighlightOverrule _hlOverRule 还没有初始化。可在SetHighlightColor函数中处理解决
2.亮显的重定义对 块图元 不起作用,不知如何解决。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-6-27 12:57:09 来自手机 | 显示全部楼层
还没有学到这个 OverRule
块图元的颜色修改可以看eachy的"图层合并"帖子讨论,块图元改颜色需要块内图元为Byblock
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2014-6-27 13:28:28 | 显示全部楼层
块图元改颜色需要块内图元为Byblock

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

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-21 19:57 , Processed in 0.353677 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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