Copy To Block (Python)¶
Copy To Block (Python)¶
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.
13blkInsert = None
14for ent in lay.root_block.entities:
15 if ent.entity_type == ipEntityType.etInsert:
16 if ent.insert_type == ipInsertType.itBlock:
17 if ent.inserted_block.full_name == BlockName:
18 blkInsert = ent
19 break
20if blkInsert.isNone():
21 impact.gui.output_toolbox.add("No block found called '" + str(BlockName) + "'")
22else:
23 # We store the transformation matrix, so that we can apply it to the
24 # copied entities, to maintain their position (in world coordinates).
25 blkTransformation = blkInsert.matrix
26 count = 0
27 blk = blkInsert.inserted_block
28 for ent in blk.entities:
29 # In this example we are being selective about which entities to copy.
30 if ent.geometric:
31 if ent.palette.full_name == PaletteName:
32 # Make a copy of this entity and reposition it.
33 newEnt = ent.copy_to_block( lay.root_block )
34 newEnt.transform( blkTransformation )
35 count = count + 1
36 impact.gui.output_toolbox.add(str(count) + ' new entities created')