Skip to content

Geometry, fits and math

These helpers are optional. Existing CadQuery builders can continue using CadQuery directly. Lengths are millimetres, angles are degrees unless an external API explicitly says otherwise.

Reusable native shapes

from cadkit.geometry import annulus, rounded_rect_prism, capsule, polar_points

annulus

annulus(outer_diameter, inner_diameter, height)

Build a native ring from Z=0 along positive Z.

Parameters:

Name Type Description Default
outer_diameter float

Outer diameter in millimetres.

required
inner_diameter float

Inner diameter in millimetres.

required
height float

Ring height in millimetres.

required

Returns:

Type Description
Workplane

Native annular prism centred on the origin in XY.

rounded_rect_prism

rounded_rect_prism(
    length, width, height, radius, *, centered=True
)

Build an XY rounded rectangle extruded from Z=0.

Parameters:

Name Type Description Default
length float

Overall X extent in millimetres.

required
width float

Overall Y extent in millimetres.

required
height float

Z height in millimetres.

required
radius float

Corner radius, at most half the smaller XY extent.

required
centered bool

Centre XY at the origin; if false, use positive XY extents.

True

Returns:

Type Description
Workplane

Native rounded rectangular prism.

capsule

capsule(
    center=(0, 0), diameter=5, travel=10, angle=0, height=10
)

Build a rounded slot-shaped prism from two semicircular ends.

Parameters:

Name Type Description Default
center tuple

XY centre in millimetres.

(0, 0)
diameter float

Width in millimetres.

5
travel float

Distance between end-circle centres in millimetres. Overall length is travel + diameter.

10
angle float

Long-axis angle in degrees from +X toward +Y.

0
height float

Extrusion from Z=0 along +Z, in millimetres.

10

Returns:

Type Description
Shape

Native capsule prism.

polar_points

polar_points(radius, angles)

Return XY points on a circle, preserving the supplied angle order.

Parameters:

Name Type Description Default
radius float

Radius in millimetres.

required
angles tuple

Angles in degrees from +X toward +Y.

required

Returns:

Type Description
list[tuple]

XY pairs in millimetres.

normalized_to_bed

normalized_to_bed(model)

Translate geometry vertically so its lowest point touches Z=0.

Parameters:

Name Type Description Default
model object

Native Shape, Workplane, or Mesh.

required

Returns:

Type Description
Shape | Mesh

Translated geometry; X/Y placement and rotation are preserved.

Explicit fit allowances

from cadkit.fits import Bore, Countersink, fit_coupon

Allowance changes a diameter, not a radius. A nominal fit in the model is not calibration of a printer, material or machining process.

Bore dataclass

Bore(
    nominal_diameter: float,
    diametral_allowance: float = 0.2,
)

A nominal circular interface with an explicit diametral print allowance.

Parameters:

Name Type Description Default
nominal_diameter float

Mating diameter in millimetres.

required
diametral_allowance float

Added to diameter, not radius, in millimetres. Negative values make a smaller bore; final diameter must be positive.

0.2

diameter property

diameter

cutter

cutter(height, *, at=(0, 0, 0))

Return a native bore cutter along +Z.

Parameters:

Name Type Description Default
height float

Cutter height in millimetres.

required
at tuple

XYZ bottom position in millimetres.

(0, 0, 0)

Returns:

Type Description
Shape

Cylindrical cutting solid.

Countersink dataclass

Countersink(
    through_diameter: float,
    head_diameter: float,
    included_angle: float = 90,
)

A standalone conical cutter described by its throat, head, and full angle.

Parameters:

Name Type Description Default
through_diameter float

Throat diameter in millimetres.

required
head_diameter float

Head diameter in millimetres, greater than the throat.

required
included_angle float

Full cone angle in degrees, strictly between 0 and 180.

90

depth property

depth

cutter

cutter(top_z, *, at=(0, 0), overlap=0.05)

Return a cone with its nominal mouth at an explicit world Z plane.

Parameters:

Name Type Description Default
top_z float

Nominal mouth plane in millimetres.

required
at tuple

XY position of the cone axis in millimetres.

(0, 0)
overlap float

Extra cone height in millimetres to clear the entry face.

0.05

Returns:

Type Description
Shape

Native conical cutter. This cutter does not include the throat hole.

fit_coupon

fit_coupon(
    nominal_diameter,
    allowances=(-0.1, 0, 0.1, 0.2, 0.3),
    *,
    height=6,
    wall=3
)

Build an ordered ladder of bores for testing real manufactured fit.

Parameters:

Name Type Description Default
nominal_diameter float

Nominal mating diameter in millimetres.

required
allowances tuple

Nonempty sequence of diametral allowances in millimetres.

(-0.1, 0, 0.1, 0.2, 0.3)
height float

Positive block thickness in millimetres.

6
wall float

Positive surrounding wall allowance in millimetres.

3

Returns:

Type Description
Shape

Native block at positive XYZ with bores ordered along +X.

Record the position-to-allowance mapping in the Part's notes; geometry has no engraved labels. Print using the intended material and process settings.

Native and mesh boundaries

shape

shape(value)

Unwrap a Workplane's first value; preserve Shapes, Meshes, and None.

Parameters:

Name Type Description Default
value object

CadQuery Workplane, native Shape, Mesh, or empty sentinel.

required

Returns:

Type Description
object

.val() for a Workplane, otherwise the original object.

This does not combine a Workplane stack. Return an explicit compound from builders that intentionally contain multiple solids.

Mesh

Mesh(manifold)

An explicit Manifold triangle-mesh boundary in an otherwise native CAD model.

Parameters:

Name Type Description Default
manifold Manifold

Valid Manifold solid.

required

Obtain one using import_mesh or mesh. Meshes can be shown, measured approximately, transformed, and exported as STL. They do not become analytic STEP solids. Translation and rotation return new Mesh wrappers.

triangles

triangles()

Return a trimesh.Trimesh using the manifold vertices and triangles without reprocessing.

translate

translate(vector)

rotate

rotate(origin, end, degrees)

mesh

mesh(value)

Convert native geometry to the explicit Manifold mesh representation.

Parameters:

Name Type Description Default
value object

Native Shape, Workplane, Mesh, or None for an empty mesh.

required

Returns:

Type Description
Mesh

Existing Mesh unchanged or newly tessellated geometry. Native tessellation uses 0.025 mm linear and 0.08 rad angular tolerance.

import_mesh

import_mesh(path)

Load an existing mesh file into a validated Manifold representation.

Parameters:

Name Type Description Default
path str | Path

Mesh file recognized by trimesh.

required

Returns:

Type Description
Mesh

Explicit mesh geometry in the file's numerical units; no rescaling.

Raises:

Type Description
FileNotFoundError

The file does not exist.

ValueError

Manifold rejects the imported geometry.

Booleans and transforms

These functional helpers accept lists of geometry and combine them before transforming. Booleans involving a Mesh become meshes. Native solids remain native otherwise; a spatial convex hull is another explicit mesh boundary.

union

union(values)

Fuse all nonempty inputs, retaining the native/mesh boundary.

Parameters:

Name Type Description Default
values list

Shapes, Workplanes, Meshes, or None.

required

Returns:

Type Description
Shape | Mesh | None

Native result when all inputs are native; mesh result when any is a Mesh. Empty input yields None.

Planar native faces use planar booleans. union ignores None values; difference is empty if its first value is None; intersection is empty if any value is None.

difference

difference(values)

Subtract subsequent inputs from the first, retaining the native/mesh boundary.

Parameters:

Name Type Description Default
values list

Shapes, Workplanes, Meshes, or None.

required

Returns:

Type Description
Shape | Mesh | None

Native result when all inputs are native; mesh result when any is a Mesh. Empty input yields None.

Planar native faces use planar booleans. union ignores None values; difference is empty if its first value is None; intersection is empty if any value is None.

intersection

intersection(values)

Intersect all inputs, retaining the native/mesh boundary.

Parameters:

Name Type Description Default
values list

Shapes, Workplanes, Meshes, or None.

required

Returns:

Type Description
Shape | Mesh | None

Native result when all inputs are native; mesh result when any is a Mesh. Empty input yields None.

Planar native faces use planar booleans. union ignores None values; difference is empty if its first value is None; intersection is empty if any value is None.

translate

translate(values, v)

Union inputs and translate them in millimetres.

Parameters:

Name Type Description Default
values list

Input geometry.

required
v tuple

XY or XYZ displacement; omitted trailing coordinates are zero.

required

Returns:

Type Description
Shape | Mesh | None

Transformed geometry, or None for empty input.

rotate

rotate(values, a, v=None)

Union inputs and rotate about the origin using degrees.

Parameters:

Name Type Description Default
values list

Input geometry.

required
a float | tuple

One angle about v, or X/Y/Z angles applied in that order.

required
v tuple | None

Axis for a scalar angle; defaults to positive Z.

None

Returns:

Type Description
Shape | Mesh | None

Rotated geometry.

mirror

mirror(values, v)

Union inputs and reflect about a plane through the origin.

Parameters:

Name Type Description Default
values list

Input geometry.

required
v tuple

Plane normal.

required

Returns:

Type Description
Shape | Mesh | None

Reflected geometry.

scale

scale(values, v)

Union inputs and scale about the origin.

Parameters:

Name Type Description Default
values list

Nonempty input geometry.

required
v float | tuple

Uniform factor or XYZ factors.

required

Returns:

Type Description
Shape | Mesh

Scaled geometry. Nonuniform native scaling uses a general geometry transform rather than a rigid placement.

Lower-level primitives

The functional primitives return native faces or shapes, except where their description states a mesh boundary. facets is not a general tessellation control: circles and spheres ignore it; cylinders use it only for polygonal prisms with at most twelve sides.

circle

circle(r=None, d=None, facets=None)

Return a native XY disk centred at the origin. Supply radius r or diameter d in millimetres; d wins. facets is accepted but unused.

polygon

polygon(points)

Return a native closed XY face from ordered 2D/3D points in millimetres.

square

square(size, center=False)

Return a native XY rectangle. size is one length or an (x, y) pair in millimetres; center=False uses positive XY.

cube

cube(size, center=False)

Return a native box. size is one length or XYZ lengths in millimetres; center=False uses positive XYZ.

cylinder

cylinder(
    h,
    r=None,
    d=None,
    d1=None,
    d2=None,
    r1=None,
    r2=None,
    center=False,
    facets=None,
)

Return a native +Z cylinder or cone of height h in millimetres. Use r/d, or r1/d1 and r2/d2 for bottom/top. center=True centres Z. facets <= 12 selects a polygonal prism; larger values retain analytic round geometry.

sphere

sphere(r=None, d=None, facets=None)

Return an analytic sphere centred at the origin. Supply r or d in millimetres; d wins. facets is accepted but unused.

linear_extrude

linear_extrude(values, height, center=False, **kwargs)

Union planar inputs and extrude their faces along +Z by height millimetres. center=True centres Z. Extra keyword arguments are accepted but unused; twist and taper are not implemented.

rotate_extrude

rotate_extrude(values, angle=360, facets=None)

Revolve an XY profile about world Z after mapping its Y axis to Z. angle is in degrees; facets is accepted but unused.

offset

offset(values, r=None, delta=None)

Offset planar regions by r with arc joins or delta with intersection joins, in millimetres. Positive offsets expand outer boundaries and shrink holes.

hull

hull(values)

Return a convex hull. Circles use native tangent geometry; other planar curves are sampled. Spatial hulls cross to the Mesh representation.

projection

projection(values, cut=False)

Project tessellated input onto XY, or section at Z=0 with cut=True. Returns planar faces reconstructed from Manifold contours; this is not an analytic projection.

Degree-based arithmetic

cadkit.math also re-exports Python's pi, sqrt, floor, and ceil. vec arithmetic is elementwise; multiplication is not a dot or cross product.

vec

Bases: tuple

Immutable coordinate vector with elementwise arithmetic and stable cache keys.

sin

sin(angle)

Return the sin of an angle expressed in degrees.

cos

cos(angle)

Return the cos of an angle expressed in degrees.

tan

tan(angle)

Return the tan of an angle expressed in degrees.

asin

asin(value)

Return inverse sin in degrees.

acos

acos(value)

Return inverse cos in degrees.

atan

atan(value)

Return inverse tan in degrees.

atan2

atan2(y, x)

Return the signed angle of (x, y) in degrees using the standard two-argument arctangent.

norm

norm(value)

Return Euclidean vector length in the input coordinate units.

concat

concat(*values)

Concatenate coordinate sequences into an immutable vec.

sign

sign(value)

Return -1, 0, or 1 according to the sign of the input.

require

require(condition, message='Design constraint violated')

Raise ValueError with the supplied message when a design constraint is false.

inclusive_range

inclusive_range(start, step_or_stop, stop=None)

Return a tuple including the end when reached by the step. Use (start, stop) for step 1 or (start, step, stop) for an explicit nonzero step. Supports fractional and descending steps.

concat_text

concat_text(*values)

Join string representations of all values without a separator.