FoundationPartsOperationsBooleanOperation

BooleanOperation

BooleanOperation(target, tools, op) combines solids. op is one of BooleanOp.UNION (fuse), BooleanOp.SUBTRACT (the default: remove every tool from the target) and BooleanOp.INTERSECT (keep only the common volume).

Add the BooleanOperation itself to the part, not its inputs. Like Fillet and Shell, it replaces the solid built so far with its result.

The die above intersects a cube with a sphere to get the rounded body, then subtracts small spheres for the pips. Both target and tools can be any solid operation, including another BooleanOperation, a Fillet or a Mirror.

from cadbuildr.foundation import *


class BooleanDie(Part):
    """A six-sided die built from Boolean operations.

    INTERSECT a cube with a slightly smaller-than-diagonal sphere to get the
    classic rounded die body, then SUBTRACT small spheres for the pips.
    """

    def __init__(self, size=20, pip_radius=1.8):
        s = Sketch(self.xy())
        cube = Extrusion(
            Square.from_center_and_side(s.origin, size),
            size,
            end_condition=ExtrudeEndCondition.SYMMETRIC,  # centred on XY
        )
        ball = Sphere(s.origin, radius=size * 0.7)
        body = BooleanOperation(cube, [ball], BooleanOp.INTERSECT)

        # Five pips on the top face, one on the bottom face.
        top = Sketch(self.xy().get_parallel_plane(size / 2))
        bottom = Sketch(self.xy().get_parallel_plane(-size / 2))
        o = size / 4
        pips = [
            Sphere(Point(top, x, y), radius=pip_radius)
            for x, y in [(0, 0), (o, o), (-o, o), (o, -o), (-o, -o)]
        ]
        pips.append(Sphere(bottom.origin, radius=pip_radius))

        self.add_operation(BooleanOperation(body, pips, BooleanOp.SUBTRACT))
        self.paint("white")


show(BooleanDie())