Wednesday, September 1, 2010

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;
}
}
}
}



To see the custom highlight effect, click here.


Of course, you'd be very likely show custom highlight only with polylines (or whatever entities, for that matters) in interest, instead of all polylines. You can use one of the SetXXXXXXFilter() methods of the Overrule object to filter out the polylines/entities to be highlighted in customized way.



Besides changing color, you can change the polyline's other properties that have visual effect when being changed, such as thickness. Well, do remember to save the properties being changed in the overriden Highlight() method and restore them back in the overriden Unhighlight() method.

1 comment:

Mufasu CAD said...

Thanks for codes,, its help me out

AutoCAD Tutorial

Followers

About Me

My photo
After graduating from university, I worked as civil engineer for more than 10 years. It was AutoCAD use that led me to the path of computer programming. Although I now do more generic business software development, such as enterprise system, timesheet, billing, web services..., AutoCAD related programming is always interesting me and I still get AutoCAD programming tasks assigned to me from time to time. So, AutoCAD goes, I go.