如何给GetSelection()命令添加关键字

嘎子 AutoCAD二次开发 2016-01-27 1,076 次浏览 , , , 2条评论

觉得自己真够可以的,在一天之内转了Kean的三篇文章,别说还真是我今天都遇到的问题,觉得自己也得多总结,所以又把这个文章给发上来了。

我以前也用过GetSelection的Keyword功能,当时以为不能用呢,结果大神就是大神。

主要就是红色的部分,大家注意啦:

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

原创文章,文章首发于:Riley Ge (@rileyge) — Steemit

原创文章,转载请注明: 转载自TsonTec:测量解决方案提供者

本文链接地址: 如何给GetSelection()命令添加关键字

相关主题

说点什么

2 评论 在 "如何给GetSelection()命令添加关键字"

提醒
avatar
排序:   最新 | 最旧 | 得票最多
WIO
游客
WIO

能在选择集运行的时候临时改变过滤器吗?

回顶部