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
 5BlockName = "Base"
 6PaletteName = "Cut"
 7
 8# We are assuming that the block is inserted directly into the layer, not within any other blocks.
 9lay = impact.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
18        if ent_insert.insert_type == ipInsertType.itBlock:
19            ent_casted = IBlockInsert(ent_insert._com_obj)
20
21            if ent_casted.inserted_block.full_name == BlockName:
22                blk_insert = ent_casted
23                break
24if blk_insert is None or (hasattr(blk_insert, "isNone") and blk_insert.isNone()):
25    impact.gui.output_toolbox.add("No block found called '" + str(BlockName) + "'")
26else:
27
28    # We store the transformation matrix, so that we can apply it to the
29    # copied entities, to maintain their position (in world coordinates).
30    blk_transformation = blk_insert.matrix
31    count = 0
32    blk = blk_insert.inserted_block
33    for ent in blk.entities:
34
35        # In this example we are being selective about which entities to copy.
36        if ent.geometric:
37            if ent.palette.full_name == PaletteName:
38
39                # Make a copy of this entity and reposition it.
40                new_ent = ent.copy_to_block(lay.root_block)
41                new_ent.transform(blk_transformation)
42                count = count + 1
43    impact.gui.output_toolbox.add(str(count) + " new entities created")