找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 981|回复: 0

[分享] Adding keyword handling to AutoCAD .NET’s GetSelection()

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-1 15:54:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
http://through-the-interface.typepad.com/through_the_interface/2010/05/adding-keyword-handling-to-autocad-nets-getselection.html

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


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:23 , Processed in 0.260156 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表