Revit Python Shell - 迭代每个元素,找到视图名称并按 Sheet 编号分组

Revit Python Shell - Iterate over each Element, find View Name and group by Sheet Number

我想通过关联的 sheet 编号、视图名称和位置中心创建所有视口的列表。像这样:

vPorts = [('A0.01, View Name 01',[Center of ViewPort location]),('A0.02, View Name 01',[Centre of ViewPort location]),('A0.02, View Name 02',[Center of ViewPort location]),('A0.04, View Name 01',[Centre of ViewPort location]), etc.]

这样用户就可以在 WPF 的列表框中(此处未显示)将多个视口相互对齐。我有以下内容:

import clr
clr.AddReferenceByPartialName('PresentationCore')
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName('System')
clr.AddReferenceByPartialName('System.Windows.Forms')

from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Architecture import *
from Autodesk.Revit.DB.Analysis import *
from Autodesk.Revit.UI import *

doc = __revit__.ActiveUIDocument.Document

#List of ViewPort Elements
vPorts = []

#List for ViewPorts by sheet number, view name and location
vPortsloc = []

col_sheets = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Sheets).WhereElementIsNotElementType().ToElements()
for sheet in col_sheets:
    
    vPorts.append(sheet.GetAllViewports())

for vp in vPorts:
    print(vp)

这让我将所有工作表上的所有视口作为列表对象。 但我现在想如上所述格式化此列表。我尝试了以下方法:

for vp in vPorts:
    v = doc.GetElement(vp.ViewId)
    vPortsloc.append(v.SheetNumber + v.Name + (v.GetBoxCenter().ToPoint()))

我相信我没有遍历整个列表列表。更不用说我是 python 的新手。任何帮助将不胜感激。谢谢!

这样可以帮助可视化多维 Lists

vPortTriples  = [
    [Sheet Number, Sheet Name, ViewPort Center], 
    [Sheet Number, Sheet Name, ViewPort Center], 
    [Sheet Number, Sheet Name, ViewPort Center]
]

看起来您 appendingList 的三个独立项目。是否应该像这样将它们添加为新列表?

vPortTriples.append([v.SheetNumber, v.Name, v.GetBoxCenter().ToPoint()])

如果您真的想捕获项目中的每个 Viewport,您可以通过 Class 获取所有 ViewPorts,因此在您的代码中它看起来像:

# I find it easiest to convert FilteredElementCollector to a list
viewPorts = list(FilteredElementCollector(doc).OfClass(Viewport))
viewPortTriples = []

for vp in viewPorts:

    # need to fetch the ViewPorts Sheet 
    sheet = doc.GetElement(vp.SheetId)
    
    # add a new List to the 'viewPortTriples' List
    viewPorts.Add([sheet.SheetNumber, sheet.Name, vp.GetBoxCenter()]

虽然这是一种非常昂贵的方法,但最好只为相关的 sheet 获取它们 - 例如,通过特定的 Sheet 数字(这可能是你正在尝试的无论如何都要在你的例子中做!):

def getViewPorts (document, sheetNumber): # returns [[viewPort, bboxCenter], ... ]
    
    sheets = list(FilteredElementCollector(document).OfClass(ViewSheet))
    try:
        targetSheet = [i for i in sheets if i.Sheetnumber == sheetNumber][0]
    except:
        print 'failed to find sheet',sheetNumber
        return []
    
    viewPortLocations = [] # a list of [[viewPort, bboxCenter], ... ]
    for vpId in targetSheet.GetAllViewports():
        vp = document.GetElement(vpId)
        viewPortLocations.Add([vp, vp.GetBoxCenter()])
    
    return viewPortLocations

感谢 Callum 帮了大忙!我只需要修正一两个拼写错误,使用 append () 而不是 add,并获取视图名称而不是 Sheet 名称。如下

viewPorts = list(FilteredElementCollector(doc).OfClass(Viewport))
viewPortTriples = []

for vp in viewPorts:
    sheet = doc.GetElement(vp.SheetId)
    view = doc.GetElement(vp.ViewId)
    viewPortTriples.append([sheet.SheetNumber, view.ViewName, vp.GetBoxCenter()])

print(viewPortTriples)