main
John Lauer
Review component pin functions and validate isolated library model and hole corrections
7fb4d36
"""Prepare isolated nominal-model review; preserve every electrical PCB item."""
from pathlib import Path
import json,hashlib,shutil,copy
import sexp
root=Path(__file__).resolve().parents[1];out=root/'checkpoints/library-model-review';out.mkdir(exist_ok=True)
src=root/'checkpoints/library-metadata-review/d1-mpn-candidate.kicad_pcb'
raw=src.read_bytes();before=sexp.parse(raw.decode());after=copy.deepcopy(before)
selection={'U3':('opa189idbvr','opa189idbvr-marked.step'),'U7':('lm339bidr','lm339bidr.step'),'RSH1':('lvk12r020der','lvk12r020der.step'),'J9':('m20-9990346','m20-9990346.step'),'J10':('m20-9990246','m20-9990246.step'),'J11':('fts-105-01-l-s','fts-105-01-l-s.step')}
shutil.copytree(root/'kicad/3d',out/'3d',dirs_exist_ok=True)
rows=[]
for fp in sexp.find(after,'footprint'):
props={p[1]:p[2] for p in sexp.find(fp,'property')};ref=props['Reference']
if ref not in selection:continue
slug,name=selection[ref];model=sexp.first(fp,'model');old=model[1];new='${KIPRJMOD}/3d/'+name
uid=sexp.first(fp,'uuid')[1];anchor=raw.index(('(uuid "'+uid+'")').encode());end=raw.find(b'\n\t(footprint ',anchor)
if end<0:end=len(raw)
section=raw[anchor:end];assert section.count(old.encode())==1
raw=raw[:anchor]+section.replace(old.encode(),new.encode(),1)+raw[end:];model[1]=new
asset=root/'components'/slug/name;assert asset.is_file();shutil.copyfile(asset,out/'3d'/name)
rows.append({'reference':ref,'oldBinding':old,'candidateBinding':new,'source':str(asset.relative_to(root)),'sha256':hashlib.sha256(asset.read_bytes()).hexdigest(),'orientationBasis':'Catalog lead coordinates checked against footprint local pin positions; STEP Y-up versus footprint Y-down. Existing zero transforms retained. Native assembled render pending.'})
assert sexp.parse(raw.decode())==after
for fp in sexp.find(after,'footprint'):
ref=next(p[2] for p in sexp.find(fp,'property') if p[1]=='Reference')
if ref in selection:sexp.first(fp,'model')[1]=next(r['oldBinding'] for r in rows if r['reference']==ref)
assert after==before
board=out/'library-model-candidate.kicad_pcb';board.write_bytes(raw)
for ext in ['kicad_pro','kicad_dru']:shutil.copyfile(root/'aiflow/run'/('board-24.'+ext),out/('library-model-candidate.'+ext))
shutil.copyfile(root/'kicad/fp-lib-table',out/'fp-lib-table');shutil.copytree(root/'kicad/lib',out/'lib',dirs_exist_ok=True)
report={'source':str(src.relative_to(root)),'sourceSha256':hashlib.sha256(src.read_bytes()).hexdigest(),'candidate':str(board.relative_to(root)),'candidateSha256':hashlib.sha256(raw).hexdigest(),'models':rows,'electricalGeometryUnchanged':True,'Q1':'Unchanged; horizontal lead forming unresolved','nativeApplied':False,'qualification':'Nominal model review candidate only; footprint variants and physical fit remain unqualified'}
(out/'selection-review.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps({'models':len(rows),'electricalGeometryUnchanged':True,'candidate':str(board)}))