@rulecad/shapes 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -50,6 +50,31 @@ const window = gabled([-1, 1], [-1, 4], [10, 13]);
50
50
  - `diamond(size, axis, length)`: a square hole turned 45 degrees, for small holes in upright walls.
51
51
  - `vents({ count, width, pitch, length, depth })`: slots side by side along x.
52
52
 
53
+ ## Seats for bought parts
54
+
55
+ The printed side of a bearing or a shaft. Each one prints without supports in the orientation it is drawn for, and takes
56
+ its sizes from rules: the bearing's from its rule set, the fit from the printer's (`hole_comp`, `fit`, `press`, `slip`).
57
+
58
+ ```js
59
+ import { bearingSeat, seatSize, plainBearing, shaftShoulder, clipGroove } from '@rulecad/shapes'
60
+
61
+ const seat = bearingSeat(v['608'], { fit: 'press', printer: v.x1c, at: [0, 0, 12], into: '-z', through: true, relief: 5 })
62
+ seat.seat.pocket // 22.05: the diameter as drawn; clearance -0.05 once printed
63
+ plainBearing(8, { length: 10, printer: v.x1c, at: [0, 0, 10], into: '-z', through: true }) // a bore to subtract
64
+ plainBearing(8, { length: 10, wall: 2, printer: v.x1c }) // a bush, standing up, to add
65
+ shaftShoulder(8, { shoulder: 12, height: 3, at: [0, 0, 20] }) // a collar: flat top, 45 degree underside
66
+ clipGroove(8, { width: 1, depth: 0.5, at: [0, 0, 30] }) // a groove: flat lower face, sloped top
67
+ ```
68
+
69
+ | Figure | How it is worked out |
70
+ |---|---|
71
+ | Pocket, as drawn | outer + `hole_comp` - `press` (or + `slip`, or + a clearance you give, in mm across) |
72
+ | Ledge (`lip`) | a quarter of the races' radial depth by default, so it carries the outer race only; at most half |
73
+ | Plain bore, as drawn | shaft + 2 × clearance + `hole_comp`, the clearance being the printer's `fit` each side by default |
74
+
75
+ `press` and `slip` are estimates until they are measured with the seats coupon in the bench project: print it once per
76
+ filament, press a bearing into each pocket, and write the one that holds without cracking into the printer's rules.
77
+
53
78
  ## Print-aware features
54
79
 
55
80
  Holes, pockets and ribs drawn so they print without supports with the part lying as drawn (z up from the bed).
package/enclosure.mjs CHANGED
@@ -20,7 +20,7 @@ const THIN = 1e-3; // the height of a slice that hulls join into slopes
20
20
 
21
21
  /** A rectangle with rounded or cut corners, centred on 0. @returns {object} geom2 */
22
22
  export function outline(w, h, { round = 0, bevel = 0, segments = 32 } = {}) {
23
- if (w <= 0 || h <= 0) throw new Error(`outline: ${w} × ${h} mm is not a shape`);
23
+ if (!(w > 0) || !(h > 0)) throw new Error(`outline: ${w} × ${h} mm is not a shape`); // (NaN and missing too)
24
24
  if (round > 0) return roundedRectangle({ size: [w, h], roundRadius: Math.min(round, w / 2 - THIN, h / 2 - THIN), segments });
25
25
  if (bevel > 0) { const b = Math.min(bevel, w / 2, h / 2); return polygon({ points: [[-w / 2 + b, -h / 2], [w / 2 - b, -h / 2], [w / 2, -h / 2 + b], [w / 2, h / 2 - b], [w / 2 - b, h / 2], [-w / 2 + b, h / 2], [-w / 2, h / 2 - b], [-w / 2, -h / 2 + b]] }); }
26
26
  return rectangle({ size: [w, h] });
@@ -32,6 +32,7 @@ const slice = (o, d, z0, z1 = z0 + THIN) => translate([0, 0, z0], extrudeLinear(
32
32
 
33
33
  /** A block on an outline, its lower and upper edges sloped in at 45 degrees by bottom and top. @returns {object} geom3 */
34
34
  export function slab({ width, height, depth, round = 0, bevel = 0, bottom = 0, top = 0, segments = 32 }) {
35
+ for (const [k, v] of Object.entries({ width, height, depth })) if (!(typeof v === 'number' && v > 0)) throw new Error(`slab: ${k} is a size above 0 mm (got ${v})`);
35
36
  const o = { width, height, round, bevel, segments };
36
37
  if (bottom + top >= depth) throw new Error(`slab: slopes of ${bottom} and ${top} mm need more than ${depth} mm of depth`);
37
38
  if (!bottom && !top) return slice(o, 0, 0, depth);
package/features.d.mts CHANGED
@@ -8,11 +8,12 @@ export function countersink({ hole, head, length, at, into, teardrop }: {
8
8
  teardrop?: boolean;
9
9
  }): any;
10
10
  /** A round hole across a part, with a 45° roof so it prints without support. */
11
- export function teardropHole({ d, length, at, into }: {
11
+ export function teardropHole({ d, length, at, into, through }: {
12
12
  d: any;
13
13
  length: any;
14
14
  at?: number[];
15
15
  into?: string;
16
+ through?: boolean;
16
17
  }): any;
17
18
  /** A hex pocket for a captive nut, open at the face at `at`, with its roof drawn to print. */
18
19
  export function nutPocket({ flats, height, hole, at, into }: {
package/features.mjs CHANGED
@@ -5,7 +5,8 @@
5
5
  // '+y', '-y', '+z' (up from the bed) or '-z' (down from a top face).
6
6
  // countersink({ hole, head, length, at, into, teardrop }) a flat-head screw's hole and its 90° cone, the head flush
7
7
  // with the face at `at`
8
- // teardropHole({ d, length, at, into }) a round hole across a part (into ±x or ±y) with a 45° pointed roof
8
+ // teardropHole({ d, length, at, into, through }) a round hole across a part (into ±x or ±y) with a 45° pointed
9
+ // roof; through: it runs on out of the far face too (a hole exactly as long as the wall is thick)
9
10
  // nutPocket({ flats, height, hole, at, into }) a hex pocket for a captive nut, open at the face: going up (+z) it
10
11
  // gets a 45° hexagonal roof to the hole; across (±x, ±y) a flat top (a bridge across the flats)
11
12
  // crushRibs({ at, into, z, radius, protrude }) half-round ribs standing protrude into a pocket from its wall, each at
@@ -41,20 +42,29 @@ function tear(r0, r1, z0, z1, up) {
41
42
  return up ? hull(round, ridge(r0, z0), ridge(r1, z1)) : round;
42
43
  }
43
44
 
45
+
46
+ // Sizes are lengths above 0: a feature given 0 or less (or nothing) says so by name, rather than drawing garbage
47
+ function sizes(what, o) { for (const [k, v] of Object.entries(o)) if (!(typeof v === 'number' && v > 0)) throw new Error(`${what}: ${k} is a size above 0 mm (got ${v})`); }
48
+
44
49
  /** A flat-head screw's hole and 90° countersink, the head flush with the face at `at`. */
45
50
  export function countersink({ hole, head, length, at = [0, 0, 0], into = '-z', teardrop = across(into) }) {
51
+ sizes('countersink', { hole, head, length });
46
52
  const up = teardrop ? UP[into] : null, cone = (head - hole) / 2;
47
53
  return along(union(tear(head / 2, head / 2, OUT, 0, up), tear(head / 2, hole / 2, 0, -cone, up), tear(hole / 2, hole / 2, -cone / 2, -length, up)), into, at); // overlapping, not touching: clean seams
48
54
  }
49
55
 
50
56
  /** A round hole across a part, with a 45° roof so it prints without support. */
51
- export function teardropHole({ d, length, at = [0, 0, 0], into = '+x' }) {
57
+ export function teardropHole({ d, length, at = [0, 0, 0], into = '+x', through = false }) {
58
+ sizes('teardropHole', { d, length });
52
59
  if (!across(into)) throw new Error('teardropHole goes across a part: into ±x or ±y (a hole up or down is a plain cylinder)');
53
- return along(tear(d / 2, d / 2, OUT, -length, UP[into]), into, at);
60
+ // through: out past the far face as it starts out past the near one, so a hole exactly as long as its wall leaves no
61
+ // skin of faces lying on each other
62
+ return along(tear(d / 2, d / 2, OUT, -length - (through ? OUT : 0), UP[into]), into, at);
54
63
  }
55
64
 
56
65
  /** A hex pocket for a captive nut, open at the face at `at`, with its roof drawn to print. */
57
66
  export function nutPocket({ flats, height, hole, at = [0, 0, 0], into = '-z' }) {
67
+ sizes('nutPocket', { flats, height, hole });
58
68
  const corner = flats / 2 / Math.cos(Math.PI / 6);
59
69
  // Across, a flat on top (a short bridge); going up, a corner along x either way
60
70
  const hex = (g) => (across(into) && UP[into][0] !== 0 ? rotateZ(Math.PI / 6, g) : g);
@@ -68,6 +78,7 @@ export function nutPocket({ flats, height, hole, at = [0, 0, 0], into = '-z' })
68
78
 
69
79
  /** Half-round crush ribs on a pocket's wall, for a press fit that takes a range of sizes. To add to the part. */
70
80
  export function crushRibs({ at, into, z, radius, protrude }) {
81
+ sizes('crushRibs', { radius, protrude });
71
82
  return union(...at.map(([x, y], i) => {
72
83
  const [nx, ny] = into[i] || into[0], back = radius - protrude;
73
84
  return translate([x - nx * back, y - ny * back, (z[0] + z[1]) / 2], cylinder({ radius, height: z[1] - z[0], segments: 24 }));
@@ -76,6 +87,7 @@ export function crushRibs({ at, into, z, radius, protrude }) {
76
87
 
77
88
  /** A square-cornered opening through an upright wall, centred on at (a cable's plug, a switch). */
78
89
  export function wallOpening({ width, height, depth, at = [0, 0, 0], into = '-y', bridge }) {
90
+ sizes('wallOpening', { width, height, depth });
79
91
  if (!across(into)) throw new Error('wallOpening goes through an upright wall: into ±x or ±y');
80
92
  const long = depth + OUT, w = into.endsWith('x') ? [long, width, height] : [width, long, height], dir = into[0] === '+' ? 1 : -1;
81
93
  const off = into.endsWith('x') ? [dir * (depth - OUT) / 2, 0, 0] : [0, dir * (depth - OUT) / 2, 0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/shapes",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Shape helpers for printed JSCAD designs: text that prints cleanly, anchors on a face, clean local cuts, and enclosure pieces (shells, lids, rails, print-safe holes).",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,6 +16,8 @@
16
16
  "LICENSE",
17
17
  "features.mjs",
18
18
  "features.d.mts",
19
+ "seats.mjs",
20
+ "seats.d.mts",
19
21
  "ops.mjs",
20
22
  "ops.d.mts",
21
23
  "optimised.mjs",
@@ -38,11 +40,15 @@
38
40
  "url": "git+https://github.com/JoniBach/rulecad.git",
39
41
  "directory": "packages/shapes"
40
42
  },
43
+ "homepage": "https://github.com/JoniBach/rulecad/tree/main/packages/shapes#readme",
44
+ "bugs": {
45
+ "url": "https://github.com/JoniBach/rulecad/issues"
46
+ },
41
47
  "scripts": {
42
48
  "test": "node --test"
43
49
  },
44
50
  "dependencies": {
45
- "@rulecad/variables": "^0.2.0"
51
+ "@rulecad/variables": "^0.3.0"
46
52
  },
47
53
  "peerDependencies": {
48
54
  "@jscad/modeling": "^2.12.0",
package/seats.d.mts ADDED
@@ -0,0 +1,96 @@
1
+ /**
2
+ * What a seat is drawn to, in numbers. The pocket is the bearing's outside, plus the printer's hole compensation (printed
3
+ * holes come out that much small across), less the press (or plus the slip).
4
+ * @param {{ bore: number, outer_diameter: number, width: number }} bearing from its rule set
5
+ * @param {{ fit?: 'press' | 'slip' | number, lip?: number, printer: { hole_comp: number, press?: number, slip?: number } }} o
6
+ * fit: a printed clearance across the diameter, or a name for the printer's; lip: how far the ledge reaches in under the
7
+ * outer race (default a quarter of the race's depth, so it never touches the inner race)
8
+ * @returns {{ pocket: number, depth: number, relief: number, lip: number, fit: string | number, clearance: number, comp: number }}
9
+ * pocket: the diameter as drawn; relief: the diameter past the ledge; clearance: what's left across it once printed (below 0, a press)
10
+ */
11
+ export function seatSize(bearing: {
12
+ bore: number;
13
+ outer_diameter: number;
14
+ width: number;
15
+ }, { fit, lip, printer }?: {
16
+ fit?: "press" | "slip" | number;
17
+ lip?: number;
18
+ printer: {
19
+ hole_comp: number;
20
+ press?: number;
21
+ slip?: number;
22
+ };
23
+ }): {
24
+ pocket: number;
25
+ depth: number;
26
+ relief: number;
27
+ lip: number;
28
+ fit: string | number;
29
+ clearance: number;
30
+ comp: number;
31
+ };
32
+ /**
33
+ * A pocket for a ball bearing's outer race, to subtract: the fit from the printer's rules, a ledge to stop against, and
34
+ * a relief past it so the inner race runs free. Going down from a top face (-z) the ledge is flat; going up from the bed
35
+ * (+z) it is a 45° cone that takes the race on its corner; across a part (±x, ±y) the pocket has a teardrop's 45° roof.
36
+ * @param {{ bore: number, outer_diameter: number, width: number }} bearing
37
+ * @param {{ fit?: 'press' | 'slip' | number, lip?: number, relief?: number, through?: boolean, lead?: number, printer: object, at?: number[], into?: string }} o
38
+ * relief: how far past the ledge (mm); through: run the relief out of the far face; lead: the lead-in chamfer at the mouth (none across a part, where its top half would overhang)
39
+ * @returns {object} a JSCAD geom3 to subtract, with .seat holding seatSize's numbers
40
+ */
41
+ export function bearingSeat(bearing: {
42
+ bore: number;
43
+ outer_diameter: number;
44
+ width: number;
45
+ }, { fit, lip, relief, through, into, lead, printer, at }?: {
46
+ fit?: "press" | "slip" | number;
47
+ lip?: number;
48
+ relief?: number;
49
+ through?: boolean;
50
+ lead?: number;
51
+ printer: object;
52
+ at?: number[];
53
+ into?: string;
54
+ }): object;
55
+ /**
56
+ * A plain bearing for a shaft, where a ball bearing is more than the load needs. Its bore is the shaft plus the running
57
+ * clearance each side, plus the printer's hole compensation. As a bore to subtract; given a wall, a bush standing up
58
+ * from z = 0 to add (a tube lying down wouldn't print round).
59
+ * @param {number} shaft diameter
60
+ * @param {{ clearance?: number, length: number, wall?: number, through?: boolean, printer: object, at?: number[], into?: string }} o
61
+ * clearance: each side (default the printer's fit)
62
+ * @returns {object} a JSCAD geom3, with .bore the diameter as drawn and .clearance each side
63
+ */
64
+ export function plainBearing(shaft: number, { clearance, length, wall, through, printer, at, into }?: {
65
+ clearance?: number;
66
+ length: number;
67
+ wall?: number;
68
+ through?: boolean;
69
+ printer: object;
70
+ at?: number[];
71
+ into?: string;
72
+ }): object;
73
+ /**
74
+ * A collar on a printed shaft, to add, stood up as it prints: a flat top for a bearing's inner race to stop against, and
75
+ * a 45° underside so it starts from the shaft with nothing hanging.
76
+ * @param {number} shaft diameter @param {{ shoulder: number, height: number, at?: number[] }} o shoulder: the collar's diameter;
77
+ * height: from where it leaves the shaft to its flat top; at: the point on the shaft's axis at its top
78
+ * @returns {object} a JSCAD geom3 to add
79
+ */
80
+ export function shaftShoulder(shaft: number, { shoulder, height, at }?: {
81
+ shoulder: number;
82
+ height: number;
83
+ at?: number[];
84
+ }): object;
85
+ /**
86
+ * A groove round a printed shaft for a circlip or a printed clip, to subtract, stood up as it prints: a flat lower face
87
+ * for the clip to bear on, a sloped upper face so nothing bridges.
88
+ * @param {number} shaft diameter @param {{ width: number, depth: number, at?: number[] }} o width: of the flat part;
89
+ * depth: into the shaft each side; at: the point on the axis at the flat lower face
90
+ * @returns {object} a JSCAD geom3 to subtract, with .root the diameter at the groove's root
91
+ */
92
+ export function clipGroove(shaft: number, { width, depth, at }?: {
93
+ width: number;
94
+ depth: number;
95
+ at?: number[];
96
+ }): object;
package/seats.mjs ADDED
@@ -0,0 +1,145 @@
1
+ // Seats for bought parts: the printed side of a bearing or a shaft, drawn so it prints without supports and says what
2
+ // fit it was drawn to. As with the print-aware features, sizes come in from rules: a bearing's from its rule set, the
3
+ // fit from the printer's (hole_comp, fit, press, slip), and each cutter starts at a point on a face (at) and goes into
4
+ // the material along a direction (into): '+x', '-x', '+y', '-y', '+z' (up from the bed) or '-z' (down from a top face).
5
+ // seatSize(bearing, { fit, lip, printer }) the numbers: the pocket as drawn, its depth, the relief, the fit
6
+ // bearingSeat(bearing, { fit, lip, relief, through, lead, printer, at, into }) a pocket to subtract, for the outer race
7
+ // plainBearing(shaft, { clearance, length, wall, through, printer, at, into }) a bore to subtract, or with wall a bush to add
8
+ // shaftShoulder(shaft, { shoulder, height, at }) a collar to add to a printed shaft, stood up: flat on top, 45° below
9
+ // clipGroove(shaft, { width, depth, at }) a ring to subtract from a printed shaft, stood up: flat below, sloped above
10
+ import jscad from '@jscad/modeling';
11
+
12
+ const { cylinder, cylinderElliptic, cuboid, polygon } = jscad.primitives, { translate, rotateX, rotateY, rotateZ } = jscad.transforms;
13
+ const { hull } = jscad.hulls, { union } = jscad.booleans, { extrudeRotate } = jscad.extrusions;
14
+ const OUT = 0.1, TINY = 1e-3, EDGE = 0.05, SEGMENTS = 64;
15
+ const UP = { '+x': [1, 0], '-x': [-1, 0], '+y': [0, 1], '-y': [0, -1] };
16
+
17
+ function along(g, into, at = [0, 0, 0]) {
18
+ const turn = { '-z': (x) => x, '+z': (x) => rotateX(Math.PI, x), '+x': (x) => rotateY(-Math.PI / 2, x), '-x': (x) => rotateY(Math.PI / 2, x),
19
+ '+y': (x) => rotateX(Math.PI / 2, x), '-y': (x) => rotateX(-Math.PI / 2, x) }[into];
20
+ if (!turn) throw new Error(`into must be +x, -x, +y, -y, +z or -z (got ${into})`);
21
+ return translate(at, turn(g));
22
+ }
23
+ function size(what, name, v, { zero = false } = {}) { if (!(typeof v === 'number' && Number.isFinite(v) && (zero ? v >= 0 : v > 0))) throw new Error(`${what}: ${name} is a size ${zero ? 'of 0 mm or more' : 'above 0 mm'} (got ${v})`); return v; }
24
+ // A round cutter from z0 down to z1 (radius r0 to r1), with a 45° ridge on the up side when it goes across a part
25
+ function tear(r0, r1, z0, z1, up) {
26
+ const ridge = (r, z) => translate([up[0] * (r * Math.SQRT2 - EDGE / Math.SQRT2), up[1] * (r * Math.SQRT2 - EDGE / Math.SQRT2), z], rotateZ(Math.PI / 4, cuboid({ size: [EDGE, EDGE, TINY] })));
27
+ const round = r0 === r1 ? translate([0, 0, (z0 + z1) / 2], cylinder({ radius: r0, height: Math.abs(z0 - z1), segments: SEGMENTS }))
28
+ : translate([0, 0, (z0 + z1) / 2], cylinderElliptic({ height: Math.abs(z0 - z1), startRadius: [r1, r1], endRadius: [r0, r0], segments: SEGMENTS }));
29
+ return up ? hull(round, ridge(r0, z0), ridge(r1, z1)) : round;
30
+ }
31
+ function printerOf(what, p) {
32
+ if (!p || typeof p !== 'object') throw new Error(`${what}: give the printer's rules ({ printer: v.x1c }): the fit is theirs, so there are no numbers here`);
33
+ return p;
34
+ }
35
+ const FITS = ['press', 'slip'];
36
+ function bearingOf(what, b) {
37
+ if (!b || typeof b !== 'object') throw new Error(`${what}: give the bearing's rules ({ bore, outer_diameter, width }), not ${b}`);
38
+ const outer = b.outer_diameter ?? b.outer, bore = size(what, 'bore', b.bore);
39
+ if (!(size(what, 'outer_diameter', outer) > bore)) throw new Error(`${what}: the outer diameter (${outer}) is above the bore (${bore})`);
40
+ return { bore, outer, width: size(what, 'width', b.width) };
41
+ }
42
+
43
+ /**
44
+ * What a seat is drawn to, in numbers. The pocket is the bearing's outside, plus the printer's hole compensation (printed
45
+ * holes come out that much small across), less the press (or plus the slip).
46
+ * @param {{ bore: number, outer_diameter: number, width: number }} bearing from its rule set
47
+ * @param {{ fit?: 'press' | 'slip' | number, lip?: number, printer: { hole_comp: number, press?: number, slip?: number } }} o
48
+ * fit: a printed clearance across the diameter, or a name for the printer's; lip: how far the ledge reaches in under the
49
+ * outer race (default a quarter of the race's depth, so it never touches the inner race)
50
+ * @returns {{ pocket: number, depth: number, relief: number, lip: number, fit: string | number, clearance: number, comp: number }}
51
+ * pocket: the diameter as drawn; relief: the diameter past the ledge; clearance: what's left across it once printed (below 0, a press)
52
+ */
53
+ export function seatSize(bearing, { fit = 'press', lip, printer } = {}) {
54
+ const what = 'bearingSeat', b = bearingOf(what, bearing), p = printerOf(what, printer), comp = size(what, 'printer.hole_comp', p.hole_comp, { zero: true });
55
+ let clearance;
56
+ if (typeof fit === 'number') clearance = fit;
57
+ else if (fit === 'press') clearance = -size(what, 'printer.press', p.press, { zero: true });
58
+ else if (fit === 'slip') clearance = size(what, 'printer.slip', p.slip, { zero: true });
59
+ else throw new Error(`${what}: fit is ${FITS.join(' or ')}, or a clearance in mm (got ${fit})`);
60
+ const radial = (b.outer - b.bore) / 2, ledge = lip ?? radial / 4;
61
+ size(what, 'lip', ledge);
62
+ if (ledge > radial / 2) throw new Error(`${what}: a lip of ${ledge} mm reaches the inner race (the races are ${radial} mm deep together: ${radial / 2} mm at most)`);
63
+ const pocket = b.outer + comp + clearance;
64
+ return { pocket: +pocket.toFixed(4), depth: b.width, relief: +(pocket - 2 * ledge).toFixed(4), lip: ledge, fit, clearance, comp };
65
+ }
66
+
67
+ /**
68
+ * A pocket for a ball bearing's outer race, to subtract: the fit from the printer's rules, a ledge to stop against, and
69
+ * a relief past it so the inner race runs free. Going down from a top face (-z) the ledge is flat; going up from the bed
70
+ * (+z) it is a 45° cone that takes the race on its corner; across a part (±x, ±y) the pocket has a teardrop's 45° roof.
71
+ * @param {{ bore: number, outer_diameter: number, width: number }} bearing
72
+ * @param {{ fit?: 'press' | 'slip' | number, lip?: number, relief?: number, through?: boolean, lead?: number, printer: object, at?: number[], into?: string }} o
73
+ * relief: how far past the ledge (mm); through: run the relief out of the far face; lead: the lead-in chamfer at the mouth (none across a part, where its top half would overhang)
74
+ * @returns {object} a JSCAD geom3 to subtract, with .seat holding seatSize's numbers
75
+ */
76
+ export function bearingSeat(bearing, { fit = 'press', lip, relief = 1, through = false, into = '-z', lead = UP[into] ? 0 : 0.4, printer, at = [0, 0, 0] } = {}) {
77
+ const what = 'bearingSeat', s = seatSize(bearing, { fit, lip, printer });
78
+ size(what, 'relief', relief); size(what, 'lead', lead, { zero: true });
79
+ const R = s.pocket / 2, r = s.relief / 2, up = UP[into] ?? null, D = s.depth, past = D + relief + (through ? OUT : 0), cuts = [];
80
+ cuts.push(tear(R, R, lead ? 0 : OUT, -D, up));
81
+ if (lead) { cuts.push(tear(R + lead, R, 0, -lead, up)); cuts.push(tear(R + lead, R + lead, OUT, 0, up)); }
82
+ if (into === '+z') {
83
+ // the ledge printed as a ceiling: a 45° cone, to a point if nothing runs through it
84
+ const end = through ? r : TINY, rise = R - end;
85
+ cuts.push(tear(R, end, -D, -D - rise));
86
+ if (through) cuts.push(tear(r, r, -D - rise / 2, -Math.max(past, D + rise + OUT)));
87
+ } else cuts.push(tear(r, r, -D + EDGE, -past, up));
88
+ const g = along(union(cuts), into, at);
89
+ return Object.assign(g, { seat: s });
90
+ }
91
+
92
+ /**
93
+ * A plain bearing for a shaft, where a ball bearing is more than the load needs. Its bore is the shaft plus the running
94
+ * clearance each side, plus the printer's hole compensation. As a bore to subtract; given a wall, a bush standing up
95
+ * from z = 0 to add (a tube lying down wouldn't print round).
96
+ * @param {number} shaft diameter
97
+ * @param {{ clearance?: number, length: number, wall?: number, through?: boolean, printer: object, at?: number[], into?: string }} o
98
+ * clearance: each side (default the printer's fit)
99
+ * @returns {object} a JSCAD geom3, with .bore the diameter as drawn and .clearance each side
100
+ */
101
+ export function plainBearing(shaft, { clearance, length, wall, through = false, printer, at = [0, 0, 0], into = '-z' } = {}) {
102
+ const what = 'plainBearing', p = printerOf(what, printer);
103
+ size(what, 'shaft', shaft); size(what, 'length', length);
104
+ const gap = clearance ?? size(what, 'printer.fit', p.fit), comp = size(what, 'printer.hole_comp', p.hole_comp, { zero: true }), bore = +(shaft + 2 * size(what, 'clearance', gap) + comp).toFixed(4);
105
+ if (wall == null) return Object.assign(along(tear(bore / 2, bore / 2, OUT, -length - (through ? OUT : 0), UP[into] ?? null), into, at), { bore, clearance: gap });
106
+ size(what, 'wall', wall);
107
+ if (UP[into]) throw new Error(`${what}: a bush with a wall prints standing up (into +z or -z): a tube on its side wouldn't print round. Bore across a part with no wall instead`);
108
+ const outer = bore / 2 + wall, inner = bore / 2, chamfer = Math.min(0.4, wall / 2);
109
+ const g = extrudeRotate({ segments: SEGMENTS }, polygon({ points: [[inner + chamfer, 0], [outer - chamfer, 0], [outer, chamfer], [outer, length], [inner, length], [inner, chamfer]] }));
110
+ return Object.assign(translate(at, g), { bore, clearance: gap });
111
+ }
112
+
113
+ /**
114
+ * A collar on a printed shaft, to add, stood up as it prints: a flat top for a bearing's inner race to stop against, and
115
+ * a 45° underside so it starts from the shaft with nothing hanging.
116
+ * @param {number} shaft diameter @param {{ shoulder: number, height: number, at?: number[] }} o shoulder: the collar's diameter;
117
+ * height: from where it leaves the shaft to its flat top; at: the point on the shaft's axis at its top
118
+ * @returns {object} a JSCAD geom3 to add
119
+ */
120
+ export function shaftShoulder(shaft, { shoulder, height, at = [0, 0, 0] } = {}) {
121
+ const what = 'shaftShoulder', a = size(what, 'shaft', shaft) / 2, b = size(what, 'shoulder', shoulder) / 2;
122
+ size(what, 'height', height);
123
+ if (b <= a) throw new Error(`${what}: the shoulder (${shoulder}) stands proud of the shaft (${shaft})`);
124
+ if (height < b - a) throw new Error(`${what}: a ${shoulder} mm collar on a ${shaft} mm shaft is ${b - a} mm of 45° underside, and ${height} mm isn't enough to draw it`);
125
+ // the solid runs in to the axis, so it joins the shaft whatever the shaft is drawn as
126
+ return translate(at, extrudeRotate({ segments: SEGMENTS }, polygon({ points: [[0, -height], [a - TINY, -height], [b, -height + (b - a) + TINY], [b, 0], [0, 0]] })));
127
+ }
128
+
129
+ /**
130
+ * A groove round a printed shaft for a circlip or a printed clip, to subtract, stood up as it prints: a flat lower face
131
+ * for the clip to bear on, a sloped upper face so nothing bridges.
132
+ * @param {number} shaft diameter @param {{ width: number, depth: number, at?: number[] }} o width: of the flat part;
133
+ * depth: into the shaft each side; at: the point on the axis at the flat lower face
134
+ * @returns {object} a JSCAD geom3 to subtract, with .root the diameter at the groove's root
135
+ */
136
+ export function clipGroove(shaft, { width, depth, at = [0, 0, 0] } = {}) {
137
+ const what = 'clipGroove', R = size(what, 'shaft', shaft) / 2;
138
+ size(what, 'width', width); size(what, 'depth', depth);
139
+ if (depth >= R / 2) throw new Error(`${what}: a groove ${depth} mm deep leaves too little of a ${shaft} mm shaft (under ${R / 2} mm)`);
140
+ const r = R - depth, over = R + 1, rise = 1.2 * (over - r);
141
+ // The upper face is a little steeper than 45° (a slope of exactly the limit is an overhang by a hair, sliced). The
142
+ // cutter's outside runs 1 mm past the shaft, with its slope carried on so the face is one line
143
+ const g = extrudeRotate({ segments: SEGMENTS }, polygon({ points: [[r, 0], [over, 0], [over, width + rise], [r, width]] }));
144
+ return Object.assign(translate(at, g), { root: 2 * r });
145
+ }
package/shapes.d.mts CHANGED
@@ -27,4 +27,5 @@ export const scale: typeof jscad.transforms.scale;
27
27
  import jscad from '@jscad/modeling';
28
28
  export { outline, slab, shell, lid, dovetail, gabled, diamond, vents } from "./enclosure.mjs";
29
29
  export { countersink, teardropHole, nutPocket, crushRibs, wallOpening } from "./features.mjs";
30
+ export { seatSize, bearingSeat, plainBearing, shaftShoulder, clipGroove } from "./seats.mjs";
30
31
  export { fromOutlines, regionShape, optimisedOutline, solidRegion, optimisedSolid } from "./optimised.mjs";
package/shapes.mjs CHANGED
@@ -4,7 +4,7 @@
4
4
  // (optimised.mjs: what cad optimise finds, as a 2D shape to extrude or a 3D solid).
5
5
  // text({ text, height, stroke, segments }) -> a 2D outline of the text, centred on 0: JSCAD's single-line
6
6
  // font, scaled so a capital is height tall, each line thickened to stroke wide (whole print lines, so it prints).
7
- // The arrows the font lacks (up, down, left, right) are drawn in the same stroke.
7
+ // The arrows the font lacks (up, down, left, right) are drawn in the same stroke, alone or among letters.
8
8
  // anchor({ width, depth }, size, at) -> [x, y]: where something size wide and deep goes on a face width ×
9
9
  // depth (centred on 0, y to the back) at one of 9 anchors: tl tc tr / ml mc mr / bl bc br
10
10
  import jscad from '@jscad/modeling';
@@ -28,13 +28,31 @@ function arrow(char, height) {
28
28
  */
29
29
  export function text({ text: input, height, stroke, segments = 8 }) {
30
30
  if (!input) throw new Error('text: nothing to write');
31
+ if (!(height > 0) || !(stroke > 0)) throw new Error(`text: height and stroke are sizes above 0 mm (got ${height} and ${stroke})`);
31
32
  if (input in ARROWS) return union(...arrow(input, height).map((l) => expand({ delta: stroke / 2, corners: 'round', segments }, path2.fromPoints({}, l))));
32
- const lines = vectorText({ height, input }).filter((l) => l.length > 1);
33
- if (!lines.length) throw new Error(`text: the font has none of "${input}"`);
34
33
  // One scale for every legend: a capital H is height tall, so a dash or a full stop stays small. Then centre the text
35
34
  // on its own outline, and thicken the lines (after scaling, so the stroke is exactly as wide as asked)
36
35
  const capital = measureBoundingBox(path2.fromPoints({}, vectorText({ height, input: 'H' }).flat()));
37
36
  const k = height / (capital[1][1] - capital[0][1] || 1);
37
+ // The text in runs between arrows, each run as the font draws it, set one after another; each arrow drawn in a
38
+ // capital's box between them (the font has none)
39
+ const lines = [], gap = (capital[1][0] - capital[0][0]) * 0.3, mid = (capital[0][1] + capital[1][1]) / 2;
40
+ let x = 0;
41
+ for (const run of input.split(/([↑→↓←])/).filter(Boolean)) {
42
+ if (run in ARROWS) {
43
+ const w = (capital[1][1] - capital[0][1]) * 0.7;
44
+ lines.push(...arrow(run, height).map((l) => l.map(([ax, ay]) => [x + w / 2 + ax / k, mid + ay / k])));
45
+ x += w + gap;
46
+ continue;
47
+ }
48
+ const drawn = vectorText({ height, input: run }).filter((l) => l.length > 1);
49
+ if (!drawn.length) { x += gap * 2; continue; } // (spaces)
50
+ const [lo, hi] = measureBoundingBox(path2.fromPoints({}, drawn.flat())), lead = run.match(/^ */)[0].length;
51
+ const at = x + lead * gap * 2 - lo[0];
52
+ lines.push(...drawn.map((l) => l.map(([lx, ly]) => [lx + at, ly])));
53
+ x = at + hi[0] + gap + (run.match(/ *$/)[0].length) * gap * 2;
54
+ }
55
+ if (!lines.length) throw new Error(`text: the font has none of "${input}"`);
38
56
  const [lo, hi] = measureBoundingBox(path2.fromPoints({}, lines.flat()));
39
57
  const cx = (lo[0] + hi[0]) / 2, cy = (lo[1] + hi[1]) / 2;
40
58
  // Each line, scaled; points closer than half a stroke are dropped (thickening them would fold the outline over itself),
@@ -77,7 +95,10 @@ export function cut(a, b, margin = 0.05) {
77
95
  const vs = p.vertices, clear = [0, 1, 2].some((k) => Math.max(...vs.map((v) => v[k])) < lo[k] - margin || Math.min(...vs.map((v) => v[k])) > hi[k] + margin);
78
96
  (clear ? far : near).push(p);
79
97
  }
80
- if (!near.length) return a;
98
+ if (!near.length) { // nothing of a's surface near b: b is clear of a, or wholly inside it (a cavity), which only a whole subtract makes
99
+ const [alo, ahi] = measureBoundingBox(a);
100
+ return [0, 1, 2].every((k) => lo[k] > alo[k] && hi[k] < ahi[k]) ? subtract(a, b) : a;
101
+ }
81
102
  return geom3.create([...far, ...geom3.toPolygons(subtract(geom3.create(near), b))]);
82
103
  }
83
104
 
@@ -85,5 +106,6 @@ export const size = (g) => { const [lo, hi] = measureBoundingBox(g); return [hi[
85
106
  export { scale };
86
107
  export { outline, slab, shell, lid, dovetail, gabled, diamond, vents } from './enclosure.mjs';
87
108
  export { countersink, teardropHole, nutPocket, crushRibs, wallOpening } from './features.mjs';
109
+ export { seatSize, bearingSeat, plainBearing, shaftShoulder, clipGroove } from './seats.mjs';
88
110
  export { ops } from './ops.mjs';
89
111
  export { fromOutlines, regionShape, optimisedOutline, solidRegion, optimisedSolid } from './optimised.mjs';