@rulecad/gears 0.3.5 → 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/CHANGELOG.md +66 -0
- package/contact.mjs +127 -36
- package/core.mjs +867 -212
- package/diagram.mjs +134 -31
- package/gears.mjs +8 -1
- package/package.json +3 -3
- package/planetary.mjs +264 -74
- package/tooth.mjs +123 -37
- package/worm.mjs +139 -29
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 {
|
|
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 = {
|
|
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, '&').replace(/</g, '<');
|
|
11
|
-
const turn = (pts, a, [dx, dy] = [0, 0]) => {
|
|
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) =>
|
|
14
|
-
|
|
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') =>
|
|
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]],
|
|
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',
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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];
|
|
40
|
-
|
|
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];
|
|
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];
|
|
46
|
-
|
|
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') {
|
|
49
|
-
|
|
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 =
|
|
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(),
|
|
59
|
-
|
|
60
|
-
|
|
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),
|
|
65
|
-
|
|
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,
|
|
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),
|
|
76
|
-
|
|
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(
|
|
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(
|
|
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 ??
|
|
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,
|
|
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 {
|
|
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
|
+
"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": {
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"cad"
|
|
32
32
|
],
|
|
33
33
|
"engines": {
|
|
34
|
-
"node": ">=
|
|
34
|
+
"node": ">=22.13"
|
|
35
35
|
},
|
|
36
36
|
"sideEffects": false,
|
|
37
37
|
"repository": {
|
|
@@ -51,6 +51,6 @@
|
|
|
51
51
|
},
|
|
52
52
|
"types": "./gears.d.mts",
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@rulecad/fea": "^0.
|
|
54
|
+
"@rulecad/fea": "^0.4.0"
|
|
55
55
|
}
|
|
56
56
|
}
|