Shape Intersection¶
Shape Intersection¶
1# Check whether the closed paths within blocks 'block1' and 'block2' overlap each other
2
3
4def create_shape_from_block_insertion(ins_entity):
5 _result = None
6 shape_creator = impact.creator.shape_creator()
7 shape_creator.reset()
8 shape_creator.entities = ins_entity.inserted_block.entities
9 shape_creator.matrix = ins_entity.matrix
10 _result = shape_creator.perform(ipShapeCreationType.sctInsert)
11 return _result
12
13def find_any_insertion_of_named_block(containing_block, name):
14 _result = None
15 for ent in containing_block.entities:
16 if ent.entity_type == ipEntityType.etInsert:
17 ent_insert = IInsertEntity(ent._com_obj)
18
19 if ent_insert.insert_type == ipInsertType.itBlock:
20 ent_casted = IBlockInsert(ent_insert._com_obj)
21
22 if ent_casted.inserted_object.full_name == name:
23 _result = ent_casted
24 break
25 return _result
26
27
28if impact.active_drawing.isNone():
29 impact.gui.output_toolbox.add("Unable to continue: no active drawing")
30else:
31 lay = impact.active_drawing.active_layer
32 insert1 = find_any_insertion_of_named_block(lay.active_block, "block1")
33 insert2 = find_any_insertion_of_named_block(lay.active_block, "block2")
34
35 if insert1 is None or insert2 is None:
36 impact.gui.output_toolbox.add("Did not find 'block1' and 'block2'")
37 else:
38 shape1 = create_shape_from_block_insertion(insert1)
39 shape2 = create_shape_from_block_insertion(insert2)
40
41 # Check that the shapes actually enclose an area
42 if shape1.area == 0 or shape2.area == 0:
43 impact.gui.output_toolbox.add("One or both shapes have zero size")
44 else:
45
46 # now intersect them (it doesn't matter which way around we do it)
47 overlap_shape = shape1.intersection(shape2)
48
49 if overlap_shape.subshape_count == 0:
50 impact.gui.output_toolbox.add("The two shapes do not overlap")
51 else:
52 impact.gui.output_toolbox.add("The two shapes overlap in " + str(overlap_shape.subshape_count) + " place(s), " + "with an overlap area of " + str(impact.system.convertor.area_as_string(overlap_shape.area())))