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
 3NameOfExistingSymbol = "Rotary 8.00 Dia Hole"
 4NameOfReplacementSymbol = "Rotary 9.50 Dia Hole"
 5
 6if not impact.active_drawing.isNone():
 7
 8    # We need an array to keep track of the symbol insertion entities
 9    SymbolInsertionsToDelete = []
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 = impact.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
19            if ent_insert.insert_type == ipInsertType.itSymbol:
20                ent_casted = ISymbolInsert(ent_insert._com_obj)
21
22                sym = ent_casted.inserted_object
23                if sym.full_name == NameOfExistingSymbol:
24                    sym_insertion = ent_casted
25
26                    # Read the location/orientation of the original symbol
27                    Position = sym_insertion.origin
28                    Scale = sym_insertion.scale()
29                    Angle = sym_insertion.angle
30                    MirrorX = ipBoolean.bFalse
31                    MirrorY = sym_insertion.mirrored
32
33                    # Now insert the new symbol using the same properties
34                    new_insertion = ab.symbol_insert_a(NameOfReplacementSymbol, Position, Scale, Angle, MirrorX, MirrorY)
35                    if new_insertion is None:
36                        impact.gui.output_toolbox.add("Failed to insert symbol - does the specified symbol exist in this database?")
37                    else:
38
39                        # We will want to delete the original symbol, but not while we are looping through
40                        # the entities, so we'll store it for later
41                        # Redim (resize the list as needed)
42                        SymbolInsertionsToDelete[item_count] = sym_insertion
43                        item_count = item_count +1
44
45    impact.gui.output_toolbox.add(str(item_count) + " symbols inserted")
46
47    # Now loop through the array of symbol insertions to be removed
48    for ent in SymbolInsertionsToDelete:
49        ent.delete()
50
51