#!/usr/bin/env python3
"""
Window 20x60mm - Painel de conexão entre caixas WAGO.
Recriação paramétrica baseada em análise de STL + 7 imagens do Inventor.

Estrutura:
1. Base (Z=0-1.6): retângulo cheio 33.9x13.9mm
2. Moldura (Z=1.6-3.2): moldura retangular com recorte oval central + frisos borda
3. Colar esquerdo (Z=3.2-4.8): ressalto para encaixe (~14.9x9.9mm)
"""
from build123d import *
import os, math

OUT = "/app/projects/esp32-box/exports"
os.makedirs(OUT, exist_ok=True)

# ========= PARÂMETROS =========
W = 33.9    # Comprimento X total
H = 13.9    # Altura Y total
T = 4.8     # Espessura Z total

# Alturas dos tiers
Z_BASE   = 1.6   # Base (Z=0 a Z=1.6)
Z_FRAME  = 3.2   # Moldura (Z=1.6 a Z=3.2)
Z_COLLAR = 4.8   # Colar (Z=3.2 a Z=4.8)

# Moldura (frame)
F_W = 27.5    # Largura externa X
F_H = 8.0     # Altura externa Y
F_R = 2.0     # Raio cantos arredondados

# Recorte oval central
O_W = 22.0    # Largura oval X
O_H = 7.0     # Altura oval Y
O_R = O_H/2   # Raio das extremidades (semicírculos)

# Colar esquerdo
C_W = 14.9
C_H = 9.9
C_R = 2.0

# Frisos/ranhuras
N_GROOVES = 3
G_W = 1.5     # Largura do friso
G_D = 0.3     # Profundidade do friso

# ========= CONSTRUÇÃO =========

# 1. BASE (Z=0 a Z=1.6)
base_r = Rectangle(W, H)
base_r = fillet(base_r.vertices(), 0.5)  # cantos arredondados
base = extrude(base_r, Z_BASE)

# 2. MOLDURA (Z=1.6 a Z=3.2)
# Perfil externo
outer = Rectangle(F_W, F_H)
outer = fillet(outer.vertices(), F_R)

# Recorte oval - slot/obround
oval = Rectangle(O_W, O_H)
oval = fillet(oval.vertices(), O_H/2)  # semicírculos nas extremidades -> obround

# Moldura = externo - interno
frame_section = outer - oval
frame = extrude(frame_section, Z_FRAME - Z_BASE)
frame = Pos(0, 0, Z_BASE) * frame

# 3. FRISOS na borda superior/inferior da moldura
for i in range(N_GROOVES):
    g_spacing = (F_W - N_GROOVES * G_W) / (N_GROOVES + 1)
    gx = -F_W/2 + g_spacing + i * (G_W + g_spacing)
    
    # Friso superior
    g_top = Pos(gx, F_H/2 - G_D/2, Z_BASE) * Box(G_W, G_D, Z_FRAME - Z_BASE)
    frame -= g_top
    # Friso inferior
    g_bot = Pos(gx, -F_H/2 + G_D/2, Z_BASE) * Box(G_W, G_D, Z_FRAME - Z_BASE)
    frame -= g_bot

# 4. COLAR ESQUERDO (Z=3.2 a Z=4.8)
collar_s = Rectangle(C_W, C_H)
collar_s = fillet(collar_s.vertices(), C_R)
collar = extrude(collar_s, Z_COLLAR - Z_FRAME)
collar = Pos(-W/2 + C_W/2, 0, Z_FRAME) * collar

# ========= COMBINAR =========
body = base + frame + collar

# ========= FILE TES DE TRANSIÇÃO =========
try:
    e_base = body.edges().filter_by_position(Axis.Z, Z_BASE-0.05, Z_BASE+0.05)
    body = fillet(e_base, 0.3)
except: pass

try:
    e_frame = body.edges().filter_by_position(Axis.Z, Z_FRAME-0.05, Z_FRAME+0.05)
    body = fillet(e_frame, 0.2)
except: pass

# ========= EXPORTAR =========
stp_path = os.path.join(OUT, "window_connection.stp")
stl_path = os.path.join(OUT, "window_connection.stl")
export_step(body, stp_path)
export_stl(body, stl_path)

sz = body.bounding_box().size
print("STEP: window_connection.stp")
print("STL:  window_connection.stl")
print("Dim:  %.1f x %.1f x %.1f mm" % (sz.X, sz.Y, sz.Z))
print("Base:  33.9x13.9 x 1.6mm (full rect)")
print("Frame: %.1fx%.1f x 1.6mm (oval cutout + %d grooves)" % (F_W, F_H, N_GROOVES))
print("Collar: %.1fx%.1f x 1.6mm (left side)" % (C_W, C_H))
