Search code examples
python-2.7revit-apirevitpythonshell

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


I want to create a list of all Viewports by associated sheet number, View Name and Location Center. Like so:

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.]

This so a user can align Multiple Viewports to each other in a listbox (not shown here) in WPF. I have the below:

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)

Which gets me all the Viewports on all Sheets as List objects. But I now want to format this list as stated above. I tried the following:

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

I believe I am not iterating over the whole List of lists. Not to mention I am new to python.. Any help would be really appreciated. Thanks!


Solution

  • Thanks Callum that helped alot! I just had to fix one or two typos, use append () instead of add, and get the View Name as well instead of the Sheet Name. As below

    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)