fix(add_schematic_wire): support hierarchical sub-sheets (#170)

WireManager.add_wire and add_polyline_wire bailed out with
"No sheet_instances section found in schematic" when called on a
hierarchical sub-sheet — that block only exists in the root .kicad_sch.
The handler in kicad_interface.py converted the False return into a
flat "Failed to add wire" message, leaving callers with no diagnostic.

Apply the same fallback that WireManager.add_label has used since the
hierarchical-design support: when (sheet_instances ...) is absent,
append the new (wire ...) item at the end of the outer (kicad_sch ...)
form.

Adds tests/test_add_wire_sub_sheet.py with 6 regression tests covering
both add_wire and add_polyline_wire on a sub-sheet, including paren
balance, sexpdata round-trip, and segment count for an N-point polyline.
This commit is contained in:
Matthew Runo
2026-05-18 11:05:18 -07:00
committed by GitHub
parent 9843d75c91
commit e66c13361b
2 changed files with 146 additions and 7 deletions

View File

@@ -174,7 +174,9 @@ class WireManager:
start_point, end_point, stroke_width, stroke_type
)
# Find insertion point (before sheet_instances)
# Find insertion point (before sheet_instances on the root sheet,
# or appended to the end on a hierarchical sub-sheet which has no
# sheet_instances block).
sheet_instances_index = None
for i, item in enumerate(sch_data):
if isinstance(item, list) and len(item) > 0 and item[0] == _SYM_SHEET_INSTANCES:
@@ -182,10 +184,10 @@ class WireManager:
break
if sheet_instances_index is None:
logger.error("No sheet_instances section found in schematic")
return False
# Sub-sheets in hierarchical designs don't have (sheet_instances).
sheet_instances_index = len(sch_data)
# Insert wire before sheet_instances
# Insert wire before sheet_instances (or at end for sub-sheets)
sch_data.insert(sheet_instances_index, wire_sexp)
logger.info(f"Injected wire from {start_point} to {end_point}")
@@ -249,7 +251,9 @@ class WireManager:
for i in range(len(points) - 1)
]
# Find insertion point
# Find insertion point (before sheet_instances on the root sheet,
# or appended to the end on a hierarchical sub-sheet which has no
# sheet_instances block).
sheet_instances_index = None
for i, item in enumerate(sch_data):
if isinstance(item, list) and len(item) > 0 and item[0] == _SYM_SHEET_INSTANCES:
@@ -257,8 +261,8 @@ class WireManager:
break
if sheet_instances_index is None:
logger.error("No sheet_instances section found in schematic")
return False
# Sub-sheets in hierarchical designs don't have (sheet_instances).
sheet_instances_index = len(sch_data)
# Insert all segments (in reverse so order is preserved after inserts)
for wire_sexp in reversed(wire_sexps):