68 lines
2.7 KiB
Python
68 lines
2.7 KiB
Python
import math
|
|
|
|
from build123d import Axis, SortBy
|
|
from build123d import fillet as _fillet
|
|
|
|
|
|
def validate(context, target, params):
|
|
errors = []
|
|
if target is None:
|
|
errors.append("NO_TARGET: fillet_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"
|
|
)
|
|
|
|
if params["edge_selector"] == "near_point":
|
|
missing = [f for f in ("x", "y", "z", "search_radius") if f not in params]
|
|
if missing:
|
|
errors.append(
|
|
f"MISSING_PARAM: edge_selector为near_point时,{missing}都是必填参数"
|
|
)
|
|
return errors
|
|
|
|
|
|
def run(context, target, params):
|
|
selector = params["edge_selector"]
|
|
|
|
if selector == "by_index":
|
|
edges = target.edges()
|
|
return target.fillet(params["radius"], [edges[i] for i in params["edge_indices"]])
|
|
if selector == "outer_top":
|
|
face = target.faces().sort_by(Axis.Z)[-1]
|
|
edge = face.edges().sort_by(SortBy.LENGTH)[-1]
|
|
return _fillet(edge, radius=params["radius"])
|
|
elif selector == "outer_bottom":
|
|
face = target.faces().sort_by(Axis.Z)[0]
|
|
edge = face.edges().sort_by(SortBy.LENGTH)[-1]
|
|
return _fillet(edge, radius=params["radius"])
|
|
elif selector == "all":
|
|
return _fillet(target.edges(), radius=params["radius"])
|
|
elif selector == "near_point":
|
|
cx, cy, cz = params["x"], params["y"], params["z"]
|
|
r = params["search_radius"]
|
|
picked = [
|
|
e
|
|
for e in target.edges()
|
|
if math.dist((e.position_at(0.5)), (cx, cy, cz)) <= r
|
|
]
|
|
if not picked:
|
|
raise ValueError(
|
|
f"NO_EDGES_FOUND: 在点({cx},{cy},{cz})附近{r}mm范围内没有找到任何边,"
|
|
"检查坐标或search_radius是否正确"
|
|
)
|
|
return _fillet(picked, radius=params["radius"])
|
|
else:
|
|
raise ValueError(f"未知 edge_selector: {selector}")
|