Using recursed entities to count symbols

Using recursed entities to count symbols
 1# Counting certain entities within a layer, first without recursiing into sub-blocks, then with recursion
 2
 3ents = impact.active_drawing.active_layer.root_block.entities
 4count = 0
 5for ent in ents:
 6    if ent.entity_type == ipEntityType.etInsert:
 7        ent_insert = IInsertEntity(ent._com_obj)
 8
 9        if ent_insert.insert_type == ipInsertType.itSymbol:
10            ent_casted = ISymbolInsert(ent_insert._com_obj)
11
12            count = count +1
13
14impact.gui.output_toolbox.add(str(count) + " symbols found at top level")
15
16
17
18rec_ents = impact.active_drawing.active_layer.root_block.recursed_entities
19
20# rec_ents will be an IRecursedEntities collection
21count = 0
22for rec_ent in rec_ents:
23
24    # rec_ent will be an IRecursedEntity object, which holds an entity and a transformation
25    ent = rec_ent.entity
26    if ent.entity_type == ipEntityType.etInsert:
27        ent_insert = IInsertEntity(ent._com_obj)
28
29        if ent_insert.insert_type == ipInsertType.itSymbol:
30            ent_casted = ISymbolInsert(ent_insert._com_obj)
31
32            count = count +1
33
34impact.gui.output_toolbox.add(str(count) + " symbols found in entire layer")
35
36