@rulecad/pcb 0.7.0 → 0.8.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 +43 -0
- package/README.md +8 -0
- package/circuit.mjs +7 -2
- package/package.json +3 -3
- package/pcb.mjs +175 -37
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,49 @@
|
|
|
3
3
|
What changed in each release, for someone upgrading a workspace. A **drilled size** here is a dimension the fab
|
|
4
4
|
makes (a hole's diameter in the drill file). Each release says whether any drilled or printed size moved, and why.
|
|
5
5
|
|
|
6
|
+
## 0.8.0: The readers of KiCad's files refuse text over 64 MB, or made to unfold into far more than its size, saying the limit
|
|
7
|
+
|
|
8
|
+
### Fixed
|
|
9
|
+
|
|
10
|
+
- `fromKicadPcb`, `fromKicad`, `wrlMesh` and `stepBounds` read whatever text they were given, however long, and parsed
|
|
11
|
+
it into many times its size: a board or a part's model from someone else's repository (what `cad clone` downloads)
|
|
12
|
+
could fill the memory. Each now refuses text of more than 64 MB before it parses any, saying what it is and the
|
|
13
|
+
limit (`the KiCad board is more than 64 MB of text, the most @rulecad/pcb reads`). A large board is a few MB, and so
|
|
14
|
+
are KiCad's largest models.
|
|
15
|
+
- Text within 64 MB could still be written to cost far more than its size. A model's face of millions of corners was
|
|
16
|
+
fanned into as many triangles (4 MB of text into most of a gigabyte), and every bracketed list of a board took some
|
|
17
|
+
200 bytes once read, however short: 12 MB of the one or 20 MB of the other filled the memory. `wrlMesh` now refuses
|
|
18
|
+
a model of more than 2 million triangles, or of more than three times as many points in one shape, and
|
|
19
|
+
`fromKicadPcb` a file of more than 4 million bracketed lists (a large board has a few hundred thousand), each saying
|
|
20
|
+
its limit: `the VRML model has more than 2,000,000 triangles, the most @rulecad/pcb reads`. What is within the
|
|
21
|
+
limits is read in less memory than before.
|
|
22
|
+
- Text made so that a reader began again from every place in it took time with the square of its size: 256 KB of
|
|
23
|
+
`coordIndex[` took `wrlMesh` ten seconds, 256 KB of `\"` took `fromKicadPcb` a minute and 128 KB of pads with no
|
|
24
|
+
`(layers` took `fromKicad` as long, so a file of the size that is read would have taken days. Each of the four
|
|
25
|
+
readers now takes time in proportion to its text, whatever is in it.
|
|
26
|
+
- `fromKicad` on a footprint whose courtyard has hundreds of thousands of corners gave `RangeError: Maximum call
|
|
27
|
+
stack size exceeded`. It is measured.
|
|
28
|
+
|
|
29
|
+
### Added
|
|
30
|
+
|
|
31
|
+
- `wrlMesh(text, unit, { triangles })` reads with another limit than 2 million triangles.
|
|
32
|
+
|
|
33
|
+
For someone upgrading: nothing to change unless you read KiCad text of more than 64 MB, a board of more than 4 million
|
|
34
|
+
bracketed lists or a model of more than 2 million triangles, which are now refused. No board, footprint or model
|
|
35
|
+
within those limits reads differently, and no printed or fabricated size moves.
|
|
36
|
+
|
|
37
|
+
## 0.7.1: routes are kept whole, and never written through a link
|
|
38
|
+
|
|
39
|
+
### Fixed
|
|
40
|
+
|
|
41
|
+
- A route the autorouter found was written into the route cache (`RULECAD_ROUTE_CACHE`, a workspace's `.cache/routes`
|
|
42
|
+
when cad builds a trusted board) in place: a run stopped part way through left the file cut short, and a file there
|
|
43
|
+
that was a link to a file elsewhere, as a workspace cloned from someone else may carry, had that file overwritten. A
|
|
44
|
+
route is now written to a new file beside its place and then renamed over it: the file is whole or not there, and a
|
|
45
|
+
link in its place is replaced, never written through.
|
|
46
|
+
|
|
47
|
+
For someone upgrading: nothing to do. No printed size moves.
|
|
48
|
+
|
|
6
49
|
## 0.7.0: unused names removed and ignored failures explained; a hole that breaks through a round or rounded board's edge fails; KiCad footprints with oval, slotted or turned pads import; back text mirrored; simple mistakes refused; needs Node 24 or later, as two of the libraries it draws with do; on a shaped board, rectangular pads and slotted holes are checked against the edge by their whole shape
|
|
7
50
|
|
|
8
51
|
### Changed
|
package/README.md
CHANGED
|
@@ -144,6 +144,14 @@ export function main(v) {
|
|
|
144
144
|
whose shape can't be shared (no licence), or comes only as STEP, as a box its size (`stepBounds`). The board's
|
|
145
145
|
build then has a `components` part beside `board`, each model where its part is (`partModels`), so fit checks
|
|
146
146
|
see the real parts. `--no-models` skips them.
|
|
147
|
+
- **Files too large to read:** the four readers of KiCad's files (`fromKicad`, `fromKicadPcb`, `wrlMesh`,
|
|
148
|
+
`stepBounds`) take text of up to 64 MB. More is refused, with what it is and the limit said ("the KiCad board is more
|
|
149
|
+
than 64 MB of text, the most @rulecad/pcb reads"): a large board is a few MB, and so are KiCad's largest models.
|
|
150
|
+
Text within that size can be written to unfold into far more than its size, so what it unfolds into is limited too:
|
|
151
|
+
`fromKicadPcb` refuses a file of more than 4 million bracketed lists (a large board has a few hundred thousand), and
|
|
152
|
+
`wrlMesh` a model of more than 2 million triangles, or of three times as many points in one shape ("the VRML model
|
|
153
|
+
has more than 2,000,000 triangles, the most @rulecad/pcb reads"). `wrlMesh(text, unit, { triangles })` reads with
|
|
154
|
+
another limit. Each of the four takes time in proportion to its text, whatever is in it.
|
|
147
155
|
- **Pad shapes:** round SMD pads stay round, and rounded pads keep their corner radius, so clearances are measured to
|
|
148
156
|
the real copper.
|
|
149
157
|
- **Castellated and oval pads:** a pad `{ castellated: true }` is a half hole on the board's edge (tracks join it on
|
package/circuit.mjs
CHANGED
|
@@ -2542,12 +2542,17 @@ async function routeCache(input) {
|
|
|
2542
2542
|
return null;
|
|
2543
2543
|
}
|
|
2544
2544
|
},
|
|
2545
|
+
// (written whole, to a new file beside it that is then renamed over it: a run stopped part way leaves no file cut
|
|
2546
|
+
// short, and a file there that is a link, one a workspace cloned from someone else may carry in its .cache/routes,
|
|
2547
|
+
// is replaced, never written through)
|
|
2545
2548
|
write: (traces) => {
|
|
2549
|
+
const temp = path.join(dir, `.${key}.${process.pid}-${crypto.randomBytes(6).toString('hex')}.tmp`);
|
|
2546
2550
|
try {
|
|
2547
2551
|
fs.mkdirSync(dir, { recursive: true });
|
|
2548
|
-
fs.writeFileSync(
|
|
2552
|
+
fs.writeFileSync(temp, JSON.stringify(traces), { flag: 'wx' });
|
|
2553
|
+
fs.renameSync(temp, file);
|
|
2549
2554
|
} catch {
|
|
2550
|
-
// (a cache that can't be written is only slower)
|
|
2555
|
+
fs.rmSync(temp, { force: true }); // (a cache that can't be written is only slower)
|
|
2551
2556
|
}
|
|
2552
2557
|
},
|
|
2553
2558
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/pcb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Circuit boards from rules: parts from rule sets or any standard footprint, quarter-turn rotation, nets and autorouting; keyboards with every matrix track drawn by rule. Gerbers, KiCad files and a JLCPCB BOM. Circuit JSON written directly: no React.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./pcb.mjs",
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@flatten-js/core": "1.6.14",
|
|
40
40
|
"@jscad/modeling": "^2.12.0",
|
|
41
|
-
"@rulecad/fixings": "^0.
|
|
42
|
-
"@rulecad/rules": "^0.
|
|
41
|
+
"@rulecad/fixings": "^0.3.1",
|
|
42
|
+
"@rulecad/rules": "^0.6.0",
|
|
43
43
|
"@resvg/resvg-js": "2.6.2",
|
|
44
44
|
"@rulecad/smooth": "^0.3.0",
|
|
45
45
|
"@tscircuit/capacity-autorouter": "0.0.982",
|
package/pcb.mjs
CHANGED
|
@@ -938,10 +938,11 @@ function kicadPlatedPad({ name, shape, rest, deg, turn, quarter, w, h, x, y, dri
|
|
|
938
938
|
* Unplated holes in holes: [{ x, y, d }].
|
|
939
939
|
* Pads named alike (several GND pads) keep their name; unnamed plated pads (mechanical) are left out. Text that isn't
|
|
940
940
|
* a footprint, a KiCad 5 footprint ("(module"), one with nothing on it, and a through-hole pad that isn't round turned
|
|
941
|
-
* by other than a quarter turn are refused.
|
|
941
|
+
* by other than a quarter turn are refused; so is text of more than 64 MB, the most that is read.
|
|
942
942
|
* @param {string} text @returns {{ pads: object[], holes?: object[], courtyard: { w: number, h: number } | null }}
|
|
943
943
|
*/
|
|
944
944
|
export function fromKicad(text) {
|
|
945
|
+
notTooLong(text, 'fromKicad: the KiCad footprint');
|
|
945
946
|
if (typeof text === 'string' && /^\s*\(module\s/.test(text))
|
|
946
947
|
throw new Error(
|
|
947
948
|
'fromKicad: this is a KiCad 5 footprint (it starts with "(module"): open it in KiCad 6 or later, save it, and import the saved file',
|
|
@@ -950,25 +951,55 @@ export function fromKicad(text) {
|
|
|
950
951
|
throw new Error('fromKicad: not a KiCad footprint (a .kicad_mod starts with "(footprint")');
|
|
951
952
|
const pads = [],
|
|
952
953
|
holes = [];
|
|
953
|
-
|
|
954
|
-
|
|
954
|
+
// Each pad's start, then all up to the "(layers" that follows it. (As one pattern, every pad with no "(layers" after
|
|
955
|
+
// it read on to the text's end: text of the most that is read, made of such pads, took a day.)
|
|
956
|
+
const start = /\(pad\s+"([^"]*)"\s+(smd|thru_hole|np_thru_hole)\s+(\w+)/g;
|
|
957
|
+
for (let m; (m = start.exec(text));) {
|
|
958
|
+
const end = text.indexOf('(layers', start.lastIndex);
|
|
959
|
+
if (end === -1) break;
|
|
960
|
+
const p = kicadPad(m[1], m[2], m[3], text.slice(start.lastIndex, end), holes);
|
|
955
961
|
if (p) pads.push(p);
|
|
962
|
+
start.lastIndex = end + '(layers'.length;
|
|
956
963
|
}
|
|
957
964
|
if (!pads.length && !holes.length) throw new Error('fromKicad: the footprint has no pads');
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
+
// The courtyard's corners, measured as they are read. (Kept as a list and spread as arguments to find the largest,
|
|
966
|
+
// a courtyard of a hundred thousand corners overflowed the stack.)
|
|
967
|
+
const min = [Infinity, Infinity],
|
|
968
|
+
max = [-Infinity, -Infinity];
|
|
969
|
+
let corners = 0;
|
|
970
|
+
for (const el of kicadElements(text))
|
|
971
|
+
if (/^\(fp_(?:line|rect|poly)/.test(el) && /\(layer\s+"F\.CrtYd"\)/.test(el))
|
|
972
|
+
for (const m of el.matchAll(/\((?:start|end|xy)\s+([-\d.]+)\s+([-\d.]+)\)/g)) {
|
|
973
|
+
corners++;
|
|
974
|
+
for (const k of [0, 1]) {
|
|
975
|
+
min[k] = Math.min(min[k], Number(m[k + 1]));
|
|
976
|
+
max[k] = Math.max(max[k], Number(m[k + 1]));
|
|
977
|
+
}
|
|
978
|
+
}
|
|
965
979
|
return {
|
|
966
980
|
pads,
|
|
967
981
|
...(holes.length ? { holes } : {}),
|
|
968
|
-
courtyard:
|
|
982
|
+
courtyard: corners ? { w: +(max[0] - min[0]).toFixed(3), h: +(max[1] - min[1]).toFixed(3) } : null,
|
|
969
983
|
};
|
|
970
984
|
}
|
|
971
985
|
|
|
986
|
+
// A footprint's text cut into its elements: at each line's end that only blank space parts from the "(fp_", "(pad",
|
|
987
|
+
// "(model" or "(zone" that starts the next (the line's end and the blank space left out). Each element's start is
|
|
988
|
+
// found, and the blank space before it walked back over once. (As one pattern, split at "a line's end, blank space,
|
|
989
|
+
// then an element", every line's end in a run of blank lines read the whole run again: 128 KB of them took a minute.)
|
|
990
|
+
function* kicadElements(text) {
|
|
991
|
+
const start = /\((?:fp_|pad|model|zone)/g;
|
|
992
|
+
let from = 0;
|
|
993
|
+
for (let m; (m = start.exec(text));) {
|
|
994
|
+
let cut = -1;
|
|
995
|
+
for (let i = m.index - 1; i >= from && /\s/.test(text[i]); i--) if (text[i] === '\n') cut = i;
|
|
996
|
+
if (cut === -1) continue;
|
|
997
|
+
yield text.slice(from, cut);
|
|
998
|
+
from = m.index;
|
|
999
|
+
}
|
|
1000
|
+
yield text.slice(from);
|
|
1001
|
+
}
|
|
1002
|
+
|
|
972
1003
|
/**
|
|
973
1004
|
* A part from footprint data (fromKicad's, kept in a rule set): pads as given, pins named by pad name (GND pads
|
|
974
1005
|
* alike are joined in the part); data.labels ({ pin1: 'GND' }) and data.mpn name them. o: as part() (rotation,
|
|
@@ -1000,19 +1031,79 @@ export function footprint(name, data, x, y, o = {}) {
|
|
|
1000
1031
|
// build boards from it with clone(): the circuit stays the source's, and the layout is redrawn by rules. sameCircuit()
|
|
1001
1032
|
// checks that a board still joins exactly the pins the source joins.
|
|
1002
1033
|
|
|
1034
|
+
// KiCad's files from elsewhere (a board cad clone downloads, its parts' models) are read whole, so each reader stops
|
|
1035
|
+
// at a size first: past 64 MB of text (a large board is a few MB, KiCad's largest models about as much), the file is
|
|
1036
|
+
// refused, named, before it is parsed into many times its size.
|
|
1037
|
+
const KICAD_TEXT_MB = 64;
|
|
1038
|
+
function notTooLong(text, what) {
|
|
1039
|
+
if (typeof text === 'string' && text.length > KICAD_TEXT_MB * 1024 * 1024)
|
|
1040
|
+
throw new Error(`${what} is more than ${KICAD_TEXT_MB} MB of text, the most @rulecad/pcb reads`);
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
// Text within that size can still be made to unfold: every bracketed list takes 60 bytes or more once read, however
|
|
1044
|
+
// short it is in the text ("(a)" is three characters), and took some 200 as lists were first read here, so that 20 MB
|
|
1045
|
+
// of them filled the memory. A large board has a few hundred thousand lists, and 64 MB of a filled zone's corners as
|
|
1046
|
+
// KiCad writes them some three million: past four million the file is refused, the limit said.
|
|
1047
|
+
const KICAD_LISTS = 4_000_000;
|
|
1048
|
+
const counted = (n) => n.toLocaleString('en-GB'); // 4,000,000
|
|
1049
|
+
|
|
1050
|
+
// Where the quoted string that starts at text[at] stops: at its closing quote or, if it has none, where it can't go on
|
|
1051
|
+
// (the text's end, or a backslash with no character on its line to take with it)
|
|
1052
|
+
function quoteEnd(text, at) {
|
|
1053
|
+
let i = at + 1;
|
|
1054
|
+
for (; i < text.length; i++) {
|
|
1055
|
+
const c = text.charCodeAt(i);
|
|
1056
|
+
if (c === 34) break; // "
|
|
1057
|
+
if (c === 92) {
|
|
1058
|
+
const next = text.charCodeAt(i + 1); // (NaN at the text's end)
|
|
1059
|
+
if (!(next >= 0) || next === 10 || next === 13 || next === 0x2028 || next === 0x2029) break;
|
|
1060
|
+
i++;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
return i;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
// A KiCad file's words, one at a time: "(", ")", a quoted string (its quotes kept) or a run of other characters. A
|
|
1067
|
+
// quote that is never closed is passed over, as it always was, and so is every quote up to where reading it stopped:
|
|
1068
|
+
// none of those can close either. (As one pattern for all four, each such quote read on to that place again: a board
|
|
1069
|
+
// of 256 KB of them took a minute, and one of the most that is read weeks.)
|
|
1070
|
+
function* kicadWords(text) {
|
|
1071
|
+
const word = /"|\(|\)|[^\s()"]+/g;
|
|
1072
|
+
let unclosed = 0; // quotes before here don't close
|
|
1073
|
+
for (let m; (m = word.exec(text));) {
|
|
1074
|
+
if (m[0] !== '"') yield m[0];
|
|
1075
|
+
else if (m.index >= unclosed) {
|
|
1076
|
+
const end = quoteEnd(text, m.index);
|
|
1077
|
+
if (text[end] === '"') {
|
|
1078
|
+
yield text.slice(m.index, end + 1);
|
|
1079
|
+
word.lastIndex = end + 1;
|
|
1080
|
+
} else unclosed = end;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1003
1085
|
// KiCad's files are S-expressions: (name value (child ...) ...) as nested arrays, strings unquoted
|
|
1004
1086
|
function sexpr(text) {
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1087
|
+
// What is read and not yet in a closed list, and where in it each list still open starts. A list is cut out of it
|
|
1088
|
+
// when it closes, at exactly its size: an array added to an item at a time keeps room for many more than it holds.
|
|
1089
|
+
const read = [],
|
|
1090
|
+
open = [];
|
|
1091
|
+
let lists = 0;
|
|
1092
|
+
// (word by word, not all the words at once: those are an array several times the text's size)
|
|
1093
|
+
for (const t of kicadWords(text)) {
|
|
1094
|
+
if (t === '(') {
|
|
1095
|
+
if (++lists > KICAD_LISTS)
|
|
1096
|
+
throw new Error(
|
|
1097
|
+
`the KiCad file has more than ${counted(KICAD_LISTS)} bracketed lists, the most @rulecad/pcb reads`,
|
|
1098
|
+
);
|
|
1099
|
+
open.push(read.length);
|
|
1100
|
+
} else if (t === ')') {
|
|
1101
|
+
if (!open.length) throw new Error('not a KiCad file: a ")" closes nothing');
|
|
1102
|
+
read.push(read.splice(open.pop()));
|
|
1103
|
+
} else read.push(t[0] === '"' ? t.slice(1, -1).replace(/\\(.)/g, '$1') : t);
|
|
1013
1104
|
}
|
|
1014
|
-
if (
|
|
1015
|
-
return
|
|
1105
|
+
if (open.length) throw new Error('not a KiCad file: a "(" is never closed');
|
|
1106
|
+
return read[0];
|
|
1016
1107
|
}
|
|
1017
1108
|
const kidsOf = (l, name) => l.filter((c) => Array.isArray(c) && c[0] === name);
|
|
1018
1109
|
const kidOf = (l, name) => kidsOf(l, name)[0];
|
|
@@ -1110,9 +1201,12 @@ function commonest(root) {
|
|
|
1110
1201
|
* nets: { name: ['U1.3', 'C4.1'] }, part and pad, for nets joining two pads or more. planes: nets the source fills on
|
|
1111
1202
|
* whole layers ({ GND: ['In1.Cu', 'B.Cu'] }). routing: the source's own tracks and vias (for keeping its layout of an
|
|
1112
1203
|
* area: moveGroup with keep). source: what the file says beyond the circuit (counts, its commonest via and clearance).
|
|
1204
|
+
* Text of more than 64 MB is refused, with that limit said: a large board is a few MB. So is text that holds more than
|
|
1205
|
+
* 4 million bracketed lists (a large board has a few hundred thousand).
|
|
1113
1206
|
* @param {string} text @returns {object}
|
|
1114
1207
|
*/
|
|
1115
1208
|
export function fromKicadPcb(text) {
|
|
1209
|
+
notTooLong(text, 'the KiCad board');
|
|
1116
1210
|
const root = sexpr(text);
|
|
1117
1211
|
if (root?.[0] !== 'kicad_pcb') throw new Error('not a KiCad board: a .kicad_pcb starts with (kicad_pcb');
|
|
1118
1212
|
if (!kidOf(root, 'footprint'))
|
|
@@ -2137,29 +2231,71 @@ export function sameCircuit(json, circuit, o = {}, added = []) {
|
|
|
2137
2231
|
}
|
|
2138
2232
|
|
|
2139
2233
|
// ---------- 3D models of parts ----------
|
|
2234
|
+
// A model within the text limit can still be made to unfold: a face of many corners is fanned into a triangle for
|
|
2235
|
+
// every two characters of its list, each some hundred bytes once read, so a model of 12 MB filled the memory. Two
|
|
2236
|
+
// million triangles (the most cad reads of any mesh; a part's model has thousands) and three points for each are the
|
|
2237
|
+
// most that is read: past that the model is refused, the limit said.
|
|
2238
|
+
const WRL_TRIANGLES = 2_000_000;
|
|
2239
|
+
// What a list in a model's text holds between its commas, one at a time (never all of them at once as an array)
|
|
2240
|
+
function* listed(list) {
|
|
2241
|
+
for (let at = 0; at <= list.length;) {
|
|
2242
|
+
const comma = list.indexOf(',', at),
|
|
2243
|
+
end = comma === -1 ? list.length : comma;
|
|
2244
|
+
yield list.slice(at, end);
|
|
2245
|
+
at = end + 1;
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2140
2249
|
/**
|
|
2141
2250
|
* A VRML model (KiCad's .wrl) as triangles, in mm: [[x, y, z] x3, ...]. KiCad's models are in tenths of an inch, z up
|
|
2142
2251
|
* out of the board, x and y as the footprint's (y towards its top). Faces with more than three corners are fanned.
|
|
2143
|
-
*
|
|
2252
|
+
* Text of more than 64 MB is refused, with that limit said, and so is a model of more than 2 million triangles, or
|
|
2253
|
+
* of three times as many points in one shape (a part's model has thousands).
|
|
2254
|
+
* @param {string} text @param {number} [unit] mm per model unit (KiCad's: 2.54)
|
|
2255
|
+
* @param {{ triangles?: number }} [limits] the most triangles to read, if not 2 million
|
|
2256
|
+
* @returns {number[][][]}
|
|
2144
2257
|
*/
|
|
2145
|
-
export function wrlMesh(text, unit = 2.54) {
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2258
|
+
export function wrlMesh(text, unit = 2.54, { triangles: most = WRL_TRIANGLES } = {}) {
|
|
2259
|
+
notTooLong(text, 'the VRML model');
|
|
2260
|
+
const out = [],
|
|
2261
|
+
tooMany = (n, what) => new Error(`the VRML model has more than ${counted(n)} ${what}, the most @rulecad/pcb reads`);
|
|
2262
|
+
// Each "coordIndex [ ... ]" with the "point [ ... ]" that follows it, found by walking the text once. (As one pattern
|
|
2263
|
+
// for the pair, every "coordIndex [" with no "]" or no points after it read on to the text's end: a model of 256 KB
|
|
2264
|
+
// of them took ten seconds, and one of the most that is read a week.)
|
|
2265
|
+
const index = /coordIndex\s*\[/g,
|
|
2266
|
+
points = /point\s*\[/g;
|
|
2267
|
+
for (let at = 0; ;) {
|
|
2268
|
+
index.lastIndex = at;
|
|
2269
|
+
if (!index.exec(text)) break;
|
|
2270
|
+
const corners = index.lastIndex,
|
|
2271
|
+
cornersEnd = text.indexOf(']', corners);
|
|
2272
|
+
if (cornersEnd === -1) break;
|
|
2273
|
+
points.lastIndex = cornersEnd + 1;
|
|
2274
|
+
if (!points.exec(text)) break;
|
|
2275
|
+
const places = points.lastIndex,
|
|
2276
|
+
placesEnd = text.indexOf(']', places);
|
|
2277
|
+
if (placesEnd === -1) break;
|
|
2278
|
+
at = placesEnd + 1;
|
|
2279
|
+
const pts = [];
|
|
2280
|
+
for (const p of listed(text.slice(places, placesEnd))) {
|
|
2281
|
+
// (a point is three numbers: what is too short to hold three is none, and no more than a fourth piece is cut,
|
|
2282
|
+
// which is enough to know that there are not three)
|
|
2283
|
+
if (p.length < 5) continue;
|
|
2284
|
+
const xyz = p
|
|
2285
|
+
.trim()
|
|
2286
|
+
.split(/\s+/, 4)
|
|
2287
|
+
.map((n) => Number(n) * unit);
|
|
2288
|
+
if (xyz.length === 3 && xyz.every(Number.isFinite) && pts.push(xyz) > 3 * most)
|
|
2289
|
+
throw tooMany(3 * most, 'points in one shape');
|
|
2290
|
+
}
|
|
2157
2291
|
let face = [];
|
|
2158
|
-
for (const
|
|
2292
|
+
for (const x of listed(text.slice(corners, cornersEnd))) {
|
|
2293
|
+
const n = Number(x.trim());
|
|
2159
2294
|
if (n === -1) {
|
|
2295
|
+
if (out.length + face.length - 2 > most) throw tooMany(most, 'triangles');
|
|
2160
2296
|
for (let i = 1; i + 1 < face.length; i++) out.push([pts[face[0]], pts[face[i]], pts[face[i + 1]]]);
|
|
2161
2297
|
face = [];
|
|
2162
|
-
} else if (Number.isInteger(n) && pts[n]
|
|
2298
|
+
} else if (Number.isInteger(n) && pts[n] && face.push(n) - 2 > most) throw tooMany(most, 'triangles in one face');
|
|
2163
2299
|
}
|
|
2164
2300
|
}
|
|
2165
2301
|
return out;
|
|
@@ -2167,10 +2303,12 @@ export function wrlMesh(text, unit = 2.54) {
|
|
|
2167
2303
|
|
|
2168
2304
|
/**
|
|
2169
2305
|
* A STEP model's size: the box round every point it lists ({ min: [x, y, z], max }, mm), with no geometry kernel. For a
|
|
2170
|
-
* model whose shape can't be shared (no licence) only its size is used, which is a fact about the part.
|
|
2306
|
+
* model whose shape can't be shared (no licence) only its size is used, which is a fact about the part. Text of more
|
|
2307
|
+
* than 64 MB is refused, with that limit said.
|
|
2171
2308
|
* @param {string} text @returns {{ min: number[], max: number[] } | null}
|
|
2172
2309
|
*/
|
|
2173
2310
|
export function stepBounds(text) {
|
|
2311
|
+
notTooLong(text, 'the STEP model');
|
|
2174
2312
|
const k = /SI_UNIT\s*\(\s*\.MILLI\.\s*,\s*\.METRE\.\s*\)/i.test(text)
|
|
2175
2313
|
? 1
|
|
2176
2314
|
: /INCH/i.test(text)
|