main
John Lauer Add local values, both-face contact leaders, J1 per-pin labels and MOSFET pin1 markers 181f6be 20d ago
from pathlib import Path
import json,re,uuid
D=Path(__file__).parent;s=(D/'esc-g431-astra-silkscreen-v20.kicad_pcb').read_text();m=json.loads((D/'labels-v20.json').read_text())
for ref,x,y,ang in [('C20',129.55,85.65,90),('C22',123.35,82.4,90)]:
 l=next(l for l in m['labels'] if l.get('reference')==ref and 'value' in l.get('role',''));old=f'(gr_text {json.dumps(l["text"])} (at {l["x"]} {l["y"]} {l.get("angle",0)})';assert old in s;s=s.replace(old,f'(gr_text {json.dumps(l["text"])} (at {x} {y} {ang})',1);l.update(x=x,y=y,angle=ang)
# Clear only redundant back resistor-table rows to reserve the actual J1 position.
for l in list(m['labels']):
 if l['layer']=='B.SilkS' and l['role']=='service-key' and l['x']==115 and 108<l['y']<121:
  pattern=r'\(gr_text '+re.escape(json.dumps(l['text']))+r' .*?\(justify mirror\)\)\)';s,n=re.subn(pattern,'',s,count=1,flags=re.S);assert n==1,l;m['labels'].remove(l)
new=[];names=['NC','NC','+3V3','SWDIO','GND','SWCLK','GND','NC','NC','NC','GND','nRST','UTX','URX']
for i,name in enumerate(names,1):
 for layer in ['F.SilkS','B.SilkS']:
  x=112.05 if i%2 else 115.95;y=109.19+((i-1)//2)*1.27+.635;t=f'{i} {name}';mirror=' (justify mirror)' if layer=='B.SilkS' else ''
  new.append(f'(gr_text "{t}" (at {x} {y}) (layer "{layer}") (uuid "{uuid.uuid4()}") (effects (font (size 0.2 0.2) (thickness 0.03)){mirror}))');m['labels'].append(dict(text=t,x=x,y=y,size=.2,layer=layer,reference='J1',pin=i,role='per-pin function',source='J1 numbered pad net; NC is electrically unconnected'))
out=D/'esc-g431-astra-silkscreen-v21.kicad_pcb';out.write_text(s.rstrip()[:-1]+'\n'+'\n'.join(new)+'\n)\n');m['board']=str(out);(D/'labels-v21.json').write_text(json.dumps(m,indent=2))