@rulecad/shapes 0.4.8 → 0.5.0
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/CHANGELOG.md +26 -0
- package/enclosure.mjs +69 -17
- package/features.mjs +73 -22
- package/ops.mjs +77 -17
- package/optimised.d.mts +5 -3
- package/optimised.mjs +32 -12
- package/package.json +6 -15
- package/seats.d.mts +1 -1
- package/seats.mjs +313 -63
- package/shapes.mjs +91 -31
package/shapes.mjs
CHANGED
|
@@ -10,15 +10,32 @@
|
|
|
10
10
|
import jscad from '@jscad/modeling';
|
|
11
11
|
import { ANCHORS } from '@rulecad/variables';
|
|
12
12
|
|
|
13
|
-
const { vectorText } = jscad.text,
|
|
14
|
-
|
|
13
|
+
const { vectorText } = jscad.text,
|
|
14
|
+
{ path2 } = jscad.geometries,
|
|
15
|
+
{ expand } = jscad.expansions,
|
|
16
|
+
{ circle } = jscad.primitives;
|
|
17
|
+
const { union, subtract } = jscad.booleans,
|
|
18
|
+
{ scale } = jscad.transforms,
|
|
19
|
+
{ measureBoundingBox } = jscad.measurements;
|
|
15
20
|
|
|
16
21
|
// Arrows the font doesn't have, as lines in a box height tall: a shaft and a head, pointing up, then turned
|
|
17
22
|
const ARROWS = { '↑': 0, '→': -90, '↓': 180, '←': 90 };
|
|
18
23
|
function arrow(char, height) {
|
|
19
|
-
const h = height / 2,
|
|
24
|
+
const h = height / 2,
|
|
25
|
+
head = height * 0.35,
|
|
26
|
+
turn = (ARROWS[char] * Math.PI) / 180;
|
|
20
27
|
const r = ([x, y]) => [x * Math.cos(turn) - y * Math.sin(turn), x * Math.sin(turn) + y * Math.cos(turn)];
|
|
21
|
-
return [
|
|
28
|
+
return [
|
|
29
|
+
[
|
|
30
|
+
[0, -h],
|
|
31
|
+
[0, h],
|
|
32
|
+
],
|
|
33
|
+
[
|
|
34
|
+
[-head, h - head],
|
|
35
|
+
[0, h],
|
|
36
|
+
[head, h - head],
|
|
37
|
+
],
|
|
38
|
+
].map((l) => l.map(r));
|
|
22
39
|
}
|
|
23
40
|
|
|
24
41
|
/**
|
|
@@ -28,15 +45,23 @@ function arrow(char, height) {
|
|
|
28
45
|
*/
|
|
29
46
|
export function text({ text: input, height, stroke, segments = 8 }) {
|
|
30
47
|
if (!input) throw new Error('text: nothing to write');
|
|
31
|
-
if (!(height > 0) || !(stroke > 0))
|
|
32
|
-
|
|
48
|
+
if (!(height > 0) || !(stroke > 0))
|
|
49
|
+
throw new Error(`text: height and stroke are sizes above 0 mm (got ${height} and ${stroke})`);
|
|
50
|
+
if (input in ARROWS)
|
|
51
|
+
return union(
|
|
52
|
+
...arrow(input, height).map((l) =>
|
|
53
|
+
expand({ delta: stroke / 2, corners: 'round', segments }, path2.fromPoints({}, l)),
|
|
54
|
+
),
|
|
55
|
+
);
|
|
33
56
|
// One scale for every legend: a capital H is height tall, so a dash or a full stop stays small. Then centre the text
|
|
34
57
|
// on its own outline, and thicken the lines (after scaling, so the stroke is exactly as wide as asked)
|
|
35
58
|
const capital = measureBoundingBox(path2.fromPoints({}, vectorText({ height, input: 'H' }).flat()));
|
|
36
59
|
const k = height / (capital[1][1] - capital[0][1] || 1);
|
|
37
60
|
// The text in runs between arrows, each run as the font draws it, set one after another; each arrow drawn in a
|
|
38
61
|
// capital's box between them (the font has none)
|
|
39
|
-
const lines = [],
|
|
62
|
+
const lines = [],
|
|
63
|
+
gap = (capital[1][0] - capital[0][0]) * 0.3,
|
|
64
|
+
mid = (capital[0][1] + capital[1][1]) / 2;
|
|
40
65
|
let x = 0;
|
|
41
66
|
for (const run of input.split(/([↑→↓←])/).filter(Boolean)) {
|
|
42
67
|
if (run in ARROWS) {
|
|
@@ -46,29 +71,42 @@ export function text({ text: input, height, stroke, segments = 8 }) {
|
|
|
46
71
|
continue;
|
|
47
72
|
}
|
|
48
73
|
const drawn = vectorText({ height, input: run }).filter((l) => l.length > 1);
|
|
49
|
-
if (!drawn.length) {
|
|
50
|
-
|
|
74
|
+
if (!drawn.length) {
|
|
75
|
+
x += gap * 2;
|
|
76
|
+
continue;
|
|
77
|
+
} // (spaces)
|
|
78
|
+
const [lo, hi] = measureBoundingBox(path2.fromPoints({}, drawn.flat())),
|
|
79
|
+
lead = run.match(/^ */)[0].length;
|
|
51
80
|
const at = x + lead * gap * 2 - lo[0];
|
|
52
81
|
lines.push(...drawn.map((l) => l.map(([lx, ly]) => [lx + at, ly])));
|
|
53
|
-
x = at + hi[0] + gap +
|
|
82
|
+
x = at + hi[0] + gap + run.match(/ *$/)[0].length * gap * 2;
|
|
54
83
|
}
|
|
55
84
|
if (!lines.length) throw new Error(`text: the font has none of "${input}"`);
|
|
56
85
|
const [lo, hi] = measureBoundingBox(path2.fromPoints({}, lines.flat()));
|
|
57
|
-
const cx = (lo[0] + hi[0]) / 2,
|
|
86
|
+
const cx = (lo[0] + hi[0]) / 2,
|
|
87
|
+
cy = (lo[1] + hi[1]) / 2;
|
|
58
88
|
// Each line, scaled; points closer than half a stroke are dropped (thickening them would fold the outline over itself),
|
|
59
89
|
// and a line smaller than the stroke (a full stop's tiny loop) becomes a round dot
|
|
60
|
-
const strokes = lines
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
90
|
+
const strokes = lines
|
|
91
|
+
.map((l) => {
|
|
92
|
+
const pts = l.map(([x, y]) => [(x - cx) * k, (y - cy) * k]),
|
|
93
|
+
kept = [pts[0]];
|
|
94
|
+
for (const p of pts.slice(1))
|
|
95
|
+
if (Math.hypot(p[0] - kept.at(-1)[0], p[1] - kept.at(-1)[1]) >= stroke / 2) kept.push(p);
|
|
96
|
+
const span = Math.max(
|
|
97
|
+
...[0, 1].map((i) => Math.max(...pts.map((p) => p[i])) - Math.min(...pts.map((p) => p[i]))),
|
|
98
|
+
);
|
|
99
|
+
if (kept.length < 2 || span < stroke) {
|
|
100
|
+
const c = [0, 1].map((i) => (Math.max(...pts.map((p) => p[i])) + Math.min(...pts.map((p) => p[i]))) / 2);
|
|
101
|
+
return circle({ center: c, radius: Math.max(stroke * 0.75, (span + stroke) / 2), segments: segments * 2 });
|
|
102
|
+
}
|
|
103
|
+
// Each straight piece thickened on its own, then joined: thickening a whole line folds over itself at sharp turns
|
|
104
|
+
// (the point of an M), which leaves holes
|
|
105
|
+
return kept
|
|
106
|
+
.slice(1)
|
|
107
|
+
.map((p, i) => expand({ delta: stroke / 2, corners: 'round', segments }, path2.fromPoints({}, [kept[i], p])));
|
|
108
|
+
})
|
|
109
|
+
.flat();
|
|
72
110
|
return strokes.length > 1 ? union(...strokes) : strokes[0];
|
|
73
111
|
}
|
|
74
112
|
|
|
@@ -80,8 +118,9 @@ export function text({ text: input, height, stroke, segments = 8 }) {
|
|
|
80
118
|
*/
|
|
81
119
|
export function anchor({ width, depth }, [w, d], at = 'mc') {
|
|
82
120
|
if (!ANCHORS.includes(at)) throw new Error(`anchor: one of ${ANCHORS.join(', ')} (got "${at}")`);
|
|
83
|
-
const col = { l: -1, c: 0, r: 1 }[at[1]],
|
|
84
|
-
|
|
121
|
+
const col = { l: -1, c: 0, r: 1 }[at[1]],
|
|
122
|
+
row = { t: 1, m: 0, b: -1 }[at[0]];
|
|
123
|
+
return [(col * Math.max(width - w, 0)) / 2, (row * Math.max(depth - d, 0)) / 2];
|
|
85
124
|
}
|
|
86
125
|
|
|
87
126
|
// cut(a, b): a with b taken out, touching only a's faces near b. JSCAD's subtract splits every face of a along the
|
|
@@ -89,23 +128,44 @@ export function anchor({ width, depth }, [w, d], at = 'mc') {
|
|
|
89
128
|
// above). Here the faces clear of b's box pass through unchanged, and only the rest are cut.
|
|
90
129
|
/** a with b taken out, cutting only a's faces near b. @param {object} a JSCAD geom3 @param {object} b JSCAD geom3 @param {number} [margin] @returns {object} */
|
|
91
130
|
export function cut(a, b, margin = 0.05) {
|
|
92
|
-
const { geom3 } = jscad.geometries,
|
|
93
|
-
|
|
131
|
+
const { geom3 } = jscad.geometries,
|
|
132
|
+
[lo, hi] = measureBoundingBox(b);
|
|
133
|
+
const near = [],
|
|
134
|
+
far = [];
|
|
94
135
|
for (const p of geom3.toPolygons(a)) {
|
|
95
|
-
const vs = p.vertices,
|
|
136
|
+
const vs = p.vertices,
|
|
137
|
+
clear = [0, 1, 2].some(
|
|
138
|
+
(k) => Math.max(...vs.map((v) => v[k])) < lo[k] - margin || Math.min(...vs.map((v) => v[k])) > hi[k] + margin,
|
|
139
|
+
);
|
|
96
140
|
(clear ? far : near).push(p);
|
|
97
141
|
}
|
|
98
|
-
if (!near.length) {
|
|
142
|
+
if (!near.length) {
|
|
143
|
+
// 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
144
|
const [alo, ahi] = measureBoundingBox(a);
|
|
100
145
|
return [0, 1, 2].every((k) => lo[k] > alo[k] && hi[k] < ahi[k]) ? subtract(a, b) : a;
|
|
101
146
|
}
|
|
102
147
|
return geom3.create([...far, ...geom3.toPolygons(subtract(geom3.create(near), b))]);
|
|
103
148
|
}
|
|
104
149
|
|
|
105
|
-
export const size = (g) => {
|
|
150
|
+
export const size = (g) => {
|
|
151
|
+
const [lo, hi] = measureBoundingBox(g);
|
|
152
|
+
return [hi[0] - lo[0], hi[1] - lo[1]];
|
|
153
|
+
};
|
|
106
154
|
export { scale };
|
|
107
155
|
export { outline, slab, shell, lid, dovetail, gabled, diamond, vents } from './enclosure.mjs';
|
|
108
156
|
export { countersink, teardropHole, nutPocket, crushRibs, wallOpening } from './features.mjs';
|
|
109
|
-
export {
|
|
157
|
+
export {
|
|
158
|
+
seatSize,
|
|
159
|
+
bearingSeat,
|
|
160
|
+
shaftBore,
|
|
161
|
+
SHAFT_FITS,
|
|
162
|
+
plainBearing,
|
|
163
|
+
shaftShoulder,
|
|
164
|
+
clipGroove,
|
|
165
|
+
lm8uuPocket,
|
|
166
|
+
nutTrap,
|
|
167
|
+
pulleyBore,
|
|
168
|
+
beltClamp,
|
|
169
|
+
} from './seats.mjs';
|
|
110
170
|
export { ops } from './ops.mjs';
|
|
111
171
|
export { fromOutlines, regionShape, optimisedOutline, solidRegion, optimisedSolid } from './optimised.mjs';
|