Indexer and FilteredElementCollector

Indexer and FilteredElementCollector

michellem
Advocate Advocate
645 Views
3 Replies
Message 1 of 4

Indexer and FilteredElementCollector

michellem
Advocate
Advocate

Hello Everyone;

 

I am trying to get a list of the elementIDs of text styles (TextNoteType) by suppling the names of the text styles. The text style names come from a CSV data file where each line (record) contains the name plus other info.

 

So I have used the following code in Revit Python Shell to get a list of text styles:

 

 

 

ListOfTextStyles = FilteredElementCollector(doc).OfClass (TextNoteType)
a = ListOfTextStyles.ToElements()

 

 

 

 

One of the properties shown by RPS when I type "a.it.." is this:

Indexer.jpg

Am I correct that Item is an indexer of some kind and that if I formatted the call correctly that I would get a specific TextNoteType by specifying its name? A call something like this: TextStyle = a.Item(Property = "Type Name")["style name"]

 

My understanding of indexers is fuzzy at best but I gather an indexer is a way to access a collection of objects. For example, the indexer used to get items from a python list would be an integer indicating the position of the item in the collection. And for a python dictionary, the indexer would be the key value of the item in the collection. Would this be correct?

 

Can someone tell me if I can do what I think I can? And if so, how?

 

Sincerely;

Michelle

 

Michelle

 

0 Likes
Accepted solutions (2)
646 Views
3 Replies
Replies (3)
Message 2 of 4

RPTHOMAS108
Mentor
Mentor
Accepted solution

.ToElememts returns a generic IList<T>

 

The property IList<T>.Item is an indexer but takes integer to get the item by its index in the list (0-based). To get a specific item by its name you would either have to filter more specifically with the API collector or filter the elements you get from it.

 

In C# you would use Linq in Python you would likely iterate the list and pick out the item via name property etc. or if matching multiple items create a new list based on elements passing the condition.

 

Similar to

 

You may get better advice on a Python specific forum since filtering lists is a general programming thing following the syntax of your chosen language and not specific to the API.

Message 3 of 4

jeremy_tammik
Alumni
Alumni
Accepted solution

No need for the ToElements method. You can mostly just leave it away. Doing so may even save memory and improve performance:

   

  

Also answered on StackOverflow:

  

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 4 of 4

michellem
Advocate
Advocate

Hello  RPTHOMAS108;

 

I think you are right - not possible to get item simply but passing in the name.

 

Still, I would like to know where Python came up with Item and what its autocomplete suggestion means...

 

Indexer.jpg

Oh well, I'll see if I can find a Revit + Python forum...

0 Likes