API Reference¶
- class cgal_pycad.Modifier(func, *args)[source]¶
Bases:
objectHelper for functional-style transformations (e.g. translate(v)(obj)).
- class cgal_pycad.Solid(base: Solid = None)[source]¶
Bases:
objectWrapper class for CGAL exact Nef Polyhedrons.
This class represents a 3D solid using exact rational arithmetic. It is immutable; transformations and boolean operations return new Solid instances.
- _solid¶
The underlying C++ object (implementation detail).
- difference(other: Solid) Solid[source]¶
Compute the boolean difference (self - other).
- Parameters:
other – The Solid to subtract.
- Returns:
A new Solid representing the difference.
- intersect(other: Solid) Solid[source]¶
Compute the boolean intersection of this solid and another.
- Parameters:
other – The other Solid to intersect with.
- Returns:
A new Solid representing the intersection.
- mirror(plane: List[int | Fraction | tuple]) Solid[source]¶
Mirror the solid across a plane defined by ax + by + cz + d = 0.
- Parameters:
plane – A 4-element list [a, b, c, d].
- Returns:
A new mirrored Solid instance.
- Raises:
ValueError – If plane is not length 4.
- resize(x: int | Fraction | tuple | None = None, y: int | Fraction | tuple | None = None, z: int | Fraction | tuple | None = None)[source]¶
Resize the solid to specific dimensions (Bounding Box).
Not Implemented: Currently issues a warning and returns self.
- Parameters:
x – Target size in X.
y – Target size in Y.
z – Target size in Z.
- Returns:
self (unchanged).
- rotate(axis: List[int | Fraction | tuple], angle: int | Fraction | tuple | float) Solid[source]¶
Rotate the solid around an axis by an angle.
- Parameters:
axis – A 3-element vector [x, y, z] defining the axis of rotation.
angle – The rotation angle in degrees. Can be exact (int, Fraction) or float (automatically converted to high-precision rational).
- Returns:
A new rotated Solid instance. The result is a valid rational Nef Polyhedron, approximating the rotation matrix to high precision if necessary.
- Raises:
ValueError – If axis is not length 3.
Example
>>> s = cube([10, 10, 10]) >>> # Rotate 45 degrees around Z axis >>> s_rot = s.rotate([0, 0, 1], 45)
- scale(vec: List[int | Fraction | tuple]) Solid[source]¶
Scale the solid by factors along X, Y, Z.
- Parameters:
vec – A 3-element list [sx, sy, sz].
- Returns:
A new scaled Solid instance.
- to_off(path: str)[source]¶
Export the solid to an OFF file.
- Parameters:
path – Target file path (e.g., “output.off”).
- to_stl(path: str)[source]¶
Export the solid to an STL file.
- Parameters:
path – Target file path (e.g., “output.stl”).
- translate(vec: List[int | Fraction | tuple]) Solid[source]¶
Translate the solid by a vector.
- Parameters:
vec – A 3-element list/tuple [dx, dy, dz]. Elements must be exact types (int, Fraction, or (num, den) tuple). Floats are forbidden.
- Returns:
A new translated Solid instance.
- Raises:
ValueError – If vec is not length 3.
TypeError – If elements are floats.
Example
>>> s = cube([10, 10, 10]) >>> s_moved = s.translate([5, 0, 0])
- cgal_pycad.cone(r1: int | Fraction | tuple, r2: int | Fraction | tuple, h: int | Fraction | tuple, tolerance: int | Fraction | tuple | float = Fraction(1, 1000), center=False) Solid[source]¶
Create a cone (truncated).
- Parameters:
r1 – Bottom radius.
r2 – Top radius.
h – Height.
tolerance – Error tolerance.
center – If True, centered along Z.
- Returns:
A Solid representing the cone.
- cgal_pycad.cube(size: List[int | Fraction | tuple], center=False) Solid[source]¶
Create a cube (rectangular algorithm).
- Parameters:
size – [x, y, z] dimensions. Must be strict exact types (int, Fraction).
center – If True, the cube is centered at the origin. Otherwise, it starts at [0,0,0] extends to +x,+y,+z.
- Returns:
A Solid representing the cube.
Example
>>> c = cube([10, 20, 5], center=True)
- cgal_pycad.cylinder(r: int | Fraction | tuple, h: int | Fraction | tuple, tolerance: int | Fraction | tuple | float = Fraction(1, 1000), center=False) Solid[source]¶
Create a cylinder approximation.
- Parameters:
r – Radius.
h – Height.
tolerance – Error tolerance for circular approximation.
center – If True, centered along Z (from -h/2 to h/2). If False, from 0 to h.
- Returns:
A Solid representing the cylinder.
- cgal_pycad.mirror(plane: List[int | Fraction | tuple]) Modifier[source]¶
Functional wrapper for mirroring.
- cgal_pycad.resize(x=None, y=None, z=None) Modifier[source]¶
Functional wrapper for resizing (Not Implemented).
- cgal_pycad.rotate(axis: List[int | Fraction | tuple], angle: int | Fraction | tuple | float) Modifier[source]¶
Functional wrapper for rotation.
- Usage:
>>> obj = cube([10,10,10]) >>> rotated = rotate([0,0,1], 45)(obj)
- cgal_pycad.scale(vec: List[int | Fraction | tuple]) Modifier[source]¶
Functional wrapper for scaling.
- cgal_pycad.sphere(r: int | Fraction | tuple, tolerance: int | Fraction | tuple | float = Fraction(1, 1000)) Solid[source]¶
Create a sphere approximation using a convex hull of points.
- Parameters:
r – Radius. Strict exact type.
tolerance – Maximum distance error from ideal sphere surface. Floats allowed here (convenience). Controls resolution. Smaller tolerance = more vertices = higher quality.
- Returns:
A Solid representing the polyhedral sphere.