@rulecad/shapes 0.4.0 → 0.4.2
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/enclosure.mjs +2 -1
- package/features.d.mts +2 -1
- package/features.mjs +15 -3
- package/optimised.d.mts +2 -2
- package/optimised.mjs +14 -12
- package/package.json +1 -1
- package/shapes.mjs +25 -4
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
|
|
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
|
|
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
|
-
|
|
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/optimised.d.mts
CHANGED
|
@@ -31,7 +31,7 @@ export function optimisedOutline(result: {
|
|
|
31
31
|
/**
|
|
32
32
|
* A 3D region (a box, a cylinder along x, y or z, a teardrop, or a ball) as a JSCAD solid; a part's mesh region gives
|
|
33
33
|
* null.
|
|
34
|
-
* @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?:
|
|
34
|
+
* @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: string, part?: string }} r
|
|
35
35
|
* @param {number} [segments]
|
|
36
36
|
*/
|
|
37
37
|
export function solidRegion(r: {
|
|
@@ -40,7 +40,7 @@ export function solidRegion(r: {
|
|
|
40
40
|
size?: number[];
|
|
41
41
|
length?: number;
|
|
42
42
|
axis?: string;
|
|
43
|
-
teardrop?:
|
|
43
|
+
teardrop?: string;
|
|
44
44
|
part?: string;
|
|
45
45
|
}, segments?: number): jscad.geometries.geom3.Geom3;
|
|
46
46
|
/**
|
package/optimised.mjs
CHANGED
|
@@ -48,24 +48,26 @@ export function optimisedOutline(result, { segments = 48 } = {}) {
|
|
|
48
48
|
/**
|
|
49
49
|
* A 3D region (a box, a cylinder along x, y or z, a teardrop, or a ball) as a JSCAD solid; a part's mesh region gives
|
|
50
50
|
* null.
|
|
51
|
-
* @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?:
|
|
51
|
+
* @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: string, part?: string }} r
|
|
52
52
|
* @param {number} [segments]
|
|
53
53
|
*/
|
|
54
54
|
export function solidRegion(r, segments = 48) {
|
|
55
55
|
if (r.part || r.triangles) return null;
|
|
56
56
|
if (r.size) return cuboid({ center: r.at, size: r.size });
|
|
57
57
|
if (r.length != null) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
//
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
//
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
if (
|
|
67
|
-
const
|
|
68
|
-
|
|
58
|
+
const c = cylinder({ radius: r.r, height: r.length, segments }), axis = r.axis || 'z', ax = 'xyz'.indexOf(axis);
|
|
59
|
+
const turned = translate(r.at, axis === 'x' ? rotateY(Math.PI / 2, c) : axis === 'y' ? rotateZ(Math.PI / 2, rotateY(Math.PI / 2, c)) : c);
|
|
60
|
+
// A teardrop (across the axis): the cylinder hulled with a thin ridge r√2 from its axis, below it (a boss) or
|
|
61
|
+
// above it (a hole), so that side is 45°
|
|
62
|
+
// The ridge: a thin slice of a square turned 45° about the axis at each end, its far corner a little past r√2: a
|
|
63
|
+
// round of segments is a little inside its circle, so the faces from it to a point at exactly r√2 would be a touch
|
|
64
|
+
// flatter than 45°
|
|
65
|
+
const pa = r.teardrop ? 'xyz'.indexOf(r.teardrop[1]) : -1;
|
|
66
|
+
if (pa < 0 || pa === ax) return turned;
|
|
67
|
+
const edge = 0.05, slice = 1e-3, reach = (r.teardrop[0] === '-' ? -1 : 1) * (r.r * Math.SQRT2 / Math.cos(Math.PI / segments) - edge / Math.SQRT2);
|
|
68
|
+
const square = [0, 1, 2].map((a) => (a === ax ? slice : edge)), spin = [rotateX, rotateY, rotateZ][ax];
|
|
69
|
+
const end = (along) => translate(r.at.map((x, a) => x + (a === pa ? reach : 0) + (a === ax ? along : 0)), spin(Math.PI / 4, cuboid({ size: square })));
|
|
70
|
+
return hull(turned, end((slice - r.length) / 2), end((r.length - slice) / 2));
|
|
69
71
|
}
|
|
70
72
|
return sphere({ center: r.at, radius: r.r, segments });
|
|
71
73
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/shapes",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
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": {
|
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)
|
|
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
|
|