Boolean operations
CSG geometry relies on Boolean operations. ZenCad provides union, difference and intersection for 3D and 2D objects, in two forms:
- functions union, difference and intersect for arrays of shapes;
- operators +, - and ^ for pairs of shapes.
! Note: ! To join simple curves into a composite wire or sew faces into a shell, use the dedicated stitching operations described in the corresponding sections.
Union
Signature:
# Function:
result = union(array)
# Operator:
result = shp0 + shp1
Example:
#with operators:
sphere(r=10) + cylinder(r=5, h=10, center=True) + cylinder(r=5, h=10, center=True).rotateX(deg(90))
#with function:
union([
sphere(r=10),
cylinder(r=5, h=10, center=True),
cylinder(r=5, h=10, center=True).rotateX(deg(90))
])

Difference
Signature:
# Function:
result = difference(array)
# Operator:
result = shp0 - shp1
Example:
#with operators:
sphere(r=10) - cylinder(r=5, h=10, center=True) - cylinder(r=5, h=10, center=True).rotateX(deg(90))
#with function:
difference([
sphere(r=10),
cylinder(r=5, h=10, center=True),
cylinder(r=5, h=10, center=True).rotateX(deg(90))
])

Intersection
Signature:
# Function:
result = intersect(array)
# Operator:
result = shp0 ^ shp1
Example:
#with operators:
sphere(r=10) ^ cylinder(r=5, h=10, center=True) ^ cylinder(r=5, h=10, center=True).rotateX(deg(90))
#with function:
intersect([
sphere(r=10),
cylinder(r=5, h=10, center=True),
cylinder(r=5, h=10, center=True).rotateX(deg(90))
])

Shell intersection
A relative of intersect that computes the intersection of the solids' shells.
Signature:
# Function:
result = section(a, b)
Example:
m0 = section(box(10, center=True) - sphere(4))
m1 = section(box(10, center=True), sphere(7))

Splitting and slicing with a plane
split(body, tools) splits a solid with Shape tools; slice(body, z=..., axis=...) uses a single plane. Both return collections of solid parts and preserve uncut solids. An empty tool collection for split raises ValueError.
slice orders its parts along the plane normal. A two-part result can be unpacked as lower, upper. See Splitting solids for details and examples.
parts = split(box(10), (infplane().up(3), infplane().up(7)))
lower, upper = slice(box(10), z=4)
left, right = slice(box(10), z=2, axis="x")
negative, positive = slice(box(10), plane=((0, 5, 0), (0, 1, 0)))
Boolean operations on 2D shapes
The same operations apply to two-dimensional objects as long as they lie in the same plane.
Example:
m0 = circle(10) - square(10)
m1 = circle(10) + square(10)
m2 = circle(10) ^ square(10)
m3 = section(circle(10), square(10))
