main
John Lauer Save native two-sided contact artwork and recorded GLB material correction; qualification remains open 57e2489
#!/usr/bin/env python3
"""Compare native saved readback with the original and the reviewed artwork."""
import json,hashlib,collections,math
from pathlib import Path
from sexp import parse,find,first
R=Path(__file__).resolve().parents[1];P=R/'design/silkscreen-review';old=R/'kicad/adom-custom-electronic-load.kicad_pcb';new=P/'landed/adom-custom-electronic-load.kicad_pcb';a=json.loads((P/'bottom-artwork.json').read_text());before=parse(old.read_text());after=parse(new.read_text());assert hashlib.sha256(old.read_bytes()).hexdigest()==a['boardSha256']
def stable(nodes):return sorted(json.dumps(n,sort_keys=True) for n in nodes)
physical=['footprint','segment','arc','via','zone','net','setup','layers','gr_arc','gr_poly']
checks={kind:stable(find(before,kind))==stable(find(after,kind)) for kind in physical}
# KiCad serializes near-zero reference coordinates as literal zero. Restrict
# normalization to that exact harmless save behavior, never pads or copper.
import copy
def normalized_footprints(board):
 rows=copy.deepcopy(find(board,'footprint'))
 for f in rows:
  for p in find(f,'property'):
   if p[1]!='Reference':continue
   at=first(p,'at')
   for i in [1,2]:
    if abs(float(at[i]))<1e-12:at[i]='0'
 return rows
checks['footprint']=stable(normalized_footprints(before))==stable(normalized_footprints(after))
assert all(checks.values()),checks
oldtexts=find(before,'gr_text');newtexts=find(after,'gr_text');oldlines=find(before,'gr_line');newlines=find(after,'gr_line')
def key(n):return first(n,'uuid')[1]
oldmap={key(n):n for n in oldtexts+oldlines};newmap={key(n):n for n in newtexts+newlines};assert all(newmap.get(k)==n for k,n in oldmap.items()),'Existing standalone artwork changed'
texts=[n for n in newtexts if key(n) not in oldmap];lines=[n for n in newlines if key(n) not in oldmap];assert len(texts)==len(a['texts']) and len(lines)==len(a['graphics'])
remaining=texts.copy();mapping=[]
for t in a['texts']:
 size=t.get('size',t.get('height'));rot=t.get('angle',t.get('rotation',0));matches=[]
 for n in remaining:
  at=first(n,'at');effects=first(n,'effects');font=first(effects,'font');sz=first(font,'size');stroke=first(font,'thickness');justify=first(effects,'justify')
  if n[1]==t['text'] and first(n,'layer')[1]=='B.SilkS' and math.dist(list(map(float,at[1:3])),[t['x'],t['y']])<1e-5 and abs(float(at[3] if len(at)>3 else 0)-rot)<1e-5 and abs(float(sz[1])-size)<1e-6 and abs(float(sz[2])-t.get('width',size))<1e-6 and abs(float(stroke[1])-t['stroke'])<1e-6 and justify and 'mirror' in justify:matches.append(n)
 assert len(matches)==1,(t['text'],len(matches));n=matches[0];remaining.remove(n);mapping.append(dict(id=key(n),text=t['text'],reference=t.get('reference'),x=t['x'],y=t['y']))
remaining=lines.copy()
for g in a['graphics']:
 matches=[n for n in remaining if first(n,'layer')[1]=='B.SilkS' and math.dist(list(map(float,first(n,'start')[1:3])),g['points'][0])<1e-5 and math.dist(list(map(float,first(n,'end')[1:3])),g['points'][1])<1e-5 and abs(float(first(first(n,'stroke'),'width')[1])-g['stroke'])<1e-6]
 assert len(matches)==1,(g,len(matches));remaining.remove(matches[0])
report=dict(status='native saved readback matches reviewed artwork',sourceSha256=a['boardSha256'],savedSha256=hashlib.sha256(new.read_bytes()).hexdigest(),unchanged=checks,addedTexts=len(texts),addedGraphics=len(lines),nativeTextIds=mapping,nativeGraphicIds=[key(n) for n in lines],nativeDRCStillRequired=True,normalizationNote='Eight pre-existing Reference coordinates below 1e-12 mm serialized to zero; no pad, component placement or copper change')
(P/'bottom-landing-verification.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps({k:v for k,v in report.items() if k not in ['nativeTextIds','nativeGraphicIds']}))