August 15, 2011

Preventing AutoCAD objects from being highlighted using .NET

Thanks to Balaji Ramamoorthy, from DevTech India, for the basis of this post (some code he provided in a recent reply to an ADN member).

The original question came from someone who wanted to stop text objects from being highlighted when selected. I’ve extended the mechanism to make it a little more flexible: it now maintains a list of object types (their DXF names) of objects that should not be highlighted, when selected.

Balaji implemented a HighlightOverrule to control the highlighting. This doesn’t stop the objects from actually being selected, however: in the next post I’ll add some additional code from Balaji’s implementation that also stops the objects from being selected.

Here’s the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using System.Collections.Generic;

using System.Text;

namespace PreventSelectionHighlight

{

publicclassTest : IExtensionApplication

{

staticUnhighlightOverrule _ho = newUnhighlightOverrule();

[CommandMethod("UH")]

publicstaticvoid Unhighlight()

{

Document doc = Application.DocumentManager.MdiActiveDocument;

Editor ed = doc.Editor;

// Print the list of currently unhighlighted classes

ed.WriteMessage(_ho.ListToPrint());

// Get the type to add to the list

PromptResult pr =

ed.GetString(

"\nEnter the type of object to stop from " +

"being highlighted: "

);

if (pr.Status != PromptStatus.OK)

return;

if (_ho.IsInList(pr.StringResult))

{

ed.WriteMessage("\nItem already in the list.");

}

else

{

_ho.AddToList(pr.StringResult);

ed.WriteMessage("\nItem added to the list.");

}

}

[CommandMethod("RH")]

publicstaticvoid Rehighlight()

{

Document doc = Application.DocumentManager.MdiActiveDocument;

Editor ed = doc.Editor;

// Print the list of currently unhighlighted classes

ed.WriteMessage(_ho.ListToPrint());

// Get the type to remove from the list

PromptResult pr =

ed.GetString(

"\nEnter the type of object to remove from the " +

"list: "

);

if (pr.Status != PromptStatus.OK)

return;

if (!_ho.IsInList(pr.StringResult))

{

ed.WriteMessage("\nItem not currently in the list.");

}

else

{

_ho.RemoveFromList(pr.StringResult);

ed.WriteMessage("\nItem removed from the list.");

}

}

voidIExtensionApplication.Initialize()

{

// Add our overrule

Overrule.AddOverrule(

RXObject.GetClass(typeof(Entity)), _ho, false

);

Overrule.Overruling = true;

}

voidIExtensionApplication.Terminate()

{

// Remove our overrule

Overrule.RemoveOverrule(

RXObject.GetClass(typeof(Entity)), _ho

);

Overrule.Overruling = false;

}

}

classUnhighlightOverrule : HighlightOverrule

{

// The list of types to unhighlight

List<string> _unhighlighted = newList<string>();

// Add a type to the list

publicvoid AddToList(string name)

{

string upper = name.ToUpper();

if (!_unhighlighted.Contains(upper))

{

_unhighlighted.Add(upper);

}

}

// Remove a type from the list

publicvoid RemoveFromList(string name)

{

string upper = name.ToUpper();

if (_unhighlighted.Contains(upper))

{

_unhighlighted.Remove(upper);

}

}

// Check whether the list contains a type

publicbool IsInList(string name)

{

return _unhighlighted.Contains(name.ToUpper());

}

// Get a string printing the contents of the list

publicstring ListToPrint()

{

string toPrint;

if (_unhighlighted.Count == 0)

{

toPrint =

"\nThere are currently no objects in the list " +

"to stop from being highlighted.";

}

else

{

StringBuilder sb =

newStringBuilder(

"\nObjects of these types will not be highlighted " +

"during selection:"

);

foreach (string name in _unhighlighted)

{

sb.Append(" " + name);

}

toPrint = sb.ToString();

}

return toPrint;

}

// Called when an entity is highlighted

publicoverridevoid Highlight(

Entity entity, FullSubentityPath subId, bool highlightAll

)

{

// If our object's type is in the list, return

// without calling the base implementation

if (IsInList(entity.GetRXClass().DxfName))

return;

base.Highlight(entity, subId, highlightAll);

}

// Called when an entity is unhighlighted

publicoverridevoid Unhighlight(

Entity entity, FullSubentityPath subId, bool highlightAll

)

{

base.Unhighlight(entity, subId, highlightAll);

}

}

}

You can use the UH command to “unhighlight” objects via their type’s DXF name and RH to re-highlight them.

Let’s draw some simple geometry – lines, arcs and circles.

When we select them, they all get highlighted (see? nothing up my sleeves. ;-)

Now we can use the UH command to stop lines and arcs from being highlighted:

Command: UH

There are currently no objects in the list to stop from being highlighted.

Enter the type of object to stop from being highlighted: line

Item added to the list.

Command: UH

Objects of these types will not be highlighted during selection: LINE

Enter the type of object to stop from being highlighted: arc

Item added to the list.

When we select our geometry, only the circles are now highlighted:

Next time we’ll take a look at ways to stop objects from being selected, not just highlighted.