Get index on a for loop

Hi all,

First time on the forum and quite a newbie regarding dynamo and python, but I’m trying my best.

I’m creating a graph that basically gets AutoCAD block locations from an excel, matches block names with Revit groups names and moves the Revit groups to the block locations.

on the image below you can see the lists I use, on the python script I get the list of groups directly from Revit and with a for loop check if the group names are the same as the block names to return a list with the groups to be place and its location.

This is the full script:

# Enable Python support and load DesignScript library
import clr
import math

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

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

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

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

def tolist(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]
	
doc = DocumentManager.Instance.CurrentDBDocument

blocks = IN[0]
locations = IN[1]
mglist = []
loclist = []

#Gets groups from Revit

collector = FilteredElementCollector(doc, doc.ActiveView.Id)

filter = ElementCategoryFilter(BuiltInCategory.OST_IOSModelGroups)
mgroups = collector.WherePasses(filter).ToElements()

#Gets a list with groups to be placed and locations

for mg in mgroups:
	for block in blocks:
		if mg.GroupType.ToDSType(True).Name == block:
			mglist.append(mg.GroupType)
			bindex = blocks.index(block)
			newloc = locations[bindex]
			loclist.append(newloc)

#place the groups
	
outList = []
i = 0

locs = tolist(loclist)

if len(mglist) == len(locs):
	for g in mglist:
		loc = locs[i].ToRevitType(True)
		TransactionManager.Instance.EnsureInTransaction(doc)
		newGrp = doc.Create.PlaceGroup(loc, g)
		outList.append(newGrp)
		TransactionManager.Instance.TransactionTaskDone()
		i = i+1
else:
	outList.append("Could not Create Groups")
	
# Assign your output to the OUT variable.
OUT = outList

And Dynamo file:

Place & Rotate Model groups.dyn (86.0 KB)

The issue that I detect happens on this part:

#Gets a list with groups to be placed and locations

    for mg in mgroups:
    	for block in blocks:
    		if mg.GroupType.ToDSType(True).Name == block:
    			mglist.append(mg.GroupType)
    			bindex = blocks.index(block)
    			newloc = locations[bindex]
    			loclist.append(newloc)

On the image below you can see that group T6_AS matches index 5 and 9 of block list, next to it you can see their respective locations, but on the OUT it duplicates location of index 5 instead of append locations on index 5 and 9.

I thinks it has something to do on how the for loop works, I tried to find a solution but I can not find anything.

Any ideas? Thanks for the help in advance!

Hello try this

import clr

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

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

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

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

blocks = IN[0]
locations = IN[1]
outList = []

#Gets groups from Revit

mgroups = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_IOSModelGroups).WhereElementIsNotElementType().ToElements()


TransactionManager.Instance.EnsureInTransaction(doc)
for  block, locDS in zip(blocks, locations):
	for mg in mgroups:
		if mg.GroupType.ToDSType(True).Name == block:
			loc = locDS.ToXyz()
			newGrp = doc.Create.PlaceGroup(loc, mg.GroupType)
			outList.append(newGrp)
			break
TransactionManager.Instance.TransactionTaskDone()		

OUT = outList
1 Like

It worked perfectly fine. Thank you so much.

I didn’t know about the zip() function.