import cadquery2 as cq
from cadquery2 import exporters

thickness = 12
width = 60
depth = 10
holes = 6
screws = 2

cable_radius = 1.6

# Threaded insert hole dimensions
ti_radius = 2.35
ti_depth = 6.5

# M3x8 screw
screw_l = 10
screw_shaft = 8
screw_head_h = 2.5
screw_head_radius = 3.25
screw_radius = 1.5

# Constants here are fudge factors to make things look nice
screw_hole_positions = [(6 - width / 2, 0), (width / 2 - 6, 0)]
cable_hole_positions = [((i + 1.5) * width / 8 - width / 2, 0) for i in range(holes)]


def clamp():
    return (
        cq.Workplane("XY")
        .box(width, depth, thickness)
        .edges("|Z")
        .fillet(2)
        # Screw holes
        .faces("<Z")
        .workplane()
        .pushPoints(screw_hole_positions)
        .circle(ti_radius)
        .cutBlind(-ti_depth)
        .faces(">Z")
        .workplane()
        .pushPoints(screw_hole_positions)
        .circle(screw_radius)
        .cutBlind(-thickness)
        .faces(">Z")
        .workplane()
        .pushPoints(screw_hole_positions)
        .circle(screw_head_radius)
        .cutBlind(-screw_head_h)
        # Cable holes
        .faces(">Y")
        .workplane(centerOption="CenterOfBoundBox")
        .pushPoints(cable_hole_positions)
        .circle(cable_radius)
        .cutBlind(-depth)
    )


exporters.export(clamp(), "full_clamp.stl")

top_clamp = clamp().faces("<Z").workplane(offset=-thickness / 2).split(keepTop=True)
exporters.export(top_clamp, "top_clamp.stl")

bottom_clamp = (
    clamp().faces("<Z").workplane(offset=-thickness / 2).split(keepBottom=True)
)
exporters.export(bottom_clamp, "bottom_clamp.stl")
