All Elements in Network

Hi all,
My end goal is to obtain all unique networks of systems within a project. MEPover has a node Elements in connected network which will find all elements connected to the input element. To get all unique networks, you would need to insert an individual element from each network. (If you know of a way or a node that does this feel free to stop reading and I would love to hear it)

I am trying to modify Elements in connected network from the MEPover package to take a list of all duct elements in the project. The supplied list will contain elements found in the same network, because of this, I am trying to remove all elements from the supplied list as they are found in the network of a previous element.

My python skills still lack a little but I was able to get this to work on a sample script.

elmlist = IN[0]
listout = IN[1]

id1 = []
for i in elmlist:
	id1.append(i.Id)
	
breakcounter = 0

while len(elmlist) > 0:
	id2 = []
	for i in listout:
		for y in i:
			id2.append(y.Id)	
	for e, d in zip (elmlist[:],id1):
		if d in id2:
			elmlist.remove(e)
			id1.remove(d)
	breakcounter += 1
	
	if breakcounter == 4:
		break
		
OUT = elmlist

The problem:
The elements are not being removed from my input list like they are in my sample. Essentially I want to take the first item and get all of its connections. I then want to remove all of the connections (this list includes the starting element) from the input list and start over until there are no elements left in the input list.

My additions are running with no errors but it is stuck in an endless loop (if not broken after a forced 4 loops for safety precaution as I am working on it)

elmlist = IN[0]
bool = IN[1]
fullList = []

breakcounter = 0

id1 = []
for i in elmlist:
	id1.append(i.Id)

while len(elmlist) > 0:
	firstelm = []
	firstelm.append(elmlist[0])

	if isinstance(firstelm,list):
		connector = UnwrapElement(firstelm)
		toggle = 0
	else:
		connector = [UnwrapElement(firstelm)]
		toggle = 1
	
	

	listout = []
	if bool:
		for x in connector:
			lookup = collections.OrderedDict()
			collectorWithOwner(x)
			listout.append(lookup.Values)
	else:
		for x in connector:
			lookup = collections.OrderedDict()
			if x.GetType() == Connector:
				ownerId = x.Owner.Id
			else:
				ownerId = x.Id
			collector(x)
			listout.append(lookup.Values)
	orlist = []		
	if toggle:
		orlist = lookup.Values
	else:
		orlist = listout

	
	id2 = []
	for i in orlist:
		for y in i:
			id2.append(y.Id)	
	for e, d in zip (elmlist[:],id1):
		if d in id2:
			elmlist.remove(e)
			id1.remove(d)
	fullList.append(orlist)
	breakcounter += 1
	if breakcounter == 4:
		break

OUT = fullList

Here is the original code pulled from MEPover for reference.

import clr

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
#The inputs to this node will be stored as a list in the IN variables.
if isinstance(IN[0],list):
	connector = UnwrapElement(IN[0])
	toggle = 0
else:
	connector = [UnwrapElement(IN[0])]
	toggle = 1
	
bool = IN[1]
		
def nextElements(elem):
	listout = []
	if elem.GetType() == Connector:
		conn = elem
		for c in conn.AllRefs:
			if c.Owner.Id.Equals(elem.Owner.Id):
				continue
			elif isinstance(c.Owner,MEPSystem):
				continue
			else:
				newelem = c.Owner
			listout.append(newelem)
		return listout
	try:
		connectors = elem.ConnectorManager.Connectors
	except:
		connectors = elem.MEPModel.ConnectorManager.Connectors
	for conn in connectors:
		for c in conn.AllRefs:			
			if c.Owner.Id.Equals(elem.Id):
				continue
			elif isinstance(c.Owner,MEPSystem):
				continue
			elif c.Owner.Id.Equals(ownerId):
				continue
			else:
				newelem = c.Owner
			listout.append(newelem)
	return listout

def nextElementsWithOwner(elem):
	listout = []
	if elem.GetType() == Connector:
		conn = elem
		for c in conn.AllRefs:
			if c.Owner.Id.Equals(elem.Owner.Id):
				continue
			elif isinstance(c.Owner,MEPSystem):
				continue
			else:
				newelem = c.Owner
			listout.append(newelem)
		return listout
	try:
		connectors = elem.ConnectorManager.Connectors
	except:
		connectors = elem.MEPModel.ConnectorManager.Connectors
	for conn in connectors:
		for c in conn.AllRefs:			
			if c.Owner.Id.Equals(elem.Id):
				continue
			elif isinstance(c.Owner,MEPSystem):
				continue
			else:
				newelem = c.Owner
			listout.append(newelem)
	return listout
	
def collector(elem):
	cont = 0
	elements = nextElements(elem)
	for x in elements:
		if x.Id in lookup:
			cont += 1
		else:
			item = doc.GetElement(x.Id)

			lookup[x.Id] = item
			collector(x)
	if cont == len(elements):
		return elem
		
def collectorWithOwner(elem):
	cont = 0
	elements = nextElementsWithOwner(elem)
	for x in elements:
		if x.Id in lookup:
			cont += 1
		else:
			item = doc.GetElement(x.Id)

			lookup[x.Id] = item
			collectorWithOwner(x)
	if cont == len(elements):
		return elem
		

listout = []
if bool:
	for x in connector:
		lookup = collections.OrderedDict()
		collectorWithOwner(x)
		listout.append(lookup.Values)
else:
	for x in connector:
		lookup = collections.OrderedDict()
		if x.GetType() == Connector:
			ownerId = x.Owner.Id
		else:
			ownerId = x.Id
		collector(x)
		listout.append(lookup.Values)

#Assign your output to the OUT variable.
if toggle:
	OUT = lookup.Values
else:
	OUT = listout

Thank you for any help that you can provide.
Steven

@Steven,
look into Genuis Loci package. It contains a lot of MEP nodes.

Hello @Steven
try this
image

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

#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB 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

def getAllElementsInSystem(system):
	a = list(x.Elements)
	b = [x.BaseEquipment] if x.BaseEquipment is not None else []
	c = list(system.PipingNetwork) if hasattr(system, 'PipingNetwork') else []
	d = list(system.DuctNetwork) if hasattr(system, 'DuctNetwork') else []
	return a + b + c + d

mepSystemSet = FilteredElementCollector(doc).OfClass(MEPSystem).WhereElementIsNotElementType()

OUT = [[x, getAllElementsInSystem(x)] for x in mepSystemSet]
2 Likes