49 lines
2.0 KiB
Python
49 lines
2.0 KiB
Python
from build123d import GeomType
|
|
|
|
|
|
def validate(context, target, params):
|
|
errors = []
|
|
if target is None:
|
|
errors.append("NO_TARGET: chamfer_edge 需要一个实体作为输入")
|
|
return errors
|
|
|
|
if params.get("edge_selector") == "by_index":
|
|
indices=params.get("edge_indices")
|
|
count=len(target.edges())
|
|
if not isinstance(indices,list) or not 1 <= len(indices) <= 100 or any(type(i) is not int or not 0 <= i < count for i in indices):
|
|
errors.append("INVALID_EDGE_SELECTION: 使用inspect_geometry(include_edges=true)取得当前实体边索引,提供1至100个有效整数")
|
|
elif len(set(indices)) != len(indices):
|
|
errors.append("INVALID_EDGE_SELECTION: 边索引不能重复")
|
|
bbox = target.bounding_box()
|
|
min_dim = min(bbox.size.X, bbox.size.Y, bbox.size.Z)
|
|
if params["radius"] >= min_dim / 2:
|
|
errors.append(
|
|
f"RADIUS_TOO_LARGE: 倒角半径{params['radius']}mm过大,应小于{min_dim / 2:.2f}mm"
|
|
)
|
|
return errors
|
|
|
|
|
|
def run(context, target, params):
|
|
selector = params["edge_selector"]
|
|
|
|
if selector == "by_index":
|
|
edges=target.edges()
|
|
return target.chamfer(params["radius"], None, [edges[i] for i in params["edge_indices"]])
|
|
if selector == "outer_top":
|
|
face = _horizontal_end(target, True)
|
|
return target.chamfer(params["radius"], None, face.outer_wire().edges())
|
|
elif selector == "outer_bottom":
|
|
face = _horizontal_end(target, False)
|
|
return target.chamfer(params["radius"], None, face.outer_wire().edges())
|
|
elif selector == "all":
|
|
return target.chamfer(params["radius"], None, target.edges())
|
|
else:
|
|
raise ValueError(f"未知 edge_selector: {selector}")
|
|
|
|
|
|
def _horizontal_end(target, top):
|
|
faces=[f for f in target.faces() if f.geom_type==GeomType.PLANE and abs(f.normal_at().Z)>.999999]
|
|
if not faces:
|
|
raise ValueError('NO_END_FACE: 未找到水平端面,请用by_index选择边')
|
|
return sorted(faces,key=lambda f:f.center().Z)[-1 if top else 0]
|