翻译自:
Preventing AutoCAD objects from being highlighted using .NET
问题源自有人问:如何禁止 AutoCAD 中的文本对象在被选中时高亮?
我提供了一个更有弹性的解决方法,程序维护一个 AutoCAD 中的对象类型列表,该列表中的类型将在被选中时不被高亮显示。
通过类:HighlightOverrule 实现。
示例代码如下:
public class UnhightlightTestApp : IExtensionApplication
{
static UnhighlightOverrule _ho = new UnhighlightOverrule();
void IExtensionApplication.Initialize()
{
// Add our overrule
Overrule.AddOverrule(RXObject.GetClass(typeof(Entity)), _ho, false);
Overrule.Overruling = true;
}
void IExtensionApplication.Terminate()
{
// Remove our overrule
Overrule.RemoveOverrule(RXObject.GetClass(typeof(Entity)), _ho);
Overrule.Overruling = false;
}
[CommandMethod("UH")]
public static void 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")]
public static void 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.");
}
}
}
class UnhighlightOverrule : HighlightOverrule
{
// The list of types to unhighlight
List<string> _unhighlighted = new List<string>();
// Add a type to the list
public void AddToList(string name)
{
string upper = name.ToUpper();
if (!_unhighlighted.Contains(upper))
{
_unhighlighted.Add(upper);
}
}
// Remove a type from the list
public void RemoveFromList(string name)
{
string upper = name.ToUpper();
if (_unhighlighted.Contains(upper))
{
_unhighlighted.Remove(upper);
}
}
// Check whether the list contains a type
public bool IsInList(string name)
{
return _unhighlighted.Contains(name.ToUpper());
}
// Get a string printing the contents of the list
public string ListToPrint()
{
string toPrint;
if (_unhighlighted.Count == 0)
{
toPrint =
"\nThere are currently no objects in the list " +
"to stop from being highlighted.";
}
else
{
StringBuilder sb =
new StringBuilder(
"\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
public override void 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
public override void Unhighlight(
Entity entity,
FullSubentityPath subId,
bool highlightAll)
{
base.Unhighlight(entity, subId, highlightAll);
}
}
你可以使用以上代码中定义的 UH 命令来禁止特定类型的对象被高亮显示。
相应地,使用 RH 命令来重新高亮显示特定类型对象。
让我们绘制一些简单的几何图元:线、圆、圆弧。
如下图所示:
当我们选中它们时,全被高亮显示。
现在我们来使用 UH 命令禁止线和圆弧被高亮显示。
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.
现在,当我们重新选中它们时,只有圆被高亮了。