"""FreeCAD Python; nominal geometry in mm. Surface of revolution coordinates (radius, z)."""
import math,csv
from pathlib import Path
import FreeCAD as A,Part,MeshPart
V=A.Vector
d=Path(__file__).resolve().parent
doc=A.newDocument('Chassis')
rows=[]
def cyl(r,z,h,x=0,y=0): return Part.makeCylinder(r,h,V(x,y,z))
def profile(points):
    p=[V(r,0,z) for r,z in points]
    return Part.Face(Part.makePolygon(p+[p[0]])).revolve(V(),V(0,0,1),360)
def add(code,name,mat,shape):
    shape=shape.removeSplitter()
    assert shape.isValid() and len(shape.Solids)==1,code
    ob=doc.addObject('Part::Feature',code);ob.Label=name;ob.Shape=shape
    Part.export([ob],str(d/(code+'.step')))
    mesh=MeshPart.meshFromShape(Shape=shape,LinearDeflection=.015,AngularDeflection=.10,Relative=False)
    assert mesh.isSolid(),code
    mesh.write(str(d/(code+'.stl')))
    rows.append((code,name,mat,'Chassis',code+'.stl'))
# Contour rolls at base, subtle longitudinal taper; recessed annular grip cuts.
outer=[(9.64,3),(9.89,3.10),(10.06,3.36),(10.12,3.8),(10.12,7)]
for i in range(9):
    z=8+i*.78
    outer.extend([(10.12,z),(10.03,z+.11),(10.03,z+.21),(10.12,z+.33)])
outer.extend([(10.12,15.8),(10.19,23),(10.46,42),(10.50,52.6),(10.48,53.15),(10.30,53.45),(9,53.45),(9,3.0)])
lower=profile(outer)
for x in [-7,7]:
    x0=x-2.25 if x<0 else x-1.54
    lug=Part.makeBox(3.79,3.5,1.22,V(x0,-1.75,3)).cut(cyl(1.37,2.99,1.24,x))
    lower=lower.fuse(lug)
add('C01','Lower grip sleeve','Midnight PVD titanium candidate',lower)
upper=profile([(10.30,54.15),(10.46,54.39),(10.50,54.68),(10.50,85.4),(10.47,85.75),(10.27,86),(7.10,86),(7.10,84.4),(9,84.4),(9,54.15)])
for y in [-8,8]:upper=upper.cut(cyl(.50,84.35,1.75,0,y))
# Open radial keyways clear the inward conductor jog while preserving the axial
# mounting lands at y=+/-8. The .68 end radius includes jacket clearance.
for sign in [-1,1]:
    x=7.8*sign
    slot=Part.makeBox(1.8,1.36,1.85,V(min(x,sign*6),-.68,84.3)).fuse(cyl(.68,84.3,1.85,x))
    upper=upper.cut(slot)
    gauge=Part.makeBox(1.8,.80,1.6,V(min(x,sign*6),-.40,84.4)).fuse(cyl(.40,84.4,1.6,x))
    assert upper.common(gauge).Volume < 1e-8
vent=Part.makeCylinder(.42,12,V(0,0,55.18),V(0,-1,0))
upper=upper.cut(vent)
add('C02','Upper protective sleeve','Midnight PVD titanium candidate',upper)
add('C03','Waist index band','Satin champagne titanium candidate',profile([(9.85,53.45),(10.29,53.45),(10.38,53.56),(10.38,54.04),(10.29,54.15),(9.85,54.15)]))
part=cyl(9.85,53.45,.7).fuse(cyl(8.98,54.15,1.85))
for x in [-2.15,0,2.15]:part=part.cut(cyl(.88,53,1.00,x,1.8))
for x in [-7.8,7.8]:part=part.cut(cyl(.68,53,4,x))
part=part.cut(vent).cut(cyl(.65,55.13,1))
add('C04','Feedthrough bulkhead','PEEK candidate',part)
# Vented elastomer carrier lands at cartridge lower and upper ends.
for code,z in [('C05',56),('C06',80)]:
    seat=profile([(7,z),(8.90,z),(9,z+.2),(9,z+1.7),(8.85,z+2),(7,z+2)])
    for x in [-7.8,7.8]:seat=seat.cut(cyl(.68,z-1,4,x))
    add(code,'Cartridge isolation collar '+code,'elastomer candidate',seat)
# Radiused charging aperture, blank base also acts as bottom stop for test cassette.
base=profile([(0,0),(8.9,0),(9.55,.13),(9.94,.49),(10.09,1),(10.12,2.5),(9.88,3),(0,3)])
slot=Part.makeBox(5.9,3.8,5,V(-2.95,-1.9,-1)).fuse(cyl(1.9,-1,5,-2.95)).fuse(cyl(1.9,-1,5,2.95))
base=base.cut(slot)
for x in [-7,7]:base=base.cut(cyl(.85,-1,6,x)).cut(cyl(1.45,-1,2.35,x))
add('C07','Service sole with recessed charging aperture','Satin champagne titanium candidate',base)
for code,x in [('C08',-7),('C09',7)]:
    s=cyl(1.36,.25,1.1,x).fuse(cyl(.73,1.35,2.55,x))
    verts=[V(x+.62*math.cos(i*math.pi/3),.62*math.sin(i*math.pi/3),.2) for i in range(6)]
    socket=Part.Face(Part.makePolygon(verts+[verts[0]])).extrude(V(0,0,.58))
    add(code,'Recessed service fastener '+code,'316L candidate',s.cut(socket))
for code,x in [('C10',-7),('C11',7)]:
    add(code,'Service nut '+code,'316L candidate',cyl(1.36,3,1.1,x).cut(cyl(.75,2,4,x)))
doc.recompute();doc.saveAs(str(d/'Chassis.FCStd'))
with (d/'manifest.csv').open('w') as f:
    w=csv.writer(f);w.writerow(['id','name','material','group','file']);w.writerows(rows)
