diff --git a/src/Mod/FreeCADExchange/AutoRouting.py b/src/Mod/FreeCADExchange/AutoRouting.py index fce56d0..9437829 100644 --- a/src/Mod/FreeCADExchange/AutoRouting.py +++ b/src/Mod/FreeCADExchange/AutoRouting.py @@ -1115,7 +1115,11 @@ def route_eplan_connection_between_terminals( raise if existing_replacements: - _remove_routing_connection_objects(doc, existing_replacements) + removed_existing = _remove_routing_connection_objects(doc, existing_replacements) + if removed_existing != len(existing_replacements): + if wire is not None: + _remove_routing_connection_objects(doc, [wire]) + raise AutoRoutingError("Failed to replace existing routed connection.") try: doc.recompute() diff --git a/tests/python/freecad_exchange_auto_routing_test.py b/tests/python/freecad_exchange_auto_routing_test.py index f22aca8..bc77a15 100644 --- a/tests/python/freecad_exchange_auto_routing_test.py +++ b/tests/python/freecad_exchange_auto_routing_test.py @@ -504,6 +504,49 @@ class AutoRoutingTest(unittest.TestCase): self.assertEqual([first], routed_wires) self.assertEqual(0, len([obj for obj in doc.Objects if obj.Name == "Wire"])) + def test_eplan_connection_route_keeps_existing_wire_when_old_replacement_removal_fails(self): + _install_fake_freecad() + terminal_objects, wiring_objects, routing_network, auto_routing = _reload_modules() + app = sys.modules["FreeCAD"] + doc = FakeDocument() + terminal_objects.ensure_root_group(doc, "project-1") + start = _terminal(doc, terminal_objects, "TerminalStart", "terminal-start", app.Vector(0, 0, 0)) + end = _terminal(doc, terminal_objects, "TerminalEnd", "terminal-end", app.Vector(100, 0, 0)) + routing_network.create_route_carrier( + doc, + [app.Vector(0, 0, 20), app.Vector(100, 0, 20)], + project_uuid="project-1", + kind="WireDuct", + ) + first = auto_routing.route_eplan_connection_between_terminals( + doc, + start, + end, + wire_uuid="wire-1", + )["wire"] + original_remove = auto_routing._remove_routing_connection_objects + + def failing_remove(target_doc, objects): + if first in list(objects or []): + return 0 + return original_remove(target_doc, objects) + + auto_routing._remove_routing_connection_objects = failing_remove + try: + with self.assertRaises(auto_routing.AutoRoutingError): + auto_routing.route_eplan_connection_between_terminals( + doc, + start, + end, + wire_uuid="wire-1", + ) + finally: + auto_routing._remove_routing_connection_objects = original_remove + + routed_wires = list(wiring_objects.iter_routed_wire_objects(doc)) + + self.assertEqual([first], routed_wires) + def test_route_carrier_styles_make_generated_objects_distinguishable(self): _install_fake_freecad() terminal_objects, _wiring_objects, routing_network, _auto_routing = _reload_modules()