Getting next Element through parameter values and looping

Hi everyone,
i have been trying to figure this one out myself but am stuck now.

A brief explanation of my goal:
I created a duct system, used the MEPover node " Elements in connected Network" to get a sorted list of all my elements. I then created two shared parameters; “Number of element” and “GUID previous element”.

I am now trying to get a list of elements of a branch from this network.
I input a starting element in my python node, then check if the “number of element” is != 1 (1 being the first element of the network). If so, get the “GUID of the previous element” and get the corresponding element. Append it to a list and then start all over again with the new element, till i reach the first element.

The functions are working, but i can’t get the loop working. I tried adepting the MEPover to my needs, but i’m stuck there.

I hope i was able to explain what i am trying to achieve and that someone might be able to help me.

My phyton code is added below. If there is an easier way to doing this, i would be interested as well, since i am learning phyton while doing this.

Greetings.

import sys
import clr

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

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

import collections
from collections import OrderedDict

#The inputs to this node will be stored as a list in the IN variables.
ElementList = UnwrapElement(IN[0])

outlist = []

#Definition der Funktion Bauteilnummer; ruft den Wert des Parameters "Bauteilnummer" ab
# Number of Element
def Bauteilnummer(element):
    nummer = element.LookupParameter("Bauteilnummer").AsInteger()
    return nummer

#ruft Wert des Parameters "GUID nächstes Bauteil" ab; kann noch in Funktion umgewandelt werden
# GUID of previous element
def GUID_abrufen(element):
        parameter = element.LookupParameter("GUID vorheriges Bauteil").AsString()
        return parameter   
     
def nextElements(elem):
    listout = []
    IDList = []
    if Bauteilnummer(elem) != 1:
        IDList.append(GUID_abrufen(elem))
        for ID in IDList:
            listout.append(doc.GetElement(ID))
        return listout
    
def collector(elem):
	cont = 0
	for i in nextElements(elem):
		if i.Id in lookup:
			cont += 1
		else:
			item = doc.GetElement(i.Id)
			lookup[i.Id] = item
			collector(i)
	if cont == len(elements):
		return elem
		

for x in ElementList:
    lookup = OrderedDict()
    ownerId = x.Id
    collector(x)
    outlist.append(lookup.values())

#Assign your output to the OUT variable.
OUT = outlist

Hi,

I think you haven’t defined ‘elements’ in your def collector? Should:

if cont == len(elements):

read:

if cont == len(nextElements(elem)):

Perhaps? :slight_smile:

You also state:

collector(x)

But you don’t appear to do anything with it?

Perhaps instead of this:

outlist.append(lookup.values())

Perhaps you maybe meant to do this?

outlist.append(collector(x))

It is quite hard to interrogate without some sample files :slight_smile:

I hope that helps,

Mark

Hi Mark, thanks for your answer and sorry for the late reply.
I changed the code the way you suggested it, but i had quite a few different errors.
I was able to figure out a solution by simplifying the whole code.

Thanks for your help.

Reinhard