any2orca 0.10.0 → 0.12.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 (49) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +2 -2
  3. package/dist/cli/commands/convert.d.ts +2 -1
  4. package/dist/cli/commands/convert.js +1 -0
  5. package/dist/cli/commands/nozzle.d.ts +9 -0
  6. package/dist/cli/commands/nozzle.js +38 -0
  7. package/dist/cli/config.d.ts +4 -0
  8. package/dist/cli/config.js +5 -0
  9. package/dist/cli/main.js +41 -0
  10. package/dist/cli/surface.d.ts +11 -0
  11. package/dist/cli/surface.js +3 -0
  12. package/dist/diff/diffSettings.js +14 -5
  13. package/dist/engine/applyFilamentComplianceMode.d.ts +3 -1
  14. package/dist/engine/applyFilamentComplianceMode.js +5 -1
  15. package/dist/engine/applyFilamentFlowVariants.d.ts +2 -2
  16. package/dist/engine/applyFilamentFlowVariants.js +4 -4
  17. package/dist/engine/applyFilamentPresets.d.ts +50 -5
  18. package/dist/engine/applyFilamentPresets.js +162 -39
  19. package/dist/engine/buildProjectSettings.js +4 -2
  20. package/dist/engine/detectNozzleDiameter.d.ts +28 -0
  21. package/dist/engine/detectNozzleDiameter.js +44 -0
  22. package/dist/engine/detectVariantIndex.d.ts +13 -0
  23. package/dist/engine/detectVariantIndex.js +25 -13
  24. package/dist/engine/extractFilamentSlots.js +7 -1
  25. package/dist/engine/filamentVariantLayout.d.ts +31 -3
  26. package/dist/engine/filamentVariantLayout.js +54 -2
  27. package/dist/engine/fillFilamentSlots.js +2 -1
  28. package/dist/engine/padFilamentsToToolheads.d.ts +20 -0
  29. package/dist/engine/padFilamentsToToolheads.js +82 -0
  30. package/dist/engine/policy.d.ts +17 -0
  31. package/dist/engine/policy.js +45 -0
  32. package/dist/engine/runConvertPipeline.d.ts +15 -0
  33. package/dist/engine/runConvertPipeline.js +136 -9
  34. package/dist/engine/setFilamentDiameter.d.ts +13 -0
  35. package/dist/engine/setFilamentDiameter.js +30 -0
  36. package/dist/explain/whatChanged.js +2 -1
  37. package/dist/index.d.ts +3 -3
  38. package/dist/index.js +1 -1
  39. package/dist/inspect.d.ts +8 -2
  40. package/dist/inspect.js +2 -1
  41. package/dist/profiles.d.ts +9 -0
  42. package/dist/profiles.js +10 -0
  43. package/dist/project.d.ts +42 -8
  44. package/dist/project.js +71 -19
  45. package/dist/stamp.d.ts +12 -0
  46. package/dist/stamp.js +7 -0
  47. package/dist/version.js +1 -1
  48. package/dist/workerProtocol.d.ts +2 -0
  49. package/package.json +3 -3
@@ -3,12 +3,21 @@
3
3
  * The Snapmaker and Generic filament options: a slot renamed to a preset Snapmaker Orca ships takes that
4
4
  * preset's own settings, standard and high-flow values and its G-code included, so the file, Snapmaker Orca's
5
5
  * app (which resets a filament whose name matches a shipped preset) and its command line (which uses the file's
6
- * values) all agree. And every filament's entry in `different_settings_to_system` lists every filament setting,
7
- * so Snapmaker Orca keeps what the file says when it opens it, the creator's values under Original included.
6
+ * values) all agree. And each filament's entry in `different_settings_to_system` names the settings that differ
7
+ * from the preset the app resets that filament to, so the app keeps what the file says, the creator's values
8
+ * under Original included, and warns about G-code only where it really differs.
8
9
  */
9
10
  import { SETTINGS_CATALOG } from "../diff/settingsCatalog.js";
10
11
  import { FILAMENT_FLOW_VARIANT_KEYS } from "./applyFilamentFlowVariants.js";
12
+ import { resolveFilamentName } from "./filamentComplianceNames.js";
11
13
  const VARIANT_KEYS = new Set(FILAMENT_FLOW_VARIANT_KEYS);
14
+ /** Filament settings that are project state rather than a preset's (Snapmaker Orca's presets hold no color), so never named. */
15
+ const PROJECT_STATE_KEYS = new Set(["filament_colour"]);
16
+ /**
17
+ * Named for a filament no U1 preset fits: the app resets it to an installed preset of the same name, and one made for
18
+ * another printer (Bambu's, say) lists only that maker's printers, so the filament couldn't be used with the U1.
19
+ */
20
+ const COMPATIBILITY_KEYS = ["compatible_printers"];
12
21
  /** One preset's settings: the values the printer's presets share, with its own over them. Undefined when there are none. */
13
22
  export function presetSettingsFor(presets, name) {
14
23
  if (!presets || !Object.hasOwn(presets.presets, name))
@@ -27,58 +36,172 @@ export function presetChanges(values, creator) {
27
36
  }
28
37
  return out;
29
38
  }
39
+ /** Writes `values` into one slot: the project's per-filament lists, and the embedded preset file named `name`. */
40
+ function writeSlot(settings, files, slot, name, values, slotCount) {
41
+ for (const [key, value] of Object.entries(values)) {
42
+ const list = settings[key];
43
+ if (!Array.isArray(list))
44
+ continue;
45
+ const next = [...list];
46
+ if (VARIANT_KEYS.has(key) && next.length === slotCount * 2) {
47
+ next[slot * 2] = value[0];
48
+ next[slot * 2 + 1] = value[1] ?? value[0];
49
+ }
50
+ else if (next.length === slotCount) {
51
+ next[slot] = value[0];
52
+ }
53
+ else
54
+ continue;
55
+ settings[key] = next;
56
+ }
57
+ for (const cfg of Object.values(files)) {
58
+ if (cfg.name !== name)
59
+ continue;
60
+ for (const [key, value] of Object.entries(values)) {
61
+ const own = cfg[key];
62
+ // syncEmbeddedPresets lays these out as the project holds them once every filament step has run.
63
+ cfg[key] = Array.isArray(own) && own.length === 1 ? [value[0]] : [...value];
64
+ }
65
+ }
66
+ }
30
67
  /**
31
68
  * Writes each applied preset's settings into its slot: the project's per-filament lists (both values of a
32
69
  * per-variant setting where the nozzle has a high-flow option) and the slot's embedded preset file. Runs after
33
70
  * every other filament step, so nothing later overwrites it.
34
71
  */
35
72
  export function writeFilamentPresets(settings, files, applied, presets, slotCount) {
36
- for (const { slot, preset } of applied) {
73
+ for (const { slot, preset, keys } of applied) {
37
74
  const values = presetSettingsFor(presets, preset);
38
75
  if (!values)
39
76
  continue;
40
- for (const [key, value] of Object.entries(values)) {
41
- const list = settings[key];
42
- if (!Array.isArray(list))
43
- continue;
44
- const next = [...list];
45
- if (VARIANT_KEYS.has(key) && next.length === slotCount * 2) {
46
- next[slot * 2] = value[0];
47
- next[slot * 2 + 1] = value[1] ?? value[0];
48
- }
49
- else if (next.length === slotCount) {
50
- next[slot] = value[0];
51
- }
52
- else
53
- continue;
54
- settings[key] = next;
55
- }
56
- for (const cfg of Object.values(files)) {
57
- if (cfg.name !== preset)
58
- continue;
59
- for (const [key, value] of Object.entries(values)) {
60
- const own = cfg[key];
61
- // The embedded preset keeps its layout: one value where it holds one, the preset's own list otherwise.
62
- cfg[key] = Array.isArray(own) && own.length === 1 ? [value[0]] : [...value];
63
- }
64
- }
77
+ const chosen = keys ? Object.fromEntries(keys.filter((k) => Object.hasOwn(values, k)).map((k) => [k, values[k]])) : values;
78
+ writeSlot(settings, files, slot, preset, chosen, slotCount);
65
79
  }
66
80
  }
67
81
  /**
68
- * Every filament's entry in `different_settings_to_system` lists every filament setting the project holds.
69
- * Snapmaker Orca resets a filament whose name matches one of its presets to that preset's values for every
70
- * setting its entry doesn't list (PresetBundle.cpp adds a few bookkeeping keys to each entry, so the reset
71
- * always runs); listing them all keeps the file's values, the creator's or the chosen preset's.
82
+ * Each slot's base. Under Original, a filament whose name isn't a U1 preset gets a U1 parent, the preset the
83
+ * Snapmaker option would choose (Snapmaker's own for the product line, or Generic where Snapmaker has none): the
84
+ * creator's name and values stay, and what the file doesn't name resets to a preset made for the U1 rather than
85
+ * to another printer's preset of the same name.
72
86
  */
73
- export function listFilamentSettings(settings, filamentCount) {
74
- if (filamentCount < 1)
87
+ export function filamentBases(settings, slotCount, presets, table, printerId,
88
+ /** The Snapmaker or Generic option was chosen. */
89
+ option) {
90
+ const names = Array.isArray(settings.filament_settings_id) ? settings.filament_settings_id : [];
91
+ const types = Array.isArray(settings.filament_type) ? settings.filament_type : [];
92
+ const offered = table && Object.hasOwn(table.printers, printerId) ? table.printers[printerId].presets : {};
93
+ return Array.from({ length: slotCount }, (_, i) => {
94
+ const name = typeof names[i] === "string" ? names[i] : "";
95
+ const own = presetSettingsFor(presets, name);
96
+ if (own)
97
+ return { kind: "preset", values: own };
98
+ if (Object.hasOwn(offered, name))
99
+ return option ? { kind: "named" } : { kind: "source" };
100
+ if (!table)
101
+ return { kind: "source" };
102
+ const parent = resolveFilamentName(table, printerId, typeof types[i] === "string" ? types[i] : "", name, "snapmaker").preset?.name;
103
+ const values = parent === undefined ? undefined : presetSettingsFor(presets, parent);
104
+ return values ? { kind: "preset", values, parent } : { kind: "source" };
105
+ });
106
+ }
107
+ /**
108
+ * A slot whose base is a U1 preset, its own or its parent, takes that preset's G-code, the U1's own, so the app
109
+ * (which resets unnamed settings to the base's) and the command line (which uses the file's) run the same
110
+ * G-code, and neither calls it modified.
111
+ */
112
+ export function writeBaseGcode(settings, files, bases, slotCount) {
113
+ const names = Array.isArray(settings.filament_settings_id) ? settings.filament_settings_id : [];
114
+ bases.forEach((base, slot) => {
115
+ const name = names[slot];
116
+ if (base.kind !== "preset" || typeof name !== "string")
117
+ return;
118
+ const gcode = Object.fromEntries(Object.entries(base.values).filter(([key]) => key.endsWith("_gcode")));
119
+ writeSlot(settings, files, slot, name, gcode, slotCount);
120
+ });
121
+ }
122
+ const text = (v) => (typeof v === "string" || typeof v === "number" || typeof v === "boolean" ? String(v) : undefined);
123
+ /** One slot's values of a filament setting: one per slot, or two (standard, high flow); undefined for any other layout. */
124
+ function slotValues(value, slot, slotCount) {
125
+ const values = !Array.isArray(value) ? [value] : value.length === slotCount * 2 ? value.slice(slot * 2, slot * 2 + 2) : value.length === slotCount ? [value[slot]] : undefined;
126
+ const out = values?.map(text);
127
+ return out?.every((v) => v !== undefined) ? out : undefined;
128
+ }
129
+ /** Whether two value lists mean the same, one value standing for both variants as Snapmaker Orca reads it. */
130
+ function sameValues(a, b) {
131
+ if (!a || !b)
132
+ return false;
133
+ if (a.length === b.length)
134
+ return a.every((v, i) => v === b[i]);
135
+ if (a.length === 1)
136
+ return b.every((v) => v === a[0]);
137
+ return b.length === 1 && a.every((v) => v === b[0]);
138
+ }
139
+ /** The settings the source file says the creator changed in one filament; null when its list can't be read for that slot. */
140
+ function sourceEntry(source, slot, slotCount) {
141
+ const list = source.different_settings_to_system;
142
+ if (!Array.isArray(list) || list.length !== slotCount + 2)
143
+ return null;
144
+ const entry = list[slot + 1];
145
+ return typeof entry === "string" ? entry.split(";") : null;
146
+ }
147
+ /**
148
+ * Each filament's entry in `different_settings_to_system` names the settings that differ from its base, and
149
+ * a parent goes in `inherits_group`. The app resets every setting an entry doesn't name to the base's value,
150
+ * which is the file's anyway, and warns about every G-code setting one names (PresetBundle.cpp,
151
+ * validate_presets), so an entry naming more than differs warns about G-code nobody changed. A filament no
152
+ * U1 preset fits names what the source file says the creator changed, what the conversion changed (its
153
+ * G-code, the U1's own, among them) and the printers it suits; every filament setting when the source's list
154
+ * can't be read.
155
+ */
156
+ export function listFilamentSettings(settings, bases, source, slotCount) {
157
+ if (slotCount < 1)
75
158
  return;
76
159
  const keys = Object.keys(settings)
77
- .filter((k) => Object.hasOwn(SETTINGS_CATALOG, k) && SETTINGS_CATALOG[k].group === "filament")
78
- .sort()
79
- .join(";");
160
+ .filter((k) => Object.hasOwn(SETTINGS_CATALOG, k) && SETTINGS_CATALOG[k].group === "filament" && !PROJECT_STATE_KEYS.has(k))
161
+ .sort();
162
+ const known = new Set(keys);
163
+ const entries = Array.from({ length: slotCount }, (_, slot) => {
164
+ const base = bases[slot] ?? { kind: "source" };
165
+ if (base.kind === "named")
166
+ return "";
167
+ const values = (key) => slotValues(settings[key], slot, slotCount);
168
+ if (base.kind === "preset")
169
+ return keys.filter((k) => !sameValues(values(k), Object.hasOwn(base.values, k) ? base.values[k] : undefined)).join(";");
170
+ const creator = sourceEntry(source, slot, slotCount);
171
+ const named = creator === null ? keys : [...creator.filter((k) => known.has(k)), ...keys.filter((k) => !sameValues(slotValues(source[k], slot, slotCount), values(k)))];
172
+ return [...new Set([...named, ...COMPATIBILITY_KEYS])].sort().join(";");
173
+ });
80
174
  const current = Array.isArray(settings.different_settings_to_system) ? settings.different_settings_to_system : [];
81
175
  const print = typeof current[0] === "string" ? current[0] : "";
82
- const printer = current.length === filamentCount + 2 && typeof current[filamentCount + 1] === "string" ? current[filamentCount + 1] : "";
83
- settings.different_settings_to_system = [print, ...Array.from({ length: filamentCount }, () => keys), printer];
176
+ const last = current.length >= 2 ? current[current.length - 1] : undefined;
177
+ const printer = typeof last === "string" ? last : "";
178
+ settings.different_settings_to_system = [print, ...entries, printer];
179
+ const parents = bases.map((base) => (base.kind === "preset" && base.parent !== undefined ? base.parent : ""));
180
+ if (parents.some((parent) => parent !== ""))
181
+ settings.inherits_group = ["", ...parents, ""];
182
+ }
183
+ /**
184
+ * Each embedded preset holds what the project holds for the first filament of its name, in the project's layout
185
+ * (standard and high-flow values where the project keeps two), with that filament's parent in `inherits`. The app
186
+ * builds a project preset from each embedded one and compares the file's filament with it, marking the first
187
+ * filament modified when they differ (Preset.cpp, load_external_preset). The presets were built from the source's
188
+ * values, before the steps that lay out, cap or replace them, so they're brought in line last.
189
+ */
190
+ export function syncEmbeddedPresets(settings, files, bases, slotCount) {
191
+ const names = Array.isArray(settings.filament_settings_id) ? settings.filament_settings_id : [];
192
+ for (const cfg of Object.values(files)) {
193
+ const slot = names.indexOf(cfg.name);
194
+ if (slot < 0 || slot >= slotCount)
195
+ continue;
196
+ for (const key of Object.keys(cfg)) {
197
+ if (key === "filament_settings_id" || !Array.isArray(cfg[key]))
198
+ continue;
199
+ const values = slotValues(settings[key], slot, slotCount);
200
+ if (values)
201
+ cfg[key] = values;
202
+ }
203
+ const base = bases[slot];
204
+ if (base?.kind === "preset" && base.parent !== undefined)
205
+ cfg.inherits = base.parent;
206
+ }
84
207
  }
@@ -6,6 +6,7 @@ import { coerceValue } from "./coerceValue.js";
6
6
  import { collapseNozzleVariants } from "./collapseNozzleVariants.js";
7
7
  import { detectVariantIndex } from "./detectVariantIndex.js";
8
8
  import { jsonEqual } from "./internal/jsonEqual.js";
9
+ import { PRINTER_KEYS_KEPT } from "./policy.js";
9
10
  import { resolveMinIfLower } from "./resolveMinIfLower.js";
10
11
  import { stripBambuOnlyNozzleVariantKeys } from "./stripBambuOnlyNozzleVariantKeys.js";
11
12
  /** Bambu/Orca both store this as an array whose first element is a `;`-joined key list (remaining elements are unused padding). */
@@ -153,7 +154,8 @@ release) {
153
154
  //
154
155
  // A stock baseline has no list of its own (Snapmaker Orca writes none
155
156
  // for an unmodified project), so one is created with Orca's layout: the
156
- // print entry, one entry per filament, then the printer entry.
157
+ // print entry, one entry per filament, then the printer entry, which
158
+ // names the printer settings the file keeps (PRINTER_KEYS_KEPT).
157
159
  const allKeys = Object.keys(newSettings)
158
160
  .filter((k) => k !== "different_settings_to_system")
159
161
  .sort();
@@ -161,7 +163,7 @@ release) {
161
163
  const filamentCount = Array.isArray(newSettings.filament_colour) ? newSettings.filament_colour.length : 1;
162
164
  newSettings.different_settings_to_system = Array.isArray(diffs)
163
165
  ? [allKeys.join(";"), ...diffs.slice(1)]
164
- : [allKeys.join(";"), ...Array.from({ length: filamentCount }, () => ""), ""];
166
+ : [allKeys.join(";"), ...Array.from({ length: filamentCount }, () => ""), PRINTER_KEYS_KEPT.join(";")];
165
167
  return {
166
168
  newSettings,
167
169
  changedKeys,
@@ -15,3 +15,31 @@ export type SupportedNozzleDiameter = KnownNozzleDiameter | (string & {});
15
15
  * profile rather than guess.
16
16
  */
17
17
  export declare function detectNozzleDiameter(projectSettings: JsonRecord): SupportedNozzleDiameter | null;
18
+ /** What a conversion can be asked to convert for: a U1 nozzle size, or the creator's own ("match"). */
19
+ export declare const NOZZLE_CHOICES: readonly ["0.2", "0.4", "0.6", "0.8", "match"];
20
+ export type KnownNozzleChoice = (typeof NOZZLE_CHOICES)[number];
21
+ /** One of {@link NOZZLE_CHOICES}, or a size a later catalog added. */
22
+ export type NozzleChoice = KnownNozzleChoice | (string & {});
23
+ /** The nozzle size a conversion is for unless told otherwise: the U1's stock 0.4 mm, which most U1s run. */
24
+ export declare const DEFAULT_NOZZLE: KnownNozzleDiameter;
25
+ export interface SourceNozzleSizes {
26
+ /** The distinct nozzle sizes the project uses, as written (e.g. "0.2", "0.4"), smallest first. */
27
+ sizes: string[];
28
+ /** The project uses more than one size (Bambu Studio's multi-diameter mode, or several sizes in its nozzle list). */
29
+ mixed: boolean;
30
+ /** Each filament's nozzle size as the file maps it, or null where the file doesn't say. */
31
+ filamentSizes: (string | null)[];
32
+ }
33
+ /**
34
+ * The nozzle sizes a source project uses. A single-nozzle file has one, its
35
+ * `nozzle_diameter`. Two layouts carry several:
36
+ * - Bambu Studio's one-extruder multi-nozzle printers (the H3C's nine
37
+ * PrintTools) list each nozzle's size in `extruder_nozzle_diameter`, with
38
+ * `multi_nozzle_diameter_mode` "1" when they differ, and map each filament
39
+ * to a nozzle (0-based) in `filament_nozzle_map`; `nozzle_diameter` then
40
+ * holds only the first nozzle's size.
41
+ * - Multi-extruder printers (Creality's K3, with a nozzle size per toolhead)
42
+ * list one size per extruder in `nozzle_diameter` and map each filament
43
+ * to an extruder (1-based) in `filament_map`.
44
+ */
45
+ export declare function sourceNozzleSizes(projectSettings: JsonRecord): SourceNozzleSizes;
@@ -27,3 +27,47 @@ export function detectNozzleDiameter(projectSettings) {
27
27
  }
28
28
  return closest;
29
29
  }
30
+ /** What a conversion can be asked to convert for: a U1 nozzle size, or the creator's own ("match"). */
31
+ export const NOZZLE_CHOICES = [...SUPPORTED_NOZZLE_DIAMETERS, "match"];
32
+ /** The nozzle size a conversion is for unless told otherwise: the U1's stock 0.4 mm, which most U1s run. */
33
+ export const DEFAULT_NOZZLE = "0.4";
34
+ const sizeText = (value) => {
35
+ const n = typeof value === "string" ? Number.parseFloat(value) : typeof value === "number" ? value : NaN;
36
+ return Number.isFinite(n) && n > 0 ? String(n) : null;
37
+ };
38
+ /**
39
+ * The nozzle sizes a source project uses. A single-nozzle file has one, its
40
+ * `nozzle_diameter`. Two layouts carry several:
41
+ * - Bambu Studio's one-extruder multi-nozzle printers (the H3C's nine
42
+ * PrintTools) list each nozzle's size in `extruder_nozzle_diameter`, with
43
+ * `multi_nozzle_diameter_mode` "1" when they differ, and map each filament
44
+ * to a nozzle (0-based) in `filament_nozzle_map`; `nozzle_diameter` then
45
+ * holds only the first nozzle's size.
46
+ * - Multi-extruder printers (Creality's K3, with a nozzle size per toolhead)
47
+ * list one size per extruder in `nozzle_diameter` and map each filament
48
+ * to an extruder (1-based) in `filament_map`.
49
+ */
50
+ export function sourceNozzleSizes(projectSettings) {
51
+ const perNozzle = Array.isArray(projectSettings.extruder_nozzle_diameter) ? projectSettings.extruder_nozzle_diameter.map(sizeText) : [];
52
+ const perExtruder = Array.isArray(projectSettings.nozzle_diameter) ? projectSettings.nozzle_diameter.map(sizeText) : [sizeText(projectSettings.nozzle_diameter)];
53
+ const nozzleList = perNozzle.some((s) => s !== null);
54
+ const listed = (nozzleList ? perNozzle : perExtruder).filter((s) => s !== null);
55
+ const sizes = [...new Set(listed)].sort((a, b) => Number(a) - Number(b));
56
+ const flagged = projectSettings.multi_nozzle_diameter_mode === "1" || projectSettings.multi_nozzle_diameter_mode === 1;
57
+ const filamentCount = Array.isArray(projectSettings.filament_colour) ? projectSettings.filament_colour.length : 0;
58
+ const nozzleMap = Array.isArray(projectSettings.filament_nozzle_map) ? projectSettings.filament_nozzle_map : [];
59
+ const extruderMap = Array.isArray(projectSettings.filament_map) ? projectSettings.filament_map : [];
60
+ const at = (list, i, offset) => {
61
+ const raw = list[i];
62
+ const n = typeof raw === "string" ? Number.parseInt(raw, 10) : typeof raw === "number" ? raw : NaN;
63
+ return Number.isInteger(n) ? n - offset : null;
64
+ };
65
+ const filamentSizes = Array.from({ length: filamentCount }, (_, i) => {
66
+ if (sizes.length === 1)
67
+ return sizes[0];
68
+ const table = nozzleList ? perNozzle : perExtruder;
69
+ const index = nozzleList ? at(nozzleMap, i, 0) : at(extruderMap, i, 1);
70
+ return index !== null && index >= 0 && index < table.length ? table[index] : null;
71
+ });
72
+ return { sizes, mixed: sizes.length > 1 || (flagged && sizes.length > 0), filamentSizes };
73
+ }
@@ -1,4 +1,17 @@
1
1
  import type { JsonRecord } from "./types.js";
2
+ /**
3
+ * A variant name with whitespace stripped, lowercased: Bambu spells the
4
+ * same variant "Standard"/"HighFlow" in nozzle_volume_type but "Direct
5
+ * Drive High Flow" (with spaces) in the variant-name lists.
6
+ */
7
+ export declare function variantKey(name: string): string;
8
+ /**
9
+ * The flow variant a single-nozzle source was sliced with, from
10
+ * `nozzle_volume_type` (or `default_nozzle_volume_type`), as a
11
+ * {@link variantKey}; `null` when the source doesn't say. Scoped to one
12
+ * physical nozzle for the reason given on detectVariantIndex.
13
+ */
14
+ export declare function activeNozzleVolumeType(bambuSettings: JsonRecord): string | null;
2
15
  /**
3
16
  * Determines which column of Bambu's newer per-nozzle-variant paired
4
17
  * arrays (e.g. `outer_wall_speed: ["210", "350"]`) reflects the settings
@@ -16,6 +16,27 @@ function extractVariantNames(bambuSettings) {
16
16
  }
17
17
  return null;
18
18
  }
19
+ /**
20
+ * A variant name with whitespace stripped, lowercased: Bambu spells the
21
+ * same variant "Standard"/"HighFlow" in nozzle_volume_type but "Direct
22
+ * Drive High Flow" (with spaces) in the variant-name lists.
23
+ */
24
+ export function variantKey(name) {
25
+ return name.replace(/\s+/g, "").toLowerCase();
26
+ }
27
+ /**
28
+ * The flow variant a single-nozzle source was sliced with, from
29
+ * `nozzle_volume_type` (or `default_nozzle_volume_type`), as a
30
+ * {@link variantKey}; `null` when the source doesn't say. Scoped to one
31
+ * physical nozzle for the reason given on detectVariantIndex.
32
+ */
33
+ export function activeNozzleVolumeType(bambuSettings) {
34
+ const nozzleDiameter = bambuSettings.nozzle_diameter;
35
+ if (!Array.isArray(nozzleDiameter) || nozzleDiameter.length !== 1)
36
+ return null;
37
+ const raw = bambuSettings.nozzle_volume_type ?? bambuSettings.default_nozzle_volume_type;
38
+ return Array.isArray(raw) && typeof raw[0] === "string" && raw[0] ? variantKey(raw[0]) : null;
39
+ }
19
40
  /**
20
41
  * Determines which column of Bambu's newer per-nozzle-variant paired
21
42
  * arrays (e.g. `outer_wall_speed: ["210", "350"]`) reflects the settings
@@ -48,22 +69,13 @@ function extractVariantNames(bambuSettings) {
48
69
  * existing "can't determine, ask for manual review" behavior in that case.
49
70
  */
50
71
  export function detectVariantIndex(bambuSettings) {
51
- const nozzleDiameter = bambuSettings.nozzle_diameter;
52
- if (!Array.isArray(nozzleDiameter) || nozzleDiameter.length !== 1)
72
+ const active = activeNozzleVolumeType(bambuSettings);
73
+ if (active === null)
53
74
  return null;
54
75
  const names = extractVariantNames(bambuSettings);
55
76
  if (!names)
56
77
  return null;
57
- const activeVariantRaw = bambuSettings.nozzle_volume_type ?? bambuSettings.default_nozzle_volume_type;
58
- const activeVariant = Array.isArray(activeVariantRaw) && typeof activeVariantRaw[0] === "string" ? activeVariantRaw[0] : null;
59
- if (!activeVariant)
60
- return null;
61
- // Compare with whitespace stripped -- Bambu spells the same variant
62
- // "Standard"/"HighFlow" in nozzle_volume_type but "Direct Drive High
63
- // Flow" (with spaces) in the variant-name lists.
64
- const normalize = (s) => s.replace(/\s+/g, "").toLowerCase();
65
- const activeNormalized = normalize(activeVariant);
66
- const index = names.findIndex((n) => normalize(n).includes(activeNormalized));
78
+ const index = names.findIndex((n) => variantKey(n).includes(active));
67
79
  return index >= 0 ? index : 0;
68
80
  }
69
81
  /**
@@ -75,6 +87,6 @@ export function detectHighFlowVariantIndex(bambuSettings) {
75
87
  const names = extractVariantNames(bambuSettings);
76
88
  if (!names)
77
89
  return null;
78
- const index = names.findIndex((n) => n.replace(/\s+/g, "").toLowerCase().includes("highflow"));
90
+ const index = names.findIndex((n) => variantKey(n).includes("highflow"));
79
91
  return index >= 0 ? index : null;
80
92
  }
@@ -1,7 +1,7 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
  import { FILAMENT_FLOW_VARIANT_KEYS } from "./applyFilamentFlowVariants.js";
3
3
  import { detectVariantIndex } from "./detectVariantIndex.js";
4
- import { filamentVariantLayout } from "./filamentVariantLayout.js";
4
+ import { bambuFilamentVariantLayout, filamentVariantLayout } from "./filamentVariantLayout.js";
5
5
  import { reshapeToCount } from "./reshapeToCount.js";
6
6
  const VARIANT_KEYS = new Set(FILAMENT_FLOW_VARIANT_KEYS);
7
7
  /** Up to six setting names, then how many more. */
@@ -33,6 +33,8 @@ export function extractFilamentSlots(bambuSettings, filamentTemplateKeys, extraP
33
33
  const variantIndex = detectVariantIndex(bambuSettings) ?? 0;
34
34
  // A source saved by an Orca-family slicer says where each filament's standard value sits, even when filaments carry different numbers of values.
35
35
  const layout = filamentVariantLayout(bambuSettings, filamentCount);
36
+ // A Bambu Studio source names each value's filament and variant instead, so filaments with different numbers of values are read correctly too.
37
+ const bambuLayout = layout ? null : bambuFilamentVariantLayout(bambuSettings, filamentCount);
36
38
  const perProjectKeyArrays = {};
37
39
  // Stale lists usually affect dozens of settings at once; they're reported as one line per kind, not one per setting.
38
40
  const stockUsed = new Map();
@@ -49,6 +51,10 @@ export function extractFilamentSlots(bambuSettings, filamentTemplateKeys, extraP
49
51
  perProjectKeyArrays[key] = layout.slots.map((slot) => bval[slot.standard]);
50
52
  continue;
51
53
  }
54
+ if (bambuLayout && bval.length === bambuLayout.length) {
55
+ perProjectKeyArrays[key] = bambuLayout.slots.map((slot) => bval[slot.standard]);
56
+ continue;
57
+ }
52
58
  // Several values per filament are read as one per nozzle variant only when the file names the variants;
53
59
  // otherwise the extra values are stale entries, and the first one per filament is used, as the slicer does.
54
60
  const variantNames = bambuSettings.filament_extruder_variant;
@@ -1,7 +1,7 @@
1
1
  import type { JsonRecord } from "./types.js";
2
- /** Where one filament's values sit in a per-variant array of an Orca-layout source. */
2
+ /** Where one filament's values sit in a per-variant array of the source. */
3
3
  export interface FilamentVariantPositions {
4
- /** Index of the filament's standard value. */
4
+ /** Index of the value the converted file's standard value comes from: the filament's standard value (for a Bambu Studio source, the variant it was sliced with). */
5
5
  standard: number;
6
6
  /** Index of its high-flow value, or null when the filament has none. */
7
7
  highFlow: number | null;
@@ -24,6 +24,34 @@ export interface FilamentVariantLayout {
24
24
  * in one file can have different counts (a filament with only a standard
25
25
  * value next to one with both), so the array isn't always a clean multiple
26
26
  * of the slot count. Bambu Studio sources don't write these keys; they
27
- * name their variants instead (detectVariantIndex.ts).
27
+ * name their variants instead (bambuFilamentVariantLayout).
28
28
  */
29
29
  export declare function filamentVariantLayout(source: JsonRecord, slotCount: number): FilamentVariantLayout | null;
30
+ /** Which entries of a Bambu Studio source's per-variant filament settings belong to each filament. */
31
+ export interface BambuFilamentEntries {
32
+ /** How many entries a per-variant filament setting has in this source. */
33
+ length: number;
34
+ /** One list of entry indexes per filament slot, in slot order. */
35
+ entries: number[][];
36
+ }
37
+ /**
38
+ * Which entries of a Bambu Studio source's per-variant filament settings
39
+ * belong to each filament, or null when the source doesn't say or every
40
+ * filament has one value.
41
+ *
42
+ * Bambu Studio stores a value only for the nozzle variants each filament's
43
+ * preset has (Generic PLA: standard only; Bambu PLA Basic: standard and
44
+ * high flow), and names each entry's filament in `filament_self_index`
45
+ * (from 1) and its variant in `filament_extruder_variant` ("Direct Drive
46
+ * Standard", "Direct Drive High Flow"), so four filaments can have seven
47
+ * values.
48
+ */
49
+ export declare function bambuFilamentEntries(source: JsonRecord, slotCount: number): BambuFilamentEntries | null;
50
+ /**
51
+ * How a Bambu Studio source lays out its per-variant filament settings
52
+ * (bambuFilamentEntries), or null. Each filament reads its entry for the
53
+ * variant the source was sliced with (activeNozzleVolumeType), or its first
54
+ * entry when it has none for that variant or the source doesn't say, as
55
+ * the slicer does (get_index_for_extruder in PrintConfig.cpp).
56
+ */
57
+ export declare function bambuFilamentVariantLayout(source: JsonRecord, slotCount: number): FilamentVariantLayout | null;
@@ -1,4 +1,6 @@
1
- /** Most variants Snapmaker Orca stores for one filament (standard and high flow today). */
1
+ // SPDX-License-Identifier: MIT
2
+ import { activeNozzleVolumeType, variantKey } from "./detectVariantIndex.js";
3
+ /** Most variants a slicer stores for one filament (standard and high flow today). */
2
4
  const MAX_STEP = 4;
3
5
  /**
4
6
  * How a source saved by Snapmaker Orca (or another Orca-family slicer)
@@ -12,7 +14,7 @@ const MAX_STEP = 4;
12
14
  * in one file can have different counts (a filament with only a standard
13
15
  * value next to one with both), so the array isn't always a clean multiple
14
16
  * of the slot count. Bambu Studio sources don't write these keys; they
15
- * name their variants instead (detectVariantIndex.ts).
17
+ * name their variants instead (bambuFilamentVariantLayout).
16
18
  */
17
19
  export function filamentVariantLayout(source, slotCount) {
18
20
  const steps = source.filament_flow_step_size;
@@ -36,3 +38,53 @@ export function filamentVariantLayout(source, slotCount) {
36
38
  }
37
39
  return { length, slots };
38
40
  }
41
+ /**
42
+ * Which entries of a Bambu Studio source's per-variant filament settings
43
+ * belong to each filament, or null when the source doesn't say or every
44
+ * filament has one value.
45
+ *
46
+ * Bambu Studio stores a value only for the nozzle variants each filament's
47
+ * preset has (Generic PLA: standard only; Bambu PLA Basic: standard and
48
+ * high flow), and names each entry's filament in `filament_self_index`
49
+ * (from 1) and its variant in `filament_extruder_variant` ("Direct Drive
50
+ * Standard", "Direct Drive High Flow"), so four filaments can have seven
51
+ * values.
52
+ */
53
+ export function bambuFilamentEntries(source, slotCount) {
54
+ const owners = source.filament_self_index;
55
+ const variants = source.filament_extruder_variant;
56
+ if (!Array.isArray(owners) || !Array.isArray(variants) || slotCount < 1 || owners.length !== variants.length)
57
+ return null;
58
+ if (owners.length <= slotCount || owners.length > slotCount * MAX_STEP || !variants.every((v) => typeof v === "string"))
59
+ return null;
60
+ const entries = Array.from({ length: slotCount }, () => []);
61
+ for (const [index, owner] of owners.entries()) {
62
+ const n = typeof owner === "string" || typeof owner === "number" ? Number(owner) : NaN;
63
+ if (!Number.isInteger(n) || n < 1 || n > slotCount)
64
+ return null;
65
+ entries[n - 1].push(index);
66
+ }
67
+ if (entries.some((own) => own.length === 0 || own.length > MAX_STEP))
68
+ return null;
69
+ return { length: owners.length, entries };
70
+ }
71
+ /**
72
+ * How a Bambu Studio source lays out its per-variant filament settings
73
+ * (bambuFilamentEntries), or null. Each filament reads its entry for the
74
+ * variant the source was sliced with (activeNozzleVolumeType), or its first
75
+ * entry when it has none for that variant or the source doesn't say, as
76
+ * the slicer does (get_index_for_extruder in PrintConfig.cpp).
77
+ */
78
+ export function bambuFilamentVariantLayout(source, slotCount) {
79
+ const found = bambuFilamentEntries(source, slotCount);
80
+ if (!found)
81
+ return null;
82
+ const names = source.filament_extruder_variant.map(variantKey);
83
+ const active = activeNozzleVolumeType(source);
84
+ const find = (own, variant) => own.find((index) => names[index].includes(variant));
85
+ const slots = found.entries.map((own) => ({
86
+ standard: (active === null ? undefined : find(own, active)) ?? own[0],
87
+ highFlow: find(own, "highflow") ?? null,
88
+ }));
89
+ return { length: found.length, slots };
90
+ }
@@ -19,7 +19,8 @@ export function fillFilamentSlots(settings, filamentKeys, fromSource, slotCount)
19
19
  if (fromSource.has(key))
20
20
  continue;
21
21
  const value = settings[key];
22
- if (!Array.isArray(value) || value.length === 0 || value.length === slotCount)
22
+ // Always one filament's values, even a standard and high-flow pair in a two-filament project.
23
+ if (!Array.isArray(value) || value.length === 0)
23
24
  continue;
24
25
  settings[key] = Array.from({ length: slotCount }, () => value[0]);
25
26
  }
@@ -0,0 +1,20 @@
1
+ import type { JsonRecord } from "./types.js";
2
+ /**
3
+ * The colors Snapmaker Orca gives the filaments it adds, in order (Plater.cpp, get_next_color_for_filament). The app
4
+ * keeps one counter for the whole session, so these are the colors a freshly started app gives its first ones.
5
+ */
6
+ export declare const NEW_FILAMENT_COLORS: readonly string[];
7
+ /**
8
+ * Pads a project with fewer filaments than the printer has toolheads to one filament per toolhead, as Snapmaker Orca
9
+ * does when a project is switched to a printer with several (Tab.cpp, "sync filament num if it's a multi tool
10
+ * printer"): each added filament is a copy of the last one, its preset, parent and settings included, in the next of
11
+ * the app's new-filament colors the project doesn't use. No object uses the copies, and the project's own filaments
12
+ * stay as they are. A file with fewer is set up for every toolhead anyway when the app opens it, and then checked
13
+ * against its own shorter lists: the app lists a nameless preset as customized and marks a one-filament project's
14
+ * filament modified. Printers with more toolheads than the palette has colors are left alone.
15
+ *
16
+ * @param filamentKeys the profile's filament settings (its filament template), which the converter reads per filament
17
+ * when it converts a file again, besides those the settings catalog lists as filament settings.
18
+ * @returns how many filaments were added.
19
+ */
20
+ export declare function padFilamentsToToolheads(settings: JsonRecord, slotCount: number, toolheads: number, filamentKeys?: ReadonlySet<string>): number;