From 0b0c32e0c3c6fe4f2b79f4872d0db4a357d4b78f Mon Sep 17 00:00:00 2001 From: Zhaowenlong Date: Sat, 30 May 2026 16:09:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=9D=E7=95=99FreeCAD=E5=B7=B2?= =?UTF-8?q?=E5=B8=83=E7=BA=BF=E8=AF=8A=E6=96=AD=E5=85=83=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Mod/FreeCADExchange/WiringObjects.py | 17 ++++++++++ tests/python/freecad_exchange_wiring_test.py | 35 ++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Mod/FreeCADExchange/WiringObjects.py b/src/Mod/FreeCADExchange/WiringObjects.py index 719e83f..43d2360 100644 --- a/src/Mod/FreeCADExchange/WiringObjects.py +++ b/src/Mod/FreeCADExchange/WiringObjects.py @@ -314,6 +314,21 @@ def _json_property(obj, prop_name, fallback=None): return fallback +def _float_property(obj, prop_name, fallback=0.0): + try: + return float(getattr(obj, prop_name, fallback) or fallback) + except Exception: + return float(fallback) + + +def _route_metadata_payload(wire_obj): + return { + "wire_style_id": getattr(wire_obj, "QetWireStyleId", "").strip(), + "length_mm": _float_property(wire_obj, "QetRouteLengthMm", 0.0), + "route_diagnostics": _json_property(wire_obj, "QetRouteDiagnosticsJson", {}), + } + + def wire_shape_points(wire_obj): if wire_obj is None: return [] @@ -374,6 +389,7 @@ def wire_payload_from_object(wire_obj): "route_nodes": [], "route_track": {}, "terminal_exit_length": float(getattr(wire_obj, "QetTerminalExitLength", 0.0) or 0.0), + **_route_metadata_payload(wire_obj), } points = [_point_from_vector(point) for point in wire_shape_points(wire_obj)] return { @@ -395,6 +411,7 @@ def wire_payload_from_object(wire_obj): "route_nodes": _json_array_property(wire_obj, "QetRouteNodesJson"), "route_track": _json_property(wire_obj, "QetRouteTrackJson", {}), "terminal_exit_length": float(getattr(wire_obj, "QetTerminalExitLength", 0.0) or 0.0), + **_route_metadata_payload(wire_obj), } diff --git a/tests/python/freecad_exchange_wiring_test.py b/tests/python/freecad_exchange_wiring_test.py index 4a1a313..29bcb3f 100644 --- a/tests/python/freecad_exchange_wiring_test.py +++ b/tests/python/freecad_exchange_wiring_test.py @@ -1,3 +1,4 @@ +import json import os import sys import tempfile @@ -341,6 +342,40 @@ class WiringTest(unittest.TestCase): ) self.assertEqual("face", payload["route_nodes"][2]["anchor_kind"]) + def test_wire_payload_includes_auto_route_diagnostics_metadata(self): + _install_fake_freecad() + terminal_objects, wiring_objects, _manual_wiring, _write_back = _reload_modules() + app = sys.modules["FreeCAD"] + + doc = FakeDocument() + wire = doc.addObject("Part::Feature", "QETWire_auto") + wire.Shape = [app.Vector(0, 0, 0), app.Vector(100, 0, 0)] + terminal_objects.ensure_string_property(wire, "QetProjectUuid", "QET Exchange", "", "project-1") + terminal_objects.ensure_string_property(wire, "QetWireUuid", "QET Exchange", "", "wire-1") + terminal_objects.ensure_string_property(wire, "QetWireLabel", "QET Exchange", "", "N4111") + terminal_objects.ensure_string_property(wire, "QetStartTerminalUuid", "QET Exchange", "", "terminal-start") + terminal_objects.ensure_string_property(wire, "QetEndTerminalUuid", "QET Exchange", "", "terminal-end") + terminal_objects.ensure_string_property(wire, "QetStartInstanceId", "QET Exchange", "", "instance-start") + terminal_objects.ensure_string_property(wire, "QetEndInstanceId", "QET Exchange", "", "instance-end") + terminal_objects.ensure_string_property(wire, "RouteType", "QET Exchange", "", "RoutedConnection") + terminal_objects.ensure_string_property(wire, "RouteStatus", "QET Exchange", "", "CollisionWarning") + terminal_objects.ensure_string_property(wire, "RouteMode", "QET Exchange", "", "EplanRoute") + terminal_objects.ensure_string_property(wire, "QetWireStyleId", "QET Exchange", "", "42") + terminal_objects.ensure_string_property(wire, "QetRouteLengthMm", "QET Exchange", "", "123.5") + terminal_objects.ensure_string_property( + wire, + "QetRouteDiagnosticsJson", + "QET Exchange", + "", + json.dumps({"collision_count": 1, "wire_style_id": "42"}), + ) + + payload = wiring_objects.wire_payload_from_object(wire) + + self.assertEqual("42", payload["wire_style_id"]) + self.assertEqual(123.5, payload["length_mm"]) + self.assertEqual(1, payload["route_diagnostics"]["collision_count"]) + def test_wire_writeback_omits_scene_routed_wire_payload(self): _install_fake_freecad() terminal_objects, wiring_objects, manual_wiring, write_back = _reload_modules()