from fabricator import *
import math
# Measurements record: U1 user-selected spout OD is already derated. NO second derating.
SPOUT_OD = 25.0
FIT_EXTRA_DERATING = 0.0  # U1: keep zero; calibration not supplied.
# U2: actual opening 27-something mm; conservative lower bound for clearance calculation only.
OPENING_MIN_D = 27.0
WALL_FROM_OPENING_EDGE = 35.0  # U3: approximate user measurement, NOT center distance.
# D1 engineering choices: generous mouth, near-vertical wall side, steep far-side chute.
MOUTH_OD = 140.0
MOUTH_OFFSET = 55.0
INSERTION_H = 18.0
CHUTE_H = 145.0
WALL = 1.6  # P1: nonstructural PLA wall, provisional 0.4 mm nozzle.
BED_CHAMFER = 0.3  # P2: 45-degree outer lead-in, retains 1.3 mm tip land.
NOZZLE = 0.4  # PROVISIONAL, not user confirmed.
LAYER_H = 0.2  # PROVISIONAL, inspect slicer line coverage.
BUILD_X = BUILD_Y = BUILD_Z = 180.0  # Bambu A1 mini manufacturer technical specs.
BRIM = 10.0  # External-only removable slicer brim; not modeled into part.
PLA_DENSITY_G_MM3 = 0.00124  # Generic estimate, not specific filament certification.
# Process: prototype FDM PLA for one-off non-load-bearing pouring aid.
# Print frame: outlet-down, bed at z=0. Installed frame is identical; no PCB.
# Source centered at mouth XY; spout center (-55,0), wall toward -X.
EPS = 1e-6
if WALL < 1.2-EPS or SPOUT_OD-2*WALL < 21.8-EPS:
    reject('Keep wall >=1.2 mm and bore >=21.8 mm; raise OD rather than reducing user OD.')
if CHUTE_H < (MOUTH_OFFSET + MOUTH_OD/2 - SPOUT_OD/2)-EPS:
    reject('Increase CHUTE_H until the far-side chute exceeds 45 degrees from horizontal.')
r = (SPOUT_OD-FIT_EXTRA_DERATING)/2
x = -MOUTH_OFFSET
# Fuse outer solids with positive axial overlap, then hollow in one shell operation.
tube = Pos(x,0,(INSERTION_H+0.2)/2) * Cylinder(r,INSERTION_H+0.2)
def circle_profile(rad):
    return make_face(Wire([CenterArc((0,0),rad,0,180), CenterArc((0,0),rad,180,180)]))
cone = loft([Pos(x,0,INSERTION_H)*circle_profile(r), Pos(0,0,INSERTION_H+CHUTE_H)*circle_profile(MOUTH_OD/2)], ruled=True)
outer = tube + cone
openings = list(outer.faces().top()) + list(outer.faces().bottom())
body = outer.shell(WALL, openings=openings)
# Chamfer only the longest bottom edge (outer circular bed edge), never shrink bore.
bottom_edges = list(body.edges().bottom())
outer_bottom = max(bottom_edges, key=lambda e:e.length)
body = body.chamfer([outer_bottom], BED_CHAMFER, propagate=False)
body.color = '#9a9a9a'
result = body
check = result.check()
assert check.valid, str(check.faults)
print('CHECK_VALID', check.valid)
print('BOUNDING_BOX_MM', result.bbox())
mp = result.mass_properties(density=PLA_DENSITY_G_MM3)
print('VOLUME_MM3', mp.volume, 'ESTIMATED_MASS_G', mp.mass, 'CENTROID_MM', mp.center)
wall_check = result.min_wall_thickness()
print('MIN_WALL_CHECK', wall_check)
# Exact exported section measurement is independently performed after upload.
print('FRAME: Z-up, outlet bed z=0, mouth centered XY, spout center x=-55; wall toward -X')
