このセクションでは、Revit から選択した要素を取得するアドイン アプリケーションについて説明します。
アドインアプリケーションでは、特定の要素に対する特定の操作を実行できます。たとえば、要素のパラメータ値を取得または変更することができます。パラメータ値を取得するには、次の手順を実行します。
GetElementIds()は、選択した要素の ElementIds のコレクションを返します。これは foreach ループで繰り返すことができます。選択した各 ElementId の Element オブジェクトを取得するには、Document.GetElement()メソッドを使用します。
次のコードは、選択した要素の ID を取得する方法の例です。
|
コード領域 2-7: 選択した要素を取得 |
using System;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using System.Collections.Generic;
namespace Revit_Snippets
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.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;
ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
if (0 == selectedIds.Count)
{
// 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 (ElementId id in selectedIds)
{
info += "\n\t" + id.IntegerValue;
}
TaskDialog.Show("Revit",info);
}
}
catch (Exception e)
{
message = e.Message;
return Autodesk.Revit.UI.Result.Failed;
}
return Autodesk.Revit.UI.Result.Succeeded;
}
/// </ExampleMethod>
}
}
|
選択した要素を取得した後は、要素のプロパティまたはパラメータを取得できます。詳細については、「パラメータ」を参照してください。