000

Index Labels

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.

Blog Archive

Labels

3D Modeling 3D Sketch Inventor AI Design AI in Manufacturing AI Tools Architecture Artificial Intelligence AutoCAD AutoCAD advice AutoCAD Basics AutoCAD Beginners AutoCAD Civil3D AutoCAD commands AutoCAD efficiency AutoCAD features AutoCAD File Management AutoCAD Layer AutoCAD learning AutoCAD print settings AutoCAD productivity AutoCAD Teaching AutoCAD Techniques AutoCAD tips AutoCAD training. AutoCAD tricks AutoCAD Tutorial AutoCAD workflow AutoCAD Xref Autodesk Autodesk 2025 Autodesk AI Tools Autodesk AutoCAD Autodesk Fusion 360 Autodesk Inventor Autodesk Inventor Frame Generator Autodesk Inventor iLogic Autodesk Recap Autodesk Revit Autodesk Software Autodesk Video Automation Automation Tutorial Basic Commands Basics Beginner Beginner Tips BIM BIM Implementation Block Editor ByLayer CAD comparison CAD Design CAD File Size Reduction CAD line thickness CAD Optimization CAD Productivity CAD software clean CAD file cleaning command Cloud Collaboration command abbreviations Construction Technology Contraints Create resizable blocks CTB STB Data Reference Data Shortcut design software Design Workflow Digital Design Digital Twin Drafting Standards Drawing Automation Dref Dynamic Block Dynamic Block AutoCAD Dynamic Blocks Dynamic doors Dynamic windows eco design editing commands energy efficiency Engineering Engineering Design Engineering Innovation Engineering Technology engineering tools Excel Express Tools External Reference Fast Structural Design Fusion 360 Generative Design green building Grips heavy CAD file Heavy CAD Files iLogic Industry 4.0 Insight Inventor API Inventor Drawing Template Inventor Frame Generator Inventor Graphics Issues Inventor IDW Inventor Tips Keyboard Shortcuts Learn AutoCAD Machine Learning in CAD maintenance command Management Manufacturing Innovation Metal Structure ObjectARX .NET API Organization OVERKILL OVERKILL AutoCAD Palette PDF Plot Style AutoCAD Practice Drawing Printing Quality professional printing Professional Tips PTC Creo PURGE PURGE AutoCAD ReCap reduce CAD file size Resizable Block Revit Revit Best Practices Revit Workflow Ribbon screen shortcut keys Shortcuts Siemens NX Sketch Small Firms Smart Block Smart Factory SolidWorks Steel Structure Design sustainability Sustainable Manufacturing toolbar Tutorial User Interface (UI) Workbook Workspace XLS Xref