"""Test ways to inflate the servo for clearance, since offset() on the full geared
compound fails. The holder only contacts the OUTER body, so try offsetting just that.
"""
from functools import reduce
from build123d import import_step, offset

STEP = "/data/cameron/repos/cad_experiments/parts/waveshare_feetech_st3215_servo.step"
s = import_step(STEP)
sols = sorted(s.solids(), key=lambda x: -x.volume)
print("n_solids:", len(sols), " top vols:", [round(x.volume) for x in sols[:6]])


def trial(label, shape, amt=0.3):
    try:
        o = offset(shape, amount=amt)
        nb = len(o.solids()) if hasattr(o, "solids") else "?"
        print(f"  {label}: OFFSET OK  vol={o.volume:.0f}  solids={nb}")
    except Exception as e:
        print(f"  {label}: FAIL  {type(e).__name__}: {str(e)[:70]}")


from build123d import Kind

fused = reduce(lambda a, b: a + b, sols)
fsolid = fused.solids()[0]
print(f"  full fuse: solids={len(fused.solids())} vol={fused.volume:.0f}")
for k in [Kind.ARC, Kind.INTERSECTION, Kind.TANGENT]:
    for label, shp in [("fused-compound", fused), ("fused-solid", fsolid)]:
        try:
            o = offset(shp, amount=0.3, kind=k)
            print(f"  {label} kind={k}: OK  vol={o.volume:.0f}  solids={len(o.solids())}")
        except Exception as e:
            print(f"  {label} kind={k}: FAIL {str(e)[:55]}")
