"""Generate loss annotations on clipped copies of the native website schematics.""" from pathlib import Path import math,re,copy import xml.etree.ElementTree as ET ROOT=Path(__file__).resolve().parent NS='http://www.w3.org/2000/svg' ET.register_namespace('',NS) def el(tag,**attrs):return ET.Element('{'+NS+'}'+tag,{k.replace('_','-'):str(v) for k,v in attrs.items()}) # Coordinates are in the native SVG frame; topology wiring is unchanged. CONFIG={ 'xfly6s':dict(xs=[378,522],ys=[212.72,284.72,371.12],r=[1,2,1/3],caps=[(417.6,288.32),(482.4,288.32)],coils=[(432.72,371.84),(467.28,371.84)],io=[(450,206.96),(450,424.4)]), 'xfly4p':dict(xs=[400.5,515.7],ys=[194,227.408,297.68,331.088],r=[(3+math.sqrt(5))/6]*3+[(3+math.sqrt(5))/(6*math.sqrt(5))],caps=[(432.18,246.992),(484.02,246.992)],clamps=[(374.58,259.088),(541.62,259.088)],coils=[(444.276,352.4),(471.924,352.4)],io=[(458.1,189.968),(458.1,390.992)]), '3lbuck':dict(xs=[615.06,730.26],ys=[194,245.84,297.68,349.52],r=[(1+math.sqrt(3))/3]*2+[(1+math.sqrt(3))/(3*math.sqrt(3))]*2,caps=[(646.74,246.992),(698.58,246.992)],coils=[(658.836,352.4),(686.484,352.4)],io=[(672.66,189.968),(672.66,390.992)]), '2sbuck':dict(xs=[829.62,944.82],ys=[194,245.84,297.68,349.52],r=[1,1,1,1/3],caps=[(861.30,246.992),(913.14,246.992)],clamps=[(803.7,259.088),(970.74,259.088)],coils=[(873.396,352.4),(901.044,352.4)],io=[(887.22,189.968),(887.22,390.992)]), 'buck':dict(xs=[1044.18,1159.38],ys=[194,245.84,297.68,349.52],r=[(1+math.sqrt(3))/3]*2+[(1+math.sqrt(3))/(3*math.sqrt(3))]*2,caps=[],coils=[(1087.956,344.912),(1115.604,344.912)],io=[(1101.78,189.968),(1101.78,390.992)]), } def generate(): for name,c in CONFIG.items(): src=ET.parse(ROOT/'static/assets'/f'{name}.svg').getroot();vx,vy,w,h=map(float,src.get('viewBox').split());pad=70 root=el('svg',viewBox=f'{-pad} -5 {w+2*pad} {h+30}',width=w+2*pad,height=h+30,role='img') title=el('title');title.text=f'{name}: individual device resistances at the fixed 4:1 allocation';root.append(title) root.append(el('rect',x=-pad,y=-5,width=w+2*pad,height=h+30,fill='white')) src.set('x','0');src.set('y','0');src.set('width',str(w));src.set('height',str(h));src.set('overflow','hidden');root.append(src) def badge(x,y,text,kind,anchor='middle'): g=el('g',data_resistance=kind);root.append(g);tw=len(text)*5.15+5;left=x-tw/2 if anchor=='middle' else x-tw if anchor=='end' else x g.append(el('rect',x=left,y=y-9,width=tw,height=13,rx=2,fill='white',opacity='.96')) t=el('text',x=x,y=y,font_family='Arial,Helvetica,sans-serif',font_size=9.5,text_anchor=anchor,fill='#157e70');t.text=text;g.append(t) for branch,x in enumerate(c['xs']): for i,(y,r) in enumerate(zip(c['ys'],c['r'])): lx=-7 if branch==0 else w+7;anchor='end' if branch==0 else 'start';yy=y-vy+12 root.append(el('path',d=f'M{lx} {yy-3} H{x-vx}',stroke='#8bb4a5',stroke_width='.6',stroke_dasharray='2 2',fill='none')) num='Rₛ' if r==1 else '2Rₛ' if r==2 else 'Rₛ/3' if abs(r-1/3)<1e-9 else f'{r:.3f}Rₛ' badge(lx,yy,f'{"S" if branch==0 else "Q"}{i+1}: {num}','switch',anchor) for x,y in c['caps']:badge(x-vx,y-vy+25,'R꜀','capacitor') for x,y in c.get('clamps',[]):badge(x-vx,y-vy+20,'ESR=0','clamp') for i,(x,y) in enumerate(c['coils']):badge(x-vx+(-35 if i==0 else 35),y-vy+(45 if name=='xfly6s' else 30),'Rₗ','winding') for x,y in c['io']:badge(x-vx+22,y-vy+22,'ESR omitted','filter',anchor='start') badge(w/2,h+14,'Rₛ: unit-area FET · R꜀: main-cap ESR · Rₗ: each winding','legend') ET.ElementTree(root).write(ROOT/'static/assets'/f'{name}-loss.svg',encoding='unicode') def candidates(): # Presentation-only assets: preserve MOSFET gate terminals and circuit wiring, # omit the blue gating annotations, AND glyphs, overbars and control notes. for name in [*CONFIG,'buck2','sp2']: path=ROOT/'static/assets'/f'{name}{'-loss' if name in CONFIG else ''}.svg' root=ET.parse(path).getroot() for parent in list(root.iter()): for item in list(parent): tag=item.tag.rsplit('}',1)[-1];txt=''.join(item.itertext()).strip() blue=item.get('fill','').lower()=='#205c85' or item.get('stroke','').lower()=='#205c85' control_text=tag=='text' and ((blue and re.fullmatch(r'[ABCD]+',txt)) or 'A and B:' in txt or '50% clocks' in txt) control_graphic=tag in ['polyline','polygon','path','line'] and blue if control_text or control_graphic:parent.remove(item) title=next((x for x in root if x.tag.endswith('title')),None) if title is not None:title.text=f'{name}: topology candidate with Rs, Rc and Rl; gating annotations omitted' ET.ElementTree(root).write(ROOT/'static/assets'/f'{name}-candidate.svg',encoding='unicode') if __name__=='__main__':generate();candidates()