fix: replace kicad-skip mutation with sexpdata in delete_schematic_wire

kicad-skip's write() serializes from original parsed data, silently
discarding in-memory _elements mutations. Switch to the same sexpdata
approach used by add_wire: parse, find matching wire, delete the entry,
write back. Also adds WireManager.delete_wire() static method.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Eugene Mikhantyev
2026-03-13 00:39:23 +00:00
parent 2956b4fa9d
commit 9d9234abd0
2 changed files with 72 additions and 36 deletions

View File

@@ -1898,43 +1898,13 @@ class KiCADInterface:
if not schematic_path:
return {"success": False, "message": "schematicPath is required"}
schematic = SchematicManager.load_schematic(schematic_path)
if not schematic:
return {"success": False, "message": "Failed to load schematic"}
from pathlib import Path
from commands.wire_manager import WireManager
start_point = [start.get("x", 0), start.get("y", 0)]
end_point = [end.get("x", 0), end.get("y", 0)]
if not hasattr(schematic, "wire"):
return {"success": False, "message": "Schematic has no wires"}
tolerance = 0.5
wire_to_remove = None
for wire in schematic.wire:
if not hasattr(wire, "pts") or not hasattr(wire.pts, "xy"):
continue
points = []
for point in wire.pts.xy:
if hasattr(point, "value"):
points.append([float(point.value[0]), float(point.value[1])])
if len(points) < 2:
continue
sx, sy = start.get("x", 0), start.get("y", 0)
ex, ey = end.get("x", 0), end.get("y", 0)
# Check both directions
match_fwd = (abs(points[0][0] - sx) < tolerance and abs(points[0][1] - sy) < tolerance and
abs(points[-1][0] - ex) < tolerance and abs(points[-1][1] - ey) < tolerance)
match_rev = (abs(points[0][0] - ex) < tolerance and abs(points[0][1] - ey) < tolerance and
abs(points[-1][0] - sx) < tolerance and abs(points[-1][1] - sy) < tolerance)
if match_fwd or match_rev:
wire_to_remove = wire
break
if wire_to_remove:
schematic.wire._elements.remove(wire_to_remove)
SchematicManager.save_schematic(schematic, schematic_path)
deleted = WireManager.delete_wire(Path(schematic_path), start_point, end_point)
if deleted:
return {"success": True}
else:
return {"success": False, "message": "No matching wire found"}