One way to exit the “GetSelection” API on keyword selection is to throw an AutoCAD exception from the keyword press event handler. Below is a sample example of the procedure. On press of any keyword, below code throws the “Autodesk.AutoCAD.Runtime.ErrorStatus.OK” exception passing the keyword pressed. This exception is handled in the code to identify the keyword pressed.
[CommandMethod("SELKW")]
publicvoid GetSelectionWithKeywords()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
// Create our options object
PromptSelectionOptions pso = newPromptSelectionOptions();
// 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;
pso.KeywordInput +=
newSelectionTextInputEventHandler(pso_KeywordInput);
PromptSelectionResult psr = null;
try
{
psr = ed.GetSelection(pso);
if (psr.Status == PromptStatus.OK)
{
//your logic
}
}
catch (System.Exception ex)
{
if (ex is Autodesk.AutoCAD.Runtime.Exception)
{
Autodesk.AutoCAD.Runtime.Exception aEs =
ex as Autodesk.AutoCAD.Runtime.Exception;
//user has pressed keyword.
if (aEs.ErrorStatus ==
Autodesk.AutoCAD.Runtime.ErrorStatus.OK)
{
ed.WriteMessage("\nKeyword entered: {0}",
ex.Message);
}
else
{
//other exception, please handle
}
}
}
}
void pso_KeywordInput(object sender, SelectionTextInputEventArgs e)
{
//user has pressed keyword, so throw Exception
throw new Autodesk.AutoCAD.Runtime.Exception(
Autodesk.AutoCAD.Runtime.ErrorStatus.OK, e.Input);
}