【问题标题】:Modify selection to first element by Selection.SetElementIds通过 Selection.SetElementIds 将选择修改为第一个元素
【发布时间】:2019-06-16 22:25:07
【问题描述】:

我在编写让 med 选择我选择的第一个元素的脚本时遇到问题。这对我很有用,因为我从时间表中选择了正确的 Air Terminal(在那里我可以看到我想要使用的类似气流),并从选择中使用命令 Create Similar。 The trouble is that this command does not work when multiple elements are selected.因此,我想要列表中的第一个对象。 这是我正在尝试的代码:

from Autodesk.Revit.UI.Selection.Selection import SetElementIds
from System.Collections.Generic import List

uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
selection = [ doc.GetElement( elId ) for elId in __revit__.ActiveUIDocument.Selection.GetElementIds() ]
sel=[]
for i in selection:
    sel.append(i.Id)
uidoc.Selection.SetElementIds(List[ElementId](sel[0]))

这将返回以下错误消息:

Exception : Microsoft.Scripting.ArgumentTypeException: expected int, got ElementId 好的,那我试试替换

uidoc.Selection.SetElementIds(List[ElementId](sel[0]))

uidoc.Selection.SetElementIds(List[ElementId](sel[0].IntegerValue))

This seems to work, but selection is not modified

我刚刚开始编写 RPS 脚本,但我希望有人能告诉我我在这里做错了什么,即使它很明显。

谢谢。 凯尔

编辑: 谢谢杰里米,为我解决了这个问题!诀窍是生成一个列表,而不是 python 列表。 .add 方法是我没有得到的。

如果有人感兴趣,最终代码:

from Autodesk.Revit.UI.Selection.Selection import SetElementIds
from System.Collections.Generic import List
from Autodesk.Revit.DB import ElementId
uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
selection = [ doc.GetElement( elId ) for elId in __revit__.ActiveUIDocument.Selection.GetElementIds() ]
sel=[]
for i in selection:
    sel.append(i.Id)
ids=List[ElementId](1)
ids.Add(sel[0]) 
uidoc.Selection.SetElementIds(ids)

【问题讨论】:

    标签: revit-api revitpythonshell pyrevit


    【解决方案1】:

    SetElementIds 采用 .NET ICollection<ElementId> 参数,您可以从 Revit API documentation 中看到。

    您的语句调用 .NET List 构造函数,该构造函数需要一个整数参数,该参数指定要为 List[ElementId](N) 分配空间的元素数量 N

    sel[0]ElementId,而不是整数,这会导致第一个错误。

    sel[0].IntegerValue 是一个(非常大且半任意的)整数,因此不会导致错误。但是,您仍然将 List 留空,无人居住。

    您应该为单个元素初始化 List 并添加:

    ids = List[ElementId](1)
    ids.Add(sel[0])
    

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多