From b7acf2d1ccea491d9726ff9fd0961a0c2daf3cc1 Mon Sep 17 00:00:00 2001 From: Zhaowenlong Date: Thu, 28 May 2026 14:03:52 +0800 Subject: [PATCH] fix: write back exchange json beside qet input --- src/Mod/FreeCADExchange/ExchangeWriteBack.py | 10 +++++- tests/python/freecad_exchange_wiring_test.py | 36 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Mod/FreeCADExchange/ExchangeWriteBack.py b/src/Mod/FreeCADExchange/ExchangeWriteBack.py index 2e2f28d..d8e3eb6 100644 --- a/src/Mod/FreeCADExchange/ExchangeWriteBack.py +++ b/src/Mod/FreeCADExchange/ExchangeWriteBack.py @@ -17,6 +17,7 @@ except ImportError: STATE_WRITEBACK_OBSERVER = "_qet_exchange_writeback_observer" +ENV_JSON_PATH = "QET_2D_TO_3D_JSON" class ExchangeWriteBackError(RuntimeError): @@ -110,6 +111,13 @@ def _output_path_for_scene(scene_path): return str(path.parent / "3d_to_2d.json") +def _output_path_for_exchange_json(): + json_path = os.environ.get(ENV_JSON_PATH, "").strip() + if not json_path: + return "" + return str(Path(json_path).with_name("3d_to_2d.json")) + + def _format_timestamp(): return datetime.now().astimezone().isoformat(timespec="seconds") @@ -174,7 +182,7 @@ def write_back_document(doc=None, scene_path="", payload=None): raise ExchangeWriteBackError("No active FreeCAD document is available.") scene_path = _scene_path_from_doc(doc, scene_path) - output_path = _output_path_for_scene(scene_path) + output_path = _output_path_for_exchange_json() or _output_path_for_scene(scene_path) if not output_path: raise ExchangeWriteBackError( "Cannot determine the 3d_to_2d.json output path." diff --git a/tests/python/freecad_exchange_wiring_test.py b/tests/python/freecad_exchange_wiring_test.py index b9ffc2e..4f7a7e0 100644 --- a/tests/python/freecad_exchange_wiring_test.py +++ b/tests/python/freecad_exchange_wiring_test.py @@ -1,4 +1,6 @@ +import os import sys +import tempfile import types import unittest from pathlib import Path @@ -364,6 +366,40 @@ class WiringTest(unittest.TestCase): self.assertNotIn("manual_wires", report) + def test_writeback_prefers_qet_exchange_json_directory_over_scene_directory(self): + _install_fake_freecad() + terminal_objects, _wiring_objects, _manual_wiring, write_back = _reload_modules() + + doc = FakeDocument() + terminal_objects.ensure_root_group(doc, "project-1") + + old_scene = os.environ.get("QET_FREECAD_SCENE_FILE") + old_json = os.environ.get("QET_2D_TO_3D_JSON") + try: + with tempfile.TemporaryDirectory() as tmp_dir: + tmp_path = Path(tmp_dir) + scene_dir = tmp_path / "3D" + exchange_dir = tmp_path / ".qet_freecad" + scene_dir.mkdir() + exchange_dir.mkdir() + os.environ["QET_FREECAD_SCENE_FILE"] = str(scene_dir / "current.FCStd") + os.environ["QET_2D_TO_3D_JSON"] = str(exchange_dir / "2d_to_3d.json") + + report = write_back.write_back_document(doc, payload={"project_uuid": "project-1"}) + + self.assertEqual(str(exchange_dir / "3d_to_2d.json"), report["output_path"]) + self.assertTrue((exchange_dir / "3d_to_2d.json").is_file()) + self.assertFalse((scene_dir / "3d_to_2d.json").exists()) + finally: + if old_scene is None: + os.environ.pop("QET_FREECAD_SCENE_FILE", None) + else: + os.environ["QET_FREECAD_SCENE_FILE"] = old_scene + if old_json is None: + os.environ.pop("QET_2D_TO_3D_JSON", None) + else: + os.environ["QET_2D_TO_3D_JSON"] = old_json + if __name__ == "__main__": unittest.main()