ユーザ選択

ユーザ選択

Selection クラスには、新しいオブジェクト、または画面上でポイントを選択できるメソッドがあります。これを使用すると、カーソルを使用して 1 つ以上の要素(またはエッジや面などの他のオブジェクト)を選択し、アプリケーションにコントロールを返すことができます。これらの関数は、新しい選択をアクティブな選択した集合に自動的に追加しません。

選択するオブジェクトのタイプは、PickObject()または PickObjects の呼び出し時に指定されます。指定できるオブジェクトのタイプは、要素、PointOnElement、エッジまたは面です。

StatusbarTip プロパティは、アプリケーションがオブジェクトまたは要素を選択するよう表示すると、ステータス バーにメッセージを表示します。各 Pick 関数には、カスタム ステータス メッセージを提供できる String パラメータを持つオーバーロードがあります。

領域 7-2: PickObject()および PickElementsByRectangle()を使用して選択した要素を追加

UIDocument uidoc = new UIDocument(document);
Selection choices = uidoc.Selection;
// Pick one object from Revit.
Reference hasPickOne = choices.PickObject(ObjectType.Element);
if (hasPickOne != null)
{
    TaskDialog.Show("Revit", "One element selected.");
}

// Use the rectangle picking tool to identify model elements to select.
IList<Element> pickedElements = uidoc.Selection.PickElementsByRectangle("Select by rectangle");
if (pickedElements.Count > 0)
{ 
    // Collect Ids of all picked elements
    IList<ElementId> idsToSelect = new List<ElementId>(pickedElements.Count);
    foreach (Element element in pickedElements)
    {
        idsToSelect.Add(element.Id);
    }
                
    // Update the current selection
    uidoc.Selection.SetElementIds(idsToSelect);
    TaskDialog.Show("Revit", string.Format("{0} elements added to Selection.", idsToSelect.Count));
}

PickPoint()メソッドのスナップの種類の選択に使用されるタイプを指定するために使用される、ObjectSnapTypes パラメータを持つ 2 つのオーバーロードがあります。次の例に示すように、複数を指定できます。

コード領域 7-3: スナップ ポイント

public void PickPoint(UIDocument uidoc)
{
        
        ObjectSnapTypes snapTypes = ObjectSnapTypes.Endpoints | ObjectSnapTypes.Intersections;
        XYZ point = uidoc.Selection.PickPoint(snapTypes, "Select an end point or intersection");
        
        string strCoords = "Selected point is " + point.ToString();
        
        TaskDialog.Show("Revit", strCoords);
}

PickBox()メソッドは、PickBoxStyle エニュメレータを取得します。オプションには、完全にまたは一部がボックス内にあるオブジェクトを選択した場合に使用されるスタイルである Crossing、ボックスで完全に閉じられたオブジェクトを選択するのに使用されるスタイルである Enclosing、ボックスのスタイルがボックスが描画されている方向に依存する Directional があります。右から左に描画されている場合は、Crossing スタイルを使用し、反対の方向に描画される場合は Enclosing スタイルを使用します。

PickBox()は、選択された最小ポイントおよび最大ポイントが含まれる PickedBox を返します。次の例では、点群選択での PickBox()の使用方法を示しています。

コード領域: PickBox

public void PromptForPointCloudSelection(UIDocument uiDoc, PointCloudInstance pcInstance)
{
    Application app = uiDoc.Application.Application;
    Selection currentSel = uiDoc.Selection;

    PickedBox pickedBox = currentSel.PickBox(PickBoxStyle.Enclosing, "Select region of cloud for highlighting");

    XYZ min = pickedBox.Min;
    XYZ max = pickedBox.Max;

    //Transform points into filter
    View view = uiDoc.ActiveView;
    XYZ right = view.RightDirection;
    XYZ up = view.UpDirection;

    List<Plane> planes = new List<Plane>();

    // X boundaries
    bool directionCorrect = IsPointAbovePlane(right, min, max);
    planes.Add(app.Create.NewPlane(right, directionCorrect ? min : max));
    planes.Add(app.Create.NewPlane(-right, directionCorrect ? max : min));

    // Y boundaries
    directionCorrect = IsPointAbovePlane(up, min, max);
    planes.Add(app.Create.NewPlane(up, directionCorrect ? min : max));
    planes.Add(app.Create.NewPlane(-up, directionCorrect ? max : min));

    // Create filter
    PointCloudFilter filter = PointCloudFilterFactory.CreateMultiPlaneFilter(planes);
    Transaction t = new Transaction(uiDoc.Document, "Highlight");
    t.Start();
    pcInstance.SetSelectionFilter(filter);
    pcInstance.FilterAction = SelectionFilterAction.Highlight;
    t.Commit();
    uiDoc.RefreshActiveView();
}

private static bool IsPointAbovePlane(XYZ normal, XYZ planePoint, XYZ point)
{
    XYZ difference = point - planePoint;
    difference = difference.Normalize();
    double dotProduct = difference.DotProduct(normal);
    return dotProduct > 0;
}