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