ウォークスルー: 選択した要素を取得する

ウォークスルー: 選択した要素を取得する

このセクションでは、Revit から選択した要素を取得するアドイン アプリケーションについて説明します。

アドインアプリケーションでは、特定の要素に対する特定の操作を実行できます。たとえば、要素のパラメータ値を取得または変更することができます。パラメータ値を取得するには、次の手順を実行します。

  1. 新しいプロジェクトを作成し、前のウォークスルーで簡単に説明したとおりに参照を追加します。
  2. 選択したオブジェクトを取得するには、UIApplication.ActiveUIDocument.Selection.Elements プロパティを使用します。

選択したオブジェクトは、Revit SelElementSet です。ElementSet を検索するには、IEnumerator インタフェースまたは foreach loop を使用します。

次のコードは、選択した要素を取得する方法の一例です。

コード領域 2-7: 選択した要素を取得

[Autodesk.Revit.Attributes.Transaction(TransactionMode.ReadOnly)]    
public class Document_Selection : IExternalCommand
{
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
                ref string message, ElementSet elements)
        {
                try
                {
                        // Select some elements in Revit before invoking this command
                        
                        // Get the handle of current document.
                        UIDocument uidoc = commandData.Application.ActiveUIDocument;
                        // Get the element selection of current document.
                        Selection selection = uidoc.Selection;
                        ElementSet collection = selection.Elements;
 
                        if (0 == collection.Size)
                        {
                                // If no elements selected.
                                TaskDialog.Show("Revit","You haven't selected any elements.");
                        }
                        else
                        {
                                String info = "Ids of selected elements in the document are: ";
                                foreach (Element elem in collection)
                                {
                                        info += "\n\t" + elem.Id.IntegerValue;
                                }
                                
                                TaskDialog.Show("Revit",info);
                }
                }
                catch (Exception e)
                {
                        message = e.Message;
                        return Autodesk.Revit.UI.Result.Failed;
                }
                
                return Autodesk.Revit.UI.Result.Succeeded;
        }     
}

選択した要素を取得した後は、要素のプロパティまたはパラメータを取得できます。詳細については、「パラメータ」を参照してください。