Prompt for User Input 提示用户输入 The Editor object, which is a child of the Document object, defines the user input methods. The user input methods display a prompt on the AutoCAD command line or in a dynamic input tooltip, and request input of various types. This type of user input is most useful for interactive input of screen coordinates, entity selection, and short-string or numeric values. If your application requires the input of numerous options or values, a Windows form may be more appropriate than individual prompts. 用户输入方法由Document对象的派生类Editor对象定义。用户输入方法在AutoCAD命令行或动态输入提示框里显示一个提示,请求各种不同类型的输入。这种用户输入在交互输入屏幕坐标、实体选择、短字串及数值时特别有用。如果程序需要输入多个选项或值时,用Windows窗口比单个单个的提示更合适。 Each user input method displays an optional prompt on the command line and returns a value specific to the type of input requested. For example, GetString returns a PromptResult which allows you to determine the status of the GetString method and retrieve the string the user entered. Each one of the user input methods has a specific return value. 每个用户输入方法都在命令行显示可选提示并返回一个输入所需类型的值。例如,GetString方法返回一个PromptResult类型的值,该值允许我们确定GetString方法的状态并取回用户所输入的值。每个用户输入方法都有与之相应的返回值。 The input methods accept a string for the prompt message to be displayed or a specific object type which controls the input from the user. These object types let you control things such as NULL input (pressing Enter), base point, input of zero or negative numbers, and input of arbitrary text values. 输入方法接受一个字符串用来显示提示信息,或接受一个指定对象类型来控制来自用户的输入。这些对象类型用来控制诸如NULL输入(按了Enter键)、基点、输入了0或负数、乱七八糟的文本输入等情况。 To force the prompt to be displayed on a line by itself, use the carriage return/linefeed constant (vbCrLf) or linefeed constant (vbLf) at the beginning of your prompt strings when using VB.NET, or "\n" with strings in C#. 要强制提示显示在单独一行上,使用VB.NET时可以在提示字符串开头加上回车/换行常量(vbCrLf)或换行常量(vbLf),用C#的话就在字符串前加上“\n”。 Topics in this section本节主题
1、GetString Method GetString方法The GetString method prompts the user for the input of a string at the Command prompt. The PromptStringOptions object allows you to control the input entered and how the prompt message appears. The AllowSpaces property of the PromptStringOptions object controls if spaces are allowed or not at the prompt. If set to false, pressing the Spacebar terminates the input. GetString方法提示用户在Command提示光标处输入一个字符串。PromptStringOptions对象用来控制键入的输入及提示信息呈现方式。PromptStringOptions对象的AllowSpaces属性控制提示是否可以输入空格,如果设置为false,按空格键就终止输入。 Get a string value from the user at the AutoCAD command line 在AutoCAD命令行获取用户输入的字符串
The following example displays the Enter Your Name prompt, and requires that the input from the user be terminated by pressing Enter (spaces are allowed in the input string). The entered string is displayed in a message box. 下面例子显示“Enter Your Name”的提示,并要求用户按Enter键完成输入(允许输入空格)。输入的字符串显示在消息框内。 VB.NETImports Autodesk.AutoCAD.ApplicationServices - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.EditorInput</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.Runtime</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">
- </div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><CommandMethod("GetStringFromUser")> _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Public Sub GetStringFromUser()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">
- </div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pStrOpts As PromptStringOptions = New PromptStringOptions(vbLf & _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> "Enter your name: ")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pStrOpts.AllowSpaces = True</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pStrRes As PromptResult = acDoc.Editor.GetString(pStrOpts)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">
- </div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("The name entered was: " & _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pStrRes.StringResult)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Sub</div>
复制代码C#using Autodesk.AutoCAD.ApplicationServices; - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.EditorInput;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.Runtime;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">
- </div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">[CommandMethod("GetStringFromUser")]</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">public static void GetStringFromUser()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">{</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">
- </div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptStringOptions pStrOpts = new PromptStringOptions("\nEnter your name: ");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pStrOpts.AllowSpaces = true;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptResult pStrRes = acDoc.Editor.GetString(pStrOpts);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">
- </div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("The name entered was: " +</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pStrRes.StringResult);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">}</div>
复制代码
- <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong>VBA/ActiveX Code Reference</strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Sub GetStringFromUser()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim retVal As String</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> retVal = ThisDrawing.Utility.GetString _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> (1, vbCrLf & "Enter your name: ")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> MsgBox "The name entered was: " & retVal</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Sub</div>
复制代码 2、GetPoint Method GetPoint方法The GetPoint method prompts the user to specify a point at the Command prompt. The PromptPointOptions object allows you to control the input entered and how the prompt message appears. The UseBasePoint and BasePoint properties of the PromptPointOptions object controls if a rubber-band line is drawn from a base point. The Keywords property of the PromptPointOptions object allows you to define keywords that can be entered at the Command prompt in addition to specifying a point. GetPoint方法提示用户在Command提示时指定一个点。PromptPointOptions对象用来控制键入的输入及提示信息呈现方式。PromptPointOptions对象的UseBasePoint属性和BasePoint属性控制是否从基点绘制一条橡皮带线。PromptPointOptions对象的Keywords属性用来定义除了指定点外还可以在Command提示光标处输入的关键字。 Get a point selected by the user 获取用户选取的点
The following example prompts the user for two points, then draws a line using those points as the start point and endpoint. 下例提示用户选取两个点,然后哟个这两个点作为起止点画一条线。 VB.NETImports Autodesk.AutoCAD.ApplicationServices - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.DatabaseServices</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.EditorInput</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.Geometry</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.Runtime</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><CommandMethod("GetPointsFromUser")> _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Public Sub GetPointsFromUser()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Get the current database and start the Transaction Manager</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acCurDb As Database = acDoc.Database</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pPtRes As PromptPointResult</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Prompt for the start point</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtOpts.Message = vbLf & "Enter the start point of the line: "</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtRes = acDoc.Editor.GetPoint(pPtOpts)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim ptStart As Point3d = pPtRes.Value</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Exit if the user presses ESC or cancels the command</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> If pPtRes.Status = PromptStatus.Cancel Then Exit Sub</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Prompt for the end point</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtOpts.Message = vbLf & "Enter the end point of the line: "</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtOpts.UseBasePoint = True</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtOpts.BasePoint = ptStart</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtRes = acDoc.Editor.GetPoint(pPtOpts)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim ptEnd As Point3d = pPtRes.Value</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> If pPtRes.Status = PromptStatus.Cancel Then Exit Sub</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Start a transaction</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acBlkTbl As BlockTable</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acBlkTblRec As BlockTableRecord</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Open Model space for write</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> OpenMode.ForRead)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> OpenMode.ForWrite)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Define the new line</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acLine As Line = New Line(ptStart, ptEnd)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Add the line to the drawing</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTblRec.AppendEntity(acLine)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acTrans.AddNewlyCreatedDBObject(acLine, True)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Zoom to the extents or limits of the drawing</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acDoc.SendStringToExecute("._zoom _all ", True, False, False)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Commit the changes and dispose of the transaction</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acTrans.Commit()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> End Using</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Sub</div>
复制代码C#using Autodesk.AutoCAD.ApplicationServices; - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.DatabaseServices;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.EditorInput;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.Geometry;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.Runtime;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">[CommandMethod("GetPointsFromUser")]</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">public static void GetPointsFromUser()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">{</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Get the current database and start the Transaction Manager</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Database acCurDb = acDoc.Database;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptPointResult pPtRes;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptPointOptions pPtOpts = new PromptPointOptions("");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Prompt for the start point</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtOpts.Message = "\nEnter the start point of the line: ";</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtRes = acDoc.Editor.GetPoint(pPtOpts);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Point3d ptStart = pPtRes.Value;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Exit if the user presses ESC or cancels the command</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> if (pPtRes.Status == PromptStatus.Cancel) return;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Prompt for the end point</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtOpts.Message = "\nEnter the end point of the line: ";</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtOpts.UseBasePoint = true;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtOpts.BasePoint = ptStart;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pPtRes = acDoc.Editor.GetPoint(pPtOpts);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Point3d ptEnd = pPtRes.Value;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> if (pPtRes.Status == PromptStatus.Cancel) return;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Start a transaction</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> {</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> BlockTable acBlkTbl;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> BlockTableRecord acBlkTblRec;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Open Model space for write</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Define the new line</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Line acLine = new Line(ptStart, ptEnd);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Add the line to the drawing</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTblRec.AppendEntity(acLine);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acTrans.AddNewlyCreatedDBObject(acLine, true);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Zoom to the extents or limits of the drawing</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acDoc.SendStringToExecute("._zoom _all ", true, false, false);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Commit the changes and dispose of the transaction</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acTrans.Commit();</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> }</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">}</div>
复制代码VBA/ActiveX Code Reference - <div align="left">Sub GetPointsFromUser()</div><div align="left"> Dim startPnt As Variant</div><div align="left"> Dim endPnt As Variant</div><div align="left"> Dim prompt1 As String</div><div align="left"> Dim prompt2 As String</div><div align="left"> prompt1 = vbCrLf & "Enter the start point of the line: "</div><div align="left"> prompt2 = vbCrLf & "Enter the end point of the line: "</div><div align="left"> ' Get the first point without entering a base point</div><div align="left"> startPnt = ThisDrawing.Utility.GetPoint(, prompt1)</div><div align="left"> ' Use the point entered above as the base point</div><div align="left"> endPnt = ThisDrawing.Utility.GetPoint(startPnt, prompt2)</div><div align="left"> ' Create a line using the two points entered</div><div align="left"> ThisDrawing.ModelSpace.AddLine startPnt, endPnt</div><div align="left"> ThisDrawing.Application.ZoomAll</div><div align="left">End Sub</div>
复制代码
3、GetKeywords Method GetKeywords方法The GetKeywords method prompts the user for input of a keyword at the Command prompt. The PromptKeywordOptions object allows you to control the input entered and how the prompt message appears. The Keywords property of the PromptKeywordOptions object allows you to define keywords that can be entered at the Command prompt. GetKeywords方法提示用户在Command提示光标处输入一个关键字。PromptKeywordOptions对象用来控制键入及提示信息呈现方式。PromptKeywordOptions对象的Keywords属性用来定义可在Command提示光标处键入的关键字。 Get a keyword from the user at the AutoCAD command line 从AutoCAD命令行获取用户输入的关键字
The following example forces the user to enter a keyword by setting the property AllowNone to false, which disallows NULL input (pressing Enter). The Keywords property is used to add the valid keywords allowed. 下例将PromptKeywordOptions对象的AllowNone属性设置为false(不允许直接回车),这样使用户必须输入一个关键字。Keywords属性用来添加允许的合法关键字。 VB.NETImports Autodesk.AutoCAD.ApplicationServices - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.EditorInput</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.Runtime</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><CommandMethod("GetKeywordFromUser")> _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Public Sub GetKeywordFromUser()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pKeyOpts As PromptKeywordOptions = New PromptKeywordOptions("")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Message = vbLf & "Enter an option "</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Line")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Circle")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Arc")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.AllowNone = False</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pKeyRes As PromptResult = acDoc.Editor.GetKeywords(pKeyOpts)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Entered keyword: " & pKeyRes.StringResult)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Sub</div>
复制代码C#using Autodesk.AutoCAD.ApplicationServices; - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.EditorInput;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.Runtime;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">[CommandMethod("GetKeywordFromUser")]</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">public static void GetKeywordFromUser()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">{</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Message = "\nEnter an option ";</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Line");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Circle");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Arc");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.AllowNone = false;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Entered keyword: " + pKeyRes.StringResult);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">}</div>
复制代码VBA/ActiveX Code Reference - <div align="left">Sub GetKeywordFromUser()</div><div align="left"> Dim keyWord As String</div><div align="left"> ThisDrawing.Utility.InitializeUserInput 1, "Line Circle Arc"</div><div align="left"> keyWord = ThisDrawing.Utility.GetKeyword _</div><div align="left"> (vbCrLf & "Enter an option [Line/Circle/Arc]: ")</div><div align="left"> MsgBox keyWord, , "GetKeyword Example"</div><div align="left">End Sub</div>
复制代码A more user-friendly keyword prompt is one that provides a default value if the user presses Enter (NULL input). Notice the minor modifications to the following example. 更用户友好的关键字提示方式是如果用户按Enter键(没有输入)时提供一个缺省值。注意下面的这个小小改动。 VB.NETImports Autodesk.AutoCAD.ApplicationServices - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.EditorInput</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.Runtime</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><CommandMethod("GetKeywordFromUser2")> _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Public Sub GetKeywordFromUser2()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pKeyOpts As PromptKeywordOptions = New PromptKeywordOptions("")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Message = vbLf & "Enter an option "</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Line")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Circle")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Arc")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Default = "Arc"</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.AllowNone = True</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pKeyRes As PromptResult = acDoc.Editor.GetKeywords(pKeyOpts)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Entered keyword: " & pKeyRes.StringResult)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Sub</div>
复制代码C#using Autodesk.AutoCAD.ApplicationServices; - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.EditorInput;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.Runtime;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">[CommandMethod("GetKeywordFromUser2")]</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">public static void GetKeywordFromUser2()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">{</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Message = "\nEnter an option ";</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Line");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Circle");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Add("Arc");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.Keywords.Default = "Arc";</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pKeyOpts.AllowNone = true;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Entered keyword: " + pKeyRes.StringResult);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">}</div>
复制代码- <div align="left"><strong>VBA/ActiveX Code Reference</strong></div><div align="left">Sub GetKeywordFromUser2()</div><div align="left"> Dim keyWord As String</div><div align="left"> ThisDrawing.Utility.InitializeUserInput 0, "Line Circle Arc"</div><div align="left"> keyWord = ThisDrawing.Utility.GetKeyword _</div><div align="left"> (vbCrLf & "Enter an option [Line/Circle/Arc] <Arc>: ")</div><div align="left"> If keyWord = "" Then keyWord = "Arc"</div><div align="left"> MsgBox keyWord, , "GetKeyword Example"</div><div align="left">End Sub</div>
复制代码
4、Control User Input 控制用户输入When collecting input from the user, you want to make sure you limit the type of information they can enter so you can get the desired response. The various prompt option objects are used to not only define the prompt displayed at the Command prompt, but also restrict the input that the user can provide. With some of the input methods, not only can you get a return value based on the type of method used but also get a keyword. 当收集用户输入时,我们要确保能够限制用户输入信息的类型,这样我们就可以得到我们想要的回应。使用各种不同的提示选项对象,不仅可以定义Command提示上显示的提示信息,而且可以限制用户的输入。使用有些输入方法,我们不仅可以获得基于方法所用类型的返回值,而且还可以获得关键字。 For example, you can use the GetPoint method to have the user specify a point or respond with a keyword. This is how commands like LINE, CIRCLE, and PLINE work. 例如我们可以使用GetPoint方法让用户指定一个点或回应以一个关键字,就像使用LINE、CIRCLE及PLINE这样的命令那样。 Get an integer value or a keyword 获取一个整数或一个关键字
The following example prompts the user for a positive non-zero integer value or a keyword. 下例提示用户输入一个非零的正整数或一个关键字。 VB.NETImports Autodesk.AutoCAD.ApplicationServices - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.EditorInput</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.Runtime</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><CommandMethod("GetIntegerOrKeywordFromUser")> _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Public Sub GetIntegerOrKeywordFromUser()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pIntOpts As PromptIntegerOptions = New PromptIntegerOptions("")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Message = vbCrLf & "Enter the size or "</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Restrict input to positive and non-negative values</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.AllowZero = False</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.AllowNegative = False</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Define the valid keywords and allow Enter</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Keywords.Add("Big")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Keywords.Add("Small")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Keywords.Add("Regular")</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Keywords.Default = "Regular"</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.AllowNone = True</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Get the value entered by the user</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim pIntRes As PromptIntegerResult = acDoc.Editor.GetInteger(pIntOpts)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> If pIntRes.Status = PromptStatus.Keyword Then</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Entered keyword: " & pIntRes.StringResult)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Else</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Entered value: " & pIntRes.Value.ToString())</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> End If</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Sub</div>
复制代码C#using Autodesk.AutoCAD.ApplicationServices; - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.EditorInput;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.Runtime;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">[CommandMethod("GetIntegerOrKeywordFromUser")]</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">public static void GetIntegerOrKeywordFromUser()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">{</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptIntegerOptions pIntOpts = new PromptIntegerOptions("");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Message = "\nEnter the size or ";</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Restrict input to positive and non-negative values</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> //限制输入必须大于0;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.AllowZero = false;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.AllowNegative = false;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Define the valid keywords and allow Enter</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> //定义合法关键字并允许直接按Enter键;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Keywords.Add("Big");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Keywords.Add("Small");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Keywords.Add("Regular");</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.Keywords.Default = "Regular";</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> pIntOpts.AllowNone = true;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Get the value entered by the user</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> //获取用户键入的值</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> PromptIntegerResult pIntRes = acDoc.Editor.GetInteger(pIntOpts);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> if (pIntRes.Status == PromptStatus.Keyword)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> {</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Entered keyword: " + pIntRes.StringResult);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> }</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> else</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> {</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Entered value: " + pIntRes.Value.ToString());</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> }</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">}</div>
复制代码VBA/ActiveX Code ReferenceSub GetIntegerOrKeywordFromUser() - <div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' The first parameter of InitializeUserInput (6)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' restricts input to positive and non-negative</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' values. The second parameter is the list of</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' valid keywords.</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ThisDrawing.Utility.InitializeUserInput 6, "Big Small Regular"</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' Set the prompt string variable</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim promptStr As String</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> promptStr = vbCrLf & "Enter the size or [Big/Small/Regular] <Regular>:"</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' At the GetInteger prompt, entering a keyword or pressing</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' ENTER without entering a value results in an error. To allow</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' your application to continue and check for the error</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' description, you must set the error handler to resume on error.</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> On Error Resume Next</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' Get the value entered by the user</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim returnInteger As Integer</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> returnInteger = ThisDrawing.Utility.GetInteger(promptStr)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' Check for an error. If the error number matches the</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' one shown below, then use GetInput to get the returned</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' string; otherwise, use the value of returnInteger.</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> If Err.Number = -2145320928 Then</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim returnString As String</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Debug.Print Err.Description</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> returnString = ThisDrawing.Utility.GetInput()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> If returnString = "" Then 'ENTER returns null string</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> returnString = "Regular" 'Set to default</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> End If</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Err.Clear</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Else 'Otherwise,</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> returnString = returnInteger 'Use the value entered</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> End If</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' Display the result</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> MsgBox returnString, , "InitializeUserInput Example"</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Sub</div>
复制代码 |