SectionView from SectionLine in Python

Hello Dynamo Community :slight_smile:

I want to get SectionViews from SectionLines in active view.

I can

  • get the view names of the Sectionlines
  • get the view name of all SectionViews

now im struggling at comparing names and get the views from there

import clr
import System

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

sectionNames = []
# get sectionlines in active view
sectionlines = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Viewers).ToElements()
# get viewname parameter
for sectionline in sectionlines:
	sectionNames.append(sectionline.Name)
# get all views in project
allViews = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).ToElements()

viewNames = []
views = []
# get viewnames of all views in project
for allView in allViews:
	viewNames.append(allView.Name)
# find view by name	
for viewName, sectionName, allView in zip(viewNames, sectionNames, allViews):
	if viewName == sectionName:	
		views.append(allView)

OUT =  views

image

You can use loop in loop in this case so it can check all of them one by one.
In your script, each item of lists are compared with only the same order, so it can’t find match.
Not sure this is the best way, but it works.

for sectionName in sectionNames:
	for viewName,allView in zip(viewNames,allViews):
		if viewName == sectionName:	
			views.append(allView)
			break
2 Likes

And your script makes too many temporary lists which are not necessary.
The more list making the more time taking.

import clr
import System

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument


# get sectionlines in active view
sectionlines = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Viewers).ToElements()
# get viewname parameter

# get all views in project
allViews = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).ToElements()
views = []
# get viewnames of all views in project
for sectionline in sectionlines:
	sectionName=sectionline.Name
	for allView in allViews:
		viewName=allView.Name
		if viewName == sectionName:	
			views.append(allView)
			break #stop looping if match found
OUT =  views
1 Like

Thank you for your help and explanation @Hyunu_Kim :slight_smile: