- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
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;
}
}
}
}
|
|