#!/usr/bin/env python3
"""Schematic cross-section of the magnetic coupling in the TVAC CAD model.

This simplified explanatory drawing is based on PARAMS from testbed_pot_cad.py,
not an exact CAD section export. All distances are in millimetres.
"""
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Circle
from testbed_pot_cad import PARAMS as p

out = Path(__file__).resolve().parent / 'output' / 'renders'
out.mkdir(parents=True, exist_ok=True)
fig,ax=plt.subplots(figsize=(11.8,5.8), dpi=145, facecolor='#f3f5f7')
ax.set_facecolor('#f3f5f7')
zt=p['pot_height']; membrane=p['membrane_t']; clearance=p['coupling_clearance']; rt=p['rotor_t'];
zl=zt-rt-clearance; zu=zt+membrane+clearance
# Lid cross-section: 20 mm acrylic, top pocket leaving 4 mm membrane
ax.add_patch(Rectangle((-68,zt),136,p['lid_t'],facecolor='#9ed8ec',edgecolor='#37758a',lw=1.8,alpha=.75))
ax.add_patch(Rectangle((-p['membrane_pocket_d']/2,zt+membrane),p['membrane_pocket_d'],p['lid_t']-membrane+.8,facecolor='#f3f5f7',edgecolor='#37758a',lw=1.4))
# Rotors
ax.add_patch(Rectangle((-p['rotor_d']/2,zl),p['rotor_d'],rt,facecolor='#374357',edgecolor='#18222d',lw=1.5))
ax.add_patch(Rectangle((-p['rotor_d']/2,zu),p['rotor_d'],rt,facecolor='#374357',edgecolor='#18222d',lw=1.5))
# Magnet pairs visible in section x = +/- 15 mm
for x in (-15,15):
 ax.add_patch(Rectangle((x-p['magnet_d']/2,zt-clearance-p['magnet_t']),p['magnet_d'],p['magnet_t'],facecolor='#f4ae66',edgecolor='#79401b',lw=1))
 ax.add_patch(Rectangle((x-p['magnet_d']/2,zu),p['magnet_d'],p['magnet_t'],facecolor='#f4ae66',edgecolor='#79401b',lw=1))
# Axial shaft to shutter
ax.add_patch(Rectangle((-p['shaft_d']/2,210),p['shaft_d'],zl-210,facecolor='#aab4bd',edgecolor='#596576',lw=1))
ax.add_patch(Rectangle((-28,209),56,2,facecolor='#fa802a',edgecolor='#a74d19',lw=1))
# motor outside cavity above pocket
ax.add_patch(Rectangle((-14,zu+rt+2),28,14,facecolor='#232b37',edgecolor='#141a23',lw=1.4))
ax.add_patch(Rectangle((-2.5,zu+rt),5,4,facecolor='#9da9b5',edgecolor='#596576'))
# Pot wall cut representation for chamber context
ax.add_patch(Rectangle((-67,210),2,zt-210,facecolor='#7c8997',edgecolor='#53616b'))
ax.add_patch(Rectangle((65,210),2,zt-210,facecolor='#7c8997',edgecolor='#53616b'))
# Vertical dimensions (not scaled?) actual geometry y equal z numeric
ax.annotate('',xy=(37,zt-clearance),xytext=(37,zu),arrowprops={'arrowstyle':'<->','color':'#334963','lw':1.7})
ax.text(39,(zt-clearance+zu)/2,'5 mm magnet-face gap',fontsize=10,va='center',color='#263c50')
ax.annotate('',xy=(-44,zt),xytext=(-44,zt+membrane),arrowprops={'arrowstyle':'<->','color':'#2b6582','lw':1.5})
ax.text(-47,zt+membrane/2,'4 mm membrane',fontsize=10,ha='right',va='center',color='#2b6582')
ax.annotate('External motor + rotor',xy=(12,zu+rt+5),xytext=(41,283),arrowprops={'arrowstyle':'->','color':'#333f4d'},fontsize=10,color='#333f4d')
ax.annotate('Internal rotor + shaft',xy=(15,zl+rt/2),xytext=(35,230),arrowprops={'arrowstyle':'->','color':'#333f4d'},fontsize=10,color='#333f4d')
ax.annotate('Occluder vane',xy=(19,210),xytext=(39,218),arrowprops={'arrowstyle':'->','color':'#b96129'},fontsize=10,color='#b96129')
ax.text(-62,273,'OUTSIDE / ATMOSPHERE',fontsize=10,color='#66758a',weight='bold')
ax.text(-62,219,'INSIDE / VACUUM',fontsize=10,color='#66758a',weight='bold')
ax.set_xlim(-88,104);ax.set_ylim(203,292);ax.set_aspect('equal'); ax.axis('off')
fig.tight_layout(pad=1.1)
fig.savefig(out/'coupling_section.png',dpi=165,facecolor='#f3f5f7',bbox_inches='tight')
plt.close(fig)
print('wrote',out/'coupling_section.png')
