from build123d import Circle, Plane, PolarLocations, Sketch, extrude def validate(context, target, params): errors = [] if target is None: errors.append("NO_TARGET: hole_pattern 需要一个实体作为输入") return errors bbox = target.bounding_box() max_reach = params["circle_radius"] + params["diameter"] / 2 half_extent = max(bbox.size.X, bbox.size.Y) / 2 if max_reach > half_extent + 1e-6: errors.append( f"HOLE_OUT_OF_BOUNDS: 孔位置(半径约{max_reach:.2f}mm)超出零件范围(约{half_extent:.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 PolarLocations(params["circle_radius"], params["count"]): hole_sketch += loc * Circle(params["diameter"] / 2) hole_sketch = plane * hole_sketch holes_solid = extrude(hole_sketch, amount=depth) return target - holes_solid