Skip to content

DIN-Rail Clip Enclosure ​

DIN-Rail Clip Enclosure, rendered in the brepjs playground
Mechanical · 247 lines▶ Run it in the playground

Three modular enclosures on a perforated 35 mm top-hat rail. Each housing hooks on the upper rail flange, and a sprung slide latch grips the lower flange. The middle module swings off the rail with its latch pulled, and the right module is open to show its board, terminals, latch and spring. The rail section and slots, the 17.5 mm module width, the 45 mm front and the M3 and M4 screw threads and heads are published figures. Each screw has a helical thread in a mating thread: the rail screws in the plate, the board screws in their posts and the clamp screws in the terminal poles. The kernel measures the rail mass per metre against the catalogue figure. The housings, the latch and the electronics are a display model.

Code ​

This is the full program. The playground runs it in your browser without a change.

ts
import { box, compound, cut, cutAll, cylinder, edgeFinder, extrude, fillet, fuseAll, line, loft, measureVolume, polygon, revolve, rotate, torus, translate, unwrap, wire } from 'brepjs/quick';
import { color } from 'brepjs/playground';

// Modular enclosures on a 35 mm top-hat rail. The rail runs along X on a back plate, Z points to the front.
// Each housing hooks on the upper rail flange and a sprung slide latch grips the lower flange.
// The rail section, its slots, the module width, the 45 mm front and the screw threads and heads are published figures.
// The housings, the latch and the electronics are a display model.
interface DinParams {
  tilt?: number; // degrees the middle module swings off the rail on its upper hook, 0 to 15
  release?: number; // 0 = latch of that module closed, 1 = latch pulled open
  cover?: boolean; // true fits the side cover of the right module
}

type P2 = [number, number];
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
const RAIL = { width: 35, hat: 27, depth: 7.5, sheet: 1, slot: [6.2, 15], gramsPerMetre: 330 }; // EN 60715, 35 x 7.5, perforated
const MODULE = 17.5; // width of one module, DIN 43880
const FRONT = 45; // height of the front that goes through the cover of a distribution board
const M4 = { major: 4, minor: 3.242, pitch: 0.7, head: 8, headHeight: 3.1 }; // ISO 7045 pan head, coarse thread
const M3 = { major: 3, minor: 2.459, pitch: 0.5, head: 5.6, headHeight: 2.4 };
const SET = { length: 4, slot: 0.4, slotDepth: 0.9 }; // ISO 4766 slotted set screw, M3 x 4
const STEEL = 7.85e-3; // grams per cubic millimetre

// ---- display model
const BODY = { height: 90, shoulder: 44, depth: 58, wall: 1.5 };
const SLOT_PITCH = 25;
const RAIL_LENGTH = 125;
const BOARD_Z = 22; // board above the back of the housing
const POLE = { width: 6.5, at: 35.5, depth: 9, height: 12 }; // screw terminals at both ends of the board
const LATCH = { width: 8, travel: 3 };
const RZ = RAIL.depth; // the back of every housing lies on the rail flanges
const PLAY = 0.08; // radial play between a screw thread and its mating thread

const brick = (l: number, w: number, h: number, x: number, y: number, z: number): Shape => box(l, w, h, { at: [x, y, z + h / 2] });
const lathe = (pts: P2[]): Shape => {
  const profile = own(unwrap(polygon(pts.map(([r, z]): P3 => [r, 0, z]))));
  return unwrap(revolve(profile, { axis: [0, 0, 1] })) as Shape;
};
// An outline in the (y, z) plane at x, extruded along +X. Its z starts at the rail front.
const profileX = (pts: P2[], x: number, length: number): Shape =>
  unwrap(extrude(own(unwrap(polygon(pts.map(([y, z]): P3 => [x, y, RZ + z])))), [length, 0, 0])) as Shape;
// Slot with round ends, long along X.
const obround = (l: number, w: number, h: number, x: number, z: number): Shape =>
  unwrap(fillet(own(brick(l, w, h, x, 0, z)), edgeFinder().inDirection('Z'), w / 2 - 0.01)) as Shape;
// 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 tooth is a row of smooth lofts of 4 turns or less, end to end on one helix: a long loft is slow to mesh.
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 TURNS = 4; // turns in one loft
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 pieces = Math.ceil((to - from) / (TURNS * pitch));
  const n = Math.max(2, Math.round(((to - from) / pieces / pitch) * FACETS)); // sections in each piece
  const section = (z: number) => {
    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]!))))));
  };
  const lofts = Array.from({ length: pieces }, (_, k) =>
    own(unwrap(loft(Array.from({ length: n + 1 }, (_, j) => section(from + ((to - from) * (k + j / n)) / pieces)), { ruled: false }))));
  return compound(lofts) as Shape;
};
// The thread of a screw between two heights, and the mating thread of a nut or a tapped hole. The two are half a pitch apart.
const screwTooth = (m: { major: number; minor: number; pitch: number }, z0: number, z1: number): Shape =>
  helixTooth(m.minor / 2, m.major / 2, m.pitch, z0 + 0.42 * m.pitch, z1 - 0.42 * m.pitch, 0);
const nutTooth = (m: { major: number; minor: number; pitch: number }, z0: number, z1: number): Shape =>
  helixTooth(m.major / 2 + PLAY, m.minor / 2 + PLAY, m.pitch, z0 + 0.42 * m.pitch, z1 - 0.42 * m.pitch, m.pitch / 2);
// Pan-head screw with a cross recess, head top at z = 0, thread to the head.
const panScrew = (m: { major: number; minor: number; pitch: number; head: number; headHeight: number }, length: number): Shape => {
  const k = m.headHeight;
  const blank = own(lathe([[0, 0], [m.head / 2 - 1, 0], [m.head / 2, -1], [m.head / 2, -k], [0, -k]]));
  return compound([
    own(unwrap(cutAll(blank, [own(brick(m.head * 0.6, 0.8, 1.6, 0, 0, -1.3)), own(brick(0.8, m.head * 0.6, 1.6, 0, 0, -1.3))])) as Shape),
    own(cylinder(m.minor / 2, length, { at: [0, 0, -k - length] })),
    own(screwTooth(m, -k - length, -k)),
  ]) as Shape;
};
const volume = (s: Shape): number => unwrap(measureVolume(s));

function dinRail({ tilt = 13, release = 1, cover = false }: DinParams = {}) {
  const half = BODY.height / 2;
  const nose = FRONT / 2;
  const t = BODY.wall;
  const zt = BOARD_Z + 1.6; // top of the board

  // Rail: the hat section with slots, on a back plate, held by two M4 x 6 screws in tapped holes of the plate.
  const r = RAIL;
  const hat: P2[] = [
    [-r.width / 2, -r.sheet], [-r.hat / 2, -r.sheet], [-r.hat / 2, -r.depth], [r.hat / 2, -r.depth], [r.hat / 2, -r.sheet], [r.width / 2, -r.sheet],
    [r.width / 2, 0], [r.hat / 2 - r.sheet, 0], [r.hat / 2 - r.sheet, r.sheet - r.depth], [r.sheet - r.hat / 2, r.sheet - r.depth], [r.sheet - r.hat / 2, 0], [-r.width / 2, 0],
  ];
  const slotsAt = [-2, -1, 0, 1, 2].map((i) => i * SLOT_PITCH);
  const slot = own(obround(r.slot[1]!, r.slot[0]!, 3, 0, -1));
  const rail = unwrap(cutAll(own(profileX(hat, -RAIL_LENGTH / 2, RAIL_LENGTH)), slotsAt.map((x) => own(translate(slot, [x, 0, 0]))))) as Shape;
  const gramsPerMetre = (volume(rail) * STEEL * 1000) / RAIL_LENGTH;
  const fixings = [slotsAt[0]!, slotsAt[4]!];
  const tapped = own(unwrap(cutAll(own(brick(150, 116, 2, 0, 0, -2)), fixings.map((x) => own(cylinder(M4.major / 2 + PLAY, 4, { at: [x, 0, -3] }))))) as Shape);
  const seat = r.sheet + 0.8 + M4.headHeight; // head top of a rail screw above the plate
  const plateTooth = own(nutTooth(M4, -seat - 2, -seat));
  const plate = compound([tapped, ...fixings.map((x) => own(translate(plateTooth, [x, 0, seat])))]) as Shape;

  const shells: Shape[] = [];
  const dark: Shape[] = [];
  const steel: Shape[] = [];
  const brass: Shape[] = [];
  const orange: Shape[] = [];
  const white: Shape[] = [];
  const teal: Shape[] = [];
  const railScrew = own(panScrew(M4, 6));
  const washer = own(lathe([[2.2, r.sheet], [5.5, r.sheet], [5.5, r.sheet + 0.8], [2.2, r.sheet + 0.8]]));
  for (const x of fixings) {
    steel.push(own(translate(washer, [x, 0, 0])));
    steel.push(own(translate(railScrew, [x, 0, seat])));
  }

  // Masters that every module copies: a tapped terminal pole, its slotted clamp screw, the latch and a board screw.
  const poleBlank = own(brick(POLE.width, POLE.depth, POLE.height, 0, POLE.at, RZ + zt));
  const pole = own(unwrap(cutAll(poleBlank, [
    own(cylinder(1.7, 6, { at: [0, POLE.at - 1, RZ + zt + 4], axis: [0, 1, 0] })),
    own(cylinder(M3.major / 2 + PLAY, POLE.height, { at: [0, POLE.at - 1, RZ + zt + 4] })),
  ])) as Shape);
  // The clamp screw is made with its point at z = 0, on the top of the wire hole. The pole has the mating thread.
  const clampAt: P3 = [0, POLE.at - 1, RZ + zt + 5.7];
  const driverSlot = own(brick(4, SET.slot, 1, 0, 0, SET.length - SET.slotDepth));
  const clamp = own(translate(own(compound([
    own(unwrap(cut(own(cylinder(M3.minor / 2, SET.length)), driverSlot)) as Shape),
    own(unwrap(cut(own(screwTooth(M3, 0, SET.length)), driverSlot)) as Shape),
  ]) as Shape), clampAt));
  // The thread of the pole is as long as the screw. Above it the hole is a plain guide for the screwdriver.
  const poleTooth = own(translate(own(nutTooth(M3, 0.3, SET.length)), clampAt));
  const latchBlank = own(profileX([[-half - 5, t], [-19.5, t], [-19.5, -2.2], [-16, -2.2], [-16, -1.2], [-17.7, -1.2], [-17.7, t], [-14, t], [-14, t + 2.5], [-half - 5, t + 2.5]], 0, LATCH.width));
  const latch = own(unwrap(cutAll(latchBlank, [own(brick(5, 12, 4, LATCH.width / 2, -34, RZ + t - 0.5)), own(brick(5, 1.6, 4, LATCH.width / 2, -half - 2.6, RZ + t - 0.5))])) as Shape);
  // A board screw (M3 x 5) goes through the board and 3.4 mm into the thread that it forms in its post.
  const boardScrew = own(panScrew(M3, 5));
  const postTooth = own(nutTooth(M3, -M3.headHeight - 5, -M3.headHeight - 1.6));

  // One module: x0 is its left face, units is its width in modules.
  const housing = (x0: number, units: number, swing: number, pull: number, open: boolean, populated: boolean): void => {
    const w = units * MODULE;
    const pose = (s: Shape): Shape => (swing > 0 ? own(rotate(s, -swing, { axis: [1, 0, 0], at: [0, r.width / 2, RZ] })) : s);
    const add = (list: Shape[], s: Shape): void => { list.push(pose(own(s))); };
    const poles = Array.from({ length: units * 2 }, (_, k) => x0 + t + 0.3 + ((k + 0.5) * (w - 2 * t - 0.6)) / (units * 2));
    const latchX = x0 + w - LATCH.width - 1.8;
    const posts: P2[] = [[x0 + 4.6, 28], [x0 + 4.6, -28], [x0 + w - 4.6, 28]];

    // Housing: the stepped outline, hollow, with the upper hook, board posts and the spring peg.
    const s = BODY.shoulder;
    const d = BODY.depth;
    const outer = own(profileX([[-half, 0], [half, 0], [half, s - 8], [half - 6, s], [nose, s], [nose, d], [-nose, d], [-nose, s], [6 - half, s], [-half, s - 8]], x0, w));
    const inner = own(profileX([
      [t - half, t], [half - t, t], [half - t, s - 8.6], [half - 6.6, s - t], [nose - t, s - t], [nose - t, d - t],
      [t - nose, d - t], [t - nose, s - t], [6.6 - half, s - t], [t - half, s - 8.6],
    ], x0 + t, open ? w : w - 2 * t));
    const solid = own(unwrap(fuseAll([
      own(unwrap(cut(outer, inner)) as Shape),
      own(brick(w, 2, 2.9, x0 + w / 2, r.width / 2 + 1.6, RZ - 2.4)),
      own(brick(w, 1.9, 0.9, x0 + w / 2, r.width / 2 - 0.35, RZ - 2.4)),
      ...posts.map(([x, y]) => own(cylinder(2.4, BOARD_Z - 1, { at: [x, y, RZ + 1] }))),
      own(brick(4, 1.8, 3, latchX + LATCH.width / 2, -39.1, RZ + 1)),
    ])) as Shape);
    const shell = unwrap(cutAll(solid, [
      ...poles.flatMap((x) => [1, -1].flatMap((side) => [
        own(brick(5, 4, 5.5, x, side * half, RZ + zt + 1.2)),
        own(cylinder(2.1, 4, { at: [x, side * (POLE.at - 1), RZ + s - 3] })),
      ])),
      own(brick(LATCH.width + 0.4, 4, 2.7, latchX + LATCH.width / 2, -half, RZ + t)),
      own(brick(LATCH.width + 0.4, 7, 4, latchX + LATCH.width / 2, -19.3, RZ - 1)),
      own(brick(w - 5, 14, 1, x0 + w / 2, -11, RZ + d - 0.5)),
      ...[5, 11].map((dx) => own(cylinder(1.3, 4, { at: [x0 + dx, 17, RZ + d - 3] }))),
      ...(populated ? [own(cylinder(1.7, 4, { at: [x0 + 26, 3, RZ + d - 3] }))] : []),
      ...posts.map(([x, y]) => own(cylinder(M3.major / 2 + PLAY, 8, { at: [x, y, RZ + BOARD_Z - 7] }))),
    ])) as Shape;
    shells.push(pose(own(shell)));
    posts.forEach(([x, y]) => add(shells, translate(postTooth, [x, y, RZ + zt + M3.headHeight])));

    // Latch, its spring and the label.
    add(orange, translate(latch, [latchX, -pull * LATCH.travel, 0]));
    const y0 = -38.2;
    const y1 = -28 - pull * LATCH.travel;
    const waves = 8;
    const zig = Array.from({ length: waves + 1 }, (_, i): P2 => [latchX + LATCH.width / 2 + (i % 2 ? 1.6 : -1.6), y0 + ((y1 - y0) * i) / waves]);
    const band: P3[] = [...zig.map(([x, y]): P3 => [x + 0.35, y, RZ + t + 0.4]), ...zig.reverse().map(([x, y]): P3 => [x - 0.35, y, RZ + t + 0.4])];
    add(steel, unwrap(extrude(own(unwrap(polygon(band))), [0, 0, 1.6])) as Shape);
    add(teal, brick(w - 5, 14, 0.5, x0 + w / 2, -11, RZ + d - 0.5));

    // Board with its screws, the terminal poles and two light pipes to the front.
    const board = own(brick(w - 4, 2 * half - 7, 1.6, x0 + w / 2, 0, RZ + BOARD_Z));
    add(teal, unwrap(cutAll(board, posts.map(([x, y]) => own(cylinder(1.6, 4, { at: [x, y, RZ + BOARD_Z - 1] }))))) as Shape);
    posts.forEach(([x, y]) => add(steel, translate(boardScrew, [x, y, RZ + zt + M3.headHeight])));
    for (const x of poles) for (const side of [0, 180]) {
      add(dark, rotate(own(translate(pole, [side ? -x : x, 0, 0])), side));
      add(dark, rotate(own(translate(poleTooth, [side ? -x : x, 0, 0])), side));
      add(steel, rotate(own(translate(clamp, [side ? -x : x, 0, 0])), side));
    }
    [orange, white].forEach((list, i) => add(list, cylinder(1.2, d - zt, { at: [x0 + 5 + i * 6, 17, RZ + zt] })));
    if (!populated) return;

    // Parts of the open module: relay, capacitors, choke, chip with leads and a trimmer with its shaft.
    add(white, unwrap(fillet(own(brick(12.5, 29, 15.7, x0 + 12, -11, RZ + zt)), edgeFinder().inDirection('Z'), 1)) as Shape);
    for (const [x, y, cr, ch] of [[25, -21, 5, 12.5], [25, -9, 4, 11]] as const) {
      add(dark, cylinder(cr, ch - 0.4, { at: [x0 + x, y, RZ + zt] }));
      add(steel, cylinder(cr - 0.6, 0.4, { at: [x0 + x, y, RZ + zt + ch - 0.4] }));
    }
    add(brass, torus(4.6, 2.2, { at: [x0 + 24, 15, RZ + zt + 2.2] }));
    add(dark, brick(10, 6.4, 3.4, x0 + 9, 8, RZ + zt + 0.8));
    for (let i = 0; i < 4; i++) for (const side of [1, -1]) add(steel, brick(1.2, 1, 2.6, x0 + 9 + (i - 1.5) * 2.54, 8 + side * 3.7, RZ + zt));
    add(dark, brick(7, 7, 5, x0 + 26, 3, RZ + zt));
    add(steel, cylinder(1.5, d - zt - 4.4, { at: [x0 + 26, 3, RZ + zt + 5] }));
  };

  const clampTilt = Math.max(0, Math.min(15, tilt));
  housing(-47.5, 2, 0, 0, false, false);
  housing(-12.5, 1, clampTilt, release, false, false);
  housing(5, 2, 0, 0, !cover, true);

  const tints = ['#48515c', '#e6e9ee', '#d3a956', '#ff5a1f', '#fbfaf6', '#12b5b0'];
  return {
    rail, plate, gramsPerMetre,
    shells: compound(shells) as Shape,
    groups: [dark, steel, brass, orange, white, teal].map((list, i) => ({ shape: compound(list) as Shape, tint: tints[i]! })),
  };
}

const built = freeing(() => dinRail());
console.log('Rail: ' + built.gramsPerMetre.toFixed(0) + ' g per metre from the kernel volume. The catalogue has ' + RAIL.gramsPerMetre + ' g.');

export default [
  color(built.plate, '#48515c'),
  color(built.rail, '#c3c9d1'),
  color(built.shells, '#f5f2ea'),
  ...built.groups.map((group) => color(group.shape, group.tint)),
];

Functions used ​

box compound cut cutAll cylinder edgeFinder extrude fillet fuseAll line loft measureVolume polygon revolve rotate torus translate unwrap wire color

▶ Run DIN-Rail Clip Enclosure in the playground

Released under the Apache 2.0 License.