當使用者在畫面選取某一個構建的時候,如何透過Revit API 取得被選取構建的相關資訊。

 

首先畫面的資訊皆是由UIDocument 進行控制與管理,透過Selection取得使用者目前選取的所有構建的編號(ElementIds),如第15行。

取得ElementId 之後可以透過GetElement(ElementId)取得實際的構建物件,在針對該物件進行操作,如第27行。

以下程式範例簡單的取得構建的ID 並顯示構建得名稱。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[Transaction(TransactionMode.Manual)]
public class Document_Selection : IExternalCommand
{
    public 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)
                {
                    Element element = uidoc.Document.GetElement(id);
                    info += "\n\t" + id.IntegerValue + "---name---> " + element.Name;
                }

                TaskDialog.Show("Revit", info);
            }
        }
        catch (Exception e)
        {
            message = e.Message;
            return Autodesk.Revit.UI.Result.Failed;
        }

        return Autodesk.Revit.UI.Result.Succeeded;
    }
}

 

程式中Element的Name表示得就是Revit操作時,點擊構建後查看Properties 時所看到的構建名稱。

arrow
arrow
    文章標籤
    revit
    全站熱搜
    創作者介紹
    創作者 Lung-Yu,Tsai 的頭像
    Lung-Yu,Tsai

    Lung-Yu,Tsai 的部落格

    Lung-Yu,Tsai 發表在 痞客邦 留言(0) 人氣()