GetElementIds() 获取当前选择的问题

Problem with GetElementIds() to get current selection

我有一个小代码可以从 revit 中提取当前选择,但我收到一条错误消息,指出预期的选择,得到列表[elementId]

我也试过在 API 中传递 GetElementIds() 但它说它只需要 1 个参数(给定 0)

import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI.Selection import *

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

# Import iron python packages
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)



app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document

#get current selection from Document

from Autodesk.Revit.UI import Selection
from System.Collections.Generic import List as CL

sel = CL[ElementId]()
Selected=Selection.Selection.GetElementIds()
print(len(Selected))

这行代码中的错误:

Selected=Selection.Selection.GetElementIds()

您需要在ui中添加一个link,然后您可以使用它来获取选择:

ui = __revit__.ActiveUIDocument
Selected=ui.Selection.GetElementIds()

以下是获取用户选择的完整代码:

import clr
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
ui = __revit__.ActiveUIDocument

selectedElements = []
for elementId in ui.Selection.GetElementIds():
    selectedElements.Add(doc.GetElement(elementId))

for item in selectedElements:
    print item