I recently worked on a project to converting a fairly complicated VBA program. At a point during that VBA program progress, the VBA program converts the colors of a SelectionSet of entities into a highlight color (for example, yellow, or red), and converts the colors of all the rest of entities into a contrast color. This makes the selected entities much more visually stand out.
Although this achieves the goal of making some entities more eye-catching, it certainly has its drawbacks: it has to change almost all entities in the drawing database, just for the temporary visual effect. If something goes wrong that causes exception, it may leave the entities/drawing in unwanted state (i.e. in a color it should not be).
With Overrule, which was introduced into .NET API since AutoCAD 2010, customizing how an Entity is highlighted becomes an much easier task. There is even a HighlightOverrule class we can use (see one of my old post here ).
However, the HighlightOverrule isn't good enough to meet my need in the VBA conversion project. Besides highlighting the selected entities user selected, it also wants all entities that are not selected are in a plain/dull color so that the selected entities are visually stand-out, visually emphasized. So, I decided to give it a try of using DrawableOverrule. The code presented here is different from what I actually used in the project because of the specific business requirement, though.
Here is the code for a class called HighlightDrawableOverrule:
Code Snippet
- using System.Collections.Generic;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.GraphicsInterface;
- using Autodesk.AutoCAD.Runtime;
- namespace SelectionHighlightOverrule
- {
- public class HighlightDrawableOverrule : DrawableOverrule
- {
- private int _foreColorIndex;
- private int _backColorIndex;
- private bool _origialOverruling = false;
- private List<ObjectId> _highlightEnts;
- private bool _started = false;
- public HighlightDrawableOverrule(int foreColor, int backColor)
- {
- _foreColorIndex = foreColor;
- _backColorIndex = backColor;
- _highlightEnts = new List<ObjectId>();
- }
- public bool Started
- {
- get { return _started; }
- }
- public ObjectId[] SelectedEntIds
- {
- get { return _highlightEnts.ToArray(); }
- }
- public HighlightDrawableOverrule(int foreColor,
- int backColor, ObjectId[] entIds) : this(foreColor, backColor)
- {
- _highlightEnts.AddRange(entIds);
- }
- public void Start()
- {
- _origialOverruling = DrawableOverrule.Overruling;
- DrawableOverrule.AddOverrule(
- RXClass.GetClass(typeof(Entity)), this, true);
- DrawableOverrule.Overruling = true;
- _started = true;
- Regen();
- }
- public void Stop()
- {
- DrawableOverrule.RemoveOverrule(
- RXClass.GetClass(typeof(Entity)), this);
- DrawableOverrule.Overruling = _origialOverruling;
- _started = false;
- Regen();
- }
- public void AddHightlightEntities(ObjectId[] entIds)
- {
- bool added = false;
- foreach (var id in entIds)
- {
- if (!_highlightEnts.Contains(id))
- {
- _highlightEnts.Add(id);
- if (!added) added = true;
- }
- }
- if (_started && added) Regen();
- }
- public void RemoveHighlightEntities(ObjectId[] entIds)
- {
- bool removed = false;
- foreach (var id in entIds)
- {
- if (!_highlightEnts.Contains(id))
- {
- _highlightEnts.Remove(id);
- if (!removed) removed = true;
- }
- }
- if (_started && removed) Regen();
- }
- public void ClearHightlightEntities()
- {
- _highlightEnts.Clear();
- if (_started) Regen();
- }
- public override bool WorldDraw(Drawable drawable, WorldDraw wd)
- {
- wd.SubEntityTraits.Color = (short)_backColorIndex;
- if (_highlightEnts.Count > 0)
- {
- Entity ent = drawable as Entity;
- if (ent != null)
- {
- if (_highlightEnts.Contains(ent.ObjectId))
- wd.SubEntityTraits.Color = (short)_foreColorIndex;
- }
- }
- return base.WorldDraw(drawable, wd);
- }
- private void Regen()
- {
- Application.DocumentManager.MdiActiveDocument.Editor.Regen();
- }
- }
- }
The code is fairly simple and straightforward: it draws all entities except for the selected in a background color and all selected entities in a highlighted color.
Here is the example of how the HighlightDrawableOverrule class is used:
Code Snippet
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- [assembly: CommandClass(typeof(SelectionHighlightOverrule.MyCommands))]
- namespace SelectionHighlightOverrule
- {
- public class MyCommands
- {
- [CommandMethod("HighlightSel")]
- public static void HighlightedSelection()
- {
- Document dwg = Application.DocumentManager.MdiActiveDocument;
- Editor ed = dwg.Editor;
- int foreColor = 2; //Yellow
- int backColor = 7; //White
- ObjectId[] selectedIds = null;
- using (HighlightDrawableOverrule hOverrule =
- new HighlightDrawableOverrule(foreColor, backColor))
- {
- hOverrule.Start();
- while (true)
- {
- PromptEntityOptions opt =
- new PromptEntityOptions("\nPlease pick an entity:");
- PromptEntityResult res = ed.GetEntity(opt);
- if (res.Status == PromptStatus.OK)
- {
- hOverrule.AddHightlightEntities(
- new ObjectId[] { res.ObjectId });
- }
- else
- {
- break;
- }
- }
- hOverrule.Stop();
- selectedIds = hOverrule.SelectedEntIds;
- }
- ed.WriteMessage("\n{0} entitit{1} selected.",
- selectedIds.Length, selectedIds.Length>1?"ies":"y");
- }
- }
- }
This video clip shows the effect of running the "HighlightSel" command.
As you can see, using Overrule to change entity's color for temporary visual effect is definitely better than what one have to do in VBA: the entity's color property is not changed at all, thus the draw database remain unchanged, although AutoCAD presents a different color as you needed.
Notice that Editor.Regen() has to be called when the DrawableOverrule started/stopped and when ObjectId is added into selected entity list. If the drawing database is huge, it may take too much time to Regen().