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
dc = dia_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 = 4.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

with BuildPart() as connector:
    # Build the multiconnector
    with BuildSketch() as sk_connector:
        Circle(dia_connector/2)
    extrude(amount = thickness_total_connector-thickness_edge_until_board)
    chamfer(connector.edges().group_by(Axis.Z)[-1], length = thickness_bevel, length2 = thickness_bevel)
    extrude(connector.faces().sort_by(Axis.Z)[-1], amount = thickness_edge_until_board)
    # Add the clamp to connect to the board
    with BuildSketch(Plane.XY.offset(thickness_total_connector)) as sk_clamp:
        with Locations((-clamp_board_width/4, 0)):
            RectangleRounded(clamp_board_width, clamp_width, .5)
    extrude(amount = thickness_toolboard)
    with BuildSketch(Plane.XY.offset(thickness_total_connector+thickness_toolboard)) as sk_behindBoard:
        with Locations((-clamp_board_width/2, 0)):
            Rectangle(clamp_width, clamp_width)
    extrude(amount = thickness_clamp)
    chamfer(sk_behindBoard.edges().filter_by(GeomType.PLANE).sort_by(Axis.Z)[0], length = .5, length2 = 1)
#    fillet(clamp.edges(), .5)


#    chamfer(connector.faces().filter_by(GeomType.OTHER).sort_by(Axis.X)[-1].sort_by(Axis.Z)[0], length = 0.2, length2 = 0.1)
    # Make a hole for the security pin
    with BuildSketch(connector.faces().sort_by(Axis.Z)[0]):
        with Locations((width_clamp_hole, 0)):
            RectangleRounded(width_clamp_hole, length_clamp_hole, .75)
    extrude(amount = -thickness_total_connector, mode=Mode.SUBTRACT)
    with BuildSketch(connector.faces().sort_by(Axis.Z)[0]):
        Rectangle(length_clamp_room, length_clamp_hole)
    extrude(amount = -depth_clamp_room, mode=Mode.SUBTRACT)


#export_stl(connector.part, "connectorWithClamp.stl")
show_all()
