40 lines
1.7 KiB
Python
40 lines
1.7 KiB
Python
"""Exact planar profiles and explicit frames shared by composite capabilities."""
|
|
import math
|
|
|
|
from build123d import Face, Line, Plane, ThreePointArc, Vector, Wire
|
|
|
|
|
|
def frame(params):
|
|
x, n = Vector(*params['x_axis']), Vector(*params['normal'])
|
|
if x.length < 1e-9 or n.length < 1e-9 or abs(x.normalized().dot(n.normalized())) > 1e-6:
|
|
raise ValueError('INVALID_FRAME: x_axis and normal must be nonzero and perpendicular')
|
|
return Plane(origin=tuple(params['origin']), x_dir=x, z_dir=n)
|
|
|
|
|
|
def arc_profile(points, bulges=None):
|
|
"""Closed polygon whose edges can be exact circular arcs: bulge=tan(sweep/4)."""
|
|
if len(points) < 3:
|
|
raise ValueError('PROFILE: at least three distinct vertices required')
|
|
if points[0] == points[-1]:
|
|
raise ValueError('PROFILE: omit repeated closing vertex')
|
|
bulges = bulges or [0.] * len(points)
|
|
if len(bulges) != len(points):
|
|
raise ValueError('PROFILE: one bulge per edge, including closing edge')
|
|
edges = []
|
|
for index, a in enumerate(points):
|
|
b = points[(index + 1) % len(points)]
|
|
dx, dy = b[0] - a[0], b[1] - a[1]
|
|
if math.hypot(dx, dy) < 1e-7:
|
|
raise ValueError('PROFILE: zero length edge')
|
|
q = bulges[index]
|
|
if abs(q) < 1e-12:
|
|
edges.append(Line(tuple(a), tuple(b)).edge())
|
|
else:
|
|
midpoint = ((a[0] + b[0]) / 2 + dy * q / 2,
|
|
(a[1] + b[1]) / 2 - dx * q / 2)
|
|
edges.append(ThreePointArc(tuple(a), midpoint, tuple(b)).edge())
|
|
face = Face(Wire(edges))
|
|
if not face.is_valid or face.area <= 1e-8:
|
|
raise ValueError('PROFILE: self-intersecting or empty profile')
|
|
return face
|