ForwardIterator Method

please explain to me when to apply and what is ForwardIterator Method()
from RevitAPI:

for example here

con_set=[i.ConnectorManager.Connectors.ForwardIterator() for i in pipes]

I want to understand for what purposes I can use it

Any forward iterator know it all’s able to answer this?

Trying to decipher the code on this page (Set parameter of multiple family types in .RFA-file with python - #2 by blsalvio) and am scared to run a while loop without knowing what the forward iteration really is!

import clr
from Autodesk.Revit.DB import *

from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

param_values = list()
types = doc.FamilyManager.Types
famMan = doc.FamilyManager


familyTypesItor = famMan.Types.ForwardIterator()

while (familyTypesItor.MoveNext()):
    familyType = familyTypesItor.Current
    familyParam = famMan.get_Parameter(IN[0])
    famMan.CurrentType = familyType
    famMan.Set(familyParam, IN[1] + familyType.AsString(familyParam))


OUT = param_values


An iterator is a method that allows you to step through a data set, like a list, one object at a time.
In your specific instance, famMan.Types calls a FamilyTypeSet, the ForwardIterator() method then calls up a FamilyTypeSetIterator. If you look at the methods here, there is a .MoveNext() but no .MoveBack(). If we go back to the FamilyTypeSet page, you can see there is also a ReverseIterator() method. However, ReverseIterator() is also a FamilyTypeSetIterator, so it also only has a MoveNext().

What this means, is that ForwardIterator() and ReverseIterator() are used tell the code which direction you’re moving through the list when you call .MoveNext().

You’re code looks fine - once MoveNext() reaches the end it will return False.
That said, at least during testing it’s never a bad idea to add a safety exit.

exit_variable = 0
while ____ and exit_variable < 1000:
    exit_variable += 1
    #The rest of your code goes here

Thanks for your explanation! Makes a lot more sense now and also a great trick to try and avoid the dreaded infinite loop!


1 Like