Insertions example

Insertions example
 1# This example displays the type of each insert in the active layer
 2
 3
 4# Create a local variable for the output toolbox
 5ot = impact.gui.output_toolbox
 6
 7
 8# Define a function to convert an ipInsertType enumeration
 9# constant to a string
10_INSERT_TYPE_AS_STRING_MAP = {
11    ipInsertType.itBlock: "block",
12    ipInsertType.itSymbol: "symbol",
13    ipInsertType.itLayer: "layer",
14    ipInsertType.itMasterLayer: "master layer",
15    ipInsertType.itOther: "other",
16}
17
18def insert_type_as_string(it):
19    return _INSERT_TYPE_AS_STRING_MAP.get(it, "?")
20
21
22# Clear the output toolbox
23ot.clear()
24
25# Create a local variable for the active drawing
26ad = impact.active_drawing
27
28# Check there is an active drawing
29if ad.isNone():
30
31    # Add a message to the output toolbox
32    ot.add("unable to continue: no active drawing")
33
34else:
35
36    # Get the inserts in the active layer
37    inserts = ad.active_layer.root_block.insertions
38
39    # Iterate the inserts
40    for insert in inserts:
41
42        # Add the type of the insert as a string to the output toolbox
43        ot.add(insert_type_as_string(insert.insert_type))