From 3bb6d09549a8b927a3360a0174fc97dab97d5659 Mon Sep 17 00:00:00 2001 From: Richard Ward Date: Thu, 17 Jul 2025 18:30:55 +0100 Subject: [PATCH] Refactor primitives as dataclasses --- parametric_cad/primitives/box.py | 14 ++++++++++---- parametric_cad/primitives/cylinder.py | 14 +++++++++----- parametric_cad/primitives/sphere.py | 12 +++++++++--- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/parametric_cad/primitives/box.py b/parametric_cad/primitives/box.py index 7188612..303c55a 100644 --- a/parametric_cad/primitives/box.py +++ b/parametric_cad/primitives/box.py @@ -1,13 +1,19 @@ +from dataclasses import dataclass + from parametric_cad.core import tm from .base import Primitive +@dataclass class Box(Primitive): - def __init__(self, width: float, depth: float, height: float) -> None: + """Axis-aligned rectangular prism primitive.""" + + width: float + depth: float + height: float + + def __post_init__(self) -> None: super().__init__() - self.width = width - self.depth = depth - self.height = height def _create_mesh(self) -> tm.Trimesh: return tm.creation.box(extents=(self.width, self.depth, self.height)) diff --git a/parametric_cad/primitives/cylinder.py b/parametric_cad/primitives/cylinder.py index f0850a0..9441348 100644 --- a/parametric_cad/primitives/cylinder.py +++ b/parametric_cad/primitives/cylinder.py @@ -1,15 +1,19 @@ -from typing import Sequence +from dataclasses import dataclass from parametric_cad.core import tm from .base import Primitive +@dataclass class Cylinder(Primitive): - def __init__(self, radius: float, height: float, sections: int = 32) -> None: + """Circular cylinder primitive.""" + + radius: float + height: float + sections: int = 32 + + def __post_init__(self) -> None: super().__init__() - self.radius = radius - self.height = height - self.sections = sections def _create_mesh(self) -> tm.Trimesh: return tm.creation.cylinder( diff --git a/parametric_cad/primitives/sphere.py b/parametric_cad/primitives/sphere.py index f0586d5..4e50a7d 100644 --- a/parametric_cad/primitives/sphere.py +++ b/parametric_cad/primitives/sphere.py @@ -1,12 +1,18 @@ +from dataclasses import dataclass + from parametric_cad.core import tm from .base import Primitive +@dataclass class Sphere(Primitive): - def __init__(self, radius: float, subdivisions: int = 3) -> None: + """Icosphere primitive.""" + + radius: float + subdivisions: int = 3 + + def __post_init__(self) -> None: super().__init__() - self.radius = radius - self.subdivisions = subdivisions def _create_mesh(self) -> tm.Trimesh: return tm.creation.icosphere(