fix: 优化FreeCAD自动布线并线和碰撞诊断

dev
Zhaowenlong 3 weeks ago
parent 5c813b66dd
commit a2577f9434

@ -144,13 +144,19 @@ def _lane_payload(route_index, options):
lane_axis = (opts.get("lane_axis") or "y").lower()
if lane_axis not in {"x", "y", "z"}:
lane_axis = "y"
lane_index = int(route_index or 0)
lane_index = max(int(route_index or 0), 0)
lane_spacing = float(opts.get("lane_spacing", 0.0) or 0.0)
if lane_index <= 0:
lane_offset = 0.0
else:
lane_order = (lane_index + 1) // 2
lane_direction = 1.0 if lane_index % 2 == 1 else -1.0
lane_offset = float(lane_order) * lane_spacing * lane_direction
return {
"index": lane_index,
"axis": lane_axis,
"spacing_mm": lane_spacing,
"offset_mm": float(lane_index) * lane_spacing,
"offset_mm": lane_offset,
}
@ -809,6 +815,7 @@ def collect_obstacles(doc, exclude=None, options=None):
continue
if RoutingNetwork.is_route_carrier(obj) or WiringObjects.is_routed_wire_object(obj):
continue
raw_bbox = _bbox_payload(obj, clearance=0.0)
bbox = _bbox_payload(obj, clearance=clearance)
if bbox is None:
continue
@ -823,6 +830,7 @@ def collect_obstacles(doc, exclude=None, options=None):
"label": getattr(obj, "Label", ""),
"type_id": getattr(obj, "TypeId", ""),
"bbox": bbox,
"raw_bbox": raw_bbox or bbox,
}
)
return obstacles
@ -871,8 +879,12 @@ def detect_collisions(points, obstacles, ignored_segment_indices=None):
collisions.append(
{
"segment_index": index,
"segment_start": _point_payload(start),
"segment_end": _point_payload(end),
"obstacle_name": obstacle.get("name", ""),
"obstacle_label": obstacle.get("label", ""),
"obstacle_bbox": dict(obstacle.get("raw_bbox") or obstacle.get("bbox") or {}),
"collision_bbox": dict(obstacle.get("bbox", {}) or {}),
}
)
return collisions

@ -312,13 +312,25 @@ class AutoRoutingTest(unittest.TestCase):
wire_uuid="wire-2",
options={"lane_spacing": 12.0, "lane_axis": "y"},
)
third = auto_routing.route_eplan_connection_between_terminals(
doc,
start,
end,
route_index=2,
wire_uuid="wire-3",
options={"lane_spacing": 12.0, "lane_axis": "y"},
)
payload = json.loads(second["wire"].QetRouteDiagnosticsJson)
third_payload = json.loads(third["wire"].QetRouteDiagnosticsJson)
self.assertTrue(any(abs(point.y - 0.0) <= 0.001 for point in first["points"][1:-1]))
self.assertTrue(any(abs(point.y - 12.0) <= 0.001 for point in second["points"][1:-1]))
self.assertTrue(any(abs(point.y + 12.0) <= 0.001 for point in third["points"][1:-1]))
self.assertEqual(1, payload["lane"]["index"])
self.assertEqual("y", payload["lane"]["axis"])
self.assertEqual(12.0, payload["lane"]["offset_mm"])
self.assertEqual(2, third_payload["lane"]["index"])
self.assertEqual(-12.0, third_payload["lane"]["offset_mm"])
def test_eplan_connection_route_replaces_existing_wire_uuid_when_endpoints_change(self):
_install_fake_freecad()
@ -1364,6 +1376,10 @@ class AutoRoutingTest(unittest.TestCase):
self.assertEqual("wire-1", report["collision_samples"][0]["wire_uuid"])
self.assertEqual("N4111", report["collision_samples"][0]["wire_label"])
self.assertEqual("MiddleObstacle", report["collision_samples"][0]["obstacle_name"])
self.assertEqual({"x": 0.0, "y": 0.0, "z": 100.0}, report["collision_samples"][0]["segment_start"])
self.assertEqual({"x": 100.0, "y": 0.0, "z": 100.0}, report["collision_samples"][0]["segment_end"])
self.assertEqual(40.0, report["collision_samples"][0]["obstacle_bbox"]["xmin"])
self.assertEqual(35.0, report["collision_samples"][0]["collision_bbox"]["xmin"])
self.assertEqual("Middle Obstacle", report["routes"][0]["collision_samples"][0]["obstacle_label"])
self.assertIn("碰撞示例", message)
self.assertIn("Middle Obstacle", message)

Loading…
Cancel
Save