02-08 控制AutoCAD环境(八) 提示用户输入

前端之家收集整理的这篇文章主要介绍了02-08 控制AutoCAD环境(八) 提示用户输入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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本节主题

· GetString Method GetString方法

· GetPoint Method GetPoint方法

· GetKeywords Method GetKeywords方法

· Control User Input 控制用户输入

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

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Runtime

<CommandMethod("GetStringFromUser")> _

Public Sub GetStringFromUser()

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim pStrOpts As PromptStringOptions = New PromptStringOptions(vbLf & _

"Enter your name: ")

pStrOpts.AllowSpaces = True

Dim pStrRes As PromptResult = acDoc.Editor.GetString(pStrOpts)

Application.ShowAlertDialog("The name entered was: " & _

pStrRes.StringResult)

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

[CommandMethod("GetStringFromUser")]

public static void GetStringFromUser()

{

Document acDoc = Application.DocumentManager.MdiActiveDocument;

PromptStringOptions pStrOpts = new PromptStringOptions("\nEnter your name: ");

pStrOpts.AllowSpaces = true;

PromptResult pStrRes = acDoc.Editor.GetString(pStrOpts);

Application.ShowAlertDialog("The name entered was: " +

pStrRes.StringResult);

}

VBA/ActiveX Code Reference

Sub GetStringFromUser()

Dim retVal As String

retVal = ThisDrawing.Utility.GetString _

(1,vbCrLf & "Enter your name: ")

MsgBox "The name entered was: " & retVal

End Sub

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

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Geometry

Imports Autodesk.AutoCAD.Runtime

<CommandMethod("GetPointsFromUser")> _

Public Sub GetPointsFromUser()

'' Get the current database and start the Transaction Manager

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim acCurDb As Database = acDoc.Database

Dim pPtRes As PromptPointResult

Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")

'' Prompt for the start point

pPtOpts.Message = vbLf & "Enter the start point of the line: "

pPtRes = acDoc.Editor.GetPoint(pPtOpts)

Dim ptStart As Point3d = pPtRes.Value

'' Exit if the user presses ESC or cancels the command

If pPtRes.Status = PromptStatus.Cancel Then Exit Sub

'' Prompt for the end point

pPtOpts.Message = vbLf & "Enter the end point of the line: "

pPtOpts.UseBasePoint = True

pPtOpts.BasePoint = ptStart

pPtRes = acDoc.Editor.GetPoint(pPtOpts)

Dim ptEnd As Point3d = pPtRes.Value

If pPtRes.Status = PromptStatus.Cancel Then Exit Sub

'' Start a transaction

Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

Dim acBlkTbl As BlockTable

Dim acBlkTblRec As BlockTableRecord

'' Open Model space for write

acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,_

OpenMode.ForRead)

acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace),_

OpenMode.ForWrite)

'' Define the new line

Dim acLine As Line = New Line(ptStart,ptEnd)

'' Add the line to the drawing

acBlkTblRec.AppendEntity(acLine)

acTrans.AddNewlyCreatedDBObject(acLine,True)

'' Zoom to the extents or limits of the drawing

acDoc.SendStringToExecute("._zoom _all ",True,False,False)

'' Commit the changes and dispose of the transaction

acTrans.Commit()

End Using

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.Runtime;

[CommandMethod("GetPointsFromUser")]

public static void GetPointsFromUser()

{

// Get the current database and start the Transaction Manager

Document acDoc = Application.DocumentManager.MdiActiveDocument;

Database acCurDb = acDoc.Database;

PromptPointResult pPtRes;

PromptPointOptions pPtOpts = new PromptPointOptions("");

// Prompt for the start point

pPtOpts.Message = "\nEnter the start point of the line: ";

pPtRes = acDoc.Editor.GetPoint(pPtOpts);

Point3d ptStart = pPtRes.Value;

// Exit if the user presses ESC or cancels the command

if (pPtRes.Status == PromptStatus.Cancel) return;

// Prompt for the end point

pPtOpts.Message = "\nEnter the end point of the line: ";

pPtOpts.UseBasePoint = true;

pPtOpts.BasePoint = ptStart;

pPtRes = acDoc.Editor.GetPoint(pPtOpts);

Point3d ptEnd = pPtRes.Value;

if (pPtRes.Status == PromptStatus.Cancel) return;

// Start a transaction

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())

{

BlockTable acBlkTbl;

BlockTableRecord acBlkTblRec;

// Open Model space for write

acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead) as BlockTable;

acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;

// Define the new line

Line acLine = new Line(ptStart,ptEnd);

// Add the line to the drawing

acBlkTblRec.AppendEntity(acLine);

acTrans.AddNewlyCreatedDBObject(acLine,true);

// Zoom to the extents or limits of the drawing

acDoc.SendStringToExecute("._zoom _all ",true,false,false);

// Commit the changes and dispose of the transaction

acTrans.Commit();

}

}

VBA/ActiveX Code Reference

Sub GetPointsFromUser()

Dim startPnt As Variant

Dim endpnt As Variant

Dim prompt1 As String

Dim prompt2 As String

prompt1 = vbCrLf & "Enter the start point of the line: "

prompt2 = vbCrLf & "Enter the end point of the line: "

' Get the first point without entering a base point

startPnt = ThisDrawing.Utility.GetPoint(,prompt1)

' Use the point entered above as the base point

endpnt = ThisDrawing.Utility.GetPoint(startPnt,prompt2)

' Create a line using the two points entered

ThisDrawing.ModelSpace.AddLine startPnt,endpnt

ThisDrawing.Application.ZoomAll

End Sub

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

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Runtime

<CommandMethod("GetKeywordFromUser")> _

Public Sub GetKeywordFromUser()

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim pKeyOpts As PromptKeywordOptions = New PromptKeywordOptions("")

pKeyOpts.Message = vbLf & "Enter an option "

pKeyOpts.Keywords.Add("Line")

pKeyOpts.Keywords.Add("Circle")

pKeyOpts.Keywords.Add("Arc")

pKeyOpts.AllowNone = False

Dim pKeyRes As PromptResult = acDoc.Editor.GetKeywords(pKeyOpts)

Application.ShowAlertDialog("Entered keyword: " &pKeyRes.StringResult)

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

[CommandMethod("GetKeywordFromUser")]

public static void GetKeywordFromUser()

{

Document acDoc = Application.DocumentManager.MdiActiveDocument;

PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");

pKeyOpts.Message = "\nEnter an option ";

pKeyOpts.Keywords.Add("Line");

pKeyOpts.Keywords.Add("Circle");

pKeyOpts.Keywords.Add("Arc");

pKeyOpts.AllowNone = false;

PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);

Application.ShowAlertDialog("Entered keyword: " + pKeyRes.StringResult);

}

VBA/ActiveX Code Reference

Sub GetKeywordFromUser()

Dim keyWord As String

ThisDrawing.Utility.InitializeUserInput 1,"Line Circle Arc"

keyWord = ThisDrawing.Utility.GetKeyword _

(vbCrLf & "Enter an option [Line/Circle/Arc]: ")

MsgBox keyWord,"GetKeyword Example"

End Sub

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

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Runtime

<CommandMethod("GetKeywordFromUser2")> _

Public Sub GetKeywordFromUser2()

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim pKeyOpts As PromptKeywordOptions = New PromptKeywordOptions("")

pKeyOpts.Message = vbLf & "Enter an option "

pKeyOpts.Keywords.Add("Line")

pKeyOpts.Keywords.Add("Circle")

pKeyOpts.Keywords.Add("Arc")

pKeyOpts.Keywords.Default = "Arc"

pKeyOpts.AllowNone = True

Dim pKeyRes As PromptResult = acDoc.Editor.GetKeywords(pKeyOpts)

Application.ShowAlertDialog("Entered keyword: " & pKeyRes.StringResult)

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

[CommandMethod("GetKeywordFromUser2")]

public static void GetKeywordFromUser2()

{

Document acDoc = Application.DocumentManager.MdiActiveDocument;

PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");

pKeyOpts.Message = "\nEnter an option ";

pKeyOpts.Keywords.Add("Line");

pKeyOpts.Keywords.Add("Circle");

pKeyOpts.Keywords.Add("Arc");

pKeyOpts.Keywords.Default = "Arc";

pKeyOpts.AllowNone = true;

PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);

Application.ShowAlertDialog("Entered keyword: " + pKeyRes.StringResult);

}

VBA/ActiveX Code Reference

Sub GetKeywordFromUser2()

Dim keyWord As String

ThisDrawing.Utility.InitializeUserInput 0,"Line Circle Arc"

keyWord = ThisDrawing.Utility.GetKeyword _

(vbCrLf & "Enter an option [Line/Circle/Arc] <Arc>: ")

If keyWord = "" Then keyWord = "Arc"

MsgBox keyWord,"GetKeyword Example"

End Sub

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

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Runtime

<CommandMethod("GetIntegerOrKeywordFromUser")> _

Public Sub GetIntegerOrKeywordFromUser()

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim pIntOpts As PromptIntegerOptions = New PromptIntegerOptions("")

pIntOpts.Message = vbCrLf & "Enter the size or "

'' Restrict input to positive and non-negative values

pIntOpts.AllowZero = False

pIntOpts.AllowNegative = False

'' Define the valid keywords and allow Enter

pIntOpts.Keywords.Add("Big")

pIntOpts.Keywords.Add("Small")

pIntOpts.Keywords.Add("Regular")

pIntOpts.Keywords.Default = "Regular"

pIntOpts.AllowNone = True

'' Get the value entered by the user

Dim pIntRes As PromptIntegerResult = acDoc.Editor.GetInteger(pIntOpts)

If pIntRes.Status = PromptStatus.Keyword Then

Application.ShowAlertDialog("Entered keyword: " & pIntRes.StringResult)

Else

Application.ShowAlertDialog("Entered value: " & pIntRes.Value.ToString())

End If

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

[CommandMethod("GetIntegerOrKeywordFromUser")]

public static void GetIntegerOrKeywordFromUser()

{

Document acDoc = Application.DocumentManager.MdiActiveDocument;

PromptIntegerOptions pIntOpts = new PromptIntegerOptions("");

pIntOpts.Message = "\nEnter the size or ";

// Restrict input to positive and non-negative values

//限制输入必须大于0;

pIntOpts.AllowZero = false;

pIntOpts.AllowNegative = false;

// Define the valid keywords and allow Enter

//定义合法关键字并允许直接按Enter键;

pIntOpts.Keywords.Add("Big");

pIntOpts.Keywords.Add("Small");

pIntOpts.Keywords.Add("Regular");

pIntOpts.Keywords.Default = "Regular";

pIntOpts.AllowNone = true;

// Get the value entered by the user

//获取用户键入的值

PromptIntegerResult pIntRes = acDoc.Editor.GetInteger(pIntOpts);

if (pIntRes.Status == PromptStatus.Keyword)

{

Application.ShowAlertDialog("Entered keyword: " + pIntRes.StringResult);

}

else

{

Application.ShowAlertDialog("Entered value: " + pIntRes.Value.ToString());

}

}

VBA/ActiveX Code Reference

Sub GetIntegerOrKeywordFromUser()

' The first parameter of InitializeUserInput (6)

' restricts input to positive and non-negative

' values. The second parameter is the list of

' valid keywords.

ThisDrawing.Utility.InitializeUserInput 6,"Big Small Regular"

' Set the prompt string variable

Dim promptStr As String

promptStr = vbCrLf & "Enter the size or [Big/Small/Regular] <Regular>:"

' At the GetInteger prompt,entering a keyword or pressing

' ENTER without entering a value results in an error. To allow

' your application to continue and check for the error

' description,you must set the error handler to resume on error.

On Error Resume Next

' Get the value entered by the user

Dim returnInteger As Integer

returnInteger = ThisDrawing.Utility.GetInteger(promptStr)

' Check for an error. If the error number matches the

' one shown below,then use GetInput to get the returned

' string; otherwise,use the value of returnInteger.

If Err.Number = -2145320928 Then

Dim returnString As String

Debug.Print Err.Description

returnString = ThisDrawing.Utility.GetInput()

If returnString = "" Then 'ENTER returns null string

returnString = "Regular" 'Set to default

End If

Err.Clear

Else 'Otherwise,

returnString = returnInteger 'Use the value entered

End If

' Display the result

MsgBox returnString,"InitializeUserInput Example"

End Sub

原文链接:https://www.f2er.com/vb/261122.html

猜你在找的VB相关文章