September 2014

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        








« The “My First Plug-In” series | Main | Preventing AutoCAD objects from being selected using .NET »

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

{

  public class Test : IExtensionApplication

  {

    static UnhighlightOverrule _ho = new UnhighlightOverrule();

 

    [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.");

      }

    }

 

    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;

    }

  }

 

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

    }

  }

}

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.

Our simple geometry

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

Our selected geometry

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:

Selected but partially highlighted geometry

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

blog comments powered by Disqus

Feed/Share

10 Random Posts