Python...error...generating infinite loop

Hi all I am trying to create a python script for finding connected elements as part of a bigger script.But its generating a infinite loop. I have attached pictures explaining the logic i am using and also revit and python file.My intension is once i select one pipe from the network it should give remaining connected elements.I know there isa node in mepover package for the same…I want to built it myself , as then i will have control over the script to modify as i want… Can somebody take a look and help me where i am doing wrong.script is written based on assumption pipe network has only pipes and elbows…



Water Pressure Zoning Master2018.dyn (6.8 KB)

Do you know where the infinite loop is occurring? To put it bluntly, no one is going to want to read through your entire code (or recreate it) just to find where the loop is happening.

Start by commenting out chunks of the code and slowly work through line by line until you hit the infinite loop. Then we can start looking at a solution.

2 Likes

Infinite loop is occurring while adding the connected elements in master list…to be specific from line 111 tp 123 …marked portion in script…When i am moving that marked portion outside the main for loop… i geeting a portion of desired output…means output is given element and its connected element…

We’re still guessing at where some of your variables come from and what they represent. What is iteration? What is master? What is n? Depending on certain inputs, it currently looks like you could have n jump above 4 before reaching the break check and land you in an infinite loop if it coincides with the first element in Elementsconnected not being in Master.

Again, this is all very hard to follow with no inputs and a lot of looping code.

To make it simple i have removed all those parameters which are confusing(Which i added while trying to debug the code)…sorry
here i am attaching a simplified version of the above code…
master is the main list in which elements are stored (it is getting updated inside the for loop when a connected element which is not present inside the master is found)…i have removed parameters like iteration and n which is confusing…now output i am getting is given pipe and its connected element…i want the for loop to continue with newly found connected element…logic remains same as mention in the first post…hope everthing is clear now…my requirement is when give 1 st elemnt as input i need the network of elements inside master

Water Pressure Zoning Master2018.dyn (6.6 KB)
import clr
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import *

clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)
clr.AddReference(‘DSCoreNodes’)
from DSCore.List import Flatten

import sys
pyt_path = r’C:\Program Files (x86)\IronPython 2.7\Lib’
sys.path.append(pyt_path)
import System

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
doc = DocumentManager.Instance.CurrentDBDocument
#Converting element into list
element = UnwrapElement(IN[0])
Master =
Master.append(element)
#flattening inputlist
TransactionManager.Instance.EnsureInTransaction(doc)

for i in Master:
Listelement =

Listelement.Add(element)
Flattenedelement = Flatten(Listelement)
items = UnwrapElement(Flattenedelement)
elementlist = list()
elementlist = []
for item in items:
	try:
		elementlist.append(Revit.Elements.Category.ById(item.Category.Id.IntegerValue))
	except:
		# Is it a schedule
		try:
			elementlist.append(Revit.Elements.Category.ById(item.Definition.CategoryId.IntegerValue))
		except:
			elementlist.append(None)
	
elementliststring = str(elementlist[0])
Elementsconnected = []
if elementliststring == "Pipe Accessories":
	break

if elementliststring == "Pipes":
	Element_Fitting = element
	if isinstance(Element_Fitting, list):
		curves = UnwrapElement(Element_Fitting)
	else:
		curves = [UnwrapElement(Element_Fitting)]

	

	for curve in curves:
		connMan = curve.ConnectorManager
		fittings = []
		for conn in connMan.Connectors:
			if conn.IsConnected:
				for lc in conn.AllRefs:
					if connMan.Owner.Id !=  lc.Owner.Id:
						fittings.append(lc.Owner)
		Elementsconnected.append(fittings)
elif elementliststring == "Pipe Fittings":
	Element_Fitting = element
	if isinstance(Element_Fitting, list):
		fittings1 = UnwrapElement(Element_Fitting)
	else:
		fittings1 = [UnwrapElement(Element_Fitting)]

	

	for x in fittings1:
		connset = x.MEPModel.ConnectorManager.Connectors
		conn_pipes = []
		for c in connset:
			if c.IsConnected:
				for lc in c.AllRefs:
					conn_pipes.append(lc.Owner)
		Elementsconnected.append(conn_pipes)
else:
	Elementsconnected.append("not found")
Elementsconnected = Flatten(Elementsconnected) 
Master = Flatten(Master)
for k in Elementsconnected:
	c = 0
	for m in Master:
		if m == k:
			c = c + 1
	if c == 0:
		Master.Add(k)
		element = k

TransactionManager.Instance.TransactionTaskDone()

OUT = Master
above is a copy of code…

Don’t take this the wrong way as I’m always happy to see people trying python, but you may want to work on some of your basics first. You’re doing a lot of list manipulation that isn’t doing anything and it’s making it very confusing to follow your code.

You’re flattening and unwrapping multiple times and you use both list() and [] as well as list.append() and list.add(). This makes me think you’re pulling chunks of existing code and frankenstein-ing it all together. Which is typically a great way to learn basics, but this is way too complex of a problem to be trying to troubleshoot with all these redundancies and mismatched conventions.

For now, I’d suggest replacing the if c == 0: line in the last section with just else. If the first item in Elementsconnected already exists in Master then the counter goes up but subsequent elements won’t get added to Master regardless of if they exist or not.

3 Likes

yes you are right…I am in the learning stage only …for finding element category and connected elements i am using code from existing nodes…all those are working fine …i have tested them…issue i am facing is for loop is starting with one element first , at the end of for loop its getting updated with one more element(which is the connected element), then for loop is not continuing…i will try what you have suggested…meanwhile if you have time , can you try once by downloading the script which i have attached with previous reply…i am kind of stuck at this point for two days…:cry:

It does seem to work, yes, it’s just hard to follow and therefore difficult to troubleshoot and keep track of.

It actually doesn’t get stuck in an infinite loop for me, but does seem to break if my pipe system branches (ie. I have more than one element connected) or I hit a Pipe Accessory. Have you tried running this on a system of just pipe and fittings?

1 Like

no…I don’t have any elements other than pipe and pipe fittings…for now just making this work on a network with pipe and pipe fittings(only elbow) is my intension…if its working i can take it to next level

if its working on a network with pipe and fittings(elbow) , can you share that script?..

The dyn you posted is all I’m using. It doesn’t get the full system, but it doesn’t hit an infinite loop either.

If I manually state how many iterations (n) to run (by replacing your first line with for i in range(n):) I can get it to work up until it hits a branch. But I can also see that it’s adding duplicates to your Master list, so something isn’t working as expected. I believe that’s related to my previous statement about how you’re not really comparing every element the same way (c == 0 only) and only looking at the category of the first connected element.

yes …you are correct…event though i have same elements in master and connected elements , its not executing the marked loop…i feel elements in master and elements connected are two different types…
image

A quick test shows that that comparison does not work.

You would have to compare the element Ids.

i checked what you have tested above without unwrapping…it will work…also i have cheked with element id also…no matter its not executing that loop…

You unwrap your element at the input so it’s unwrapped for the rest of your code. Which is what you normally want to do (and why all the other unwrappings are unnecessary.) So it’s best to compare the Ids.

If it’s not executing that loop then you probably have something going on in your system.

You can see here how you’re only comparing one element from Master to one connected element at a time, which can have you skip (during that particular loop) an element that you’ve already collected. If you happen to have this happen when c == 0, you could add a previous element to Master resulting in duplicates. If you get doubly unlucky and that duplicate element also happens to skip a previous element on the first loop then you could end up with an infinite loop situation.


You need to compare all the elements at once. Try using if k in Master: instead of comparing individual elements. Of course this would again require you to compare Ids, which are probably easier to work with anyway.

2 Likes

I have modified the program as you said…still i am getting duplicate elements in Master…


Water Pressure Zoning Master2018.dyn (6.7 KB)

Hi…Finally succeeded in getting the desired result…Actually code was not working previously because the first element in Master was in a wrapped state while others were unwrapped elements as we were getting those elements using Revit API.(A s per my understanding)…Thanks a lot for your patience and time
Water Pressure Zoning Master2018.dyn (13.0 KB)