How to get the stairs of runs and landings?

Hi all,
We are trying to get the stairs of runs and landings.
Just like things in opposite way of topic below

But having problem applying the GetStairs Method for runs and landing, can anyone help us?

GetStairs Method (revitapidocs.com)

import clr

# Add references to the Revit API
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

# Import necessary classes from Revit API and RevitServices
from Autodesk.Revit.DB import Document, FilteredElementCollector, BuiltInCategory
from Autodesk.Revit.DB.Architecture import Stairs, StairsRun
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Get the active document
doc = DocumentManager.Instance.CurrentDBDocument

# Method to get the parent stairs of a given stairs run
def get_parent_stairs(stairs_run_id):
    # Start a transaction (required for most Revit API actions)
    TransactionManager.Instance.EnsureInTransaction(doc)
    
    # Get the stairs run element
    stairs_run = doc.GetElement(stairs_run_id)
    
    # Check if the element is a stairs run
    if not isinstance(stairs_run, StairsRun):
        raise Exception("Element is not a StairsRun")
    
    # Get the parent stairs of the run
    parent_stairs = stairs_run.GetStairs()
    
    # End the transaction
    TransactionManager.Instance.TransactionTaskDone()
    
    return parent_stairs

# Example usage:
# Replace 'stairs_run_element_id' with the actual ElementId of the stairs run
stairs_id= ElementId(IN[0])  # Example ElementId
parent_stairs = get_parent_stairs(stairs_id)

# Output the parent stairs
OUT = parent_stairs

There are a few problems with the code:

  • Missing import clr at top of before clr.AddReference
  • ElementId needs to be imported from Autodesk.Revit.DB
1 Like

Thank you so much Mike, the fixed code and the error message are shown below:

Warning: IronPythonEvaluator.EvaluateIronPythonScript failed。
Traceback (most recent call last):
File “”, line 39, in
TypeError: expected BuiltInParameter, got UnknownElement

import clr

# Add references to the Revit API
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

# Import necessary classes from Revit API and RevitServices
from Autodesk.Revit.DB import Document, FilteredElementCollector, BuiltInCategory,ElementId
from Autodesk.Revit.DB.Architecture import Stairs, StairsRun
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Get the active document
doc = DocumentManager.Instance.CurrentDBDocument

# Method to get the parent stairs of a given stairs run
def get_parent_stairs(stairs_run_id):
    # Start a transaction (required for most Revit API actions)
    TransactionManager.Instance.EnsureInTransaction(doc)
    
    # Get the stairs run element
    stairs_run = doc.GetElement(stairs_run_id)
    
    # Check if the element is a stairs run
    if not isinstance(stairs_run, StairsRun):
        raise Exception("Element is not a StairsRun")
    
    # Get the parent stairs of the run
    parent_stairs = stairs_run.GetStairs()
    
    # End the transaction
    TransactionManager.Instance.TransactionTaskDone()
    
    return parent_stairs

# Example usage:
# Replace 'stairs_run_element_id' with the actual ElementId of the stairs run
stairs_id= ElementId(IN[0])  # Example ElementId
parent_stairs = get_parent_stairs(stairs_id)

# Output the parent stairs
OUT = parent_stairs

ElementId expects an integer, if you are say selecting a stair run in the active view you will need to use
stairs_id = UnwrapElement(IN[0]).Id

Thank you, Mike! The Script runs perfectly when inputting a single stair run, now we are trying to make it work when we input a list of stair runs

import clr

# Add references to the Revit API
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

# Import necessary classes from Revit API and RevitServices
from Autodesk.Revit.DB import Document, FilteredElementCollector, BuiltInCategory,ElementId
from Autodesk.Revit.DB.Architecture import Stairs, StairsRun
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Get the active document
doc = DocumentManager.Instance.CurrentDBDocument

# Method to get the parent stairs of a given stairs run
def get_parent_stairs(stairs_run_id):
    # Start a transaction (required for most Revit API actions)
    TransactionManager.Instance.EnsureInTransaction(doc)
    
    # Get the stairs run element
    stairs_run = doc.GetElement(stairs_run_id)
    
    # Check if the element is a stairs run
    if not isinstance(stairs_run, StairsRun):
        raise Exception("Element is not a StairsRun")
    
    # Get the parent stairs of the run
    parent_stairs = stairs_run.GetStairs()
    
    # End the transaction
    TransactionManager.Instance.TransactionTaskDone()
    
    return parent_stairs

# Example usage:
# Replace 'stairs_run_element_id' with the actual ElementId of the stairs run
stairs_id= UnwrapElement(IN[0]).Id  # Example ElementId
parent_stairs = get_parent_stairs(stairs_id)

# Output the parent stairs
OUT = parent_stairs
1 Like

Here are the script we are working on now, but both of them aren’t working well now.

import clr

# Add references to the Revit API
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

# Import necessary classes from Revit API and RevitServices
from Autodesk.Revit.DB import Document, FilteredElementCollector, BuiltInCategory,ElementId
from Autodesk.Revit.DB.Architecture import Stairs, StairsRun
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Get the active document
doc = DocumentManager.Instance.CurrentDBDocument

# Method to get the parent stairs of a given stairs run
def get_parent_stairs(stairs_run):
    # Start a transaction (required for most Revit API actions)
    TransactionManager.Instance.EnsureInTransaction(doc)
    
    # Check if the input is a single StairsRun or a list of StairsRun
    if isinstance(stairs_run, list):
        stairs = []
        for run in stairs_run:
            if not isinstance(run, StairsRun):
                raise Exception("Element is not a StairsRun")
            # Get the parent stairs of the run
            stairs.extend(run.GetStairs())
    else:
        if not isinstance(stairs_run, StairsRun):
            raise Exception("Element is not a StairsRun")
        stairs = stairs_run.GetStairs()
    
    # End the transaction
    TransactionManager.Instance.TransactionTaskDone()
    
    return stairs

# Example usage:
# Replace 'stairs_run_element_id' with the actual ElementId of the stairs run
stairs_run_elements = UnwrapElement(IN[0]).Id  # List of StairsRun elements

# Get the parent stairs for each StairsRun element
parent_stairs = get_parent_stairs(stairs_run_elements)

# Output the parent stairs
OUT = parent_stairs

import clr

# Add references to the Revit API
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

# Import necessary classes from Revit API and RevitServices
from Autodesk.Revit.DB import Document, FilteredElementCollector, BuiltInCategory,ElementId
from Autodesk.Revit.DB.Architecture import StairsRun
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Get the active document
doc = DocumentManager.Instance.CurrentDBDocument

# Method to get the parent stairs of a given stairs run
def get_parent_stairs(stairs_runs):
    parent_stairs = []
    
    # Start a transaction (required for most Revit API actions)
    TransactionManager.Instance.EnsureInTransaction(doc)
    
    # Iterate through each StairsRun element in the input list
    for stairs_run in stairs_runs:
        if not isinstance(stairs_run, StairsRun):
            raise ValueError("Input must be a list of StairsRun elements.")
        
        # Get the parent stairs of the run
        stairs = stairs_run.GetStairs()
        parent_stairs.extend(stairs)
    
    # End the transaction
    TransactionManager.Instance.TransactionTaskDone()
    
    return parent_stairs

# Example usage:
stairs_run_elements = UnwrapElement(IN[0]).Id  # List of StairsRun elements

# Get the parent stairs for each StairsRun element
parent_stairs = get_parent_stairs(stairs_run_elements)

# Output the parent stairs
OUT = parent_stairs

above script show the message below:
Traceback (most recent call last):
File “”, line 42, in
File “”, line 31, in get_parent_stairs
TypeError: Stairs is not iterable

You are going to have to tailor the script to iterate over the input and decide what format that is going to be - say integers or elements

# Check for list
if not hasattr(IN[0], "__iter__"):
    IN[0] = [IN[0]]

# Id integers
stairs_run_elements = [ElementId(item) for item in IN[0]]

# Selected elements
stairs_run_elements = [item.Id for item in UnwrapElement(IN[0])]

Modify parent_stairs.extend(stairs) line as extend is used to join a list (or other iterable) to a list

parent_stairs.append(stairs)

Here are a few links to help you with your Python and Dynamo Forum experience
How to get help on the Dynamo forums
Dynamo Python Primer

and if you haven’t already I would consider an introduction to python course / book
Automate the Boring Stuff with Python
University of Helsinki Python Programming MOOC 2024

1 Like

Thank you so much for sharing the learning references!
I will study first, and share the code once I finish it.

1 Like