Automatic stairs path with linked models

Hi!
I’m having some issues with automatic stairs path tags when they’re in a linked model.

When the stairs is in the same model, there’s no problem, but when it’s in a linked model, the script fail and I can’t guess why.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

#Import DocumentManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB.Architecture import *

#Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
from Autodesk.Revit.DB import *
from Autodesk.Revit.Creation import *
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

toList = lambda x : x if hasattr(x, "__iter__")  else [ x ]


views = UnwrapElement(toList(IN[0])) 
typelist = list()
newpaths = list()

escVinc2 = UnwrapElement(IN[1])

vinculos = FilteredElementCollector(doc).OfClass(RevitLinkInstance).ToElements()
#vinc = vinculos[1]
documentos = [x.GetLinkDocument() for x in vinculos]

prueba,prueba2 = [],[]
escaleras = []
for view in views:
	aux = []
	nivel = view.GenLevel
	bip1 = BuiltInParameter.STAIRS_BASE_LEVEL_PARAM
	bip2 = BuiltInParameter.STAIRS_TOP_LEVEL_PARAM
	prov1 = ParameterValueProvider(ElementId(bip1))
	prov2 = ParameterValueProvider(ElementId(bip2))
	eva = FilterNumericEquals()
	regla1 = FilterElementIdRule(prov1,eva,nivel.Id)
	regla2 = FilterElementIdRule(prov2,eva,nivel.Id)
	filtro1 = ElementParameterFilter(regla1)
	filtro2 = ElementParameterFilter(regla2)
	filtro = LogicalOrFilter(filtro1,filtro2)
	#fec = FilteredElementCollector(doc, view.Id).OfCategory(BuiltInCategory.OST_Stairs).WhereElementIsNotElementType().ToElements()
	fec = FilteredElementCollector(doc, view.Id).OfCategory(BuiltInCategory.OST_Stairs).WherePasses(filtro).WhereElementIsNotElementType().ToElements()
		
	#nivel=view.LookupParameter("Nivel asociado").AsString()
	#nivel = view.get_Parameter(BuiltInParameter.PLAN_VIEW_LEVEL)
	"""
	for e in fec:
		if e.get_Parameter(BuiltInParameter.STAIRS_BASE_LEVEL_PARAM)==nivel:# or e.get_Parameter(BuiltInParameter.STAIRS_TOP_LEVEL_PARAM)==nivel:
		#if e.LookupParameter("Nivel base")==nivel or e.LookupParameter ("Nivel superior")==nivel:	
			aux.append(e)
		aux.append(e.get_Parameter(BuiltInParameter.STAIRS_BASE_LEVEL_PARAM))	
		#aux.append(e.GetParameterValue(BuiltInParameter.STAIRS_BASE_LEVEL_PARAM))
	"""
	escaleras.append(fec)	
	
		
	

TransactionManager.Instance.EnsureInTransaction(doc)

#find stairpathtype
fec = FilteredElementCollector(doc)
filtro = ElementCategoryFilter(BuiltInCategory.OST_StairsPaths)
caminosTipos = fec.WherePasses(filtro).ToElements()

for tipo in caminosTipos:
	istype = tipo.ToString()
	if istype == "Autodesk.Revit.DB.Architecture.StairsPathType":
		typelist.append(tipo)

stairpathtype = typelist[0]
pathid = stairpathtype.Id
#pathid = FilteredElementCollector(doc).OfClass(StairsPathType).WhereElementIsElementType().FirstElementId()



#create stair path for stair runs
# esto es para escaleras en modelo
for v,e in zip(views,escaleras):
	for escalera in e:
		stairid = LinkElementId(escalera.Id)
		try:
			newpath = StairsPath.Create(doc, stairid, pathid, v.Id)
			newpaths.append(newpath)
		except:
			pass
"""
	for escalera in escVinc2:		
		try:
			stairid = LinkElementId(escalera.Id)
			newpath = Autodesk.Revit.DB.Architecture.StairsPath.Create(escalera.Document, stairid, pathid, v.Id)
			newpaths.append(newpath)
		except:
			pass
"""

for view in views:
	for e in escVinc2:
		stairid = LinkElementId(e.Id)
		newpath = StairsPath.Create(doc, stairid, pathid, view.Id)
		newpaths.append(newpath)


TransactionManager.Instance.TransactionTaskDone()

doc.Regenerate()
uidoc.RefreshActiveView()

OUT = newpaths

The stairs from the linked model are collected in another node and connected with the IN[1].

Can anyone help me, please? Thank you very much in advance

Hello @Francisco.AB

use this constructor for link element

import clr
import sys
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS

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

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

pf_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
sys.path.append(pf_path + '\\IronPython 2.7\\Lib')
clr.AddReference("System.Core")
clr.ImportExtensions(System.Linq)

def get_rvtLink_inst(lnk_elem):
	return DB.FilteredElementCollector(doc)\
			.OfClass(DB.RevitLinkInstance)\
			.Where(lambda x : x.GetLinkDocument().Title == lnk_elem.Document.Title)\
			.FirstOrDefault()

linkStair = UnwrapElement(IN[0])
pathTyp_Id  = DB.FilteredElementCollector(doc).OfClass(DB.Architecture.StairsPathType).FirstElementId()
# retrieve link instance of linkElelemt
linkInstance_stair = get_rvtLink_inst(linkStair)
if linkInstance_stair is not None:
	# compute LinkElementId
	stairid = DB.LinkElementId(linkInstance_stair.Id, linkStair.Id)
	TransactionManager.Instance.EnsureInTransaction(doc)
	newpath = StairsPath.Create(doc, stairid, pathTyp_Id, doc.ActiveView.Id)
	TransactionManager.Instance.TransactionTaskDone()
	OUT = newpath

2 Likes

Hi @c.poupin!

THANK YOU!! I’ve to test with my script but it works like charm! Thank you man, specially for taking your time to reply an “old” topic! I had lost hope hehe

1 Like