@rulecad/gears 0.3.4 → 0.4.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/diagram.mjs CHANGED
@@ -2,21 +2,49 @@
2
2
  // the geometry its numbers come from: the pitch circles rolling on each other at the pitch point, the base circles the
3
3
  // flanks unwind from, the line of action tangent to both, and the stretch of it where the teeth touch (the path of
4
4
  // contact, whose length over the base pitch is the contact ratio).
5
- import { gearModel, internalModel, rackModel, spurProfile, internalProfile, rackProfile, mesh, phase, rackPhase } from './core.mjs';
5
+ import {
6
+ gearModel,
7
+ internalModel,
8
+ rackModel,
9
+ spurProfile,
10
+ internalProfile,
11
+ rackProfile,
12
+ mesh,
13
+ phase,
14
+ rackPhase,
15
+ } from './core.mjs';
6
16
 
7
17
  const DEG = Math.PI / 180;
8
- const C = { a: '#2f6fb3', b: '#e07a1f', aFill: '#d6e4f3', bFill: '#f9e0c8', line: '#333', faint: '#999', contact: '#c0392b' };
18
+ const C = {
19
+ a: '#2f6fb3',
20
+ b: '#e07a1f',
21
+ aFill: '#d6e4f3',
22
+ bFill: '#f9e0c8',
23
+ line: '#333',
24
+ faint: '#999',
25
+ contact: '#c0392b',
26
+ };
9
27
  const fmt = (x) => String(+x.toFixed(3));
10
28
  const esc = (s) => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;');
11
- const turn = (pts, a, [dx, dy] = [0, 0]) => { const c = Math.cos(a), s = Math.sin(a); return pts.map(([x, y]) => [x * c - y * s + dx, x * s + y * c + dy]); };
29
+ const turn = (pts, a, [dx, dy] = [0, 0]) => {
30
+ const c = Math.cos(a),
31
+ s = Math.sin(a);
32
+ return pts.map(([x, y]) => [x * c - y * s + dx, x * s + y * c + dy]);
33
+ };
12
34
  const path = (loops) => loops.map((l) => `M${l.map(([x, y]) => `${fmt(x)} ${fmt(-y)}`).join('L')}Z`).join('');
13
- const circle = ([x, y], r, colour, dash, w) => `<circle cx="${fmt(x)}" cy="${fmt(-y)}" r="${fmt(r)}" fill="none" stroke="${colour}" stroke-width="${fmt(w)}"${dash ? ` stroke-dasharray="${dash}"` : ''}/>`;
14
- const seg = (p, q, colour, w, dash) => `<line x1="${fmt(p[0])}" y1="${fmt(-p[1])}" x2="${fmt(q[0])}" y2="${fmt(-q[1])}" stroke="${colour}" stroke-width="${fmt(w)}"${dash ? ` stroke-dasharray="${dash}"` : ''}/>`;
35
+ const circle = ([x, y], r, colour, dash, w) =>
36
+ `<circle cx="${fmt(x)}" cy="${fmt(-y)}" r="${fmt(r)}" fill="none" stroke="${colour}" stroke-width="${fmt(w)}"${dash ? ` stroke-dasharray="${dash}"` : ''}/>`;
37
+ const seg = (p, q, colour, w, dash) =>
38
+ `<line x1="${fmt(p[0])}" y1="${fmt(-p[1])}" x2="${fmt(q[0])}" y2="${fmt(-q[1])}" stroke="${colour}" stroke-width="${fmt(w)}"${dash ? ` stroke-dasharray="${dash}"` : ''}/>`;
15
39
  const dot = (p, r, colour) => `<circle cx="${fmt(p[0])}" cy="${fmt(-p[1])}" r="${fmt(r)}" fill="${colour}"/>`;
16
- const label = (p, text, size, colour = C.line, anchor = 'start') => `<text x="${fmt(p[0])}" y="${fmt(-p[1])}" font-size="${fmt(size)}" fill="${colour}" text-anchor="${anchor}" font-family="system-ui, sans-serif">${esc(text)}</text>`;
40
+ const label = (p, text, size, colour = C.line, anchor = 'start') =>
41
+ `<text x="${fmt(p[0])}" y="${fmt(-p[1])}" font-size="${fmt(size)}" fill="${colour}" text-anchor="${anchor}" font-family="system-ui, sans-serif">${esc(text)}</text>`;
17
42
  // Where a line (through p along d, d a unit vector) crosses a circle: the crossing nearest p
18
43
  function cross(p, d, centre, r) {
19
- const f = [p[0] - centre[0], p[1] - centre[1]], b = f[0] * d[0] + f[1] * d[1], c = f[0] ** 2 + f[1] ** 2 - r * r, disc = b * b - c;
44
+ const f = [p[0] - centre[0], p[1] - centre[1]],
45
+ b = f[0] * d[0] + f[1] * d[1],
46
+ c = f[0] ** 2 + f[1] ** 2 - r * r,
47
+ disc = b * b - c;
20
48
  if (disc < 0) return null;
21
49
  const ts = [-b - Math.sqrt(disc), -b + Math.sqrt(disc)].sort((x, y) => Math.abs(x) - Math.abs(y));
22
50
  return [p[0] + ts[0] * d[0], p[1] + ts[0] * d[1]];
@@ -30,50 +58,113 @@ function cross(p, d, centre, r) {
30
58
  * @returns {string} SVG
31
59
  */
32
60
  export function diagram(a, b, { title } = {}) {
33
- const kind = b.kind ?? 'external', ga = gearModel(a), gb = kind === 'internal' ? internalModel(b) : kind === 'rack' ? rackModel(b) : gearModel(b);
34
- const m = mesh(ga, gb), al = m.pressureAngle * DEG, mod = ga.module;
35
- const A = spurProfile(a).loops, rb1 = ga.base / 2, ra1 = ga.tip / 2, rw1 = kind === 'rack' ? ga.pitch / 2 : (m.centre * ga.teeth) / Math.abs(gb.teeth + (kind === 'internal' ? -ga.teeth : ga.teeth));
36
- let O1, O2 = null, B, P, d, T1, T2 = null, rb2, ra2, rw2, aAngle = 0, extras = [];
61
+ const kind = b.kind ?? 'external',
62
+ ga = gearModel(a),
63
+ gb = kind === 'internal' ? internalModel(b) : kind === 'rack' ? rackModel(b) : gearModel(b);
64
+ const m = mesh(ga, gb),
65
+ al = m.pressureAngle * DEG,
66
+ mod = ga.module;
67
+ const A = spurProfile(a).loops,
68
+ rb1 = ga.base / 2,
69
+ ra1 = ga.tip / 2,
70
+ rw1 =
71
+ kind === 'rack'
72
+ ? ga.pitch / 2
73
+ : (m.centre * ga.teeth) / Math.abs(gb.teeth + (kind === 'internal' ? -ga.teeth : ga.teeth));
74
+ let O1,
75
+ O2 = null,
76
+ B,
77
+ P,
78
+ d,
79
+ T1,
80
+ rb2,
81
+ ra2,
82
+ rw2,
83
+ aAngle = 0;
37
84
  if (kind === 'rack') {
38
85
  // the rack along x under the pinion, its pitch line on y = 0; the pinion above, turned so a tooth points down
39
- O1 = [0, m.centre]; aAngle = -Math.PI / 2;
40
- const s0 = rackPhase(ga, 0) + (Math.PI * ga.pitch / 360) * -90, shift = s0 - Math.PI * mod * Math.round(gb.teeth / 2);
86
+ O1 = [0, m.centre];
87
+ aAngle = -Math.PI / 2;
88
+ const s0 = rackPhase(ga, 0) + ((Math.PI * ga.pitch) / 360) * -90,
89
+ shift = s0 - Math.PI * mod * Math.round(gb.teeth / 2);
41
90
  B = rackProfile(b).loops.map((l) => l.map(([x, y]) => [x + shift, y]));
42
- P = [0, m.centre - ga.pitch / 2]; d = [Math.cos(al), -Math.sin(al)];
91
+ P = [0, m.centre - ga.pitch / 2];
92
+ d = [Math.cos(al), -Math.sin(al)];
43
93
  T1 = [O1[0] - rb1 * Math.sin(al), O1[1] - rb1 * Math.cos(al)];
44
94
  } else {
45
- O1 = [0, 0]; O2 = [m.centre, 0];
46
- rb2 = gb.base / 2; ra2 = gb.tip / 2; rw2 = (m.centre * gb.teeth) / Math.abs(gb.teeth + (kind === 'internal' ? -ga.teeth : ga.teeth));
95
+ O1 = [0, 0];
96
+ O2 = [m.centre, 0];
97
+ rb2 = gb.base / 2;
98
+ ra2 = gb.tip / 2;
99
+ rw2 = (m.centre * gb.teeth) / Math.abs(gb.teeth + (kind === 'internal' ? -ga.teeth : ga.teeth));
47
100
  // b turned to its phase (a at angle 0); a ring drawn round a pinion sits the other way: its centre on −x of the pinion's
48
- if (kind === 'internal') { O2 = [-m.centre, 0]; B = internalProfile(b).loops.map((l) => turn(l, phase(ga, gb, 180) * DEG, O2)); P = [rw1, 0]; d = [Math.sin(al), Math.cos(al)]; T1 = [rb1 * Math.cos(al), -rb1 * Math.sin(al)]; T2 = [O2[0] + rb2 * Math.cos(al), -rb2 * Math.sin(al)]; }
49
- else { B = spurProfile(b).loops.map((l) => turn(l, phase(ga, gb, 0) * DEG, O2)); P = [rw1, 0]; d = [Math.sin(al), -Math.cos(al)]; T1 = [rb1 * Math.cos(al), rb1 * Math.sin(al)]; T2 = [O2[0] - rb2 * Math.cos(al), -rb2 * Math.sin(al)]; }
101
+ if (kind === 'internal') {
102
+ O2 = [-m.centre, 0];
103
+ B = internalProfile(b).loops.map((l) => turn(l, phase(ga, gb, 180) * DEG, O2));
104
+ P = [rw1, 0];
105
+ d = [Math.sin(al), Math.cos(al)];
106
+ T1 = [rb1 * Math.cos(al), -rb1 * Math.sin(al)];
107
+ } else {
108
+ B = spurProfile(b).loops.map((l) => turn(l, phase(ga, gb, 0) * DEG, O2));
109
+ P = [rw1, 0];
110
+ d = [Math.sin(al), -Math.cos(al)];
111
+ T1 = [rb1 * Math.cos(al), rb1 * Math.sin(al)];
112
+ }
50
113
  }
51
114
  const Aplaced = A.map((l) => turn(l, aAngle, O1));
52
115
  // the path of contact: the line of action between the two tip circles (or the rack's tip line)
53
116
  const endA = cross(P, d, O1, ra1);
54
- const endB = kind === 'rack' ? (() => { const y = gb.tipLine, t = (y - P[1]) / d[1]; return [P[0] + t * d[0], P[1] + t * d[1]]; })() : cross(P, d, O2, kind === 'internal' ? Math.max(ra2, rb2) : ra2);
117
+ const endB =
118
+ kind === 'rack'
119
+ ? (() => {
120
+ const y = gb.tipLine,
121
+ t = (y - P[1]) / d[1];
122
+ return [P[0] + t * d[0], P[1] + t * d[1]];
123
+ })()
124
+ : cross(P, d, O2, kind === 'internal' ? Math.max(ra2, rb2) : ra2);
55
125
  const pathLength = endA && endB ? Math.hypot(endA[0] - endB[0], endA[1] - endB[1]) : 0;
56
126
 
57
127
  // ---- the pair, whole (left) ----
58
- const all = [...Aplaced, ...B].flat(), xs = all.map((p) => p[0]), ys = all.map((p) => p[1]);
59
- const span = Math.max(Math.max(...xs) - Math.min(...xs), Math.max(...ys) - Math.min(...ys)), pad = span * 0.06, w = span * 0.004;
60
- const vb1 = [Math.min(...xs) - pad, -Math.max(...ys) - pad, Math.max(...xs) - Math.min(...xs) + 2 * pad, Math.max(...ys) - Math.min(...ys) + 2 * pad];
128
+ const all = [...Aplaced, ...B].flat(),
129
+ xs = all.map((p) => p[0]),
130
+ ys = all.map((p) => p[1]);
131
+ const span = Math.max(Math.max(...xs) - Math.min(...xs), Math.max(...ys) - Math.min(...ys)),
132
+ pad = span * 0.06,
133
+ w = span * 0.004;
134
+ const vb1 = [
135
+ Math.min(...xs) - pad,
136
+ -Math.max(...ys) - pad,
137
+ Math.max(...xs) - Math.min(...xs) + 2 * pad,
138
+ Math.max(...ys) - Math.min(...ys) + 2 * pad,
139
+ ];
61
140
  const pair = [
62
141
  `<path d="${path(B)}" fill="${C.bFill}" stroke="${C.b}" stroke-width="${fmt(w)}" fill-rule="evenodd"/>`,
63
142
  `<path d="${path(Aplaced)}" fill="${C.aFill}" stroke="${C.a}" stroke-width="${fmt(w)}" fill-rule="evenodd"/>`,
64
- circle(O1, rw1, C.a, `${fmt(span * 0.01)} ${fmt(span * 0.008)}`, w), ...(O2 ? [circle(O2, rw2, C.b, `${fmt(span * 0.01)} ${fmt(span * 0.008)}`, w)] : [seg([vb1[0], P[1]], [vb1[0] + vb1[2], P[1]], C.b, w, `${fmt(span * 0.01)} ${fmt(span * 0.008)}`)]),
65
- dot(O1, span * 0.008, C.a), ...(O2 ? [dot(O2, span * 0.008, C.b)] : []),
143
+ circle(O1, rw1, C.a, `${fmt(span * 0.01)} ${fmt(span * 0.008)}`, w),
144
+ ...(O2
145
+ ? [circle(O2, rw2, C.b, `${fmt(span * 0.01)} ${fmt(span * 0.008)}`, w)]
146
+ : [seg([vb1[0], P[1]], [vb1[0] + vb1[2], P[1]], C.b, w, `${fmt(span * 0.01)} ${fmt(span * 0.008)}`)]),
147
+ dot(O1, span * 0.008, C.a),
148
+ ...(O2 ? [dot(O2, span * 0.008, C.b)] : []),
66
149
  `<rect x="${fmt(P[0] - 4 * mod)}" y="${fmt(-P[1] - 4 * mod)}" width="${fmt(8 * mod)}" height="${fmt(8 * mod)}" fill="none" stroke="${C.contact}" stroke-width="${fmt(w)}"/>`,
67
150
  ].join('');
68
151
 
69
152
  // ---- the mesh, close up (right) ----
70
- const h = 4 * mod, vb2 = [P[0] - h, -P[1] - h, 2 * h, 2 * h], w2 = h * 0.008, fs = h * 0.055;
153
+ const h = 4 * mod,
154
+ vb2 = [P[0] - h, -P[1] - h, 2 * h, 2 * h],
155
+ w2 = h * 0.008,
156
+ fs = h * 0.055;
71
157
  const along = (p, t) => [p[0] + t * d[0], p[1] + t * d[1]];
72
158
  const close = [
73
159
  `<path d="${path(B)}" fill="${C.bFill}" stroke="${C.b}" stroke-width="${fmt(w2 * 1.5)}" fill-rule="evenodd"/>`,
74
160
  `<path d="${path(Aplaced)}" fill="${C.aFill}" stroke="${C.a}" stroke-width="${fmt(w2 * 1.5)}" fill-rule="evenodd"/>`,
75
- circle(O1, rw1, C.a, `${fmt(h * 0.03)} ${fmt(h * 0.02)}`, w2), circle(O1, rb1, C.a, `${fmt(h * 0.008)} ${fmt(h * 0.016)}`, w2),
76
- ...(O2 ? [circle(O2, rw2, C.b, `${fmt(h * 0.03)} ${fmt(h * 0.02)}`, w2), circle(O2, rb2, C.b, `${fmt(h * 0.008)} ${fmt(h * 0.016)}`, w2)]
161
+ circle(O1, rw1, C.a, `${fmt(h * 0.03)} ${fmt(h * 0.02)}`, w2),
162
+ circle(O1, rb1, C.a, `${fmt(h * 0.008)} ${fmt(h * 0.016)}`, w2),
163
+ ...(O2
164
+ ? [
165
+ circle(O2, rw2, C.b, `${fmt(h * 0.03)} ${fmt(h * 0.02)}`, w2),
166
+ circle(O2, rb2, C.b, `${fmt(h * 0.008)} ${fmt(h * 0.016)}`, w2),
167
+ ]
77
168
  : [seg([P[0] - h, P[1]], [P[0] + h, P[1]], C.b, w2, `${fmt(h * 0.03)} ${fmt(h * 0.02)}`)]),
78
169
  seg(along(P, -2 * h), along(P, 2 * h), C.line, w2), // the line of action
79
170
  ...(endA && endB ? [seg(endA, endB, C.contact, w2 * 4)] : []), // where the teeth touch
@@ -81,18 +172,30 @@ export function diagram(a, b, { title } = {}) {
81
172
  dot(P, h * 0.018, C.line),
82
173
  // a legend in the corner, on white, so nothing is written over a tooth
83
174
  `<rect x="${fmt(vb2[0] + h * 0.03)}" y="${fmt(vb2[1] + h * 0.03)}" width="${fmt(h * 1.2)}" height="${fmt(fs * 4.6)}" fill="#fff" fill-opacity="0.88" rx="${fmt(fs * 0.3)}"/>`,
84
- label([vb2[0] + h * 0.07, -(vb2[1] + h * 0.03 + fs * 1.3)], `line of action, at ${fmt(m.pressureAngle)}° (black)`, fs),
175
+ label(
176
+ [vb2[0] + h * 0.07, -(vb2[1] + h * 0.03 + fs * 1.3)],
177
+ `line of action, at ${fmt(m.pressureAngle)}° (black)`,
178
+ fs,
179
+ ),
85
180
  label([vb2[0] + h * 0.07, -(vb2[1] + h * 0.03 + fs * 2.5)], 'pitch point: where the pitch circles touch (dot)', fs),
86
- label([vb2[0] + h * 0.07, -(vb2[1] + h * 0.03 + fs * 3.7)], `path of contact: ${fmt(pathLength)} mm (red)`, fs, C.contact),
181
+ label(
182
+ [vb2[0] + h * 0.07, -(vb2[1] + h * 0.03 + fs * 3.7)],
183
+ `path of contact: ${fmt(pathLength)} mm (red)`,
184
+ fs,
185
+ C.contact,
186
+ ),
87
187
  ].join('');
88
188
 
89
189
  const lines = [
90
- title ?? `${ga.teeth} teeth on ${kind === 'rack' ? `a rack of ${gb.teeth}` : `${gb.teeth}${kind === 'internal' ? ' (a ring)' : ''}`}, module ${fmt(mod)}`,
190
+ title ??
191
+ `${ga.teeth} teeth on ${kind === 'rack' ? `a rack of ${gb.teeth}` : `${gb.teeth}${kind === 'internal' ? ' (a ring)' : ''}`}, module ${fmt(mod)}`,
91
192
  `pitch circles (dashed) roll on each other at the pitch point; each flank is the involute of its base circle (dotted), drawn by the end of a string unwound from it`,
92
193
  `centre distance ${fmt(m.centre)} mm${kind === 'rack' ? ' (to the pitch line)' : ''} · ${m.text} · pressure angle ${fmt(m.pressureAngle)}°`,
93
194
  `contact ratio ${fmt(m.contact)}: the path of contact (${fmt(pathLength)} mm, red) over the base pitch (${fmt(ga.basePitch)} mm), how many pairs of teeth share the load · backlash ${fmt(m.backlash)} mm`,
94
195
  ];
95
- const W = 1200, H = 600, text = 110;
196
+ const W = 1200,
197
+ H = 600,
198
+ text = 110;
96
199
  return `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H + text}" viewBox="0 0 ${W} ${H + text}">
97
200
  <rect width="${W}" height="${H + text}" fill="#fff"/>
98
201
  <svg x="0" y="0" width="${W / 2}" height="${H}" viewBox="${vb1.map(fmt).join(' ')}">${pair}</svg>
package/gears.mjs CHANGED
@@ -10,7 +10,14 @@
10
10
  // The package's main module: it only gathers the others. The code shared by all of them is in core.mjs.
11
11
  export * from './core.mjs';
12
12
  // Planetary gearboxes, plain and compound
13
- export { planetary, planetaryRatio, compoundPlanetary, compoundRatio, compoundEfficiency, compoundPlay } from './planetary.mjs';
13
+ export {
14
+ planetary,
15
+ planetaryRatio,
16
+ compoundPlanetary,
17
+ compoundRatio,
18
+ compoundEfficiency,
19
+ compoundPlay,
20
+ } from './planetary.mjs';
14
21
  // The mesh measured from the drawn teeth: backlash, transmission error, interference
15
22
  export { contact } from './contact.mjs';
16
23
  // A tooth's root stress by finite elements
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/gears",
3
- "version": "0.3.4",
3
+ "version": "0.4.0",
4
4
  "description": "Gears for printed parts: involute spur gears, racks and ring gears drawn from their rules, with their numbers, how a pair meshes (centre distance, ratio, contact ratio, phase) and how they print.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -14,23 +14,12 @@
14
14
  }
15
15
  },
16
16
  "files": [
17
- "gears.mjs",
18
- "core.mjs",
19
- "planetary.mjs",
20
- "contact.mjs",
21
- "tooth.mjs",
22
- "worm.mjs",
23
- "diagram.mjs",
24
- "gears.d.mts",
25
- "core.d.mts",
26
- "planetary.d.mts",
27
- "contact.d.mts",
28
- "tooth.d.mts",
29
- "worm.d.mts",
30
- "diagram.d.mts",
17
+ "*.mjs",
18
+ "*.d.mts",
31
19
  "README.md",
32
20
  "CHANGELOG.md",
33
- "LICENSE"
21
+ "LICENSE",
22
+ "!test.mjs"
34
23
  ],
35
24
  "license": "MIT",
36
25
  "keywords": [
@@ -42,7 +31,7 @@
42
31
  "cad"
43
32
  ],
44
33
  "engines": {
45
- "node": ">=20"
34
+ "node": ">=22.13"
46
35
  },
47
36
  "sideEffects": false,
48
37
  "repository": {
@@ -62,6 +51,6 @@
62
51
  },
63
52
  "types": "./gears.d.mts",
64
53
  "dependencies": {
65
- "@rulecad/fea": "^0.2.9"
54
+ "@rulecad/fea": "^0.4.0"
66
55
  }
67
56
  }