@rangefoundry/svg-drawings 0.0.0 → 0.1.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/LICENSE +661 -0
- package/README.md +21 -1
- package/dist/bullet/density.d.ts +65 -0
- package/dist/bullet/density.js +91 -0
- package/dist/bullet/figure.d.ts +34 -0
- package/dist/bullet/figure.js +50 -0
- package/dist/bullet/index.d.ts +4 -0
- package/dist/bullet/index.js +5 -0
- package/dist/bullet/outline.d.ts +144 -0
- package/dist/bullet/outline.js +683 -0
- package/dist/bullet/profile.d.ts +76 -0
- package/dist/bullet/profile.js +257 -0
- package/dist/cartridge/callouts.d.ts +18 -0
- package/dist/cartridge/callouts.js +153 -0
- package/dist/cartridge/detail.d.ts +34 -0
- package/dist/cartridge/detail.js +99 -0
- package/dist/cartridge/figure.d.ts +59 -0
- package/dist/cartridge/figure.js +113 -0
- package/dist/cartridge/figures.d.ts +47 -0
- package/dist/cartridge/figures.js +54 -0
- package/dist/cartridge/focus.d.ts +3 -0
- package/dist/cartridge/focus.js +7 -0
- package/dist/cartridge/index.d.ts +8 -0
- package/dist/cartridge/index.js +8 -0
- package/dist/cartridge/profile.d.ts +46 -0
- package/dist/cartridge/profile.js +630 -0
- package/dist/cartridge/regions.d.ts +28 -0
- package/dist/cartridge/regions.js +50 -0
- package/dist/core/advances.json +106 -0
- package/dist/core/annotate.d.ts +11 -0
- package/dist/core/annotate.js +715 -0
- package/dist/core/cache.d.ts +4 -0
- package/dist/core/cache.js +16 -0
- package/dist/core/face.d.ts +56 -0
- package/dist/core/face.js +54 -0
- package/dist/core/fillet.d.ts +7 -0
- package/dist/core/fillet.js +42 -0
- package/dist/core/geometry.d.ts +22 -0
- package/dist/core/geometry.js +136 -0
- package/dist/core/layout.d.ts +46 -0
- package/dist/core/layout.js +69 -0
- package/dist/core/placer.d.ts +60 -0
- package/dist/core/placer.js +291 -0
- package/dist/core/section.d.ts +32 -0
- package/dist/core/section.js +75 -0
- package/dist/core/text.d.ts +19 -0
- package/dist/core/text.js +38 -0
- package/dist/core/types.d.ts +185 -0
- package/dist/core/types.js +2 -0
- package/dist/core/units.d.ts +18 -0
- package/dist/core/units.js +26 -0
- package/dist/core/vector.d.ts +8 -0
- package/dist/core/vector.js +23 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +10 -0
- package/dist/seating/chamber.d.ts +21 -0
- package/dist/seating/chamber.js +23 -0
- package/dist/seating/index.d.ts +3 -0
- package/dist/seating/index.js +4 -0
- package/dist/seating/preview.d.ts +12 -0
- package/dist/seating/preview.js +30 -0
- package/dist/seating/seating.d.ts +210 -0
- package/dist/seating/seating.js +284 -0
- package/dist/target/face.d.ts +68 -0
- package/dist/target/face.js +50 -0
- package/dist/target/index.d.ts +1 -0
- package/dist/target/index.js +2 -0
- package/package.json +60 -4
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import * as face from './face.js';
|
|
2
|
+
import { arrowheadCorners, boxesOverlap, labelCorners, polygonsOverlap, segHitsBox, segsCross, } from './geometry.js';
|
|
3
|
+
import { headFace, radiusAt, steelBand } from './section.js';
|
|
4
|
+
import { textWidth } from './text.js';
|
|
5
|
+
import { add, pairs, unit } from './vector.js';
|
|
6
|
+
/** At text size `em` (mm) in the part frame: the face distances at that size, what is placed so far (from the axis line over `axis`) and the count of what a candidate runs into; `keepCorners` makes a diameter arrowhead in a radius corner's room cost a little. */
|
|
7
|
+
export function createPlacer(part, em, horizontal, axis, keepCorners) {
|
|
8
|
+
const { points, annotation, unit: lengthUnit } = part;
|
|
9
|
+
const fixedDecimals = part.fixedDecimals === true;
|
|
10
|
+
const k = em / face.baseEm;
|
|
11
|
+
const [lane0, pitch] = [face.firstLaneOffset * k, face.lanePitch * k];
|
|
12
|
+
const arrowLen = face.arrow.length;
|
|
13
|
+
const tailLen = face.arrowTailLengths * arrowLen;
|
|
14
|
+
const rPart = Math.max(...points.map(([r]) => r));
|
|
15
|
+
const rMax = part.outer ?? rPart;
|
|
16
|
+
const zMax = Math.max(...points.map(([, z]) => z));
|
|
17
|
+
const zBreak = zMax + face.breakAllowance * zMax;
|
|
18
|
+
const labels = [];
|
|
19
|
+
const segs = [{ a: [axis[0], 0], b: [axis[1], 0], kind: 'thin', owner: 'axis' }];
|
|
20
|
+
const arcs = [];
|
|
21
|
+
const tips = [];
|
|
22
|
+
const across = [];
|
|
23
|
+
const spans = [];
|
|
24
|
+
const gap = 0.12 * em;
|
|
25
|
+
/** Where an extension line at z starts: the axis past the part, else the steel edge or the outline. */
|
|
26
|
+
const start = (z) => z > zMax + 1e-9
|
|
27
|
+
? face.extensionGap
|
|
28
|
+
: (part.outer ?? radiusAt(points, Math.max(z, 0)) ?? rPart) + face.extensionGap;
|
|
29
|
+
const label = (text, c, dir, owner) => ({
|
|
30
|
+
text,
|
|
31
|
+
c,
|
|
32
|
+
dir,
|
|
33
|
+
w: textWidth(text, em),
|
|
34
|
+
h: em,
|
|
35
|
+
owner,
|
|
36
|
+
});
|
|
37
|
+
// texts keep off the outline, the edge lines, the steel band edge and the break line too
|
|
38
|
+
const edgeSegs = annotation.edges.map((z) => {
|
|
39
|
+
const r = radiusAt(points, z) ?? rPart;
|
|
40
|
+
return { a: [z, -r], b: [z, r], kind: 'thin', owner: 'outline' };
|
|
41
|
+
});
|
|
42
|
+
const breakSegs = part.outer === undefined
|
|
43
|
+
? []
|
|
44
|
+
: steelBand(points, em).sides.flatMap(({ zig }) => pairs(zig).map(([a, b]) => ({ a, b, kind: 'thin', owner: 'outline' })));
|
|
45
|
+
const head = part.outer === undefined ? undefined : headFace(points, annotation);
|
|
46
|
+
const outlineSegs = [
|
|
47
|
+
...edgeSegs,
|
|
48
|
+
...breakSegs,
|
|
49
|
+
...(head ? [{ a: head[0], b: head[1], kind: 'thin', owner: 'outline' }] : []),
|
|
50
|
+
...[1, -1].flatMap((sign) => [
|
|
51
|
+
...pairs(points).map(([[pr, pz], [r, z]]) => ({
|
|
52
|
+
a: [pz, sign * pr],
|
|
53
|
+
b: [z, sign * r],
|
|
54
|
+
kind: 'thin',
|
|
55
|
+
owner: 'outline',
|
|
56
|
+
})),
|
|
57
|
+
...(part.outer === undefined
|
|
58
|
+
? []
|
|
59
|
+
: [
|
|
60
|
+
{
|
|
61
|
+
a: [0, sign * part.outer],
|
|
62
|
+
b: [zMax, sign * part.outer],
|
|
63
|
+
kind: 'thin',
|
|
64
|
+
owner: 'outline',
|
|
65
|
+
},
|
|
66
|
+
]),
|
|
67
|
+
]),
|
|
68
|
+
];
|
|
69
|
+
const angleSymbols = new Set(annotation.angles.map((a) => a.symbol));
|
|
70
|
+
const radiusSymbols = new Set(part.outer === undefined ? [] : annotation.radii.map((r) => r.symbol));
|
|
71
|
+
/** In the material (the case, or the steel band; the chamber cavity is open), where only angle texts go. */
|
|
72
|
+
const inside = ([x, y]) => {
|
|
73
|
+
const r = radiusAt(points, x);
|
|
74
|
+
if (r === undefined)
|
|
75
|
+
return false;
|
|
76
|
+
return part.outer === undefined ? Math.abs(y) < r : Math.abs(y) > r && Math.abs(y) < part.outer;
|
|
77
|
+
};
|
|
78
|
+
const leaderLength = face.radiusLeaderLength * k;
|
|
79
|
+
/** What a radius leader costs: each line of another dimension, the outline or the axis it crosses from 0.4 of the face's leader on (the lines of its own corner pass nearer), and each leader length past the first; across the axis it weighs as 50 crossings, a last resort (weighing angle leaders' crossings moves angle texts far out). */
|
|
80
|
+
const crossings = (s, from) => {
|
|
81
|
+
let crossed = 0;
|
|
82
|
+
for (const list of [outlineSegs, segs])
|
|
83
|
+
for (const o of list)
|
|
84
|
+
if (o.owner !== s.owner && segsCross(from, s.b, o.a, o.b))
|
|
85
|
+
crossed++;
|
|
86
|
+
return crossed;
|
|
87
|
+
};
|
|
88
|
+
const leaderCost = (s) => {
|
|
89
|
+
const length = Math.hypot(s.b[0] - s.a[0], s.b[1] - s.a[1]);
|
|
90
|
+
const skip = 0.4 * leaderLength;
|
|
91
|
+
const from = add(s.a, unit(s.a, s.b), skip);
|
|
92
|
+
const crossed = length > skip ? crossings(s, from) : 0;
|
|
93
|
+
const across = s.a[1] * s.b[1] < 0 ? 50 : 0;
|
|
94
|
+
return crossed + across + Math.max(0, length / leaderLength - 1);
|
|
95
|
+
};
|
|
96
|
+
/** A candidate's radius leaders, weighed below one text conflict. */
|
|
97
|
+
const leaderCosts = (own = []) => own.reduce((sum, s) => sum + (s.leader && !angleSymbols.has(s.owner) ? leaderCost(s) / 100 : 0), 0);
|
|
98
|
+
// the room a radius arrowhead needs at its corner, whichever way its leader runs
|
|
99
|
+
const cornerRoom = annotation.radii.flatMap((c) => [1, -1].map((side) => {
|
|
100
|
+
const [z, r, a] = [c.corner[1], side * c.corner[0], arrowLen];
|
|
101
|
+
return {
|
|
102
|
+
owner: c.symbol,
|
|
103
|
+
box: [
|
|
104
|
+
[z - a, r - a],
|
|
105
|
+
[z + a, r - a],
|
|
106
|
+
[z + a, r + a],
|
|
107
|
+
[z - a, r + a],
|
|
108
|
+
],
|
|
109
|
+
};
|
|
110
|
+
}));
|
|
111
|
+
/** Diameters are placed before radii: an arrowhead from outside in another's corner room costs 0.01, below any conflict, so a clear place or arrows from inside win and the radius keeps its short leader (mostly outside the material). */
|
|
112
|
+
const cornerCosts = ({ across, tips: own = [] }) => across
|
|
113
|
+
? own.filter((t) => t.dir[1] * t.at[1] < 0 &&
|
|
114
|
+
cornerRoom.some((c) => c.owner !== t.owner && polygonsOverlap(corners(t), c.box, gap / 2))).length / 100
|
|
115
|
+
: 0;
|
|
116
|
+
const cornersOf = new WeakMap();
|
|
117
|
+
const corners = (x) => {
|
|
118
|
+
const known = cornersOf.get(x);
|
|
119
|
+
if (known)
|
|
120
|
+
return known;
|
|
121
|
+
const c = 'at' in x ? arrowheadCorners(x) : labelCorners(x);
|
|
122
|
+
cornersOf.set(x, c);
|
|
123
|
+
return c;
|
|
124
|
+
};
|
|
125
|
+
const hitsOutline = (l, hit) => outlineSegs.some((s) => segHitsBox(s, l, gap / 2) && hit());
|
|
126
|
+
const hitsLabels = (l, hit) => labels.some((m) => boxesOverlap(l, m, gap) && hit());
|
|
127
|
+
const hitsLines = (l, hit) => segs.some((s) => s.owner !== l.owner &&
|
|
128
|
+
// the axis may run through an angle text, as it does through the arc
|
|
129
|
+
!(s.owner === 'axis' && angleSymbols.has(l.owner)) &&
|
|
130
|
+
segHitsBox(s, l, gap / 2) &&
|
|
131
|
+
hit());
|
|
132
|
+
const hitsOwnLines = (own, hit) => own.some((s) => labels.some((m) => m.owner !== s.owner && segHitsBox(s, m, gap / 2)) && hit());
|
|
133
|
+
const hitsTips = (l, box, hit) => tips.some((t) => t.owner !== l.owner && polygonsOverlap(corners(t), box, gap / 2) && hit());
|
|
134
|
+
const crossesOtherArc = (s, hit) => segs.some((o) => o.kind === 'rule' &&
|
|
135
|
+
o.owner !== s.owner &&
|
|
136
|
+
angleSymbols.has(o.owner) &&
|
|
137
|
+
segsCross(s.a, s.b, o.a, o.b) &&
|
|
138
|
+
hit());
|
|
139
|
+
// an angle text keeps off its own arc, flank and arrowheads, its arc off another angle's
|
|
140
|
+
const hitsAngle = (l, own, ownTips, box, hit) => {
|
|
141
|
+
for (const s of own) {
|
|
142
|
+
if (s.leader)
|
|
143
|
+
continue;
|
|
144
|
+
if (segHitsBox(s, l, gap / 2) && hit())
|
|
145
|
+
return true;
|
|
146
|
+
// arcs only: the flanks of two angles may meet at a shared corner
|
|
147
|
+
if (s.kind === 'rule' && crossesOtherArc(s, hit))
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
return ownTips.some((t) => polygonsOverlap(corners(t), box, gap / 2) && hit());
|
|
151
|
+
};
|
|
152
|
+
const hitsOwnTips = (ownTips, hit) => ownTips.some((t) => {
|
|
153
|
+
const tri = corners(t);
|
|
154
|
+
return (labels.some((m) => m.owner !== t.owner && polygonsOverlap(tri, corners(m), gap / 2) && hit()) ||
|
|
155
|
+
tips.some((u) => u.owner !== t.owner && polygonsOverlap(tri, corners(u), gap / 2) && hit(0.5)));
|
|
156
|
+
});
|
|
157
|
+
/** Counted up to `limit`, 0 clear; arrowheads count against texts, so no text hides under one, and against each other at half weight, so two dimensions' arrowheads do not merge into one shape where a text stays clear. */
|
|
158
|
+
const textConflicts = ({ l, segs: own = [], tips: ownTips = [] }, limit = Infinity) => {
|
|
159
|
+
let n = angleSymbols.has(l.owner) || radiusSymbols.has(l.owner) || !inside(l.c) ? 0 : 10;
|
|
160
|
+
const hit = (weight = 1) => {
|
|
161
|
+
n += weight;
|
|
162
|
+
return n >= limit;
|
|
163
|
+
};
|
|
164
|
+
if (n >= limit)
|
|
165
|
+
return n;
|
|
166
|
+
const box = corners(l);
|
|
167
|
+
const stages = [
|
|
168
|
+
() => hitsOutline(l, hit),
|
|
169
|
+
() => hitsLabels(l, hit),
|
|
170
|
+
() => hitsLines(l, hit),
|
|
171
|
+
() => hitsOwnLines(own, hit),
|
|
172
|
+
() => hitsTips(l, box, hit),
|
|
173
|
+
() => angleSymbols.has(l.owner) && hitsAngle(l, own, ownTips, box, hit),
|
|
174
|
+
() => hitsOwnTips(ownTips, hit),
|
|
175
|
+
];
|
|
176
|
+
stages.some((stage) => stage());
|
|
177
|
+
return n;
|
|
178
|
+
};
|
|
179
|
+
const conflicts = (c) => textConflicts(c) + leaderCosts(c.segs);
|
|
180
|
+
const placements = [];
|
|
181
|
+
/** The first candidate that runs into nothing, else the one that runs into least; commits it. */
|
|
182
|
+
const pick = (candidates) => {
|
|
183
|
+
const [first] = candidates;
|
|
184
|
+
if (!first)
|
|
185
|
+
throw new Error('A placement needs at least one candidate.');
|
|
186
|
+
return leastConflicting(first, candidates);
|
|
187
|
+
};
|
|
188
|
+
const leastConflicting = (first, candidates) => {
|
|
189
|
+
let chosen = first;
|
|
190
|
+
let least = Infinity;
|
|
191
|
+
for (const c of candidates) {
|
|
192
|
+
const soft = leaderCosts(c.segs) + (keepCorners ? cornerCosts(c) : 0);
|
|
193
|
+
if (soft >= least)
|
|
194
|
+
continue;
|
|
195
|
+
const n = textConflicts(c, least - soft) + soft;
|
|
196
|
+
if (n < least)
|
|
197
|
+
[chosen, least] = [c, n];
|
|
198
|
+
if (n === 0)
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
return chosen;
|
|
202
|
+
};
|
|
203
|
+
const commit = (c) => {
|
|
204
|
+
labels.push(c.l);
|
|
205
|
+
segs.push(...(c.segs ?? []));
|
|
206
|
+
tips.push(...(c.tips ?? []));
|
|
207
|
+
};
|
|
208
|
+
const uncommit = (c) => {
|
|
209
|
+
const own = new Set([c.l, ...(c.segs ?? []), ...(c.tips ?? [])]);
|
|
210
|
+
for (const list of [labels, segs, tips])
|
|
211
|
+
list.splice(0, list.length, ...list.filter((x) => !own.has(x)));
|
|
212
|
+
};
|
|
213
|
+
const place = (candidates) => {
|
|
214
|
+
const chosen = pick(candidates);
|
|
215
|
+
commit(chosen);
|
|
216
|
+
placements.push({ candidates, chosen });
|
|
217
|
+
};
|
|
218
|
+
const arrowAt = (at, dir, owner) => tips.push({ at, dir, owner });
|
|
219
|
+
/** A rule from a to b with arrows at both ends, outside on tails when short. */
|
|
220
|
+
const rule = (a, b, owner, outside) => {
|
|
221
|
+
const u = unit(a, b);
|
|
222
|
+
if (outside) {
|
|
223
|
+
segs.push({ a: add(a, u, -tailLen), b: add(b, u, tailLen), kind: 'rule', owner });
|
|
224
|
+
arrowAt(a, u, owner);
|
|
225
|
+
arrowAt(b, [-u[0], -u[1]], owner);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
segs.push({ a, b, kind: 'rule', owner });
|
|
229
|
+
arrowAt(a, [-u[0], -u[1]], owner);
|
|
230
|
+
arrowAt(b, u, owner);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
/** A rule broken by a zigzag before b, one arrow at a. */
|
|
234
|
+
const broken = (a, b, owner) => {
|
|
235
|
+
const u = unit(a, b);
|
|
236
|
+
const n = [-u[1], u[0]];
|
|
237
|
+
const cut = face.breakLength * k;
|
|
238
|
+
const centre = add(b, u, -cut * 1.5);
|
|
239
|
+
const z0 = add(centre, u, -cut / 2);
|
|
240
|
+
const z1 = add(centre, u, cut / 2);
|
|
241
|
+
const peak = add(add(centre, u, -cut / 4), n, face.breakAmplitude * k);
|
|
242
|
+
const trough = add(add(centre, u, cut / 4), n, -face.breakAmplitude * k);
|
|
243
|
+
const rungs = [
|
|
244
|
+
[a, z0],
|
|
245
|
+
[z0, peak],
|
|
246
|
+
[peak, trough],
|
|
247
|
+
[trough, z1],
|
|
248
|
+
[z1, b],
|
|
249
|
+
];
|
|
250
|
+
for (const [p, q] of rungs)
|
|
251
|
+
segs.push({ a: p, b: q, kind: 'rule', owner });
|
|
252
|
+
arrowAt(a, [-u[0], -u[1]], owner);
|
|
253
|
+
};
|
|
254
|
+
return {
|
|
255
|
+
part,
|
|
256
|
+
points,
|
|
257
|
+
annotation,
|
|
258
|
+
lengthUnit,
|
|
259
|
+
fixedDecimals,
|
|
260
|
+
em,
|
|
261
|
+
horizontal,
|
|
262
|
+
k,
|
|
263
|
+
lane0,
|
|
264
|
+
pitch,
|
|
265
|
+
arrowLen,
|
|
266
|
+
tailLen,
|
|
267
|
+
rMax,
|
|
268
|
+
zMax,
|
|
269
|
+
zBreak,
|
|
270
|
+
gap,
|
|
271
|
+
leaderLength,
|
|
272
|
+
labels,
|
|
273
|
+
segs,
|
|
274
|
+
arcs,
|
|
275
|
+
tips,
|
|
276
|
+
across,
|
|
277
|
+
spans,
|
|
278
|
+
placements,
|
|
279
|
+
start,
|
|
280
|
+
label,
|
|
281
|
+
textConflicts,
|
|
282
|
+
conflicts,
|
|
283
|
+
pick,
|
|
284
|
+
commit,
|
|
285
|
+
uncommit,
|
|
286
|
+
place,
|
|
287
|
+
rule,
|
|
288
|
+
broken,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
//# sourceMappingURL=placer.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Annotation, SectionPoint } from './types.ts';
|
|
2
|
+
/** A point of the part frame in mm: x along the axis from the head (z), y across it (signed r). */
|
|
3
|
+
export type PartPoint = [x: number, y: number];
|
|
4
|
+
/** SVG path data through the points, closed with `close`. */
|
|
5
|
+
export declare const svgPath: (pts: PartPoint[], close?: boolean) => string;
|
|
6
|
+
/** Half profile [r, z] to the part frame, on the side `sign`. */
|
|
7
|
+
export declare const toPartFrame: (points: SectionPoint[], sign?: number) => PartPoint[];
|
|
8
|
+
/** The full section of a half profile from the axis: up one side, back on the mirrored side. */
|
|
9
|
+
export declare function fullSection(points: SectionPoint[]): PartPoint[];
|
|
10
|
+
/** Radius of the steel band's outer edge: the widest bore radius plus the band. */
|
|
11
|
+
export declare const steelRadius: (chamber: SectionPoint[]) => number;
|
|
12
|
+
/**
|
|
13
|
+
* The marked steel band around a chamber cavity, one closed polygon per side, its stroke (the
|
|
14
|
+
* head face, the bore and the outer edge, not the cut end) and the break line across the cut end.
|
|
15
|
+
*/
|
|
16
|
+
export declare function steelBand(chamber: SectionPoint[], em: number): {
|
|
17
|
+
outer: number;
|
|
18
|
+
sides: {
|
|
19
|
+
polygon: PartPoint[];
|
|
20
|
+
stroke: PartPoint[];
|
|
21
|
+
zig: PartPoint[];
|
|
22
|
+
}[];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The chamber head face across the cavity mouth, where a head diameter lies inside the drawn head
|
|
26
|
+
* (R1 below P1 or R3; table VII D below G): its extension lines end there, not in the open mouth.
|
|
27
|
+
*/
|
|
28
|
+
export declare function headFace(chamber: SectionPoint[], annotation: Annotation): [PartPoint, PartPoint] | undefined;
|
|
29
|
+
/** Edge lines across the section at the stations, as long as the widest radius; clipped by the caller. */
|
|
30
|
+
export declare const edgeLines: (edges: number[], r: number) => [PartPoint, PartPoint][];
|
|
31
|
+
/** Radius of a half profile at z: the largest where it steps there, undefined outside it. */
|
|
32
|
+
export declare function radiusAt(points: SectionPoint[], z: number): number | undefined;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import * as face from './face.js';
|
|
2
|
+
import { pairs } from './vector.js';
|
|
3
|
+
// to 0.01 mm, the step of the stored values
|
|
4
|
+
const num = (x) => String(Number(x.toFixed(2)));
|
|
5
|
+
/** SVG path data through the points, closed with `close`. */
|
|
6
|
+
export const svgPath = (pts, close = false) => `M${pts.map(([x, y]) => `${num(x)} ${num(y)}`).join('L')}${close ? 'Z' : ''}`;
|
|
7
|
+
/** Half profile [r, z] to the part frame, on the side `sign`. */
|
|
8
|
+
export const toPartFrame = (points, sign = 1) => points.map(([r, z]) => [z, sign * r]);
|
|
9
|
+
/** The full section of a half profile from the axis: up one side, back on the mirrored side. */
|
|
10
|
+
export function fullSection(points) {
|
|
11
|
+
return [...toPartFrame(points), ...toPartFrame(points, -1).reverse()];
|
|
12
|
+
}
|
|
13
|
+
/** Radius of the steel band's outer edge: the widest bore radius plus the band. */
|
|
14
|
+
export const steelRadius = (chamber) => Math.max(...chamber.map(([r]) => r)) + face.steelBandWidth;
|
|
15
|
+
/**
|
|
16
|
+
* The marked steel band around a chamber cavity, one closed polygon per side, its stroke (the
|
|
17
|
+
* head face, the bore and the outer edge, not the cut end) and the break line across the cut end.
|
|
18
|
+
*/
|
|
19
|
+
export function steelBand(chamber, em) {
|
|
20
|
+
const bore = chamber.slice(1);
|
|
21
|
+
const outer = steelRadius(chamber);
|
|
22
|
+
const [zEnd, rEnd] = [bore.at(-1)[1], bore.at(-1)[0]];
|
|
23
|
+
const k = em / face.baseEm;
|
|
24
|
+
const sides = [1, -1].map((sign) => {
|
|
25
|
+
const along = toPartFrame(bore, sign);
|
|
26
|
+
const polygon = [...along, [zEnd, sign * outer], [0, sign * outer]];
|
|
27
|
+
const stroke = [[zEnd, sign * outer], [0, sign * outer], ...along];
|
|
28
|
+
// a zigzag across the cut steel, from the bore to the outer edge
|
|
29
|
+
const [a, b] = [rEnd, outer];
|
|
30
|
+
const mid = (a + b) / 2;
|
|
31
|
+
const amp = Math.min(face.breakAmplitude * k, (b - a) / 4);
|
|
32
|
+
const zig = [
|
|
33
|
+
[zEnd, sign * a],
|
|
34
|
+
[zEnd, sign * (mid - amp)],
|
|
35
|
+
[zEnd + amp, sign * (mid - amp / 2)],
|
|
36
|
+
[zEnd - amp, sign * (mid + amp / 2)],
|
|
37
|
+
[zEnd, sign * (mid + amp)],
|
|
38
|
+
[zEnd, sign * b],
|
|
39
|
+
];
|
|
40
|
+
return { polygon, stroke, zig };
|
|
41
|
+
});
|
|
42
|
+
return { outer, sides };
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The chamber head face across the cavity mouth, where a head diameter lies inside the drawn head
|
|
46
|
+
* (R1 below P1 or R3; table VII D below G): its extension lines end there, not in the open mouth.
|
|
47
|
+
*/
|
|
48
|
+
export function headFace(chamber, annotation) {
|
|
49
|
+
const head = chamber[1]?.[0];
|
|
50
|
+
if (head === undefined)
|
|
51
|
+
return;
|
|
52
|
+
if (!annotation.dimensions.some((d) => d.kind === 'diameter' && d.behindHeadFace && d.radius < head - 0.01))
|
|
53
|
+
return;
|
|
54
|
+
return [
|
|
55
|
+
[0, -head],
|
|
56
|
+
[0, head],
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
/** Edge lines across the section at the stations, as long as the widest radius; clipped by the caller. */
|
|
60
|
+
export const edgeLines = (edges, r) => edges.map((z) => [
|
|
61
|
+
[z, -r],
|
|
62
|
+
[z, r],
|
|
63
|
+
]);
|
|
64
|
+
/** Radius of a half profile at z: the largest where it steps there, undefined outside it. */
|
|
65
|
+
export function radiusAt(points, z) {
|
|
66
|
+
let found;
|
|
67
|
+
for (const [[r0, z0], [r1, z1]] of pairs(points)) {
|
|
68
|
+
if (z < Math.min(z0, z1) - 1e-9 || z > Math.max(z0, z1) + 1e-9)
|
|
69
|
+
continue;
|
|
70
|
+
const r = Math.abs(z1 - z0) < 1e-12 ? Math.max(r0, r1) : r0 + ((r1 - r0) * (z - z0)) / (z1 - z0);
|
|
71
|
+
found = found === undefined ? r : Math.max(found, r);
|
|
72
|
+
}
|
|
73
|
+
return found;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=section.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type LengthUnit } from './units.ts';
|
|
2
|
+
/** Advance of a character not in the table, in em. */
|
|
3
|
+
export declare const fallbackAdvance = 0.62;
|
|
4
|
+
/** Width of a text in the drawing font at `em`, without kerning (the drawing CSS turns it off). */
|
|
5
|
+
export declare const textWidth: (text: string, em: number) => number;
|
|
6
|
+
/** The angle symbols as the drawing prints them; the values tables print them the same. */
|
|
7
|
+
export declare const greek: Record<string, string>;
|
|
8
|
+
/**
|
|
9
|
+
* Diameter sign, symbol and value in `unit`; derived values in parentheses; angles in decimal
|
|
10
|
+
* degrees; the ratio Rt/R as stored. `fixed`: lengths in the unit's fixed decimals (mm 2, in 4).
|
|
11
|
+
*/
|
|
12
|
+
export declare function labelText(d: {
|
|
13
|
+
kind: string;
|
|
14
|
+
symbol: string;
|
|
15
|
+
value: number;
|
|
16
|
+
derived?: true;
|
|
17
|
+
}, unit?: LengthUnit, fixed?: boolean): string;
|
|
18
|
+
/** Decimal degrees with up to four decimals, as the values tables print them: 40°, 71.4331°. */
|
|
19
|
+
export declare function angleText(degrees: number): string;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import table from './advances.json' with { type: 'json' };
|
|
2
|
+
import * as face from './face.js';
|
|
3
|
+
import { lengthText } from './units.js';
|
|
4
|
+
const advances = table.advances;
|
|
5
|
+
/** Advance of a character not in the table, in em. */
|
|
6
|
+
export const fallbackAdvance = 0.62;
|
|
7
|
+
/** Width of a text in the drawing font at `em`, without kerning (the drawing CSS turns it off). */
|
|
8
|
+
export const textWidth = (text, em) => [...text].reduce((sum, c) => sum + (advances[c] ?? fallbackAdvance), 0) * em;
|
|
9
|
+
/** The angle symbols as the drawing prints them; the values tables print them the same. */
|
|
10
|
+
export const greek = { alpha: 'α', alpha1: 'α1', beta: 'β', delta: 'δ' };
|
|
11
|
+
/**
|
|
12
|
+
* Diameter sign, symbol and value in `unit`; derived values in parentheses; angles in decimal
|
|
13
|
+
* degrees; the ratio Rt/R as stored. `fixed`: lengths in the unit's fixed decimals (mm 2, in 4).
|
|
14
|
+
*/
|
|
15
|
+
export function labelText(d, unit = 'mm', fixed = false) {
|
|
16
|
+
if (d.kind === 'angle') {
|
|
17
|
+
return `${greek[d.symbol] ?? d.symbol} ${angleText(d.value)}`;
|
|
18
|
+
}
|
|
19
|
+
const value = d.symbol === 'Rt/R'
|
|
20
|
+
? String(d.value)
|
|
21
|
+
: fixed && unit === 'mm'
|
|
22
|
+
? fixedMm(d.value)
|
|
23
|
+
: lengthText(d.value, unit);
|
|
24
|
+
const text = `${d.kind === 'diameter' ? face.diameterSign : ''}${d.symbol} ${value}`;
|
|
25
|
+
return d.derived ? `(${text})` : text;
|
|
26
|
+
}
|
|
27
|
+
/** A length in mm to 2 decimals, rounded as its decimal text reads (2.675 to 2.68, where `toFixed` rounds the binary value down), the zeros kept. */
|
|
28
|
+
function fixedMm(mm) {
|
|
29
|
+
// a number written with an exponent (1e-7) has no decimal text to round
|
|
30
|
+
if (String(mm).includes('e'))
|
|
31
|
+
return mm.toFixed(2);
|
|
32
|
+
return Number(`${Math.round(Number(`${mm}e2`))}e-2`).toFixed(2);
|
|
33
|
+
}
|
|
34
|
+
/** Decimal degrees with up to four decimals, as the values tables print them: 40°, 71.4331°. */
|
|
35
|
+
export function angleText(degrees) {
|
|
36
|
+
return `${Number(degrees.toFixed(4))}°`;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=text.js.map
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { PartPoint } from './section.ts';
|
|
2
|
+
import type { LengthUnit } from './units.ts';
|
|
3
|
+
/** Point of a half section: radius r and distance z from the head, both in mm. */
|
|
4
|
+
export type SectionPoint = [r: number, z: number];
|
|
5
|
+
/**
|
|
6
|
+
* A stored value of a standard as the drawing reads it, in the stored unit (mm, degrees). The API
|
|
7
|
+
* type of a spec value is assignable to it.
|
|
8
|
+
*/
|
|
9
|
+
export interface SpecValue {
|
|
10
|
+
side?: string | undefined;
|
|
11
|
+
symbol: string;
|
|
12
|
+
quantity?: string | undefined;
|
|
13
|
+
value?: number | undefined;
|
|
14
|
+
limit?: 'max' | 'min' | undefined;
|
|
15
|
+
tolerance?: {
|
|
16
|
+
lower?: number | undefined;
|
|
17
|
+
upper?: number | undefined;
|
|
18
|
+
} | undefined;
|
|
19
|
+
variant?: string | undefined;
|
|
20
|
+
}
|
|
21
|
+
/** Stored values that disagree, or a value the outline cannot follow; the text gives both. */
|
|
22
|
+
export interface Note<Code extends string = string> {
|
|
23
|
+
code: Code;
|
|
24
|
+
symbols: string[];
|
|
25
|
+
text: string;
|
|
26
|
+
/** The point of the feature the note concerns, in the half section; none on `closedFront`. */
|
|
27
|
+
at?: SectionPoint;
|
|
28
|
+
/** Its number in the figure (`recordFigures`), kept when one side is laid out alone. */
|
|
29
|
+
flagNumber?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The outline of one side and its annotation, in mm and degrees: the points, the notes on values
|
|
33
|
+
* the outline could not follow, and the dimensions, angles and radii drawn on it.
|
|
34
|
+
*/
|
|
35
|
+
export interface Profile<Code extends string = string> {
|
|
36
|
+
/** From the axis at the head (z = 0) to the mouth (cartridge) or the bore (chamber). */
|
|
37
|
+
points: SectionPoint[];
|
|
38
|
+
notes: Note<Code>[];
|
|
39
|
+
annotation: Annotation;
|
|
40
|
+
/** A cartridge e derived from delta (grooveDerivedE): for the description, never dimensioned. */
|
|
41
|
+
derivedE?: number | undefined;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A length (from a datum at z = 0), a chain (from another station), or a diameter at z, in mm.
|
|
45
|
+
* The value is the stored one, or for a derived chain its formula.
|
|
46
|
+
*/
|
|
47
|
+
export interface Dimension {
|
|
48
|
+
kind: 'length' | 'chain' | 'diameter';
|
|
49
|
+
symbol: string;
|
|
50
|
+
value: number;
|
|
51
|
+
/** The stored value; absent on a derived value. */
|
|
52
|
+
stored?: SpecValue;
|
|
53
|
+
derived?: true;
|
|
54
|
+
/** Length and chain: from z to z; diameter: the station, from = to. */
|
|
55
|
+
from: number;
|
|
56
|
+
to: number;
|
|
57
|
+
/** Diameter: half the value. */
|
|
58
|
+
radius?: number | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Dimensioned behind the head face: a head diameter (R1, E1, R3; table VII g, d, G, D) or a head
|
|
61
|
+
* length (R, E, e; table VII t, T).
|
|
62
|
+
*/
|
|
63
|
+
behindHeadFace?: true;
|
|
64
|
+
/** Diameter: the way along the axis it may move off its station, on its own segment. */
|
|
65
|
+
toward?: 1 | -1;
|
|
66
|
+
/** A diameter dimensioned past the mouth (the cartridge G1, the bullet not drawn). */
|
|
67
|
+
front?: true;
|
|
68
|
+
/** S, on the side opposite the length stack. */
|
|
69
|
+
far?: true;
|
|
70
|
+
}
|
|
71
|
+
/** An angle about its vertex: the rays to the two flank points bound it. */
|
|
72
|
+
export interface AngleMark {
|
|
73
|
+
kind: 'angle';
|
|
74
|
+
symbol: string;
|
|
75
|
+
value: number;
|
|
76
|
+
stored: SpecValue;
|
|
77
|
+
vertex: SectionPoint;
|
|
78
|
+
flanks: [SectionPoint, SectionPoint];
|
|
79
|
+
}
|
|
80
|
+
/** A fillet radius at the corner it rounds, before rounding. */
|
|
81
|
+
export interface RadiusMark {
|
|
82
|
+
kind: 'radius';
|
|
83
|
+
symbol: string;
|
|
84
|
+
value: number;
|
|
85
|
+
stored: SpecValue;
|
|
86
|
+
corner: SectionPoint;
|
|
87
|
+
}
|
|
88
|
+
/** A drawn part no value gives: never dimensioned. */
|
|
89
|
+
export interface Convention {
|
|
90
|
+
kind: 'boreStub' | 'g1Stub';
|
|
91
|
+
r: number;
|
|
92
|
+
from: number;
|
|
93
|
+
to: number;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Dimensions, angles, radii, edge stations and conventions drawn on one half section; lengths in
|
|
97
|
+
* mm, angles in degrees.
|
|
98
|
+
*/
|
|
99
|
+
export interface Annotation {
|
|
100
|
+
dimensions: Dimension[];
|
|
101
|
+
angles: AngleMark[];
|
|
102
|
+
radii: RadiusMark[];
|
|
103
|
+
/** z of the real transitions, where edge lines cross the section; not the mouth. */
|
|
104
|
+
edges: number[];
|
|
105
|
+
conventions: Convention[];
|
|
106
|
+
}
|
|
107
|
+
/** A text box: center, unit direction of the text, width and height (one em), its dimension. */
|
|
108
|
+
export interface Label {
|
|
109
|
+
text: string;
|
|
110
|
+
c: PartPoint;
|
|
111
|
+
dir: PartPoint;
|
|
112
|
+
w: number;
|
|
113
|
+
h: number;
|
|
114
|
+
owner: string;
|
|
115
|
+
/** A note flag: the number in a circle of diameter w. */
|
|
116
|
+
ring?: true;
|
|
117
|
+
}
|
|
118
|
+
/** A dimension line (`rule`) or an extension line, flank or leader (`thin`). */
|
|
119
|
+
export interface LineSegment {
|
|
120
|
+
a: PartPoint;
|
|
121
|
+
b: PartPoint;
|
|
122
|
+
kind: 'rule' | 'thin';
|
|
123
|
+
owner: string;
|
|
124
|
+
/** A leader to a text: an angle's from its arc, a radius's from its corner. */
|
|
125
|
+
leader?: true;
|
|
126
|
+
}
|
|
127
|
+
/** An angle arc about c, from angle `from` to `to` (radians in the part frame). */
|
|
128
|
+
export interface Arc {
|
|
129
|
+
c: PartPoint;
|
|
130
|
+
r: number;
|
|
131
|
+
from: number;
|
|
132
|
+
to: number;
|
|
133
|
+
owner: string;
|
|
134
|
+
}
|
|
135
|
+
/** A filled arrowhead with its tip at `at`, pointing along `dir`. */
|
|
136
|
+
export interface Arrowhead {
|
|
137
|
+
at: PartPoint;
|
|
138
|
+
dir: PartPoint;
|
|
139
|
+
owner: string;
|
|
140
|
+
}
|
|
141
|
+
/** What `annotate` places for one side: texts, rules, arcs and arrowheads, in mm in the part frame. */
|
|
142
|
+
export interface Layout {
|
|
143
|
+
labels: Label[];
|
|
144
|
+
segs: LineSegment[];
|
|
145
|
+
arcs: Arc[];
|
|
146
|
+
arrowheads: Arrowhead[];
|
|
147
|
+
/** [x0, y0, x1, y1] in the part frame. */
|
|
148
|
+
box: [number, number, number, number];
|
|
149
|
+
/** Arrow tips of the diameters across the part, for the sweep. */
|
|
150
|
+
diameterTips: {
|
|
151
|
+
owner: string;
|
|
152
|
+
tips: [PartPoint, PartPoint];
|
|
153
|
+
}[];
|
|
154
|
+
/** Spans of the length dimensions, with whether their arrows sit outside. */
|
|
155
|
+
spans: {
|
|
156
|
+
owner: string;
|
|
157
|
+
length: number;
|
|
158
|
+
outside: boolean;
|
|
159
|
+
broken: boolean;
|
|
160
|
+
}[];
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Input of `annotate`: the half section of one side, its annotation, the steel band edge (chamber),
|
|
164
|
+
* the callouts and the length unit.
|
|
165
|
+
*/
|
|
166
|
+
export interface Part {
|
|
167
|
+
points: SectionPoint[];
|
|
168
|
+
annotation: Annotation;
|
|
169
|
+
/** Steel band edge for a chamber; extension lines start there. */
|
|
170
|
+
outer?: number | undefined;
|
|
171
|
+
/** Texts on a leader from a feature (note flags, detail letters), placed after the rest. */
|
|
172
|
+
callouts?: Callout[];
|
|
173
|
+
/** The unit of the length texts; mm by default. */
|
|
174
|
+
unit?: LengthUnit | undefined;
|
|
175
|
+
/** Length texts in the unit's fixed decimals (mm 2, in 4) instead of the value as stored (the bullet drawing). */
|
|
176
|
+
fixedDecimals?: boolean | undefined;
|
|
177
|
+
}
|
|
178
|
+
/** A note flag (its number in a circle) or a detail letter, on a leader from the feature at `at`. */
|
|
179
|
+
export interface Callout {
|
|
180
|
+
owner: string;
|
|
181
|
+
text: string;
|
|
182
|
+
/** The feature in the half section. */
|
|
183
|
+
at: SectionPoint;
|
|
184
|
+
letter?: true;
|
|
185
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** The unit of a figure's length texts; the geometry stays in mm. */
|
|
2
|
+
export type LengthUnit = 'mm' | 'in';
|
|
3
|
+
/** Radians per degree. */
|
|
4
|
+
export declare const deg: number;
|
|
5
|
+
/** mm per unit. */
|
|
6
|
+
export declare const perUnit: Record<LengthUnit, number>;
|
|
7
|
+
/**
|
|
8
|
+
* A stored mm value as a label prints it: mm as stored; inches to 4 decimals (the web app's inch
|
|
9
|
+
* digits), without a zero before the point below 1 in, as inch drawings print them.
|
|
10
|
+
*/
|
|
11
|
+
export declare const lengthText: (mm: number, unit?: LengthUnit) => string;
|
|
12
|
+
/**
|
|
13
|
+
* The longest 1, 2 or 5 times a power of ten of the unit that fits `room` mm, at least 1 mm or
|
|
14
|
+
* 0.1 in; in the unit.
|
|
15
|
+
*/
|
|
16
|
+
export declare function scaleLength(room: number, unit?: LengthUnit): number;
|
|
17
|
+
/** The note that names the unit of a figure's length texts, "Dimensions in mm" or "inches". */
|
|
18
|
+
export declare const unitNote: (unit?: LengthUnit) => string;
|