import cadquery as cq
import cadkit as ck

# Parameters
PLATE_L = 120.0        # Length of the plate (mm)
PLATE_W = 80.0         # Width of the plate (mm)
PLATE_T = 10.0         # Thickness of the plate (mm)
HOLE_D = 8.0           # Diameter of mounting holes (mm)
HOLE_OFFSET = 10.0     # Distance from edges to hole centers (mm)
SLOT_L = 40.0          # Length of the central slot (mm)
SLOT_W = 10.0          # Width of the central slot (mm)
CORNER_R = 2.0         # Corner fillet radius (mm)

# Calculate hole positions
# Holes are at (±(L/2 - offset), ±(W/2 - offset))
hx = PLATE_L / 2 - HOLE_OFFSET
hy = PLATE_W / 2 - HOLE_OFFSET
hole_points = [
    (-hx, -hy),
    (hx, -hy),
    (hx, hy),
    (-hx, hy)
]

# Create the base plate using cadkit.plate for standard geometry
# ck.plate(length, width, thickness, holes=[(x, y, d), ...], corner_r=0)
# We pass the hole points as (x, y, d) tuples
plate_holes = [(x, y, HOLE_D) for (x, y) in hole_points]
base_plate = ck.plate(
    length=PLATE_L, 
    width=PLATE_W, 
    thickness=PLATE_T, 
    holes=plate_holes, 
    corner_r=CORNER_R
)

# Create the central slot
# The slot is a rectangle extruded through the thickness
slot = (
    cq.Workplane("XY")
    .rect(SLOT_L, SLOT_W)
    .extrude(PLATE_T, both=True) # Extrude both ways to ensure clean cut
)

# Cut the slot from the plate
# Since ck.plate is centered on Z axis and standing on z=0, 
# we need to align the slot to the same position.
# The slot created by .extrude(PLATE_T, both=True) is centered at z=0.
# We translate it to the center of the plate thickness.
slot = slot.translate((0, 0, PLATE_T / 2))
final_plate = base_plate.cut(slot)

# Final touches: chamfer the top edges of the slot for manufacturability
final_plate = final_plate.edges(">Z and (X or Y)").chamfer(0.5)

# Assembly for the final result
assy = cq.Assembly()
assy.add(final_plate, name="mounting_plate", color=cq.Color(0.7, 0.7, 0.75))

result = assy
