main
John Lauer Save native two-sided contact artwork and recorded GLB material correction; qualification remains open 57e2489
#!/usr/bin/env python3
"""Isolated bottom artwork candidate; the live board is never changed here."""
import json,hashlib,uuid,shutil
from pathlib import Path
from sexp import parse
R=Path(__file__).resolve().parents[1];P=R/'design/silkscreen-review';a=json.loads((P/'bottom-artwork.json').read_text());assert not a['findings']
source=R/'kicad/adom-custom-electronic-load.kicad_pcb';raw=source.read_bytes().decode();assert hashlib.sha256(raw.encode()).hexdigest()==a['boardSha256'];nodes=[];ids=[]
for t in a['texts']:
 ident=str(uuid.uuid4());ids.append(ident);size=t.get('size',t.get('height'));width=t.get('width',size);rotation=t.get('angle',t.get('rotation',0))
 nodes.append(f'(gr_text {json.dumps(t["text"])} (at {t["x"]:.9f} {t["y"]:.9f} {rotation}) (layer "B.SilkS") (uuid "{ident}") (effects (font (size {size} {width}) (thickness {t["stroke"]})) (justify mirror)))')
for g in a['graphics']:
 ident=str(uuid.uuid4());ids.append(ident);(x,y),(X,Y)=g['points'];nodes.append(f'(gr_line (start {x:.9f} {y:.9f}) (end {X:.9f} {Y:.9f}) (stroke (width {g["stroke"]}) (type solid)) (layer "B.SilkS") (uuid "{ident}"))')
pos=raw.rfind(')');new=raw[:pos]+'\n'+'\n'.join(nodes)+'\n'+raw[pos:];parsed=parse(new)
# Remove only the newly appended root nodes, then compare the entire parsed original.
assert parsed[:-len(nodes)]==parse(raw),'Unrelated board data changed'
out=R/'kicad/bottom-artwork-candidate.kicad_pcb';out.write_text(new)
for suffix in ['.kicad_pro','.kicad_dru']:shutil.copyfile(source.with_suffix(suffix),out.with_suffix(suffix))
report=dict(status='isolated candidate; live board unchanged',sourceSha256=a['boardSha256'],candidateSha256=hashlib.sha256(new.encode()).hexdigest(),textCount=len(a['texts']),graphicCount=len(a['graphics']),onlyStandaloneBottomSilkscreenAdded=True,addedIds=ids)
(P/'bottom-saved-candidate-review.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps({k:v for k,v in report.items() if k!='addedIds'}))