Using SymbolInsertA to replace symbols

Using SymbolInsertA to replace symbols
 1# We wish to replacce all insetions of one symbol
 2# with another symbol, within the current block
 3name_of_existing_symbol = "Rotary 8.00 Dia Hole"
 4name_of_replacement_symbol = "Rotary 9.50 Dia Hole"
 5
 6if not active_drawing.isNone():
 7
 8    # We need an array to keep track of the symbol insertion entities
 9    symbol_insertions_to_delete = []
10    item_count = 0
11
12    # Look In the active block for any symbol insertion; then check the
13    # symbol name to see if it's the one we want
14    ab = active_drawing.active_layer.active_block
15    for ent in ab.entities:
16        if ent.entity_type == ipEntityType.etInsert:
17            ent_insert = IInsertEntity(ent._com_obj)
18            if ent_insert.insert_type == ipInsertType.itSymbol:
19                ent_casted = ISymbolInsert(ent_insert._com_obj)
20                sym = ent_casted.inserted_object
21                if sym.full_name == name_of_existing_symbol:
22                    sym_insertion = ent_casted
23
24                    # Read the location/orientation of the original symbol
25                    position = sym_insertion.origin
26                    scale = sym_insertion.scale()
27                    angle = sym_insertion.angle
28                    mirror_x = False
29                    mirror_y = sym_insertion.mirrored
30
31                    # Now insert the new symbol using the same properties
32                    new_insertion = ab.symbol_insert_a(name_of_replacement_symbol, position, scale, angle, mirror_x, mirror_y)
33                    if new_insertion is None:
34                        impact.gui.output_toolbox.add("Failed to insert symbol - does the specified symbol exist in this database?")
35                    else:
36
37                        # We will want to delete the original symbol, but not while we are looping through
38                        # the entities, so we'll store it for later
39                        # Redim (resize the list as needed)
40                        symbol_insertions_to_delete[item_count] = sym_insertion
41                        item_count = item_count +1
42
43    impact.gui.output_toolbox.add(str(item_count) + " symbols inserted")
44
45    # Now loop through the array of symbol insertions to be removed
46    for ent in symbol_insertions_to_delete:
47        ent.delete()
48