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);
}
文章来自 http://adndevblog.typepad.com/autocad/2013/12/how-t ... ction-on-keyword-selection.html
下面是转换后的VB.Net源码:
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Public Class SELKW
<CommandMethod("SELKW")> _
Public Sub GetSelectionWithKeywords()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
' Create our options object
Dim pso As PromptSelectionOptions = New PromptSelectionOptions()
' Add our keywords
pso.Keywords.Add("First")
pso.Keywords.Add("Second")
' Set our prompts to include our keywords
Dim kws As String = pso.Keywords.GetDisplayString(True)
pso.MessageForAdding = vbLf & "Add objects to selection or " + kws
pso.MessageForRemoval = vbLf & "Remove objects from selection or " + kws
AddHandler pso.KeywordInput, AddressOf pso_KeywordInput
Dim psr As PromptSelectionResult = Nothing
Try
psr = ed.GetSelection(pso)
'your logic
If psr.Status = PromptStatus.OK Then
End If
Catch ex As System.Exception
If TypeOf ex Is Autodesk.AutoCAD.Runtime.Exception Then
Dim aEs As Autodesk.AutoCAD.Runtime.Exception = TryCast(ex, Autodesk.AutoCAD.Runtime.Exception)
'user has pressed keyword.
If aEs.ErrorStatus = Autodesk.AutoCAD.Runtime.ErrorStatus.OK Then
ed.WriteMessage(vbLf & "Keyword entered: {0}", ex.Message)
'other exception, please handle
Else
End If
End If
End Try
End Sub
Private Sub pso_KeywordInput(ByVal sender As Object, ByVal e As SelectionTextInputEventArgs)
'user has pressed keyword, so throw Exception
Throw New Autodesk.AutoCAD.Runtime.Exception(Autodesk.AutoCAD.Runtime.ErrorStatus.OK, e.Input)
End Sub
End Class
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Public Class SELKW
<CommandMethod("SELKW")> _
Public Sub GetSelectionWithKeywords()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
' Create our options object
Dim pso As PromptSelectionOptions = New PromptSelectionOptions()
' Add our keywords
pso.Keywords.Add("First")
pso.Keywords.Add("Second")
' Set our prompts to include our keywords
Dim kws As String = pso.Keywords.GetDisplayString(True)
pso.MessageForAdding = vbLf & "Add objects to selection or " + kws
pso.MessageForRemoval = vbLf & "Remove objects from selection or " + kws
AddHandler pso.KeywordInput, AddressOf pso_KeywordInput
Dim psr As PromptSelectionResult = Nothing
Try
psr = ed.GetSelection(pso)
'your logic
If psr.Status = PromptStatus.OK Then
End If
Catch ex As System.Exception
If TypeOf ex Is Autodesk.AutoCAD.Runtime.Exception Then
Dim aEs As Autodesk.AutoCAD.Runtime.Exception = TryCast(ex, Autodesk.AutoCAD.Runtime.Exception)
'user has pressed keyword.
If aEs.ErrorStatus = Autodesk.AutoCAD.Runtime.ErrorStatus.OK Then
ed.WriteMessage(vbLf & "Keyword entered: {0}", ex.Message)
'other exception, please handle
Else
End If
End If
End Try
End Sub
Private Sub pso_KeywordInput(ByVal sender As Object, ByVal e As SelectionTextInputEventArgs)
'user has pressed keyword, so throw Exception
Throw New Autodesk.AutoCAD.Runtime.Exception(Autodesk.AutoCAD.Runtime.ErrorStatus.OK, e.Input)
End Sub
End Class
【如何通过输入关键字退出Editor.GetSelection.rar】点击下载此文件
[本日志由 tiancao1001 于 2014-01-18 10:40 PM 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |