首页 > 代码库 > 用keyword实现Editor.GetSelection的退出功能

用keyword实现Editor.GetSelection的退出功能

有时候我们在使用GetSelection功能让用户选择实体时,可能会给用户提供一些keyword选项,要接收用户选择的keyword选项,需要用到PromptSelectionOptions.KeywordInput事件。

但是,有时为了有时在用户选择了某个keyword项时,需要结束GetSelection主操作(这样体验性更好,用户更方便),但是一直没有找到解决的办法,试了好多方法都以失败告终。

今天,有一个功能又需要实现这一点,于是在群里问了一句,胖子说他QQ空间里有,于是进去一看,晃然大悟:在keywordInput事件里抛出一个异常即可。

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