from ocp_vscode import *
from build123d import *
set_port(3939)

# Kennedy Pegboard hole width = 10 x 10 mm
# Hart to hart distance holes = 1-1/2" = 38.1 mm

tt = thickness_toolboard = 2.0
wc = width_connector = 20.0
tb = thickness_bevel = 2.5
teub = thickness_edge_until_board = 0.5
ttc = thickness_total_connector = 4.0
cw = clamp_width = 9.8
tc = thickness_clamp = 6.0
cbw = clamp_board_width = cw*2/3
dcr = depth_clamp_room = 1.8 # Made it a bit deeper so the securing pin stays beneath the surface
lcr = length_clamp_room = cw*0.25+2.4
wch = width_clamp_hole = cw/3
lch = length_clamp_hole = cw+0.2

# Make the connector
with BuildPart() as connector:
    with BuildSketch() as connector_sk:
        Circle(wc/2)
    extrude(amount = ttc-teub)
    # Generate a bevel
    chamfer(connector.edges().group_by(Axis.Z)[-1], length = tb, length2 = tb)
    # Extrude the last part of the connector
    extrude(connector.faces().sort_by(Axis.Z)[-1], amount = teub)
    # Make a hole for the security pin
    with BuildSketch(connector.faces().sort_by(Axis.Z)[0]):
        with Locations((cbw/2, 0)):
            RectangleRounded(wch, lch, .5)
    extrude(amount = -ttc, mode=Mode.SUBTRACT)
    with BuildSketch(connector.faces().sort_by(Axis.Z)[0]):
        RectangleRounded(lcr+1, lch, .5)
    extrude(amount = -dcr, mode=Mode.SUBTRACT)

# Build the clamp on the backside
with BuildPart() as clamp:
    with BuildSketch(connector.faces().sort_by(Axis.Z)[-1]) as sk_clamp:
        with Locations((-cbw/6, 0)):
            RectangleRounded(cbw, cw, .5)
    extrude(amount = tt)
    with BuildSketch(clamp.faces().sort_by(Axis.Z)[-1]) as sk_behindBoard:
        with Locations((-cbw/4, 0)):
            Rectangle(cw, cw)
    extrude(amount = tc)
    chamfer(clamp.edges().group_by(Axis.X)[0].sort_by(Axis.Z)[0], length = tc/5, length2 = cw-cbw-.01)
    fillet(clamp.edges(Select.LAST).filter_by(Axis.Y)[0], .5)
    fillet(clamp.edges().filter_by(Axis.Z), .5)
    fillet(clamp.edges().filter_by(Axis.X)[0], .5)

# Put the two parts together
with BuildPart() as kbMulticonnector:
    add(connector)
    add(clamp)

export_stl(kbMulticonnector.part, "connectorWithClamp.stl")
show(kbMulticonnector)
