Adding keyword handling to AutoCAD .NET’s GetSelection()

I received this question by email over the weekend:

How does the Editor-Method GetSelection works with Keywords. I can't get it to work and there are no information found in the internet (nothing in your blog, nothing in forums).

Here's some C# code that does just this:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

 

namespace MyApplication

{

  public class Commands

  {

    [CommandMethod("SELKW")]

    public void GetSelectionWithKeywords()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;

 

      // Create our options object

 

      PromptSelectionOptions pso =

        new PromptSelectionOptions();

 

      // Add our keywords

 

      pso.Keywords.Add("FIrst");

      pso.Keywords.Add("Second");

 

      // Set our prompts to include our keywords

 

      string kws = pso.Keywords.GetDisplayString(true);

      pso.MessageForAdding =

        "\nAdd objects to selection or " + kws;

      pso.MessageForRemoval =

        "\nRemove objects from selection or " + kws;

 

      // Implement a callback for when keywords are entered

 

      pso.KeywordInput +=

        delegate(object sender, SelectionTextInputEventArgs e)

        {

          ed.WriteMessage("\nKeyword entered: {0}", e.Input);

        };

 

      // Finally run the selection and show any results

 

      PromptSelectionResult psr =

        ed.GetSelection(pso);

 

      if (psr.Status == PromptStatus.OK)

      {

        ed.WriteMessage(

          "\n{0} object{1} selected.",

          psr.Value.Count,

          psr.Value.Count == 1 ? "" : "s"

        );

      }

    }

  }

}

When we run the SELKW command (after building the code into a DLL and NETLOADing it), we see a fairly classic selection prompt with our keywords enabled:

Command: SELKW

Add objects to selection or [FIrst/Second]: Specify opposite corner: 12 found

Add objects to selection or [FIrst/Second]: FI

Keyword entered: FIrst

Add objects to selection or [FIrst/Second]: S

Keyword entered: Second

Add objects to selection or [FIrst/Second]: Third

*Invalid selection*

Expects a point or

Window/Last/Crossing/BOX/ALL/Fence/WPolygon/CPolygon/Group/Add/Remove/Multiple/Previous/Undo/AUto/SIngle

Add objects to selection or [FIrst/Second]: R

Remove objects from selection or [FIrst/Second]: Specify opposite corner: 3 found, 3 removed, 9 total

Remove objects from selection or [FIrst/Second]: FI

Keyword entered: FIrst

Remove objects from selection or [FIrst/Second]: S

Keyword entered: Second

Remove objects from selection or [FIrst/Second]: Third

*Invalid selection*

Expects a point or

Window/Last/Crossing/BOX/ALL/Fence/WPolygon/CPolygon/Group/Add/Remove/Multiple/Previous/Undo/AUto/SIngle

Remove objects from selection or [FIrst/Second]:

9 objects selected.

I chose "FI" as the shortcut for "First" to avoid a conflict with the standard selection process' "Fence" option. It doesn't make sense to set a default keyword, as in any case the act of using the Enter key terminates the selection rather than causing a default keyword to be selected. We can see that when the two valid keywords are entered, our KeywordInput event is fired, but that when an invalid keyword (such as "Third") is entered, a prompt is displayed explaining the default keyword options. To get different behaviour you can implement pso.InvalidInput (at which point you might, for instance, complement the core selection keywords with your own before presenting them to the user).

  1. alexander.frumkin@sunlink.com Avatar
    alexander.frumkin@sunlink.com

    Thank you, Kean. It was very helpful - I was not able to figure it out without your sample.

  2. I know that is a old post but i have a question, exist a way to stop the selection when the user type a especific keyword, in the callback function. What I want to do is to stop the getselection method to start a getentity method. Thanks!

  3. Interesting... you could probably set an internal flag and send a cancel character (ASCII 27) to the command-line to get out of GetSelection() and then start GetEntity().

    I haven't actually tried this myself, though.

    Kean

  4. Hi kean.
    Thank you for the attention.
    I decided to put in a try ... catch block, and I threw a specific error (divide by zero) and captured it in the try block.
    It worked very well.

  5. Would it be possible to capture left or right arrow keypress inside GetSelection? Thanks.

    1. There's nothing built in, but you could try to add a Windows hook to check for keyboard input during GetSelection().

      Kean

  6. It is really great !, Because this site is important which people every day show it & they are know many other things about exit keyword & Google keyword selection. I read this article & I hope it is help full to me & others traffic.

Leave a Reply

Your email address will not be published. Required fields are marked *