fix(engine): 支持倒角角度参数并新增回归测试

This commit is contained in:
2026-08-27 18:48:27 +08:00
parent 9e5aa48c3d
commit 7e199d785d
3 changed files with 170 additions and 3 deletions
+12 -3
View File
@@ -619,12 +619,21 @@ def _execute_chamfer(node: FeaturePlanNode, session: ExecutionSession) -> Featur
distance = float(node.params.get("distance_mm") or 0)
if distance <= 0:
raise ValueError("chamfer distance_mm must be > 0")
# 3. 解析目标边(支持相切传播),执行倒角;distance_2_mm 提供时产生非对称倒角。
# 3. 解析第二距离与角度(importer 对 SolidWorks Distance-Angle 倒角产出
# angle_rad,单位为弧度)。第二距离 = 主距离 * tan(angle);angle=45° 时
# tan=1,退化为等距倒角(与历史行为一致,零回归)。
# 注意:build123d 的 length/length2 侧向分配依赖面的枚举顺序,对非 45°
# 倒角仅保证量级正确,距离所在侧可能反转。
distance_2 = node.params.get("distance_2_mm")
angle_rad = node.params.get("angle_rad")
if distance_2 is None and angle_rad is not None:
distance_2 = distance * math.tan(float(angle_rad))
# 4. 解析目标边(支持相切传播),执行倒角。
body = session.adapter.chamfer(
session.body, distance, node.params.get("distance_2_mm"),
session.body, distance, distance_2,
_selector_edges(node, session, tangent_propagation=bool(node.params.get("tangent_propagation"))),
)
# 4. 登记新主体并返回结果。
# 5. 登记新主体并返回结果。
session.register_body(node.feature_id, body, replay_node=node)
return session.result(node)
@@ -658,6 +658,40 @@ class EngineRuntimeFoundationTests(unittest.TestCase):
self.assertLess(chamfer_result["volume_mm3"], baseline["volume_mm3"])
self.assertAlmostEqual(pattern_result["volume_mm3"], 1000 - 3 * 10 * 3.141592653589793, places=5)
def test_chamfer_consumes_angle_rad_instead_of_silent_45_degree_fallback(self) -> None:
from cdsl_engine.runtime import rebuild_cdsl
import math
base = self._base_block()
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
baseline = rebuild_cdsl(base, root / "baseline.step")
edge = next(item for item in baseline["topology_records"] if item["kind"] == "edge")
selector = {
"kind": "edge", "stable_id": "edge", "source": "solidworks", "confidence": 1,
"owner_feature_id": "base_add", "geometry": edge["geometry"],
}
equal = deepcopy(base)
equal["features"].append({
"id": "chamfer_45", "atomic_id": "chamfer", "depends_on": ["base_add"],
"params": {"distance_mm": 1, "angle_rad": math.pi / 4}, "selectors": [deepcopy(selector)],
})
equal_result = rebuild_cdsl(equal, root / "chamfer-45.step")
# 45° Distance-Angle 等价于等距倒角:tan(45°)=1,切掉 0.5*1*1*10=5 mm³。
self.assertAlmostEqual(equal_result["volume_mm3"], 1000 - 5, places=5)
slanted = deepcopy(base)
slanted["features"].append({
"id": "chamfer_30", "atomic_id": "chamfer", "depends_on": ["base_add"],
"params": {"distance_mm": 1, "angle_rad": math.pi / 6}, "selectors": [deepcopy(selector)],
})
slanted_result = rebuild_cdsl(slanted, root / "chamfer-30.step")
# 30°:第二距离 = 1*tan(30°)≈0.577,切掉 0.5*1*0.577*10≈2.887 mm³,
# 体积明显大于 45° 等距倒角(995),验证 angle_rad 被消费而非静默 45°。
self.assertAlmostEqual(slanted_result["volume_mm3"], 1000 - 0.5 * math.tan(math.pi / 6) * 10, places=5)
def test_linear_pattern_replays_hole_with_explicit_host_frame(self) -> None:
from cdsl_engine.runtime import rebuild_cdsl