アプリケーションは、キーワードを使用して、ユーザに選択方法を指定するように求めるプロンプトを表示してから、実際に選択セットを作成することができます。
キーワードは、PromptSelectionOptions オブジェクトのインスタンスを作成することによって、オブジェクトの選択操作に割り当てることができます。PromptSelectionOptions オブジェクトを作成したら、SetKeywords メソッドを使用して、ユーザーがコマンドのプロンプトに対して入力できる個々のキーワードを割り当てます。キーワードを PromptSelectionOptions オブジェクトに割り当てたら、今度は PromptSelectionOptions オブジェクトをエディタの GetSelection メソッドに渡す必要があります。
ユーザーが Select objects: (「オブジェクトを選択」)というプロンプトに対して入力できるキーワードの実装は、PromptSelectionOptions.KeywordInput イベントハンドラで処理されます。この結果表示されたプロンプトにユーザーがキーワードを入力すると、KeywordInput イベントが発生し、アプリケーションのハンドラが呼び出されます。
KeywordInput ハンドラは、入力と出力のパラメータの両方として機能する SelectionTextInputEventArgs 引数を受け取ります。SelectionTextInputEventArgs 引数の Input プロパティは、選択されたキーワードを示します。典型的なハンドラは、このキーワードをアプリケーションのキーワード リストのキーワードと比較して、適切な選択方法を呼び出します。選択方法によって図形が返されると、アプリケーションはこれらの図形を SelectionTextInputEventArgs.AddObjects メソッドを使用して SelectionTextInputEventArgs 引数に追加します。元の GetSelection 呼び出しは、戻るときにキーワード リストを設定したメソッドに、選択された図形を提供します。
次の例では、5 つのキーワードを定義し、ユーザが選択したキーワードをサポートするためにハンドラを追加します。
private static void SelectionKeywordInputHandler(object sender, SelectionTextInputEventArgs eSelectionInput)
{
// Gets the current document editor and define other variables for the current scope
Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionResult acSSPrompt = null;
SelectionSet acSSet = null;
ObjectId[] acObjIds = null;
// See if the user choose the myFence keyword
switch (eSelectionInput.Input) {
case "myFence":
// Uses the four points to define a fence selection
Point3dCollection ptsFence = new Point3dCollection();
ptsFence.Add(new Point3d(5.0, 5.0, 0.0));
ptsFence.Add(new Point3d(13.0, 15.0, 0.0));
ptsFence.Add(new Point3d(12.0, 9.0, 0.0));
ptsFence.Add(new Point3d(5.0, 5.0, 0.0));
acSSPrompt = acDocEd.SelectFence(ptsFence);
break;
case "myWindow":
// Defines a rectangular window selection
acSSPrompt = acDocEd.SelectWindow(new Point3d(1.0, 1.0, 0.0), new Point3d(30.0, 20.0, 0.0));
break;
case "myWPoly":
// Uses the four points to define a polygon window selection
Point3dCollection ptsPolygon = new Point3dCollection();
ptsPolygon.Add(new Point3d(5.0, 5.0, 0.0));
ptsPolygon.Add(new Point3d(13.0, 15.0, 0.0));
ptsPolygon.Add(new Point3d(12.0, 9.0, 0.0));
ptsPolygon.Add(new Point3d(5.0, 5.0, 0.0));
acSSPrompt = acDocEd.SelectWindowPolygon(ptsPolygon);
break;
case "myLastSel":
// Gets the last object created
acSSPrompt = acDocEd.SelectLast();
break;
case "myPrevSel":
// Gets the previous object selection set
acSSPrompt = acDocEd.SelectPrevious();
break;
}
// If the prompt status is OK, objects were selected and return
if (acSSPrompt != null)
{
if (acSSPrompt.Status == PromptStatus.OK)
{
// Objects were selected, so add them to the current selection
acSSet = acSSPrompt.Value;
acObjIds = acSSet.GetObjectIds();
eSelectionInput.AddObjects(acObjIds);
}
}
}
[CommandMethod("SelectionKeywordInput")]
public static void SelectionKeywordInput()
{
// Gets the current document editor
Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
// Setups the keyword options
PromptSelectionOptions acKeywordOpts = new PromptSelectionOptions();
acKeywordOpts.Keywords.Add("myFence");
acKeywordOpts.Keywords.Add("myWindow");
acKeywordOpts.Keywords.Add("myWPoly");
acKeywordOpts.Keywords.Add("myLastSel");
acKeywordOpts.Keywords.Add("myPrevSel");
// Adds the event handler for keyword input
acKeywordOpts.KeywordInput += new SelectionTextInputEventHandler(SelectionKeywordInputHandler);
// Prompts the user for a selection set
PromptSelectionResult acSSPrompt = acDocEd.GetSelection(acKeywordOpts);
// If the prompt status is OK, objects were selected
if (acSSPrompt.Status == PromptStatus.OK)
{
// Gets the selection set
SelectionSet acSSet = acSSPrompt.Value;
// Gets the objects from the selection set
ObjectId[] acObjIds = acSSet.GetObjectIds();
Database acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
// Starts a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
try
{
// Gets information about each object
foreach (ObjectId acObjId in acObjIds)
{
Entity acEnt = (Entity)acTrans.GetObject(acObjId, OpenMode.ForWrite, true);
acDocEd.WriteMessage("\nObject selected: " + acEnt.GetType().FullName);
}
}
finally
{
acTrans.Dispose();
}
}
}
// Removes the event handler for keyword input
acKeywordOpts.KeywordInput -= new SelectionTextInputEventHandler(SelectionKeywordInputHandler);
}