Official Codebase of 3DCodeVerse
Python
from __future__ import annotations
from articraft_cq import (cq, math, MeshGeometry, _to_shape, mesh_from_geometry,
boolean_difference, boolean_union, boolean_intersection,
Box, Cylinder, Sphere, ValidationError)
from dataclasses import dataclass
from typing import Literal, Optional, Sequence
# ===========================================================================
# @section geom-wheels-tires
#
# Clean-room CadQuery B-rep reimplementation of the Articraft sdk
# wheels-and-tires geometry family (FAMILY = "wheels_tires").
#
# Wheels/tires are bodies of revolution aligned to local X: the width spans
# X, the radial profile lies in YZ, and revolves are taken about the X axis
# (matching the real sdk so the bbox/orientation are identical). Construction
# mirrors the real sdk exactly (sdk/_core/v0/_mesh/wheels.py) but keeps the
# parametric solid instead of tessellating to a triangle mesh.
# ===========================================================================
cos = math.cos
sin = math.sin
pi = math.pi
# ---- copied helpers (verbatim ports of sdk cadquery_helpers/common) -------
def _wheels_tires_clamp(x, low, high):
return max(low, min(high, x))
def _cq_annulus_x(outer_radius, inner_radius, width):
return (
cq.Workplane("YZ")
.circle(float(outer_radius))
.circle(max(float(inner_radius), 1.0e-4))
.extrude(float(width) * 0.5, both=True)
)
def _wheels_tires_centered_pattern_positions(count, spacing):
if count <= 0:
return []
if count == 1:
return [0.0]
origin = -0.5 * float(spacing) * float(count - 1)
return [origin + float(index) * float(spacing) for index in range(count)]
def _wheels_tires_rounded_slot_profile(length, width, *, segments=10):
radius = width * 0.5
straight = max(length - width, 0.0)
half = straight * 0.5
points = []
for index in range(segments + 1):
theta = -pi * 0.5 + pi * (index / float(segments))
points.append((half + radius * cos(theta), radius * sin(theta)))
for index in range(segments + 1):
theta = pi * 0.5 + pi * (index / float(segments))
points.append((-half + radius * cos(theta), radius * sin(theta)))
return points
def _wheels_tires_loft_between_radii_z(radii_and_offsets):
wp = None
previous_offset = 0.0
for radius, offset in radii_and_offsets:
if wp is None:
wp = cq.Workplane("XY").workplane(offset=offset).circle(radius)
else:
wp = wp.workplane(offset=offset - previous_offset).circle(radius)
previous_offset = offset
if wp is None:
raise ValidationError("No loft sections were provided")
return wp.loft(combine=True, ruled=False)
def _wheels_tires_cut_with_pattern(shape, cutters):
for cutter in cutters:
shape = shape.cut(cutter)
return shape
def _shift_to_x0(shape):
"""Replicate sdk center=False: shift so the body's min X sits on x=0
(inner mounting side on the origin plane)."""
xmin = _to_shape(shape).BoundingBox().xmin
return shape.translate((-xmin, 0.0, 0.0))
# ---- sub-feature specs (frozen dataclasses; signatures match sdk exactly) --
@dataclass(frozen=True)
class WheelRim:
outer_radius: Optional[float] = None
inner_radius: Optional[float] = None
flange_height: float = 0.0
flange_thickness: float = 0.0
bead_seat_depth: float = 0.0
@dataclass(frozen=True)
class WheelHub:
radius: float
width: float
cap_style: Literal["flat", "domed", "protruding", "recessed"] = "flat"
bolt_pattern: Optional["BoltPattern"] = None
@dataclass(frozen=True)
class WheelFace:
dish_depth: float = 0.0
front_inset: float = 0.0
rear_inset: float = 0.0
window_depth: float = 0.0
@dataclass(frozen=True)
class WheelSpokes:
style: Literal["none", "disc", "solid_slots", "straight", "split_y", "mesh"] = "none"
count: Optional[int] = None
thickness: float = 0.0
window_radius: float = 0.0
twist_deg: float = 0.0
@dataclass(frozen=True)
class WheelBore:
style: Literal["round", "keyed", "hex"] = "round"
diameter: float = 0.0
key_width: Optional[float] = None
@dataclass(frozen=True)
class TireCarcass:
crown_radius: Optional[float] = None
belt_width_ratio: float = 0.34
sidewall_bulge: float = 0.08
@dataclass(frozen=True)
class TireTread:
style: Literal["none", "circumferential", "block", "rib", "chevron", "lug"] = "none"
depth: float = 0.0
pitch: Optional[float] = None
count: Optional[int] = None
angle_deg: float = 0.0
land_ratio: float = 0.5
@dataclass(frozen=True)
class TireGroove:
center_offset: float = 0.0
width: float = 0.0
depth: float = 0.0
@dataclass(frozen=True)
class TireSidewall:
style: Literal["flat", "bulged", "square", "rounded"] = "rounded"
bulge: float = 0.08
inset_ratio: float = 0.18
@dataclass(frozen=True)
class TireShoulder:
style: Literal["soft", "square"] = "soft"
radius: float = 0.0
width: float = 0.0
# ---- geometry classes -----------------------------------------------------
class WheelGeometry(MeshGeometry):
"""Build a wheel/rim/hub visual aligned to local X."""
def __init__(
self,
radius: float,
width: float,
*,
rim: Optional[WheelRim] = None,
hub: Optional[WheelHub] = None,
face: Optional[WheelFace] = None,
spokes: Optional[WheelSpokes] = None,
bore: Optional[WheelBore] = None,
flange: Optional["WheelFlange"] = None,
center: bool = True,
):
super().__init__()
radius = float(radius)
width = float(width)
if radius <= 0.0 or width <= 0.0:
raise ValidationError("radius and width must be positive")
rim = rim or WheelRim()
hub = hub or WheelHub(radius=radius * 0.18, width=width * 0.55)
face = face or WheelFace()
spokes = spokes or WheelSpokes(style="disc")
bore = bore or WheelBore(style="round", diameter=max(radius * 0.18, 0.004))
rim_outer = float(rim.outer_radius) if rim.outer_radius is not None else radius
rim_inner = float(rim.inner_radius) if rim.inner_radius is not None else radius * 0.68
if rim_inner <= 0.0 or rim_inner >= rim_outer:
raise ValidationError("WheelRim.inner_radius must be positive and less than outer_radius")
if hub.radius <= 0.0 or hub.width <= 0.0 or hub.radius >= rim_inner:
raise ValidationError("WheelHub dimensions must be positive and fit inside the rim")
if bore.diameter <= 0.0 or bore.diameter >= hub.radius * 2.0:
raise ValidationError("WheelBore.diameter must fit inside the hub")
shape = _cq_annulus_x(rim_outer, rim_inner, width)
if rim.flange_height > 1e-6 and rim.flange_thickness > 1e-6:
flange_inner = max(rim_outer - rim.flange_height, rim_inner + 1.0e-4)
lip = _cq_annulus_x(rim_outer, flange_inner, rim.flange_thickness)
shape = shape.union(lip.translate((width * 0.5 - rim.flange_thickness * 0.5, 0.0, 0.0)))
shape = shape.union(
lip.translate((-(width * 0.5 - rim.flange_thickness * 0.5), 0.0, 0.0))
)
if rim.bead_seat_depth > 1e-6:
seat = _cq_annulus_x(
rim_outer,
max(rim_outer - rim.bead_seat_depth, rim_inner + 1.0e-4),
max(width * 0.22, 0.004),
)
shape = shape.cut(seat)
hub_cyl = cq.Workplane("YZ").circle(hub.radius).extrude(hub.width * 0.5, both=True)
shape = shape.union(hub_cyl)
if hub.cap_style == "domed":
front_cap = (
_wheels_tires_loft_between_radii_z(
[
(hub.radius, 0.0),
(hub.radius * 0.82, hub.width * 0.12),
(hub.radius * 0.30, hub.width * 0.28),
],
)
.rotate((0.0, 0.0, 0.0), (0.0, 1.0, 0.0), 90.0)
.translate((hub.width * 0.5, 0.0, 0.0))
)
shape = shape.union(front_cap)
elif hub.cap_style == "protruding":
cap = cq.Workplane("YZ").circle(hub.radius * 0.82).extrude(max(width * 0.12, 0.003))
shape = shape.union(cap.translate((hub.width * 0.5, 0.0, 0.0)))
elif hub.cap_style == "recessed":
pocket = cq.Workplane("YZ").circle(hub.radius * 0.55).extrude(max(width * 0.10, 0.002))
shape = shape.cut(
pocket.translate((hub.width * 0.5 - max(width * 0.10, 0.002), 0.0, 0.0))
)
disc_thickness = max(spokes.thickness, width * 0.08, 0.002)
face_outer_radius = min(
rim_outer - max(rim.flange_height * 0.25, radius * 0.02),
rim_inner + (rim_outer - rim_inner) * 0.28 + face.window_depth,
)
face_outer_radius = max(face_outer_radius, hub.radius * 1.6)
front_inset = _wheels_tires_clamp(
face.front_inset + max(face.dish_depth, 0.0), 0.0, width * 0.5 - disc_thickness * 0.5
)
rear_inset = _wheels_tires_clamp(face.rear_inset, 0.0, width * 0.5 - disc_thickness * 0.5)
disc_inner_radius = max(hub.radius * 0.78, hub.radius - max(disc_thickness * 0.35, 0.0012))
front_disc = _cq_annulus_x(
face_outer_radius, disc_inner_radius, disc_thickness
).translate((width * 0.5 - front_inset - disc_thickness * 0.5, 0.0, 0.0))
rear_disc = _cq_annulus_x(
face_outer_radius, disc_inner_radius, disc_thickness
).translate((-(width * 0.5 - rear_inset - disc_thickness * 0.5), 0.0, 0.0))
shape = shape.union(front_disc).union(rear_disc)
spoke_style = spokes.style
if spoke_style not in {"none", "disc"}:
spoke_count = spokes.count or (6 if spoke_style in {"straight", "solid_slots"} else 5)
if spoke_count < 2:
raise ValidationError("WheelSpokes.count must be at least 2")
window_width = max(
spokes.window_radius
if spokes.window_radius > 0.0
else (rim_outer - hub.radius) * 0.09,
0.003,
)
slot_length = max((face_outer_radius - hub.radius) * 0.75, 0.010)
cut_width = width + 0.02
cutters = []
def radial_slot(angle_deg, center_radius, length, width_local):
profile = _wheels_tires_rounded_slot_profile(length, width_local)
slot = (
cq.Workplane("YZ")
.polyline([(point[1], point[0] + center_radius) for point in profile])
.close()
.extrude(cut_width * 0.5, both=True)
.rotate((0.0, 0.0, 0.0), (1.0, 0.0, 0.0), angle_deg)
)
return slot
if spoke_style in {"straight", "solid_slots"}:
for index in range(spoke_count):
cutters.append(
radial_slot(
360.0 * index / float(spoke_count),
(hub.radius + face_outer_radius) * 0.5,
slot_length,
window_width * (1.5 if spoke_style == "solid_slots" else 1.0),
)
)
elif spoke_style == "split_y":
for index in range(spoke_count):
base = 360.0 * index / float(spoke_count)
cutters.append(
radial_slot(
base - 10.0,
hub.radius + (face_outer_radius - hub.radius) * 0.44,
slot_length * 0.55,
window_width * 0.85,
)
)
cutters.append(
radial_slot(
base + 10.0,
hub.radius + (face_outer_radius - hub.radius) * 0.64,
slot_length * 0.55,
window_width * 0.85,
)
)
else:
rows = (
hub.radius + (face_outer_radius - hub.radius) * 0.36,
hub.radius + (face_outer_radius - hub.radius) * 0.68,
)
for row_index, center_radius in enumerate(rows):
count = spoke_count * (2 if row_index == 1 else 1)
for index in range(count):
cutters.append(
radial_slot(
360.0 * index / float(count)
+ (180.0 / float(count) if row_index == 1 else 0.0),
center_radius,
slot_length * (0.30 if row_index == 0 else 0.24),
window_width * 0.72,
)
)
shape = _wheels_tires_cut_with_pattern(shape, cutters)
if hub.bolt_pattern is not None:
pattern = hub.bolt_pattern
if pattern.count < 2 or pattern.circle_diameter <= 0.0 or pattern.hole_diameter <= 0.0:
raise ValidationError(
"BoltPattern count, circle_diameter, and hole_diameter must be positive"
)
bolt_r = pattern.circle_diameter * 0.5
if bolt_r + pattern.hole_diameter * 0.5 >= hub.radius:
raise ValidationError("BoltPattern holes must fit inside the hub")
for index in range(pattern.count):
angle = 2.0 * pi * index / float(pattern.count)
hole = (
cq.Workplane("YZ")
.circle(pattern.hole_diameter * 0.5)
.extrude(width + 0.04, both=True)
.translate((0.0, bolt_r * cos(angle), bolt_r * sin(angle)))
)
shape = shape.cut(hole)
if bore.style == "round":
bore_cut = (
cq.Workplane("YZ").circle(bore.diameter * 0.5).extrude(width + 0.04, both=True)
)
elif bore.style == "hex":
bore_cut = cq.Workplane("YZ").polygon(6, bore.diameter).extrude(width + 0.04, both=True)
else:
key_width = bore.key_width or bore.diameter * 0.32
bore_cut = (
cq.Workplane("YZ").circle(bore.diameter * 0.5).extrude(width + 0.04, both=True)
)
key = cq.Workplane("YZ").box(width + 0.04, key_width, bore.diameter * 0.5)
bore_cut = bore_cut.union(key.translate((0.0, bore.diameter * 0.25, 0.0)))
shape = shape.cut(bore_cut)
if flange is not None:
if flange.radius <= 0.0 or flange.thickness <= 0.0:
raise ValidationError("WheelFlange radius and thickness must be positive")
ring_inner = max(flange.radius - flange.thickness, hub.radius * 1.05)
ring = _cq_annulus_x(flange.radius, ring_inner, flange.thickness * 0.6)
bridge_len = max(flange.offset + flange.thickness * 0.6, flange.thickness * 0.8)
bridge_radial = max(flange.radius - rim_outer + flange.thickness * 0.35, 0.006)
bridge_thickness = max(flange.thickness * 0.55, 0.0015)
strut = cq.Workplane("XY").box(bridge_len, bridge_radial, bridge_thickness)
x_offsets = []
if flange.side in {"front", "both"}:
x_offsets.append(width * 0.5 + flange.offset)
if flange.side in {"rear", "both"}:
x_offsets.append(-(width * 0.5 + flange.offset))
for x_offset in x_offsets:
ring_shape = ring.translate((x_offset, 0.0, 0.0))
shape = shape.union(ring_shape)
axial_mid = (
(width * 0.5 + x_offset) * 0.5
if x_offset >= 0.0
else (-(width * 0.5) + x_offset) * 0.5
)
radial_mid = (rim_outer + ring_inner) * 0.5
for angle_deg in (0.0, 90.0, 180.0, 270.0):
shape = shape.union(
strut.translate((axial_mid, radial_mid, 0.0)).rotate(
(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), angle_deg
)
)
if not center:
shape = _shift_to_x0(shape)
self._set(_to_shape(shape))
class TireGeometry(MeshGeometry):
"""Build a tire aligned to local X."""
def __init__(
self,
outer_radius: float,
width: float,
*,
inner_radius: Optional[float] = None,
carcass: Optional[TireCarcass] = None,
tread: Optional[TireTread] = None,
grooves: Sequence[TireGroove] = (),
sidewall: Optional[TireSidewall] = None,
shoulder: Optional[TireShoulder] = None,
center: bool = True,
):
super().__init__()
outer_radius = float(outer_radius)
width = float(width)
inner_radius = float(inner_radius) if inner_radius is not None else outer_radius * 0.58
carcass = carcass or TireCarcass()
tread = tread or TireTread()
sidewall = sidewall or TireSidewall()
shoulder = shoulder or TireShoulder()
if outer_radius <= 0.0 or width <= 0.0 or inner_radius <= 0.0:
raise ValidationError("outer_radius, width, and inner_radius must be positive")
if inner_radius >= outer_radius:
raise ValidationError("inner_radius must be less than outer_radius")
if tread.depth < 0.0:
raise ValidationError("TireTread.depth must be non-negative")
for groove in grooves:
if groove.width < 0.0 or groove.depth < 0.0:
raise ValidationError("TireGroove width/depth must be non-negative")
half_w = width * 0.5
belt_half = half_w * _wheels_tires_clamp(carcass.belt_width_ratio, 0.12, 0.96)
shoulder_w = shoulder.width if shoulder.width > 1e-6 else width * 0.11
side_bulge = max(sidewall.bulge, carcass.sidewall_bulge, 0.0)
inner_crown = inner_radius * 0.82
if sidewall.style == "square":
outer_side = outer_radius
shoulder_radius = max(
outer_radius - max(shoulder.radius, tread.depth * 0.5, outer_radius * 0.01),
inner_radius + 1.0e-4,
)
elif sidewall.style == "flat":
outer_side = max(outer_radius - width * 0.08, inner_radius + 1.0e-4)
shoulder_radius = outer_radius
elif sidewall.style == "bulged":
outer_side = min(outer_radius + side_bulge * width, outer_radius + width * 0.12)
shoulder_radius = outer_radius
else:
outer_side = max(outer_radius - width * 0.04, inner_radius + 1.0e-4)
shoulder_radius = outer_radius
profile = [
(-half_w, inner_radius),
(-half_w * 0.92, inner_radius),
(
-half_w * 0.78,
max(inner_radius + (outer_side - inner_radius) * 0.28, inner_radius + 1.0e-4),
),
(-(belt_half + shoulder_w), outer_side),
(-belt_half, shoulder_radius),
(0.0, outer_radius),
(belt_half, shoulder_radius),
(belt_half + shoulder_w, outer_side),
(
half_w * 0.78,
max(inner_radius + (outer_side - inner_radius) * 0.28, inner_radius + 1.0e-4),
),
(half_w * 0.92, inner_radius),
(half_w, inner_radius),
(half_w * 0.55, inner_crown),
(0.0, inner_crown),
(-half_w * 0.55, inner_crown),
(-half_w, inner_radius),
]
shape = (
cq.Workplane("XY")
.polyline(profile)
.close()
.revolve(360.0, (0.0, 0.0, 0.0), (1.0, 0.0, 0.0))
)
for groove in grooves:
if groove.width <= 1e-6 or groove.depth <= 1e-6:
continue
cutter = (
cq.Workplane("XY")
.moveTo(groove.center_offset - groove.width * 0.5, outer_radius - groove.depth)
.lineTo(groove.center_offset + groove.width * 0.5, outer_radius - groove.depth)
.lineTo(
groove.center_offset + groove.width * 0.5, outer_radius + groove.depth * 1.6
)
.lineTo(
groove.center_offset - groove.width * 0.5, outer_radius + groove.depth * 1.6
)
.close()
.revolve(360.0, (0.0, 0.0, 0.0), (1.0, 0.0, 0.0))
)
shape = shape.cut(cutter)
tread_style = tread.style
tread_depth = tread.depth
if tread_style == "circumferential" and tread_depth > 1e-6:
groove_count = tread.count or 3
band_positions = _wheels_tires_centered_pattern_positions(
groove_count, max(tread.pitch or (width * 0.20), width * 0.14)
)
for pos in band_positions:
cutter = (
cq.Workplane("XY")
.moveTo(pos - width * 0.03, outer_radius - tread_depth)
.lineTo(pos + width * 0.03, outer_radius - tread_depth)
.lineTo(pos + width * 0.03, outer_radius + tread_depth * 1.4)
.lineTo(pos - width * 0.03, outer_radius + tread_depth * 1.4)
.close()
.revolve(360.0, (0.0, 0.0, 0.0), (1.0, 0.0, 0.0))
)
shape = shape.cut(cutter)
elif tread_style == "rib" and tread_depth > 1e-6:
rib_count = tread.count or 3
rib_positions = _wheels_tires_centered_pattern_positions(
rib_count, max(tread.pitch or (width * 0.20), width * 0.14)
)
for pos in rib_positions:
rib = (
cq.Workplane("XY")
.moveTo(pos - width * 0.04, outer_radius)
.lineTo(pos + width * 0.04, outer_radius)
.lineTo(pos + width * 0.04, outer_radius + tread_depth)
.lineTo(pos - width * 0.04, outer_radius + tread_depth)
.close()
.revolve(360.0, (0.0, 0.0, 0.0), (1.0, 0.0, 0.0))
)
shape = shape.union(rib)
elif tread_style in {"block", "lug", "chevron"} and tread_depth > 1e-6:
circ_count = tread.count or (
12
if tread_style == "lug"
else max(
14,
int(
(2.0 * pi * outer_radius) / max(tread.pitch or (width * 0.45), width * 0.24)
),
)
)
axial_rows = (
[-width * 0.22, width * 0.22]
if tread_style == "lug"
else (
[-width * 0.22, 0.0, width * 0.22]
if tread_style == "block"
else [-width * 0.18, width * 0.18]
)
)
tangential = (2.0 * pi * outer_radius / float(circ_count)) * _wheels_tires_clamp(
tread.land_ratio, 0.18, 0.85
)
axial_width = width * (0.14 if tread_style == "lug" else 0.12)
for row_index, row_x in enumerate(axial_rows):
for index in range(circ_count):
angle_deg = 360.0 * index / float(circ_count) + (
180.0 / float(circ_count) if row_index % 2 == 1 else 0.0
)
if tread_style == "chevron":
for sign in (-1.0, 1.0):
lug = (
cq.Workplane("XY")
.box(axial_width, tread_depth * 1.35, tangential * 0.48)
.translate(
(
row_x + sign * axial_width * 0.38,
outer_radius + tread_depth * 0.16,
0.0,
)
)
.rotate(
(0.0, 0.0, 0.0),
(0.0, 1.0, 0.0),
tread.angle_deg * sign
if abs(tread.angle_deg) > 1e-6
else 24.0 * sign,
)
.rotate((0.0, 0.0, 0.0), (1.0, 0.0, 0.0), angle_deg)
)
shape = shape.union(lug)
else:
lug = (
cq.Workplane("XY")
.box(
axial_width,
tread_depth * 1.35,
tangential * (0.55 if tread_style == "lug" else 0.42),
)
.translate((row_x, outer_radius + tread_depth * 0.16, 0.0))
.rotate((0.0, 0.0, 0.0), (1.0, 0.0, 0.0), angle_deg)
)
shape = shape.union(lug)
if not center:
shape = _shift_to_x0(shape)
self._set(_to_shape(shape))