Activate block insertion and make changes

Activate block insertion and make changes
 1# This example looks for an insertion of a specific block, modifies the
 2# block, then saves the changes.
 3
 4
 5name_of_top_level_block = "dieboard attributes"
 6
 7ad = impact.active_drawing
 8
 9
10def get_dieboard_attributes_block(lay):
11    ent = None
12    _result = None
13    for ent in lay.entities:
14        if ent.entity_type == ipEntityType.etInsert:
15            ent_insert = IInsertEntity(ent._com_obj)
16            if ent_insert.insert_type == ipInsertType.itBlock:
17                ent_casted = IBlockInsert(ent_insert._com_obj)
18                if ent_casted.inserted_object.full_name == name_of_top_level_block:
19                    _result = ent_casted
20    return _result
21
22if not ad.isNone():
23    lay = None; blk_ins = None
24    lay = ad.active_layer
25
26    # go into the dieboard attributes block (if found)
27    blk_ins = get_dieboard_attributes_block(lay)
28    if not blk_ins.isNone():
29        blk_ins.activate()
30
31        # process entities in that block (for this example, just delete the first entity)
32        ab = lay.active_block
33        if ab.entities.count> 1:
34            ent = ab.entities.item(1)
35            ent.delete()
36
37        # move to the top-level, thus saving the changes
38        lay.set_active_block(ipActiveBlock.abLayer)