Can any one simplify this dynamo to n-number loop

So as everyone has told you, the script setup is pretty rough, I’d suggest getting your data into a list of sets of walls for each warning (list of lists).

You could then send it through this Python code, where it applies your base Python code as a function to each list instead. If you’re getting into this type of territory it is time to learn Python probably. Never use manual indexing/copying identical code branches per object, try to get your data together and work across it in a list. This sort of thinking will lead you into problems and prevent you developing scripts which scale and work in different scenarios.

I don’t have a test model so can’t guarantee this code is 100% perfect, but in principle should reflect the approach I’d recommend.

import clr

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

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

walls_list = UnwrapElement(IN[0])

DUT = DisplayUnitType.DUT_MILLIMETERS

def walls_tryToJoin(walls, myDoc = doc):
    joined = []
    for i in walls:
        for j in walls:
            filterIntersect = ElementIntersectsElementFilter(i)
            if i is not j:
                if filterIntersect.PassesFilter(j):
                    joined.append(j)
                    try:
                        JoinGeometryUtils.JoinGeometry(myDoc,i,j)
                    except:
                        pass
                else:
                    point = i.Location.Curve.Origin.ToPoint()
                    curve = j.Location.Curve.ToProtoType()
                    distance = curve.DistanceTo(point)
                    thickF = (i.Width + j.Width)/2
                    thickM = UnitUtils.ConvertFromInternalUnits(thickF,DUT)
                    if distance == thickM:
                        joined.append(J)
                        try:
                            JoinGeometryUtils.JoinGeometry(myDoc,i,j)
                        except:
                            pass
    return joined

TransactionManager.Instance.EnsureInTransaction(doc)

joined_list = []

for walls in walls_list:
    joined_list.extend(walls_tryToJoin(walls, doc))

TransactionManager.Instance.TransactionTaskDone()

OUT = joined_list
3 Likes