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            if ent_insert.insert_type == ipInsertType.itBlock:
19                ent_casted = IBlockInsert(ent_insert._com_obj)
20                if ent_casted.inserted_object.full_name == name:
21                    _result = ent_casted
22                    break
23    return _result
24
25
26if impact.active_drawing.isNone():
27    impact.gui.output_toolbox.add("Unable to continue: no active drawing")
28else:
29    lay = impact.active_drawing.active_layer
30    insert1 = find_any_insertion_of_named_block(lay.active_block, "block1")
31    insert2 = find_any_insertion_of_named_block(lay.active_block, "block2")
32
33    if insert1 is None or insert2 is None:
34        impact.gui.output_toolbox.add("Did not find 'block1' and 'block2'")
35    else:
36        shape1 = create_shape_from_block_insertion(insert1)
37        shape2 = create_shape_from_block_insertion(insert2)
38
39        # Check that the shapes actually enclose an area
40        if shape1.area() == 0 or shape2.area() == 0:
41            impact.gui.output_toolbox.add("One or both shapes have zero size")
42        else:
43
44            # now intersect them (it doesn't matter which way around we do it)
45            overlap_shape = shape1.intersection(shape2)
46
47            if overlap_shape.subshape_count == 0:
48                impact.gui.output_toolbox.add("The two shapes do not overlap")
49            else:
50                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())))