// ============================================
// Panel Mount Housing for T-Plug (Dean) Female Connectors
// ============================================

// --- User Parameters ---
num_plugs       = 4;      // Number of connectors side by side

// --- Connector Profile (female Dean's plug, long side up) ---
conn_top_w      = 8.0;
conn_top_h      = 5.0;
conn_taper_h    = 1.0;
conn_bot_w      = 6.5;
conn_bot_h      = 7.0;
conn_total_h    = 14.0;
conn_depth      = 9.0;     // How deep connector sits into housing

// --- Clearance (adjust fit for each connector pocket) ---
// Positive values = looser fit, negative values = tighter fit.
// This value is added to each side of the connector profile.
// Start with 0.2 for a snug fit. If connectors are too tight,
// increase by 0.05-0.1 increments. For a friction/press fit,
// try 0.0 or slightly negative (e.g. -0.1).
// Note: actual fit depends on printer calibration and material.
clearance       = 0.2;

// --- Housing Parameters ---
wall            = 2.0;     // Wall thickness around each pocket
spacing         = 1.5;     // Gap between adjacent pockets
flange_thick    = 2.5;     // Flange plate thickness
corner_r        = 1.5;     // Fillet radius on flange corners

// --- Flange Size Control ---
flange_ext_w    = 7.0;     // Flange extension beyond body on each side (width/X)
flange_ext_h    = 4.0;     // Flange extension beyond body on top/bottom (height/Y)

// --- Set Screw Hole ---
screw_d         = 3.0;     // Set screw diameter (M3)
screw_offset_z  = 3.0;     // Distance from back of pocket to screw center

// --- Mounting Holes ---
mount_hole_d    = 3.2;     // Panel mount screw hole diameter
mount_hole_inset= 3.5;     // Inset from flange edge

// --- Derived Dimensions ---
pocket_max_w    = conn_top_w + clearance * 2;
pocket_h        = conn_total_h + clearance * 2;
pocket_d        = conn_depth;

total_pocket_span = num_plugs * pocket_max_w + (num_plugs - 1) * spacing;
body_w          = total_pocket_span + 2 * wall;
body_h          = pocket_h + 2 * wall;

flange_w        = body_w + 2 * flange_ext_w;
flange_h        = body_h + 2 * flange_ext_h;

// --- Dean's Connector Profile (2D) ---
module deans_profile(cl = 0) {
    tw = conn_top_w / 2 + cl;
    bw = conn_bot_w / 2 + cl;
    th = conn_total_h / 2 + cl;

    y_top         =  th;
    y_taper_start =  th - conn_top_h;
    y_taper_end   =  y_taper_start - conn_taper_h;
    y_bot         = -th;

    polygon([
        [-tw, y_top],
        [ tw, y_top],
        [ tw, y_taper_start],
        [ bw, y_taper_end],
        [ bw, y_bot],
        [-bw, y_bot],
        [-bw, y_taper_end],
        [-tw, y_taper_start],
    ]);
}

// --- Rounded Rectangle Module ---
module rounded_rect(w, h, d, r) {
    linear_extrude(d)
        offset(r = r)
            offset(r = -r)
                square([w, h], center = true);
}

// --- Main Housing ---
module final_housing() {
    difference() {
        union() {
            // Flange plate at z = 0
            translate([0, 0, flange_thick / 2])
                rounded_rect(flange_w, flange_h, flange_thick, corner_r);

            // Body extending behind panel
            translate([0, 0, flange_thick + pocket_d / 2])
                cube([body_w, body_h, pocket_d], center = true);
        }

        for (i = [0 : num_plugs - 1]) {
            x_off = (i - (num_plugs - 1) / 2) * (pocket_max_w + spacing);

            // Full through-cut with Dean's profile
            translate([x_off, 0, -0.01])
                linear_extrude(flange_thick + pocket_d + 0.02)
                    deans_profile(cl = clearance);

            // Set screw hole from top into pocket
            screw_z = flange_thick + pocket_d - screw_offset_z;
            translate([x_off, body_h / 2 + 0.01, screw_z])
                rotate([90, 0, 0])
                    cylinder(d = screw_d, h = wall + 0.02, $fn = 32);

            // Set screw hole from bottom into pocket
            translate([x_off, -(body_h / 2 + 0.01), screw_z])
                rotate([-90, 0, 0])
                    cylinder(d = screw_d, h = wall + 0.02, $fn = 32);
        }

        // Mounting holes — full depth through flange + body
        total_depth = flange_thick + pocket_d;
        for (cx = [-1, 1]) {
            for (cy = [-1, 1]) {
                mx = cx * (flange_w / 2 - mount_hole_inset);
                my = cy * (flange_h / 2 - mount_hole_inset);
                translate([mx, my, -0.01])
                    cylinder(d = mount_hole_d, h = total_depth + 0.02, $fn = 32);
            }
        }
    }
}

// --- Render ---
final_housing();