# The kind of script an AI assistant writes from a prompt, followed by the checks you add yourself.
import FreeCAD as App, Part, math

def flange(od=80.0, bore=30.0, thick=8.0, holes=4, pcd=60.0, hole_d=6.6):
    plate = Part.makeCylinder(od / 2, thick)
    plate = plate.cut(Part.makeCylinder(bore / 2, thick))
    for i in range(holes):
        a = 2 * math.pi * i / holes
        c = App.Vector(pcd / 2 * math.cos(a), pcd / 2 * math.sin(a), 0)
        plate = plate.cut(Part.makeCylinder(hole_d / 2, thick, c))
    return plate

# --- your checks: never trust geometry you have not measured ---
f = flange()
bb = f.BoundBox
assert f.isValid(), 'solid is not valid'
assert abs(bb.XLength - 80) < 1e-6 and abs(bb.ZLength - 8) < 1e-6, 'wrong size'
expect = math.pi * (40**2 - 15**2) * 8 - 4 * math.pi * 3.3**2 * 8
assert abs(f.Volume - expect) < 1e-3, 'volume %.3f, expected %.3f' % (f.Volume, expect)
assert len(f.Faces) == 2 + 1 + 1 + 4, 'expected top, bottom, rim, bore and 4 hole faces, got %d' % len(f.Faces)
ligament = 60 / 2 - 6.6 / 2 - 30 / 2
assert ligament >= 2, 'too little metal between bore and holes'
print('PASS  volume %.1f mm3, metal between bore and hole %.1f mm' % (f.Volume, ligament))
# A request the checks catch: pcd=36 puts the holes into the bore
try:
    g = flange(pcd=36); assert 36 / 2 - 6.6 / 2 - 30 / 2 >= 2, 'holes run into the bore'
except AssertionError as e: print('CAUGHT', e)
