Refactor primitives as dataclasses

This commit is contained in:
Richard Ward
2025-07-17 18:30:55 +01:00
parent 9c411f4f91
commit 3bb6d09549
3 changed files with 28 additions and 12 deletions

View File

@@ -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))

View File

@@ -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(

View File

@@ -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(