from build123d import *
import numpy as np

OUT = "/app/projects/esp32-box/exports"

# Window 20x60mm (Keystone/DC) - Parametric recreation
# Dimensions from STL analysis:
W = 33.9  # X total
H = 13.9  # Y total  
T = 4.8   # Z total

# Tier dimensions:
# Tier 1 (Z=0 to Z=1.6): Full rectangle
# Tier 2 (Z=1.6 to Z=3.2): Inner rectangle
# Tier 3 (Z=3.2 to Z=4.8): Top rectangle

# Inner tier offsets from edge (analyzed from STL)
OFF1 = (33.9 - 27.5) / 2  # ~3.2mm offset X at Z=3.2
OFF2_Y = (13.9 - 7.7) / 2  # ~3.1mm offset Y at Z=3.2
OFF3_X = (33.9 - 14.9) / 2  # ~9.5mm offset X at Z=4.8 (shifted)
OFF3_Y = (13.9 - 9.9) / 2  # ~2.0mm offset Y at Z=4.8

# The top tier (Z=4.8) is NOT centered - it's shifted to the right
# From analysis: at Z=4.8 X goes from about -9.8 to 5.1 (relative to center)
# Original X: -11.83 to 22.07, center at ~5.12
# At Z=4.8: X=-9.83 to 5.07, shifted left

# Build tiers
mid = W / 2
t1 = Rectangle(W, H)
t2 = Rectangle(W - 2*OFF1, H - 2*OFF2_Y)
# Top tier is shifted - center at about -2.4 from center
shift_x = -2.4
t3_w = 14.9
t3_h = 9.9
t3 = Pos(shift_x, 0) * Rectangle(t3_w, t3_h)

# Create the stepped profile
# Base slab
base = extrude(t1, 1.6)

# Inner tier
inner = extrude(t2, 3.2)
inner = Pos(0, 0, 1.6) * inner

# Top tier  
top = extrude(t3, 4.8)
top = Pos(0, 0, 1.6) * top

# Combine
body = base + inner + top

# Add fillets between tiers for the "curves"
# The edges between tiers should be rounded

# Now add the keystone and DC cutouts
# From original analysis: the cutouts are at specific X positions

# Keystone cutout (at left, X~9mm wide centered)
# DC cutout (at right, X~9mm wide centered)

# Keystone: rectangular hole, Z=1.6 to Z=4.8 (through inner tiers)
keystone_w = 17.0
keystone_h = 9.0
kc = Pos(-8.5-OFF1/2, 0) * Rectangle(keystone_w, keystone_h)
kc_x = extrude(kc, 4.8)  # Full height minus base

# DC jack: round hole
dc_d = 8.0
dc = Pos(8.0, 0) * Circle(dc_d/2)
dc_x = extrude(dc, 4.8)

# Cut from tiers only, not from base (Z=0 to Z=1.6)
# Actually the cutouts are through the full body
body -= kc_x
# body -= dc_x

# Add fillet/round edges
# The "curves" mentioned by user - we need to fillet the transition edges

export_stl(body, f"{OUT}/window_keystone_dc_param.stl")
print("Exported: window_keystone_dc_param.stl")
