#!/usr/bin/env python3
"""
Convert STL to STEP using trimesh + manual mesh-to-BREP.
Since we can't install FreeCAD/pythonocc, we'll use a different approach:
export as OBJ (which Inventor can import as a solid) or generate an OpenSCAD
script that reconstructs the geometry parametrically.
"""
import trimesh
import numpy as np
import os

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

# Load the smooth window
m = trimesh.load(os.path.join(ORIG, "Window 20x60mm - liso.stl"))
print("Original window: %s" % m.bounds)
print("Dimensions: X=%.2f Y=%.2f Z=%.2f" % (
    m.bounds[1][0]-m.bounds[0][0],
    m.bounds[1][1]-m.bounds[0][1],
    m.bounds[1][2]-m.bounds[0][2]))

# Export as OBJ (Inventor can import OBJ as solid)
m.export(os.path.join(OUT, "window_smooth.obj"))
print("Exported as OBJ")

# Also export as PLY
m.export(os.path.join(OUT, "window_smooth.ply"))
print("Exported as PLY")

# Export as GLTF
m.export(os.path.join(OUT, "window_smooth.glb"))
print("Exported as GLB")

# Try to create a watertight solid
m2 = trimesh.load(os.path.join(ORIG, "Window 20x60mm.stl"))
m2.export(os.path.join(OUT, "window_keystone_dc.obj"))
print("Keystone exported as OBJ")

m3 = trimesh.load(os.path.join(ORIG, "Window - Flex Gland.stl"))
m3.export(os.path.join(OUT, "window_usb.obj"))
print("USB exported as OBJ")

print("\nAll exported. Inventor can import OBJ as solid body.")
