Finding matching level

Hi,

i try to understand how i can search things that are equal with Python. For example the get level by name.

I made the following code but i doesn’t give me the matching result with the name and the output level

Thanks!

import clr

# Import RevitAPI
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
# Import DocumentManager and TransactionManager
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)

doc = DocumentManager.Instance.CurrentDBDocument

DataEnteringNode = IN

levelName = IN[0]
level = None
#The inputs to this node will be stored as a list in the IN variable.

lName = FilteredElementCollector(doc).OfClass(Level).ToElements()
 
for i in lName:
  if i.Name == levelName:
  level = i
  break
  else:
  level = None
 
OUT = level

My computer is not nearby so cannot test, but there seems to be something with your indents of your if and else statement :slight_smile:

2 Likes

That’s true i did get the indents error.

But with moving some 2 lines it is working now :slight_smile:

The working code:

Hi @vanlion,
try this …

import clr
clr.AddReference("RevitAPI")
clr.AddReference("RevitServices")

from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument

collector = FilteredElementCollector(doc).OfClass(Level).ToElements();

level = [i for i in collector if i.Name == IN[0]]

OUT = level;
3 Likes

Thanks!

That one is looking nicer :slight_smile: