any2orca 0.12.1 → 0.14.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +7 -2
  3. package/dist/archive/meshPaint.d.ts +25 -0
  4. package/dist/archive/meshPaint.js +136 -0
  5. package/dist/archive/objectWorldBboxXY.d.ts +19 -1
  6. package/dist/archive/objectWorldBboxXY.js +32 -8
  7. package/dist/archive/paintColor.d.ts +21 -0
  8. package/dist/archive/paintColor.js +88 -0
  9. package/dist/archive/plateFilaments.d.ts +65 -0
  10. package/dist/archive/plateFilaments.js +363 -0
  11. package/dist/cli/commands/convert.d.ts +10 -0
  12. package/dist/cli/commands/convert.js +83 -19
  13. package/dist/cli/commands/inspect.d.ts +17 -0
  14. package/dist/cli/commands/inspect.js +75 -0
  15. package/dist/cli/main.js +43 -1
  16. package/dist/cli/surface.d.ts +29 -0
  17. package/dist/cli/surface.js +6 -0
  18. package/dist/engine/carrySourceLayerHeight.js +2 -2
  19. package/dist/engine/runConvertPipeline.d.ts +9 -2
  20. package/dist/engine/runConvertPipeline.js +24 -6
  21. package/dist/explain/filamentNotes.d.ts +17 -1
  22. package/dist/explain/filamentNotes.js +28 -12
  23. package/dist/explain/notes.d.ts +40 -0
  24. package/dist/explain/notes.js +74 -0
  25. package/dist/explain/plateNotes.d.ts +32 -0
  26. package/dist/explain/plateNotes.js +89 -0
  27. package/dist/index.d.ts +8 -1
  28. package/dist/index.js +4 -1
  29. package/dist/inspect.d.ts +9 -1
  30. package/dist/inspect.js +2 -1
  31. package/dist/problemReport.d.ts +68 -0
  32. package/dist/problemReport.js +312 -0
  33. package/dist/profiles.d.ts +12 -0
  34. package/dist/profiles.js +18 -1
  35. package/dist/project.d.ts +31 -8
  36. package/dist/project.js +91 -48
  37. package/dist/sources/bambuFamily.js +6 -1
  38. package/dist/sources/prusa/prusa.js +6 -1
  39. package/dist/sources/reader.d.ts +6 -0
  40. package/dist/version.js +1 -1
  41. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # any2orca
2
2
 
3
+ ## 0.14.0
4
+
5
+ ### Features
6
+
7
+ - **`any2orca inspect <file.3mf>` shows what a project holds without converting it.** The command line could convert and compare, but not show what the web app shows when a file is dropped on it. `inspect` prints the printer and slicer version the project was saved for, its process preset, nozzle size and layer height, every filament with its color, material and preset name, and each plate's objects and the filaments it uses, with how each is used and between which heights. `--json` prints the whole reading as data (`"format": "any2orca-inspect"`, `"version": 1`; `inspection` is what the library's `inspect()` returns), and `--max-unpacked` raises the size limit as on `convert`. It reads source projects and converted ones alike, and exits 2 for a file it can't read.
8
+ - **Which filaments each plate uses.** A project can have more filaments than the U1 has toolheads, and until now nothing said which of them a plate needs. `inspect()` now reads it per plate: each filament the plate uses, how (its objects, a part, paint, a filament set for a range of heights, supports and raft, a tool change at a layer) and between which heights, with the most the plate needs at the same height. It counts what the slicer counts: a part's own filament or its object's, none of a part's own filament when it is painted all over, and tool changes at a layer only on a plate that otherwise prints with one filament. A conversion's notes say when a plate needs more filaments at the same height than the U1 prints with (it can't print as it is), when a plate uses more than four but never more than four at once (it prints with filament changes at pauses), and, for a project with more than four filaments, which ones each plate uses. The first two are things to act on before printing, so they come near the top of the notes under their own topic, `plate-filaments`, right after a filament the U1 can't take as it is, and at the top of `report.warnings`. Checked against Snapmaker Orca 2.4.1's own slices of converted projects: the same filaments, and the same number at one height, on every plate. The number is an estimate from the model's geometry, not a slice: a filament counts from the lowest to the highest point it is used at. Converted files don't change. Library: `inspection.plateFilaments` and `report.plateFilaments` (`PlateFilaments`, `PlateFilamentUse`, `FILAMENT_USES`). CLI: the notes on `convert`, and a line per plate under `--verbose`. A new example, `examples/plate-filaments`, prints a loading plan and exits 1 on a plate the U1 can't print as it is.
9
+ - **A problem report to copy into a bug report.** Reporting a file that converts incorrectly used to mean describing it by hand, or attaching a model that isn't yours to share. Now the web app has **Copy report** on the result and on an error, the command line has `convert --report`, and the library has `problemReport()`: a short text with the any2orca and profile data versions, where it ran, the source project's slicer, printer and process preset, how many plates, objects and filaments it has, each filament's material, color and preset name, which filaments each plate uses, the options, the profile it was converted with or the error's code and the step it stopped at, and the conversion's notes. It is built on your machine and sent nowhere; you paste it where you choose. It never holds the file's name, object or plate names, a path or an error's message: preset names are cut at the "(name.3mf)" Bambu Studio adds to an edited preset, and a note's text is included only when its wording can't quote the file (any other note is listed by its topic). The bug report form now takes it as one box. Library: `problemReport()`, `describeEnvironment()`, `describeUserAgent()`, `PLAIN_NOTES`. The batch-script example writes one for each file that fails.
10
+ - **A note when a plate uses filament 17 or higher together with the filament numbered 16 lower.** Snapmaker Orca 2.4.1 can leave a filament numbered 17 or higher out of some of its layers, with no message, when the filament 16 numbers below it prints on the same plate (17 with 1, 18 with 2), and 2.4.0's source builds its tool order the same way; a fix is proposed in Snapmaker/OrcaSlicer#1208. any2orca keeps every filament and its number, so the file doesn't change: the conversion's notes now name the plate and the two filaments, say what the release does, and give the way around it (separate plates, or a number from 1 to 16 for the higher-numbered filament), so the sliced preview gets a look before printing. A filament 17 or higher that is alone on its plate, or there without that partner, gets no note: in test slices it printed every layer. The note follows the release the file is converted for: each release's entry in the profile catalog now says the highest filament number it prints in every layer whatever else is on the plate, read from that release's own source code when the profile data is generated, so a release with the fix has no limit and gets no note. Library: `ProfileCatalog.sharedLayerFilamentLimit` (16 for 2.4.0 and 2.4.1, null for a release without the limit). Profiles: `slicer.sharedLayerFilamentLimit` in each release's `index.json`. Converted files don't change.
11
+
12
+ ### Other
13
+
14
+ - **A Dockerfile for hosting the web app yourself.** `examples/self-hosting` gains a Dockerfile that builds the web app from the repository and serves it with the example's own nginx config, so a container has the same security headers, caching and 404 as the nginx and Apache configs there. Its tests check it against the repository on every run and, when asked for, build the image, run it and put the same header checks to the real web app. `docs/run-the-web-app.md` shows the two commands.
15
+ - Pins any2orca-profiles 0.5.0.
16
+
17
+ ## 0.13.0
18
+
19
+ ### Features
20
+
21
+ - `report.notes`, every note of a conversion in one list sorted by topic (`NOTE_TOPICS`: filament diameter, nozzle size, profile, filament presets, reading the file, settings, plates), each topic's headline first, each note once with the filament slots it names, in one style (a capital at the start, a period at the end, "0.4 mm"). It holds the sentences of `report.warnings`, `report.profileNotes` and `filamentNotes()`, which stay as they are, with their overlap now documented. The command line prints the notes with their topics (`note [nozzle]: ...`). Under `--nozzle match`, a filament tuned for another nozzle size now says it was converted with the U1's closest profile rather than "your nozzles". `filamentPresetNotes()` gives the filament naming option's sentences with their slots.
22
+
3
23
  ## 0.12.1
4
24
 
5
25
  ### Features
package/README.md CHANGED
@@ -11,6 +11,7 @@ npx any2orca convert model.3mf # writes model_U1.3mf next to it
11
11
  npx any2orca convert *.3mf --out-dir converted # also in Command Prompt and PowerShell
12
12
  npx any2orca convert bundle.zip # a zip of several .3mf files is a batch
13
13
  npx any2orca profiles # the profiles --profile accepts
14
+ npx any2orca inspect model.3mf # what a project holds: printer, filaments, each plate's filaments
14
15
  ```
15
16
 
16
17
  Useful options:
@@ -47,7 +48,7 @@ import { bundledProfileSource } from "any2orca/node";
47
48
 
48
49
  const { output, report } = await convert(await readFile("model.3mf"), { profiles: bundledProfileSource() });
49
50
  await writeFile("model_U1.3mf", output);
50
- console.log(report.profile.displayName, report.warnings);
51
+ console.log(report.profile.displayName, report.notes);
51
52
  ```
52
53
 
53
54
  In a UI, open once and convert as often as the user changes options:
@@ -61,6 +62,8 @@ const choice = await project.matchProfile(profiles);
61
62
  const { output, report } = await project.convert({ profiles, profileId: choice.profileId });
62
63
  ```
63
64
 
65
+ `info.plateFilaments` says which filaments each plate uses, how (its objects, a part, paint, a filament set for a range of heights, supports, a tool change at a layer) and between which heights, with the most a plate needs at the same height (`mostAtOnce`). The U1 prints with four at a time, so this is what to load for each plate; `report.plateFilaments` is the same for the converted file, and `report.notes` says in words when a plate needs more at once than the U1 has toolheads, or needs filament changes at pauses. The heights are read from the model, not from a slice, so `mostAtOnce` is an estimate: a filament counts from the lowest to the highest point it is used at. The [plate-filaments example](https://github.com/KuzuriAo/any2orca/tree/main/examples/plate-filaments) prints a loading plan from it. `catalog.sharedLayerFilamentLimit` is the highest filament number a Snapmaker Orca release prints in every layer shared with a lower-numbered filament (16 for 2.4.0 and 2.4.1, null for a release without the limit); a conversion notes a plate that goes past it, and [Snapmaker Orca releases](https://github.com/KuzuriAo/any2orca/blob/main/docs/snapmaker-orca-releases.md) says why.
66
+
64
67
  Profiles come from a source you pass in; the library never fetches anything on its own:
65
68
 
66
69
  | Source | Use |
@@ -137,7 +140,9 @@ A call that fails throws `Any2orcaError`. Its `code` is stable, so a UI can bran
137
140
  | `invalid_preset` | A preset or presets file that is malformed, over a limit, or can't be used with the options given |
138
141
  | `preset_base_unknown` | A preset can't be made from this file: it isn't set up for a profile in the catalog |
139
142
 
140
- The error's message, and every entry in `report.warnings`, is written for the person converting and doesn't name the library. Any app can show it as it is. The wording can change in any release, so branch on the error's `code`, or on whether there are any warnings at all, never on the text.
143
+ The error's message, and every entry in `report.notes`, is written for the person converting and doesn't name the library. Any app can show it as it is. `notes` is one list sorted by topic (`NOTE_TOPICS`: `filament-diameter`, `plate-filaments`, `nozzle`, `profile`, `filament-presets`, `source`, `settings`, `plates`), each topic's headline first, every note once, with the filament slots it names (`slots`, 1-based); `report.warnings`, `report.profileNotes` and `filamentNotes(report)` hold the same sentences in older shapes, so an app shows `notes` or those, not both. The wording can change in any release, so branch on the error's `code`, or on whether there are any notes at all, never on the text.
144
+
145
+ **A report to paste into a bug report.** `problemReport({ inspection, report, error, stage, options, catalog, app })` returns a text summary of what was converted and what came of it, for a "Copy report" button or a log: versions, where it ran (`describeEnvironment()`), the project's slicer, printer and counts, its filaments and each plate's, the options, the result or the error's code and step, and the notes. It holds no file name, object names, paths or error message, and includes a note's text only when the wording can't quote the file (`PLAIN_NOTES` lists them; any other note appears as its topic). It only builds the text: nothing is sent.
141
146
 
142
147
  ## Strict content security policies
143
148
 
@@ -0,0 +1,25 @@
1
+ /** Vertices kept per mesh for the heights of its paint (4 bytes each); a larger mesh reports its paint without heights. */
2
+ export declare const MAX_PAINT_VERTICES: number;
3
+ /** Whether `bytes` hold the paint attribute at all: most part files don't, and skip the scan. */
4
+ export declare function hasPaintAttribute(bytes: Uint8Array): boolean;
5
+ /** What one mesh's paint says. */
6
+ export interface MeshPaint {
7
+ /** Each painted filament (1-based) with the lowest and highest z it is painted at; NaN when the heights aren't known. */
8
+ painted: Map<number, [zMin: number, zMax: number]>;
9
+ /** Some triangle, or part of one, has no paint: the part's own filament shows there. */
10
+ unpainted: boolean;
11
+ /** How many of the mesh's triangles the scan read; 0 means the mesh wasn't found, so nothing is known about it. */
12
+ triangles: number;
13
+ }
14
+ /** One mesh to read: the z a vertex of it has on the plate, and where what is read goes. */
15
+ export interface PaintRequest {
16
+ zOf: (x: number, y: number, z: number) => number;
17
+ result: MeshPaint;
18
+ }
19
+ /**
20
+ * Reads the paint of the meshes named in `wanted` (by the mesh's object id; several requests for one mesh are the
21
+ * same mesh placed more than once) from one model file. A filament number above `filamentCount` counts as no paint,
22
+ * as the slicer treats it. Returns false when the file couldn't be scanned to its end; the requests then hold what
23
+ * was read up to there, and their meshes count as partly unpainted.
24
+ */
25
+ export declare function scanMeshPaint(bytes: Uint8Array, wanted: ReadonlyMap<string, readonly PaintRequest[]>, filamentCount: number, chunkBytes?: number): boolean;
@@ -0,0 +1,136 @@
1
+ // SPDX-License-Identifier: MIT
2
+ /**
3
+ * Which filaments are painted on a mesh, and between which heights: one pass over a model file's bytes, in chunks,
4
+ * so a mesh larger than the engine's longest string is read like any other (see streamObjectVertices.ts).
5
+ */
6
+ import { visitPaintLeaves } from "./paintColor.js";
7
+ const CHUNK_BYTES = 8 * 1024 * 1024;
8
+ /** A tag longer than this many chunks isn't a mesh's: the scan gives up on the file rather than grow its buffer without end. */
9
+ const MAX_CARRY_CHUNKS = 4;
10
+ /** Vertices kept per mesh for the heights of its paint (4 bytes each); a larger mesh reports its paint without heights. */
11
+ export const MAX_PAINT_VERTICES = 32 * 1024 * 1024;
12
+ const PAINT_ATTRIBUTE = 'paint_color="';
13
+ /** Whether `bytes` hold the paint attribute at all: most part files don't, and skip the scan. */
14
+ export function hasPaintAttribute(bytes) {
15
+ const first = PAINT_ATTRIBUTE.charCodeAt(0);
16
+ const length = PAINT_ATTRIBUTE.length;
17
+ const end = bytes.length - length;
18
+ outer: for (let i = bytes.indexOf(first); i !== -1 && i <= end; i = bytes.indexOf(first, i + 1)) {
19
+ for (let j = 1; j < length; j++)
20
+ if (bytes[i + j] !== PAINT_ATTRIBUTE.charCodeAt(j))
21
+ continue outer;
22
+ return true;
23
+ }
24
+ return false;
25
+ }
26
+ const TOKEN = /<object id="([^"]{1,64})"|<\/object>|<vertex x="([^"]{1,40})" y="([^"]{1,40})" z="([^"]{1,40})"|<triangle v1="(\d{1,10})" v2="(\d{1,10})" v3="(\d{1,10})"([^>]*)>/g;
27
+ const PAINT_VALUE = /(?:^|\s)paint_color="([^"]*)"/;
28
+ /**
29
+ * Reads the paint of the meshes named in `wanted` (by the mesh's object id; several requests for one mesh are the
30
+ * same mesh placed more than once) from one model file. A filament number above `filamentCount` counts as no paint,
31
+ * as the slicer treats it. Returns false when the file couldn't be scanned to its end; the requests then hold what
32
+ * was read up to there, and their meshes count as partly unpainted.
33
+ */
34
+ export function scanMeshPaint(bytes, wanted, filamentCount, chunkBytes = CHUNK_BYTES) {
35
+ const decoder = new TextDecoder("utf-8");
36
+ let carry = "";
37
+ const state = { open: null };
38
+ // The request a triangle's leaves go to; one visitor for the whole scan, not one per triangle.
39
+ let result = { painted: new Map(), unpainted: false, triangles: 0 };
40
+ const visit = (filament, zMin, zMax) => {
41
+ if (filament === 0 || filament > filamentCount) {
42
+ result.unpainted = true;
43
+ return;
44
+ }
45
+ const known = result.painted.get(filament);
46
+ if (!known) {
47
+ result.painted.set(filament, [zMin, zMax]);
48
+ return;
49
+ }
50
+ // NaN (a height that isn't known) stays NaN, so one unknown corner marks the filament.
51
+ if (!(zMin >= known[0]))
52
+ known[0] = Math.min(known[0], zMin);
53
+ if (!(zMax <= known[1]))
54
+ known[1] = Math.max(known[1], zMax);
55
+ };
56
+ for (let offset = 0; offset < bytes.length; offset += chunkBytes) {
57
+ const last = offset + chunkBytes >= bytes.length;
58
+ const text = carry + decoder.decode(bytes.subarray(offset, offset + chunkBytes), { stream: !last });
59
+ // Up to the last tag that may be cut off by the chunk boundary.
60
+ const upTo = last ? text.length : Math.max(0, text.lastIndexOf("<"));
61
+ TOKEN.lastIndex = 0;
62
+ for (;;) {
63
+ if (!state.open) {
64
+ // Outside a wanted mesh only the next object matters: skip its neighbors' vertices unparsed.
65
+ const at = text.indexOf('<object id="', TOKEN.lastIndex);
66
+ if (at === -1 || at >= upTo)
67
+ break;
68
+ TOKEN.lastIndex = at;
69
+ }
70
+ const m = TOKEN.exec(text);
71
+ if (!m || m.index >= upTo)
72
+ break;
73
+ const open = state.open;
74
+ if (m[1] !== undefined) {
75
+ const requests = wanted.get(m[1]);
76
+ state.open = requests ? { requests, heights: requests.map(() => new Float32Array(1024)), vertexCount: 0, tooManyVertices: false } : null;
77
+ }
78
+ else if (m[2] !== undefined) {
79
+ if (!open)
80
+ continue;
81
+ if (open.vertexCount >= MAX_PAINT_VERTICES) {
82
+ open.tooManyVertices = true;
83
+ continue;
84
+ }
85
+ const x = Number(m[2]);
86
+ const y = Number(m[3]);
87
+ const z = Number(m[4]);
88
+ for (let r = 0; r < open.requests.length; r++) {
89
+ let array = open.heights[r];
90
+ if (open.vertexCount >= array.length) {
91
+ const grown = new Float32Array(Math.min(MAX_PAINT_VERTICES, array.length * 2));
92
+ grown.set(array);
93
+ open.heights[r] = array = grown;
94
+ }
95
+ array[open.vertexCount] = open.requests[r].zOf(x, y, z);
96
+ }
97
+ open.vertexCount++;
98
+ }
99
+ else if (m[5] !== undefined) {
100
+ if (!open)
101
+ continue;
102
+ const attrs = m[8];
103
+ // Most triangles of most meshes have no paint: look for the attribute before running a pattern over it.
104
+ const code = attrs.length > PAINT_ATTRIBUTE.length && attrs.includes(PAINT_ATTRIBUTE) ? PAINT_VALUE.exec(attrs)?.[1] : undefined;
105
+ const known = !open.tooManyVertices;
106
+ const i1 = Number(m[5]);
107
+ const i2 = Number(m[6]);
108
+ const i3 = Number(m[7]);
109
+ const inRange = known && i1 < open.vertexCount && i2 < open.vertexCount && i3 < open.vertexCount;
110
+ for (let r = 0; r < open.requests.length; r++) {
111
+ result = open.requests[r].result;
112
+ result.triangles++;
113
+ if (!code) {
114
+ result.unpainted = true;
115
+ continue;
116
+ }
117
+ const array = open.heights[r];
118
+ const decoded = inRange ? visitPaintLeaves(code, array[i1], array[i2], array[i3], visit) : visitPaintLeaves(code, NaN, NaN, NaN, visit);
119
+ if (!decoded)
120
+ result.unpainted = true;
121
+ }
122
+ }
123
+ else {
124
+ state.open = null;
125
+ }
126
+ }
127
+ carry = text.slice(upTo);
128
+ if (carry.length > Math.max(MAX_CARRY_CHUNKS * chunkBytes, 4096)) {
129
+ for (const requests of wanted.values())
130
+ for (const request of requests)
131
+ request.result.unpainted = true;
132
+ return false;
133
+ }
134
+ }
135
+ return true;
136
+ }
@@ -1,2 +1,20 @@
1
+ import { type Mat3, type Vec3 } from "./transform.js";
1
2
  export type Bbox = [minX: number, maxX: number, minY: number, maxY: number];
2
- export declare function objectWorldBboxXY(objectId: string, topModelText: string, partFileCache: Map<string, string>, zipEntries: Readonly<Record<string, Uint8Array>>): Bbox | null;
3
+ /**
4
+ * One part of a top-level object as the model places it: the mesh it is (its object id, and the part file holding it,
5
+ * or null when the mesh is in the main model file), the transform that puts it on the plate, and its lowest and
6
+ * highest z there (NaN when no vertex was found). An object with no parts of its own is one part with its own id.
7
+ */
8
+ export interface PartPlacement {
9
+ id: string;
10
+ path: string | null;
11
+ R: Mat3;
12
+ T: Vec3;
13
+ zMin: number;
14
+ zMax: number;
15
+ }
16
+ /**
17
+ * @param parts when given, receives each part's placement and height from the same pass over the vertices
18
+ * (see {@link PartPlacement}).
19
+ */
20
+ export declare function objectWorldBboxXY(objectId: string, topModelText: string, partFileCache: Map<string, string>, zipEntries: Readonly<Record<string, Uint8Array>>, parts?: PartPlacement[]): Bbox | null;
@@ -52,7 +52,11 @@ function componentAttr(attrs, name) {
52
52
  const match = new RegExp(`${escapeRegExp(name)}="([^"]*)"`).exec(attrs);
53
53
  return match ? match[1] : null;
54
54
  }
55
- export function objectWorldBboxXY(objectId, topModelText, partFileCache, zipEntries) {
55
+ /**
56
+ * @param parts when given, receives each part's placement and height from the same pass over the vertices
57
+ * (see {@link PartPlacement}).
58
+ */
59
+ export function objectWorldBboxXY(objectId, topModelText, partFileCache, zipEntries, parts) {
56
60
  const objBlock = findObjectBlock(objectId, topModelText);
57
61
  const itemMatch = itemTransform(objectId, topModelText);
58
62
  if (!objBlock || !itemMatch)
@@ -70,7 +74,9 @@ export function objectWorldBboxXY(objectId, topModelText, partFileCache, zipEntr
70
74
  // just the "split across separate part files" one) -- when absent,
71
75
  // resolve objectid against topModelText itself instead of an external part.
72
76
  let anyComponent = false;
73
- const include = ([wx, wy]) => {
77
+ let zLow = Infinity;
78
+ let zHigh = -Infinity;
79
+ const include = ([wx, wy, wz]) => {
74
80
  if (wx < minX)
75
81
  minX = wx;
76
82
  if (wx > maxX)
@@ -79,8 +85,18 @@ export function objectWorldBboxXY(objectId, topModelText, partFileCache, zipEntr
79
85
  minY = wy;
80
86
  if (wy > maxY)
81
87
  maxY = wy;
88
+ if (wz < zLow)
89
+ zLow = wz;
90
+ if (wz > zHigh)
91
+ zHigh = wz;
82
92
  count++;
83
93
  };
94
+ /** Records the part just scanned, with the height the pass found for it. */
95
+ const closePart = (id, path, R, T) => {
96
+ parts?.push({ id, path, R, T, zMin: zLow <= zHigh ? zLow : NaN, zMax: zLow <= zHigh ? zHigh : NaN });
97
+ zLow = Infinity;
98
+ zHigh = -Infinity;
99
+ };
84
100
  for (const { attrs } of xmlTags(objBlock, "component")) {
85
101
  if (!isSelfClosingTag(attrs))
86
102
  continue;
@@ -100,9 +116,9 @@ export function objectWorldBboxXY(objectId, topModelText, partFileCache, zipEntr
100
116
  // A very large mesh is scanned from its bytes: decoding it whole can exceed the engine's longest string.
101
117
  if (bytes && bytes.length > LARGE_PART_BYTES) {
102
118
  // Kept aside until the block is known to close, as the whole-file path only uses a closed block.
103
- let [aMinX, aMaxX, aMinY, aMaxY, aCount] = [Infinity, -Infinity, Infinity, -Infinity, 0];
119
+ let [aMinX, aMaxX, aMinY, aMaxY, aMinZ, aMaxZ, aCount] = [Infinity, -Infinity, Infinity, -Infinity, Infinity, -Infinity, 0];
104
120
  const closed = forEachObjectVertex(bytes, subObjId, (v) => {
105
- const [wx, wy] = applyTransform(v, R, T);
121
+ const [wx, wy, wz] = applyTransform(v, R, T);
106
122
  if (wx < aMinX)
107
123
  aMinX = wx;
108
124
  if (wx > aMaxX)
@@ -111,6 +127,10 @@ export function objectWorldBboxXY(objectId, topModelText, partFileCache, zipEntr
111
127
  aMinY = wy;
112
128
  if (wy > aMaxY)
113
129
  aMaxY = wy;
130
+ if (wz < aMinZ)
131
+ aMinZ = wz;
132
+ if (wz > aMaxZ)
133
+ aMaxZ = wz;
114
134
  aCount++;
115
135
  });
116
136
  if (closed && aCount > 0) {
@@ -119,7 +139,10 @@ export function objectWorldBboxXY(objectId, topModelText, partFileCache, zipEntr
119
139
  minY = Math.min(minY, aMinY);
120
140
  maxY = Math.max(maxY, aMaxY);
121
141
  count += aCount;
142
+ zLow = aMinZ;
143
+ zHigh = aMaxZ;
122
144
  }
145
+ closePart(subObjId, key, R, T);
123
146
  continue;
124
147
  }
125
148
  if (!partFileCache.has(key)) {
@@ -130,15 +153,16 @@ export function objectWorldBboxXY(objectId, topModelText, partFileCache, zipEntr
130
153
  else {
131
154
  subBlock = findObjectBlock(subObjId, topModelText);
132
155
  }
133
- if (!subBlock)
134
- continue;
135
- for (const v of parseVertices(subBlock))
136
- include(applyTransform(v, R, T));
156
+ if (subBlock)
157
+ for (const v of parseVertices(subBlock))
158
+ include(applyTransform(v, R, T));
159
+ closePart(subObjId, path ? path.replace(/^\/+/, "") : null, R, T);
137
160
  }
138
161
  if (!anyComponent) {
139
162
  // Direct mesh in the same object definition (no components at all).
140
163
  for (const v of parseVertices(objBlock))
141
164
  include(applyTransform(v, itemR, itemT));
165
+ closePart(objectId, null, itemR, itemT);
142
166
  }
143
167
  if (count === 0)
144
168
  return null;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * A triangle's `paint_color` attribute: which filaments are painted on it, and at what heights.
3
+ *
4
+ * The slicer lets a painted triangle be split, again and again, into smaller ones, each with its own filament. The
5
+ * attribute is that tree, written as hex digits with the first digit read last. Each digit is one node: its low two
6
+ * bits say how many sides are split (0 for a leaf). A leaf's high two bits are its filament (0 = not painted, 1, 2),
7
+ * or 3 for "more digits follow": each further digit adds 15 while it is F, and the last one plus 3 is the filament.
8
+ * A split node's high two bits say which side is special; its two, three or four children follow, last child first.
9
+ *
10
+ * The file is untrusted, so the walk is bounded: the digits end, the depth is capped, and a tree that doesn't decode
11
+ * is reported as such rather than guessed at.
12
+ */
13
+ /** Called for every leaf: its filament (0 = not painted) and the lowest and highest z of its triangle. */
14
+ export type PaintLeafVisitor = (filament: number, zMin: number, zMax: number) => void;
15
+ /**
16
+ * Walks one triangle's paint tree, given the z of its three corners in the order the triangle lists them.
17
+ * Returns false when the attribute doesn't decode (a digit that isn't hex, too few digits, too deep); leaves
18
+ * visited before that point were real, and the caller decides what to do with the rest of the triangle.
19
+ * `visit` must not start another walk.
20
+ */
21
+ export declare function visitPaintLeaves(code: string, z1: number, z2: number, z3: number, visit: PaintLeafVisitor): boolean;
@@ -0,0 +1,88 @@
1
+ // SPDX-License-Identifier: MIT
2
+ /**
3
+ * A triangle's `paint_color` attribute: which filaments are painted on it, and at what heights.
4
+ *
5
+ * The slicer lets a painted triangle be split, again and again, into smaller ones, each with its own filament. The
6
+ * attribute is that tree, written as hex digits with the first digit read last. Each digit is one node: its low two
7
+ * bits say how many sides are split (0 for a leaf). A leaf's high two bits are its filament (0 = not painted, 1, 2),
8
+ * or 3 for "more digits follow": each further digit adds 15 while it is F, and the last one plus 3 is the filament.
9
+ * A split node's high two bits say which side is special; its two, three or four children follow, last child first.
10
+ *
11
+ * The file is untrusted, so the walk is bounded: the digits end, the depth is capped, and a tree that doesn't decode
12
+ * is reported as such rather than guessed at.
13
+ */
14
+ /** Deeper than any tree the slicer makes: each level halves the edges, and it stops near 0.1 mm. */
15
+ const MAX_DEPTH = 48;
16
+ /** The most "F" digits a leaf may chain: filament numbers past 300 don't exist. */
17
+ const MAX_EXTENSIONS = 20;
18
+ function hexDigit(code) {
19
+ if (code >= 0x30 && code <= 0x39)
20
+ return code - 0x30;
21
+ if (code >= 0x41 && code <= 0x46)
22
+ return code - 0x41 + 10;
23
+ if (code >= 0x61 && code <= 0x66)
24
+ return code - 0x61 + 10;
25
+ return -1;
26
+ }
27
+ // One walk at a time (the walk is synchronous), kept here rather than in closures made for every triangle: a
28
+ // painted model has millions of them.
29
+ let digits = "";
30
+ let at = -1;
31
+ let visitLeaf = () => { };
32
+ function nextDigit() {
33
+ return at < 0 ? -1 : hexDigit(digits.charCodeAt(at--));
34
+ }
35
+ function walk(a, b, c, depth) {
36
+ const node = nextDigit();
37
+ if (node < 0)
38
+ return false;
39
+ const splitSides = node & 0b11;
40
+ if (splitSides === 0) {
41
+ let filament = node >> 2;
42
+ if (filament === 3) {
43
+ let digit = nextDigit();
44
+ let chained = 0;
45
+ while (digit === 15) {
46
+ if (++chained > MAX_EXTENSIONS)
47
+ return false;
48
+ digit = nextDigit();
49
+ }
50
+ if (digit < 0)
51
+ return false;
52
+ filament = digit + 15 * chained + 3;
53
+ }
54
+ visitLeaf(filament, Math.min(a, b, c), Math.max(a, b, c));
55
+ return true;
56
+ }
57
+ if (depth >= MAX_DEPTH)
58
+ return false;
59
+ // The corners, starting at the special side.
60
+ const special = (node >> 2) % 3;
61
+ const v0 = special === 0 ? a : special === 1 ? b : c;
62
+ const v1 = special === 0 ? b : special === 1 ? c : a;
63
+ const v2 = special === 0 ? c : special === 1 ? a : b;
64
+ // A midpoint's z is the mean of its edge's ends. Children are stored last first.
65
+ if (splitSides === 1) {
66
+ const m12 = (v1 + v2) / 2;
67
+ return walk(m12, v2, v0, depth + 1) && walk(v0, v1, m12, depth + 1);
68
+ }
69
+ const m01 = (v0 + v1) / 2;
70
+ const m02 = (v0 + v2) / 2;
71
+ if (splitSides === 2) {
72
+ return walk(v1, v2, m02, depth + 1) && walk(m01, v1, m02, depth + 1) && walk(v0, m01, m02, depth + 1);
73
+ }
74
+ const m12 = (v1 + v2) / 2;
75
+ return walk(m01, m12, m02, depth + 1) && walk(m12, v2, m02, depth + 1) && walk(m01, v1, m12, depth + 1) && walk(v0, m01, m02, depth + 1);
76
+ }
77
+ /**
78
+ * Walks one triangle's paint tree, given the z of its three corners in the order the triangle lists them.
79
+ * Returns false when the attribute doesn't decode (a digit that isn't hex, too few digits, too deep); leaves
80
+ * visited before that point were real, and the caller decides what to do with the rest of the triangle.
81
+ * `visit` must not start another walk.
82
+ */
83
+ export function visitPaintLeaves(code, z1, z2, z3, visit) {
84
+ digits = code;
85
+ at = code.length - 1;
86
+ visitLeaf = visit;
87
+ return walk(z1, z2, z3, 0);
88
+ }
@@ -0,0 +1,65 @@
1
+ import { type MeshPaint } from "./meshPaint.js";
2
+ import type { PartPlacement } from "./objectWorldBboxXY.js";
3
+ /** How a plate uses a filament, in the order `uses` lists them. */
4
+ export declare const FILAMENT_USES: readonly ["object", "part", "paint", "height-range", "support", "layer-change"];
5
+ /** The uses this release of the library reports. */
6
+ export type KnownFilamentUse = (typeof FILAMENT_USES)[number];
7
+ /**
8
+ * One of {@link FILAMENT_USES}, or a use a later any2orca release added: "object" is an object's own filament,
9
+ * "part" a filament set on one of its parts, "paint" a painted color, "height-range" a filament set for a range of
10
+ * an object's height, "support" its supports or raft, "layer-change" a tool change at a layer.
11
+ */
12
+ export type FilamentUse = KnownFilamentUse | (string & {});
13
+ /** One filament a plate uses. */
14
+ export interface PlateFilamentUse {
15
+ /** The filament's slot, counting from 1 as slicers show them. */
16
+ slot: number;
17
+ /** Every way the plate uses it, in the order of {@link FILAMENT_USES}. */
18
+ uses: FilamentUse[];
19
+ /** The lowest height it is used at, in mm above the plate. */
20
+ from: number;
21
+ /** The highest height it is used at, in mm above the plate. */
22
+ to: number;
23
+ }
24
+ /** The filaments one plate uses. */
25
+ export interface PlateFilaments {
26
+ /** The plate's number, counting from 1. */
27
+ plate: number;
28
+ /** Each filament the plate uses, by slot. Empty for a plate with no objects. */
29
+ filaments: PlateFilamentUse[];
30
+ /**
31
+ * The most filaments the plate needs at the same height: an estimate from the model's geometry, in which a
32
+ * filament counts for the whole span between the lowest and highest point it is used at.
33
+ */
34
+ mostAtOnce: number;
35
+ }
36
+ /** What {@link readPlateFilaments} reads a project from. */
37
+ export interface PlateFilamentsInput {
38
+ zipEntries: Readonly<Record<string, Uint8Array>>;
39
+ /** The settings to read by: the source project's for what the creator's file says, the converted project's for what the U1 prints. */
40
+ projectSettings: Readonly<Record<string, unknown>>;
41
+ /** The project's objects with the plate each is on. */
42
+ objects: ReadonlyArray<{
43
+ id: string;
44
+ plate?: number;
45
+ }>;
46
+ /** Each object's parts as the model places them (the readers' vertex pass). */
47
+ partPlacements?: ReadonlyMap<string, readonly PartPlacement[]>;
48
+ /** How many plates the project has, empty ones included. */
49
+ plateCount: number;
50
+ /** The parts' paint, from {@link readPartPaint}: read once per project, since it scans the meshes. */
51
+ paint: PartPaint;
52
+ }
53
+ /** Each painted part's paint, by its placement. */
54
+ export type PartPaint = ReadonlyMap<PartPlacement, MeshPaint>;
55
+ /** How many filaments a project's settings have. */
56
+ export declare function filamentCountOf(projectSettings: Readonly<Record<string, unknown>>): number;
57
+ /**
58
+ * The paint of every part whose model file has any: one pass over each such file. A painted filament number above
59
+ * `filamentCount` counts as no paint, as the slicer treats it.
60
+ */
61
+ export declare function readPartPaint(zipEntries: Readonly<Record<string, Uint8Array>>, objects: ReadonlyArray<{
62
+ id: string;
63
+ }>, partPlacements: ReadonlyMap<string, readonly PartPlacement[]> | undefined, filamentCount: number): PartPaint;
64
+ /** Every plate's filaments; see the module comment for what counts and how the heights are read. */
65
+ export declare function readPlateFilaments(input: PlateFilamentsInput): PlateFilaments[];