"""Environmental closures in the service sole, nominal installed geometry, mm."""
from pathlib import Path
import math,json
import FreeCAD as A,Part
V=A.Vector;here=Path(__file__).resolve().parent
def cyl(r,z,h,x=0,y=0):return Part.makeCylinder(r,h,V(x,y,z))
def oblong(width,depth,z,h,x=0,y=0,r=None):
    r=r if r else depth*.49
    p=Part.makeBox(width,depth,h,V(x-width/2,y-depth/2,z))
    return p.makeFillet(r,[e for e in p.Edges if e.BoundBox.ZLength>h-.0001])
def ring(w,d,t,z,h,r):
    return oblong(w,d,z,h,r=r).cut(oblong(w-2*t,d-2*t,z-.01,h+.02,r=r-t))
def make(sole):
    parts={}
    def add(id,mat,s):parts.setdefault(id,[]).append((mat,s))
    # Free height .50. Installed at .40 with 0.15 mm lateral gland room per side.
    sole=sole.cut(ring(22.5,20.55,1.00,2.60,.5,3.8))
    bead=ring(22.2,20.25,.70,2.60,.40,3.65)
    bead=bead.makeFillet(.08,[e for e in bead.Edges if e.BoundBox.ZLength<1e-6])
    add('ENV-SOLE','seal',bead)
    for x,id in [(-7,'ENV-SCREW-L'),(7,'ENV-SCREW-R')]:
        sole=sole.cut(cyl(1.31,1.349,.201,x).cut(cyl(.86,1.34,.23,x)))
        add(id,'seal',cyl(1.265,1.35,.20,x).cut(cyl(.905,1.34,.23,x)))
    # Outer flexible cap sits flush; slim tether runs in a recessed track to its own blind anchor.
    sole=sole.cut(oblong(10.84,5.28,-.03,.55,r=2.10))
    sole=sole.cut(oblong(2.4,4.60,-.03,.56,0,-4.58,r=.55))
    sole=sole.cut(cyl(1.15,-.01,.62,0,-6.92)).cut(cyl(.53,.60,.84,0,-6.92)).cut(cyl(.82,1.02,.45,0,-6.92))
    # A pry recess on the opposite end remains open to the outside, away from the seal.
    sole=sole.cut(oblong(3.7,1.65,-.02,.44,0,2.75,r=.60))
    cap=oblong(10.65,5.12,.055,.465,r=2.01)
    # Two installed radial contact bands on an obround core. Aperture derives from
    # 5.90 mm centre span and 1.90 mm end radius. Rounded sealing lands follow its exact outline.
    def core(r,z,h):
        center=Part.makeBox(5.9,2*r,h,V(-2.95,-r,z))
        return center.fuse(cyl(r,z,h,-2.95)).fuse(cyl(r,z,h,2.95)).removeSplitter()
    cap=cap.fuse(core(1.68,.51,1.12))
    for z in (.78,1.21):cap=cap.fuse(core(1.9,z,.14))
    cap=cap.fuse(oblong(1.95,4.80,.10,.38,0,-4.53,r=.60)).fuse(cyl(1.04,.08,.50,0,-6.92)).fuse(cyl(.50,.54,.67,0,-6.92)).fuse(cyl(.76,1.05,.38,0,-6.92))
    add('ENV-PORT','seal',cap.removeSplitter())
    # Bonded membrane facing the dry side, with a protected 90-degree external path.
    x,y=0,6.6
    sole=sole.cut(cyl(2.0,2.74,.40,x,y)).cut(cyl(1.35,1.26,1.50,x,y))
    start=V(0,6.6,1.58);end=V(0,11.5,.96);direction=end-start
    sole=sole.cut(Part.makeCylinder(.40,direction.Length,start,direction))
    adhesive=cyl(1.91,2.74,.055,x,y).cut(cyl(1.43,2.72,.09,x,y))
    add('ENV-VENT','bond',adhesive);add('ENV-VENT','membrane',cyl(1.92,2.795,.11,x,y))
    sole=sole.removeSplitter()
    assert sole.isValid() and len(sole.Solids)==1
    for id,items in parts.items():
        assert all(s.isValid() and s.common(sole).Volume<1e-5 for _,s in items),id
        Part.makeCompound([s for _,s in items]).exportStep(str(here/(id+'.step')))
    def poly(m,s):
        vs,fs=s.tessellate(.012);return dict(finish=m,v=[[round(c,6) for c in p] for p in vs],f=fs)
    (here/'mesh-mm.json').write_text(json.dumps({id:[poly(m,s) for m,s in its] for id,its in parts.items()},separators=(',',':')))
    return sole
