AutoCAD .Net中的Editor类提供了各种获取用户输入的方法,常用的有:
* GetInteger 获取整型数值
* GetDouble 获取浮点型数值
* GetDistance 获取距离值
* GetAngle 获取角度值
* GetPoint 获取坐标点
* GetString 获取字符串
* GetKeywords 获取关键字
GetInteger 的用法
Document doc = Application.DocumentManager.MdiActiveDocument;
PromptIntegerOptions options = new PromptIntegerOptions("\n请输入整数: ");
PromptIntegerResult result = doc.Editor.GetInteger(options);
switch (result.Status)
{
case PromptStatus.OK:
doc.Editor.WriteMessage("\nValue = " + result.Value);
break;
case PromptStatus.Cancel:
doc.Editor.WriteMessage("\n用户取消了输入");
break;
default:
break;
}
PromptIntegerOptions 用于设置用户输入的控制项
* AllowNone 是否允许空输入,默认为false。
* DefaultValue 默认值。
* AllowZero 是否允许输入0。
* AllowNegative 是否允许输入负值。
* LowerLimit 控制所允许的最小值。
* UpperLimit 控制所允许的最大值。
* AllowArbitraryInput 是否允许任意输入,默认为false,在这种情况下,当用户输入无效时,比如输入的是英文字母等,GetInteger并不返回,而是提示用户继续输入。当AllowArbitraryInput 设置为true,可以接受任何无效输入,程序需要处理无效输入的情况。
简单应用
Document doc = Application.DocumentManager.MdiActiveDocument;
int value = 0;
PromptIntegerOptions options = new PromptIntegerOptions("\n请输入整数: ");
options.AllowNone = true;
options.DefaultValue = 50;
options.LowerLimit = -100;
options.UpperLimit = 100;
PromptIntegerResult result = doc.Editor.GetInteger(options);
switch (result.Status)
{
case PromptStatus.OK:
value = result.Value;
doc.Editor.WriteMessage("\nValue = " + value);
break;
// 空输入
case PromptStatus.None:
value = options.DefaultValue;
doc.Editor.WriteMessage("\nValue = " + value);
break;
case PromptStatus.Cancel:
doc.Editor.WriteMessage("\n用户取消了输入");
break;
default:
break;
}
允许任意输入
Document doc = Application.DocumentManager.MdiActiveDocument;
int value = 0;
PromptIntegerOptions options = new PromptIntegerOptions("\n请输入整数: ");
options.AllowNone = true;
options.DefaultValue = 50;
options.AllowArbitraryInput = true;
PromptIntegerResult result = doc.Editor.GetInteger(options);
switch (result.Status)
{
case PromptStatus.OK:
value = result.Value;
doc.Editor.WriteMessage("\nValue = " + value);
break;
// 空输入
case PromptStatus.None:
value = options.DefaultValue;
doc.Editor.WriteMessage("\nValue = " + value);
break;
// 任意输入
case PromptStatus.Keyword:
if (result.StringResult == "HelloWorld")
{
doc.Editor.WriteMessage("\nHello World! --- AutoCAD");
}
else
{
doc.Editor.WriteMessage("\n用户输入了无效的值");
}
break;
case PromptStatus.Cancel:
doc.Editor.WriteMessage("\n用户取消了输入");
break;
default:
break;
}
关键字
Document doc = Application.DocumentManager.MdiActiveDocument;
int value = 0;
PromptIntegerOptions options = new PromptIntegerOptions("\n请输入整数: ");
options.AllowNone = true;
options.DefaultValue = 50;
options.Keywords.Add("Circle", "Circle", "圆(L)", true, true);
options.Keywords.Add("Line", "Line", "直线(L)", true, true);
options.Keywords.Add("Esc", "Esc", "退出(Esc)", true, true);
options.AppendKeywordsToMessage = true;
PromptIntegerResult result = doc.Editor.GetInteger(options);
switch (result.Status)
{
case PromptStatus.OK:
value = result.Value;
doc.Editor.WriteMessage("\nValue = " + value);
break;
// 空输入
case PromptStatus.None:
value = options.DefaultValue;
doc.Editor.WriteMessage("\nValue = " + value);
break;
// 关键字
case PromptStatus.Keyword:
if (result.StringResult == "Circle")
{
doc.Editor.WriteMessage("\n用户输入了关键字: Circle");
}
else if (result.StringResult == "Line")
{
doc.Editor.WriteMessage("\n用户输入了关键字: Line");
}
else if (result.StringResult == "Esc")
{
doc.Editor.WriteMessage("\n用户退出");
}
break;
case PromptStatus.Cancel:
doc.Editor.WriteMessage("\n用户取消了输入");
break;
default:
break;
}