Copy To Block

Copy To Block
 1# In this example, we are looking in the current layer for a block with a certain name.
 2# We are then finding all entities in the block which use a particular palette, and then
 3# copying them into the top-level of the layer.
 4
 5block_name = "Base"
 6palette_name = "Cut"
 7
 8# We are assuming that the block is inserted directly into the layer, not within any other blocks.
 9lay = active_drawing.active_layer
10lay.set_active_block(ipActiveBlock.abLayer)
11
12# Perform the search and stop as soon as we have found any insertion of the block.
13blk_insert = None
14for ent in lay.root_block.entities:
15    if ent.entity_type == ipEntityType.etInsert:
16        ent_insert = IInsertEntity(ent._com_obj)
17        if ent_insert.insert_type == ipInsertType.itBlock:
18            ent_casted = IBlockInsert(ent_insert._com_obj)
19            if ent_casted.inserted_block.full_name == block_name:
20                blk_insert = ent_casted
21                break
22if blk_insert is None or (hasattr(blk_insert, "isNone") and blk_insert.isNone()):
23    impact.gui.output_toolbox.add("No block found called '" + str(block_name) + "'")
24else:
25
26    # We store the transformation matrix, so that we can apply it to the
27    # copied entities, to maintain their position (in world coordinates).
28    blk_transformation = blk_insert.matrix
29    count = 0
30    blk = blk_insert.inserted_block
31    for ent in blk.entities:
32
33        # In this example we are being selective about which entities to copy.
34        if ent.geometric:
35            if ent.palette.full_name == palette_name:
36
37                # Make a copy of this entity and reposition it.
38                new_ent = ent.copy_to_block(lay.root_block)
39                new_ent.transform(blk_transformation)
40                count = count + 1
41    impact.gui.output_toolbox.add(str(count) + " new entities created")