Files
2026-09-24 10:40:57 +08:00

67 lines
3.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Atomic, measured batch of local-plane cutters; every opening must remove material."""
from build123d import Axis, Circle, Pos, Rectangle, SlotOverall, extrude
from engine.profile_geometry import arc_profile, frame
def validate(context, target, params):
if target is None:
return ['NO_TARGET: choose the part to cut']
if not 1 <= len(params['openings']) <= 100:
return ['COUNT: provide 1–100 openings']
return []
def run(context, target, params):
plane = frame(params)
# Project all eight world bounding-box corners: do not copy the full BRep
# (including its tessellation) just to obtain cutter depth.
low = high = 0
if params.get("through_all", True):
box = target.bounding_box()
depths = [plane.to_local_coords((x, y, z)).Z
for x in (box.min.X, box.max.X)
for y in (box.min.Y, box.max.Y)
for z in (box.min.Z, box.max.Z)]
low, high = min(depths) - 1, max(depths) + 1
if not params.get("through_all", True):
low = params.get("start_depth", 0)
depth = params.get("depth", 0)
if depth <= 0:
raise ValueError("DEPTH: limited cut requires positive depth")
high = low + depth
profiles, result = [], target
for i, feature in enumerate(params['openings']):
kind = feature.get('kind')
allowed = {'kind', 'u', 'v', 'diameter', 'length', 'width', 'rotation', 'points', 'bulges'}
if set(feature) - allowed:
raise ValueError(f'OPENING {i}: unknown fields {sorted(set(feature)-allowed)}')
u, v, angle = feature.get('u', 0), feature.get('v', 0), feature.get('rotation', 0)
if kind == 'circle':
if feature.get('diameter', 0) <= 0: raise ValueError(f'OPENING {i}: diameter must be positive')
profile = Circle(feature['diameter']/2)
elif kind in ('slot', 'rectangle'):
length, width = feature.get('length', 0), feature.get('width', 0)
if width <= 0 or length <= 0 or (kind == 'slot' and length <= width):
raise ValueError(f'OPENING {i}: invalid length/width')
profile = SlotOverall(length, width, rotation=angle) if kind == 'slot' else Rectangle(length, width, rotation=angle)
elif kind == 'profile':
profile = arc_profile(feature.get('points', []), feature.get('bulges'))
if angle: profile = profile.rotate(Axis.Z, angle)
else:
raise ValueError(f'OPENING {i}: kind must be circle/slot/rectangle/profile')
profile = Pos(u, v, 0) * profile
if not params.get('allow_merge', False):
for previous in profiles:
intersection = profile & previous
if intersection is not None and intersection.area > 1e-7:
raise ValueError(f'OPENING_MERGE {i}: overlapping openings; preserve bridges or explicitly allow_merge')
profiles.append(profile)
tool = plane * (Pos(0, 0, low) * extrude(profile, amount=high-low, dir=(0,0,1)))
candidate = result - tool
if candidate is None or not candidate.is_valid or result.volume-candidate.volume < 1e-6:
raise ValueError(f'NO_EFFECT opening {i}: cutter must cross real material')
if len(candidate.solids()) != len(target.solids()):
raise ValueError(f'DISCONNECTED opening {i}: cutting must preserve the number of solids')
result = candidate
return result