複数の選択セットを結合するには、ObjectIdCollection オブジェクトを作成し、複数の選択セットからのオブジェクト ID を一緒に追加します。ObjectIdCollection オブジェクトにオブジェクト ID を追加するだけでなく、オブジェクト ID を削除することができます。すべてのオブジェクト ID が ObjectIdCollection オブジェクトに追加されたら、オブジェクト ID のコレクションを反復処理し、必要に応じて各オブジェクトを操作できます。
この例では、オブジェクトを 2 回選択し、作成された 2 つの選択セットを 1 つの選択セットに結合するようユーザに求めます。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
[CommandMethod("MergeSelectionSets")]
public static void MergeSelectionSets()
{
// Get the current document editor
Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
// Request for objects to be selected in the drawing area
PromptSelectionResult acSSPrompt;
acSSPrompt = acDocEd.GetSelection();
SelectionSet acSSet1;
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
// If the prompt status is OK, objects were selected
if (acSSPrompt.Status == PromptStatus.OK)
{
// Get the selected objects
acSSet1 = acSSPrompt.Value;
// Append the selected objects to the ObjectIdCollection
acObjIdColl = new ObjectIdCollection(acSSet1.GetObjectIds());
}
// Request for objects to be selected in the drawing area
acSSPrompt = acDocEd.GetSelection();
SelectionSet acSSet2;
// If the prompt status is OK, objects were selected
if (acSSPrompt.Status == PromptStatus.OK)
{
acSSet2 = acSSPrompt.Value;
// Check the size of the ObjectIdCollection, if zero, then initialize it
if (acObjIdColl.Count == 0)
{
acObjIdColl = new ObjectIdCollection(acSSet2.GetObjectIds());
}
else
{
// Step through the second selection set
foreach (ObjectId acObjId in acSSet2.GetObjectIds())
{
// Add each object id to the ObjectIdCollection
acObjIdColl.Add(acObjId);
}
}
}
Application.ShowAlertDialog("Number of objects selected: " +
acObjIdColl.Count.ToString());
}