DE-9 D-Sub Connector Pair

A DE-9 pair before it mates: a panel plug with two jackposts, and a solder-cup socket with jackscrews and wires, cut open at its upper contact row. The flange, the two shell sizes, the 2.77 by 2.84 mm contact grid and the 4-40 UNC thread are published figures. The jackscrews, the jackposts and their nuts have helical threads, and each jackscrew turns one turn for each pitch as the pair mates. The kernel measures the clearance between the mated shells. The insulators, the contacts, the hardware and the panel are a display model.
Code
This is the full program. The playground runs it in your browser without a change.
import { box, clone, compound, cut, cutAll, cylinder, edgeFinder, extrude, fillet, fuse, line, loft, measureDistance, polygon, revolve, rotate, translate, unwrap, wire } from 'brepjs/quick';
import { color } from 'brepjs/playground';
// DE-9 pair: a panel plug with jackposts and a solder-cup socket with its jackscrews. Mating axis is X, units are mm.
// The flange, the two shell sizes, the contact grid and the thread are published figures.
// The insulators, the contacts, the hardware and the panel are a display model.
interface DSubParams {
mate?: number; // 0 = apart, 1 = fully mated. The jackscrews turn into the jackposts, one turn for each pitch
section?: 'socket' | 'both' | 'none'; // halves to cut open at the upper contact row
}
type P2 = [number, number]; // [x, r]
type P3 = [number, number, number];
type Shape = ReturnType<typeof cylinder>;
// Kernel shapes live in WASM memory. Every temporary goes through own(). freeing() runs a build and then frees them all.
const owned: { delete(): void }[] = [];
const own = <T extends { delete(): void }>(s: T): T => {
owned.push(s);
return s;
};
const freeing = <T,>(build: () => T): T => { try { return build(); } finally { owned.splice(0).forEach((s) => s.delete()); } }; // also when the build throws
// ---- published figures (DE-9, standard density)
const FLANGE = { length: 30.81, width: 12.55, holes: 24.99, hole: 3.05 };
const PLUG_SHELL = { width: 16.92, height: 8.36 }; // inside of the plug shell
const SOCKET_SHELL = { width: 16.33, height: 7.9 }; // outside of the socket shell
const SIDE_ANGLE = 10; // degrees, slope of the two short sides of the D
const GRID = { pitch: 2.77, rows: 2.84, upper: 5, lower: 4 };
const THREAD = { major: 2.845, minor: 2.157, pitch: 0.635 }; // 4-40 UNC
const HEX = 4.76; // 3/16 in across the flats of a jackpost
// ---- display model
const SHEET = 0.4; // shell sheet steel
const CORNER = 1.6; // inside corner radius of the plug shell
const SHROUD = { plug: 5.9, socket: 5.8 }; // shell length in front of the flange
const BACK = { width: 18.4, height: 10, depth: 4.6, insulator: 6.5, cup: 3.5 }; // rear shell, insulator and solder cups
const PANEL = { thick: 1.2, width: 42, height: 20 };
const PIN = { radius: 0.5, length: 5.2 };
const SLEEVE = { radius: 0.8, bore: 0.55, lead: 0.5 };
const WIRE = { strand: 0.4, jacket: 0.75, strip: 1, length: 7 };
const TRAVEL = 17; // distance between the halves at mate = 0
const SEAT = PANEL.thick + 4.8; // the socket flange stops on the jackposts
const PLAY = 0.08; // radial play between a screw thread and its nut thread
const block = (x0: number, x1: number, y0: number, y1: number, z0: number, z1: number): Shape =>
box(x1 - x0, y1 - y0, z1 - z0, { at: [(x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2] });
// A closed profile in the (x, r) half-plane, revolved about the X axis.
const lathe = (pts: P2[]): Shape => {
const profile = own(unwrap(polygon(pts.map(([x, r]): P3 => [x, 0, r]))));
return unwrap(revolve(profile, { axis: [1, 0, 0] })) as Shape;
};
// A prism along X from a closed outline in the (y, z) plane.
const prism = (pts: P2[], x0: number, x1: number): Shape => {
const profile = own(unwrap(polygon(pts.map(([y, z]): P3 => [x0, y, z]))));
return unwrap(extrude(profile, [x1 - x0, 0, 0])) as Shape;
};
// The D outline as a prism: width and height are overall sizes, the wide side is up.
const dPrism = (width: number, height: number, r: number, x0: number, x1: number): Shape => {
const a = (SIDE_ANGLE * Math.PI) / 180;
const top = width / 2 + r * (Math.tan(a) + 1 / Math.cos(a) - 1); // sharp corner before the fillet
const low = top - height * Math.tan(a);
const sharp = own(prism([[-low, -height / 2], [low, -height / 2], [top, height / 2], [-top, height / 2]], x0, x1));
return unwrap(fillet(sharp, edgeFinder().inDirection('X').findAll(sharp), r)) as Shape;
};
// Sheet-metal D tube with the given inside size.
const dTube = (width: number, height: number, r: number, x0: number, x1: number): Shape =>
unwrap(cut(own(dPrism(width + 2 * SHEET, height + 2 * SHEET, r + SHEET, x0, x1)), own(dPrism(width, height, r, x0 - 1, x1 + 1)))) as Shape;
const hexBar = (flats: number, x0: number, x1: number): Shape =>
prism(Array.from({ length: 6 }, (_, i): P2 => [(flats / Math.sqrt(3)) * Math.cos((i * Math.PI) / 3), (flats / Math.sqrt(3)) * Math.sin((i * Math.PI) / 3)]), x0, x1);
// One helical thread tooth about +Z, from z = from to z = to. Its crest passes the +X side at z = ref.
// tip > core is the thread of a screw on a core of that radius. tip < core is the thread in a bore.
// The loft is smooth: each flank is one helical face.
const FACETS = 12; // sections of the loft in each turn
const SLACK = 0.998; // keeps the tooth a hair clear of its core or its bore
const helixTooth = (core: number, tip: number, pitch: number, from: number, to: number, ref: number): Shape => {
const root = tip > core ? core / SLACK : core * SLACK;
const crest = tip > core ? tip : tip / SLACK;
const half = 0.42 * pitch;
const n = Math.max(2, Math.round(((to - from) / pitch) * FACETS));
const sections = Array.from({ length: n + 1 }, (_, j) => {
const z = from + ((to - from) * j) / n;
const a = (2 * Math.PI * (z - ref)) / pitch;
const at = (r: number, dz: number): P3 => [r * Math.cos(a), r * Math.sin(a), z + dz];
const pts = [at(root, -half), at(crest, 0), at(root, half)];
return own(unwrap(wire(pts.map((p, i) => own(line(p, pts[(i + 1) % 3]!))))));
});
return unwrap(loft(sections, { ruled: false })) as Shape;
};
const alongX = (s: Shape): Shape => rotate(own(s), 90, { axis: [0, 1, 0] }) as Shape;
// The 4-40 thread of a screw between two x positions on the X axis, and the mating thread of a nut or a tapped hole. The two are half a pitch apart.
const screwTooth = (x0: number, x1: number): Shape =>
alongX(helixTooth(THREAD.minor / 2, THREAD.major / 2, THREAD.pitch, x0 + 0.42 * THREAD.pitch, x1 - 0.42 * THREAD.pitch, 0));
const nutTooth = (x0: number, x1: number): Shape =>
alongX(helixTooth(THREAD.major / 2 + PLAY, THREAD.minor / 2 + PLAY, THREAD.pitch, x0 + 0.42 * THREAD.pitch, x1 - 0.42 * THREAD.pitch, THREAD.pitch / 2));
const holes: P2[] = [
...Array.from({ length: GRID.upper }, (_, i): P2 => [(i - (GRID.upper - 1) / 2) * GRID.pitch, GRID.rows / 2]),
...Array.from({ length: GRID.lower }, (_, i): P2 => [(i - (GRID.lower - 1) / 2) * GRID.pitch, -GRID.rows / 2]),
];
// One half, built with its flange face at x = 0 and its mating side toward +X.
// The jackscrews of the socket are made in the frame of the plug, at the distance apart from the mated position.
function half(kind: 'plug' | 'socket', apart = 0) {
const plug = kind === 'plug';
const inside = plug ? PLUG_SHELL : { width: SOCKET_SHELL.width - 2 * SHEET, height: SOCKET_SHELL.height - 2 * SHEET };
const corner = plug ? CORNER : CORNER - 0.7;
const reach = plug ? SHROUD.plug : SHROUD.socket;
const o = FLANGE.holes / 2;
const back = { width: BACK.width - 2 * SHEET, height: BACK.height - 2 * SHEET };
// Shell: two flanged sheet-steel halves that clamp the insulator.
const bolts = [-o, o].map((y) => own(cylinder(FLANGE.hole / 2, 4, { at: [-2, y, 0], axis: [1, 0, 0] })));
const plate = (x0: number, x1: number, window: Shape): Shape => {
const blank = own(block(x0, x1, -FLANGE.length / 2, FLANGE.length / 2, -FLANGE.width / 2, FLANGE.width / 2));
const round = own(unwrap(fillet(blank, edgeFinder().inDirection('X').findAll(blank), 1)) as Shape);
return unwrap(cutAll(round, [...bolts, window])) as Shape;
};
const shroud = own(dTube(inside.width, inside.height, corner, -SHEET, reach));
const front = own(plate(-SHEET, 0, own(dPrism(inside.width, inside.height, corner, -2, 2))));
const collar = own(dTube(back.width, back.height, CORNER, -BACK.depth, -SHEET));
const rear = own(plate(-2 * SHEET, -SHEET, own(dPrism(back.width, back.height, CORNER, -2, 2))));
const shell = [unwrap(fuse(front, shroud)) as Shape, unwrap(fuse(rear, collar)) as Shape];
// Insulator: fills the rear shell. The socket insulator also fills its shroud.
const face = plug ? 0 : reach;
const bores = holes.map(([y, z]) => own(cylinder(plug ? PIN.radius : SLEEVE.radius, 20, { at: [-10, y, z], axis: [1, 0, 0] })));
const core = own(unwrap(fuse(own(dPrism(back.width, back.height, CORNER, -BACK.insulator, -SHEET)), own(dPrism(inside.width, inside.height, corner, -SHEET, face)))) as Shape);
const insulator = unwrap(cutAll(core, bores)) as Shape;
// Contacts: a pin or a slotted sleeve, with a solder cup behind the insulator.
const tail = -BACK.insulator - BACK.cup;
const stem: P2[] = [[tail, 0], [tail, 0.85], [-BACK.insulator, 0.85]];
const blank = own(lathe(plug
? [...stem, [-BACK.insulator, PIN.radius], [PIN.length - 0.5, PIN.radius], [PIN.length - 0.15, 0.3], [PIN.length, 0]]
: [...stem, [-BACK.insulator, SLEEVE.radius], [reach - SLEEVE.lead, SLEEVE.radius], [reach - SLEEVE.lead, SLEEVE.bore], [0, SLEEVE.bore], [0, 0]]));
const cup = [own(cylinder(0.55, 3, { at: [tail - 0.2, 0, 0], axis: [1, 0, 0] })), own(block(tail - 1, tail + 2.6, -1, 1, 0.15, 1))];
const slot = own(block(reach - 3.2, reach, -0.16, 0.16, 0.2, 1));
const upper = own(unwrap(cutAll(blank, plug ? cup : [...cup, slot])) as Shape);
const lower = own(rotate(upper, 180, { axis: [1, 0, 0] }) as Shape);
const row = (s: Shape): Shape[] => [s, own(rotate(s, 180, { axis: [1, 0, 0] }) as Shape)];
const seat = (pair: Shape[]): Shape[] => holes.map(([y, z]) => own(translate(pair[z > 0 ? 0 : 1]!, [0, y, z]) as Shape));
const contacts = seat([upper, lower]);
// Socket only: a wire in each cup. The bare strand lies on the cup floor.
const drop = WIRE.strand - 0.55;
const strands = plug ? [] : seat(row(own(cylinder(WIRE.strand, 2.6 + WIRE.strip, { at: [tail - WIRE.strip, 0, drop], axis: [1, 0, 0] }))));
const jackets = plug ? [] : seat(row(own(cylinder(WIRE.jacket, WIRE.length, { at: [tail - WIRE.strip - WIRE.length, 0, drop], axis: [1, 0, 0] }))));
// Hardware. Plug: jackposts through the panel, with a washer and a nut. Socket: two slotted jackscrews.
// Each thread is one helical tooth. The nut and the tapped hole of the jackpost have the mating tooth.
const r = THREAD.major / 2;
const both = (s: Shape): Shape[] => [-o, o].map((y) => own(translate(s, [0, y, 0]) as Shape));
const hardware: Shape[] = [];
if (plug) {
// The thread of the jackpost stops 0.3 mm from its mouth, where the plain neck of the jackscrew sits.
const tapped = own(unwrap(cut(own(hexBar(HEX, PANEL.thick, SEAT)), own(cylinder(r + PLAY, 5, { at: [PANEL.thick + 0.3, 0, 0], axis: [1, 0, 0] })))) as Shape);
const stud = own(cylinder(THREAD.minor / 2, PANEL.thick + 4.5, { at: [-4.5, 0, 0], axis: [1, 0, 0] }));
const post = own(compound([tapped, own(nutTooth(PANEL.thick + 0.3, SEAT - 0.3)), stud, own(screwTooth(-4.5, PANEL.thick))]) as Shape);
const nut = own(unwrap(cut(own(hexBar(HEX, -3.2, -1.3)), own(cylinder(r + PLAY, 3, { at: [-3.5, 0, 0], axis: [1, 0, 0] })))) as Shape);
hardware.push(...both(post));
hardware.push(...both(own(lathe([[-1.3, FLANGE.hole / 2], [-1.3, 2.7], [-2 * SHEET, 2.7], [-2 * SHEET, FLANGE.hole / 2]]))));
hardware.push(...both(own(compound([nut, own(nutTooth(-3.2, -1.3))]) as Shape)));
} else {
// Mated, the head is behind the socket flange and the thread is in the jackpost.
const head = own(lathe([[SEAT - 0.3, 0], [SEAT - 0.3, r], [SEAT + 2 * SHEET, r], [SEAT + 2 * SHEET, 2.5], [SEAT + 2, 2.5], [SEAT + 2.5, 1.8], [SEAT + 2.5, 0]]));
const screw = own(compound([
own(unwrap(cut(head, own(block(SEAT + 1.8, SEAT + 3, -0.3, 0.3, -3, 3)))) as Shape),
own(cylinder(THREAD.minor / 2, 4.1, { at: [SEAT - 4.4, 0, 0], axis: [1, 0, 0] })),
own(screwTooth(SEAT - 4.4, SEAT - 0.3)),
]) as Shape);
// A screw that is a distance out of its thread is turned by distance / pitch turns.
const backed = own(translate(own(rotate(screw, (360 * apart) / THREAD.pitch, { axis: [1, 0, 0] }) as Shape), [apart, 0, 0]) as Shape);
hardware.push(...both(backed));
}
return { shell, shroud, insulator, contacts: [...contacts, ...strands], jackets, hardware };
}
function dSub({ mate = 0, section = 'socket' }: DSubParams = {}) {
const apart = TRAVEL * (1 - Math.min(Math.max(mate, 0), 1));
const plug = half('plug');
const socket = half('socket', apart);
const o = FLANGE.holes / 2;
// Panel: the plug sits behind it, the jackposts clamp it.
const window = own(dPrism(PLUG_SHELL.width + 1.6, PLUG_SHELL.height + 1.6, CORNER + 0.8, -1, 3));
const drills = [-o, o].map((y) => own(cylinder(FLANGE.hole / 2, 4, { at: [-1, y, 0], axis: [1, 0, 0] })));
const panel = unwrap(cutAll(own(block(0, PANEL.thick, -PANEL.width / 2, PANEL.width / 2, -PANEL.height / 2, PANEL.height / 2)), [window, ...drills])) as Shape;
// The socket turns to face the plug and moves along X.
const at = (x: number) => (s: Shape): Shape => own(translate(own(rotate(s, 180, { axis: [0, 0, 1] }) as Shape), [x, 0, 0]) as Shape);
const place = at(SEAT + apart);
// Clearance between the two shells when mated, from the kernel.
const clearance = unwrap(measureDistance(plug.shroud, at(SEAT)(socket.shroud)));
// Section: the shells and the insulator come off above the axis of the upper contact row. The contacts stay whole.
const lid = own(block(-60, 60, -o + 2.6, o - 2.6, GRID.rows / 2, 40)); // the flange ears stay
const open = (cutIt: boolean, s: Shape): Shape => (cutIt ? (unwrap(cut(s, lid)) as Shape) : (unwrap(clone(s)) as Shape));
const cutPlug = section === 'both';
const cutSocket = section !== 'none';
return {
panel,
shells: [...plug.shell.map((s) => open(cutPlug, s)), ...socket.shell.map((s) => open(cutSocket, place(s)))],
insulators: [open(cutPlug, plug.insulator), open(cutSocket, place(socket.insulator))],
contacts: compound([...plug.contacts, ...socket.contacts.map(place)]) as Shape,
wires: compound(socket.jackets.map(place)) as Shape,
hardware: compound([...plug.hardware, ...socket.hardware]) as Shape,
clearance,
};
}
const pair = freeing(() => dSub());
console.log('Shell clearance when mated: ' + pair.clearance.toFixed(2) + ' mm from the kernel. The two shell heights give (8.36 - 7.90) / 2 = 0.23 mm.');
export default [
color(pair.panel, '#48515c'),
...pair.shells.map((s) => color(s, '#c3c9d1')),
...pair.insulators.map((s) => color(s, '#12b5b0')),
color(pair.contacts, '#d3a956'),
color(pair.wires, '#ff5a1f'),
color(pair.hardware, '#f5f2ea'),
];Functions used
box clone compound cut cutAll cylinder edgeFinder extrude fillet fuse line loft measureDistance polygon revolve rotate translate unwrap wire color