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

57 lines
1.9 KiB
Python

import math
from build123d import Circle, Plane, Pos, extrude, loft
def _countersink_depth(hole_diameter, countersink_diameter, angle_deg):
r_hole = hole_diameter / 2
r_cs = countersink_diameter / 2
return (r_cs - r_hole) / math.tan(math.radians(angle_deg) / 2)
def validate(context, target, params):
errors = []
if target is None:
errors.append("NO_TARGET: hole_countersink 需要一个实体作为输入")
return errors
bbox = target.bounding_box()
x, y = params["x"], params["y"]
if not (bbox.min.X <= x <= bbox.max.X and bbox.min.Y <= y <= bbox.max.Y):
errors.append(f"HOLE_OUT_OF_BOUNDS: 孔位置({x},{y})超出零件顶面范围")
if params["countersink_diameter"] <= params["hole_diameter"]:
errors.append("COUNTERSINK_SMALLER_THAN_HOLE: 埋头孔直径必须大于通孔直径")
return errors
cs_depth = _countersink_depth(
params["hole_diameter"], params["countersink_diameter"], params["countersink_angle"]
)
if params["depth"] <= cs_depth:
errors.append(
f"DEPTH_TOO_SHALLOW: 总孔深{params['depth']}mm不足以容纳埋头锥部(需要>{cs_depth:.2f}mm)"
)
return errors
def run(context, target, params):
bbox = target.bounding_box()
top_z = bbox.max.Z
x, y = params["x"], params["y"]
depth = params["depth"]
through_plane = Plane.XY.offset(top_z - depth)
through_cutter = extrude(
through_plane * (Pos(x, y, 0) * Circle(params["hole_diameter"] / 2)),
amount=depth,
)
cs_depth = _countersink_depth(
params["hole_diameter"], params["countersink_diameter"], params["countersink_angle"]
)
wide = Plane.XY.offset(top_z) * (Pos(x, y, 0) * Circle(params["countersink_diameter"] / 2))
narrow = Plane.XY.offset(top_z - cs_depth) * (Pos(x, y, 0) * Circle(params["hole_diameter"] / 2))
cs_cutter = loft([wide, narrow])
return target - through_cutter - cs_cutter