89 lines
3.1 KiB
Python
89 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import math
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
SCRIPT_PATH = Path(__file__).resolve().parents[1] / "step_brep_topology.py"
|
|
SPEC = importlib.util.spec_from_file_location("step_brep_topology", SCRIPT_PATH)
|
|
if SPEC is None or SPEC.loader is None:
|
|
raise RuntimeError(f"无法加载 STEP B-Rep 解析脚本:{SCRIPT_PATH}")
|
|
STEP_BREP = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(STEP_BREP)
|
|
|
|
|
|
def sampled_edge(points: list[list[float]]) -> dict[str, object]:
|
|
return {
|
|
"curveType": "bspline",
|
|
"samplesMm": points,
|
|
}
|
|
|
|
|
|
class FittedCircularWireTest(unittest.TestCase):
|
|
def test_accepts_closed_planar_bspline_circle(self) -> None:
|
|
center = [12.5, -3.25, 7.0]
|
|
radius = 2.5
|
|
points = [
|
|
[
|
|
center[0] + radius * math.cos(math.tau * index / 64),
|
|
center[1] + radius * math.sin(math.tau * index / 64),
|
|
center[2],
|
|
]
|
|
for index in range(65)
|
|
]
|
|
|
|
result = STEP_BREP.circular_wire_payload([sampled_edge(points)], [0.0, 0.0, 1.0])
|
|
|
|
self.assertTrue(result["isCircular"])
|
|
self.assertEqual(result["circularityMethod"], "verified-fit")
|
|
self.assertAlmostEqual(result["radiusMm"], radius, places=6)
|
|
self.assertLess(result["fitResidualMm"], 1e-6)
|
|
|
|
def test_rejects_closed_ellipse(self) -> None:
|
|
points = [
|
|
[4.0 * math.cos(math.tau * index / 64), 2.0 * math.sin(math.tau * index / 64), 0.0]
|
|
for index in range(65)
|
|
]
|
|
|
|
result = STEP_BREP.circular_wire_payload([sampled_edge(points)], [0.0, 0.0, 1.0])
|
|
|
|
self.assertFalse(result["isCircular"])
|
|
|
|
def test_rejects_open_circular_arc(self) -> None:
|
|
points = [
|
|
[2.5 * math.cos(math.pi * index / 32), 2.5 * math.sin(math.pi * index / 32), 0.0]
|
|
for index in range(33)
|
|
]
|
|
|
|
result = STEP_BREP.circular_wire_payload([sampled_edge(points)], [0.0, 0.0, 1.0])
|
|
|
|
self.assertFalse(result["isCircular"])
|
|
|
|
|
|
class InterfaceStepRegressionTest(unittest.TestCase):
|
|
def test_primary_mounting_face_exposes_nine_bspline_holes(self) -> None:
|
|
project_root = Path(__file__).resolve().parents[3]
|
|
fixture = project_root / "模型文件" / "step" / (
|
|
"未命名-_X2_817090e888c5914d_X0_dm-j10010l_X2_7535673a8f6c5b50_X0_001.step"
|
|
)
|
|
if not fixture.is_file():
|
|
self.skipTest(f"STEP 回归样例不存在:{fixture}")
|
|
|
|
topology = STEP_BREP.analyze(fixture)
|
|
face = next(item for item in topology["faces"] if item["id"] == "brep:face:7")
|
|
holes = [wire for wire in face["wires"] if not wire["isOuter"] and wire["isCircular"]]
|
|
|
|
self.assertEqual(len(holes), 9)
|
|
for hole in holes:
|
|
self.assertEqual(hole["circularityMethod"], "verified-fit")
|
|
self.assertAlmostEqual(hole["radiusMm"], 2.50005, places=4)
|
|
self.assertLessEqual(hole["fitResidualMm"], 0.005)
|
|
self.assertTrue(hole["sourceRef"].startswith("sha256:"))
|
|
self.assertTrue(hole["edges"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|