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

34 lines
1.2 KiB
Python

from build123d import Circle, GridLocations, Plane, Sketch, extrude
def validate(context, target, params):
errors = []
if target is None:
errors.append("NO_TARGET: linear_hole_pattern 需要一个实体作为输入")
return errors
bbox = target.bounding_box()
span_x = params["col_spacing"] * (params["cols"] - 1) + params["diameter"]
span_y = params["row_spacing"] * (params["rows"] - 1) + params["diameter"]
if span_x > bbox.size.X + 1e-6 or span_y > bbox.size.Y + 1e-6:
errors.append(
f"PATTERN_OUT_OF_BOUNDS: 孔阵列跨度(X{span_x:.2f}mm, Y{span_y:.2f}mm)"
f"超出零件XY范围(X{bbox.size.X:.2f}mm, Y{bbox.size.Y:.2f}mm)"
)
return errors
def run(context, target, params):
bbox = target.bounding_box()
depth = bbox.size.Z * 4 + 20
z_center = (bbox.min.Z + bbox.max.Z) / 2
plane = Plane.XY.offset(z_center - depth / 2)
hole_sketch = Sketch()
for loc in GridLocations(params["col_spacing"], params["row_spacing"], params["cols"], params["rows"]):
hole_sketch += loc * Circle(params["diameter"] / 2)
hole_sketch = plane * hole_sketch
holes_solid = extrude(hole_sketch, amount=depth)
return target - holes_solid