ユーザからの入力を収集するときに、目的の結果を得られるように入力できる情報の種類を制限することがあります。コマンド プロンプトに表示するプロンプトを定義するだけでなく、ユーザが入力できる内容を制限するために、多様なプロンプト オプション オブジェクトが使用されます。入力メソッドによっては、使用するメソッドの種類に基づいた戻り値を取得できるだけでなく、キーワードも取得できます。
たとえば、GetPoint メソッドを使用して、ユーザが点を指定するかキーワードで回答するようにすることができます。次に、LINE[線分]、CIRCLE[円]、PLINE[ポリライン]などのコマンドの機能を示します。
次の例は、正のゼロではない整数値またはキーワードの入力をユーザに要求します。
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
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
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());
}
}