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

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

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

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

注意:此功能有一个缺陷。如果Editor.GetSelection方法是在一个图形界面中调用的(比如一个 Form,或者 PaletteSet ),那么在你的子命令结束之后,在 CAD 的绘图区域内将看不到鼠标,且在绘图区域中不能进行任何操作,所有 CAD 命令的失效,CAD 关闭按钮失效;这些是我注意到的异常,或许还会有别的异常。

  而如果 GetSelection 是如下文代码一样,放在一个自定义的 CAD 命令中,且调用的时候是通过 CAD 命令调用的,则没有问题。所以如果需要在图形界面中用到 GetSelection 退出功能, 可以将实现代码封装成一个自定义命令,然后在图形界面中用 Docuemnt.SendStringToExcute 方法调用你的自定义命令。但有如果在你的GetSelection子命令完成之后还需要与你的图形界面进行交互,目前还没有非常合适的解决的办法,有几种初步的想法:

  1、在SendStringToExcute的时候先把图形界面关闭掉,然后在 GetSelection 子命令结束之后,重新初始化窗体,并根据 GetSelection 执行结果来操作窗体。

  2、将窗体对象定义为全局对象,且窗体中的控件的Modifiers属性设为 Public 或 Protected,这样就可以在自定义命令中直接操作窗体中的控件。

  3、将窗体对象定义为全局对象,将对窗体中控件的操作封装成方法,然后在自定义命令中直接调用这些方法完成图形界面的操作。

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

 

posted @ 2014-06-26 18:43  尼克劳斯  阅读(940)  评论(0编辑  收藏  举报