Shape Intersection (Python)

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