bambu-printer-mcp 1.1.51 → 1.1.53
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/README.md +33 -8
- package/dist/ams-mapping.d.ts +19 -0
- package/dist/ams-mapping.js +49 -0
- package/dist/bambu-network-bridge.d.ts +86 -9
- package/dist/bambu-network-bridge.js +284 -4
- package/dist/control-reply.d.ts +21 -0
- package/dist/control-reply.js +44 -0
- package/dist/counterpart/info.js +1 -0
- package/dist/counterpart/route.d.ts +37 -0
- package/dist/counterpart/route.js +78 -0
- package/dist/counterpart/schemas.d.ts +4 -0
- package/dist/counterpart/schemas.js +1 -0
- package/dist/design-guidance.d.ts +26 -0
- package/dist/design-guidance.js +177 -0
- package/dist/fusion-mcp-bridge.d.ts +8 -4
- package/dist/fusion-mcp-bridge.js +36 -10
- package/dist/index.js +206 -51
- package/dist/printers/bambu.d.ts +38 -13
- package/dist/printers/bambu.js +204 -69
- package/dist/printers/camera-sources.d.ts +8 -0
- package/dist/printers/camera-sources.js +66 -0
- package/dist/printers/camera-stream.d.ts +65 -0
- package/dist/printers/camera-stream.js +227 -0
- package/dist/printers/camera-tools.d.ts +192 -0
- package/dist/printers/camera-tools.js +29 -0
- package/dist/printers/camera-wire.d.ts +7 -0
- package/dist/printers/camera-wire.js +40 -0
- package/dist/safety/print-file.d.ts +3 -0
- package/dist/safety/print-file.js +11 -2
- package/dist/safety/printer-state.d.ts +7 -0
- package/dist/safety/printer-state.js +11 -1
- package/dist/slicer/layer-pauses.d.ts +47 -0
- package/dist/slicer/layer-pauses.js +130 -0
- package/dist/stl/stl-manipulator.d.ts +3 -0
- package/dist/stl/stl-manipulator.js +50 -4
- package/dist/tool-annotations.d.ts +25 -0
- package/dist/tool-annotations.js +96 -0
- package/package.json +2 -1
- package/src/ams-mapping.ts +52 -0
- package/src/bambu-network-bridge.ts +311 -8
- package/src/control-reply.ts +41 -0
- package/src/counterpart/info.ts +1 -0
- package/src/counterpart/route.ts +91 -0
- package/src/counterpart/schemas.ts +2 -0
- package/src/design-guidance.ts +188 -0
- package/src/fusion-mcp-bridge.ts +30 -10
- package/src/index.ts +199 -55
- package/src/printers/bambu.ts +194 -60
- package/src/printers/camera-sources.ts +53 -0
- package/src/printers/camera-stream.ts +165 -0
- package/src/printers/camera-tools.ts +29 -0
- package/src/printers/camera-wire.ts +39 -0
- package/src/safety/print-file.ts +12 -2
- package/src/safety/printer-state.ts +11 -1
- package/src/slicer/layer-pauses.ts +143 -0
- package/src/stl/stl-manipulator.ts +58 -5
- package/src/tool-annotations.ts +126 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Split the A1/P1 camera stream on port 6000 into JPEG frames. Each frame is
|
|
3
|
+
* a 16-byte header (payload size LE, itrack, flags, reserved) and the JPEG.
|
|
4
|
+
*/
|
|
5
|
+
export function cameraFrameParser(onFrame: (jpeg: Buffer) => void, onError: (error: Error) => void): (chunk: Buffer) => void {
|
|
6
|
+
let buffered = Buffer.alloc(0);
|
|
7
|
+
let failed = false;
|
|
8
|
+
return chunk => {
|
|
9
|
+
if (failed) return;
|
|
10
|
+
buffered = Buffer.concat([buffered, chunk]);
|
|
11
|
+
while (buffered.length >= 16) {
|
|
12
|
+
const payloadSize = buffered.readUInt32LE(0);
|
|
13
|
+
if (payloadSize <= 0 || payloadSize > 5_000_000) {
|
|
14
|
+
failed = true;
|
|
15
|
+
onError(new Error(`camera_snapshot: unreasonable payload size ${payloadSize} from header; auth likely failed.`));
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (buffered.length < 16 + payloadSize) return;
|
|
19
|
+
const jpeg = buffered.subarray(16, 16 + payloadSize);
|
|
20
|
+
buffered = buffered.subarray(16 + payloadSize);
|
|
21
|
+
if (jpeg[0] !== 0xff || jpeg[1] !== 0xd8) {
|
|
22
|
+
failed = true;
|
|
23
|
+
onError(new Error(`camera_snapshot: payload does not start with JPEG SOI (FF D8); got ${jpeg[0].toString(16)} ${jpeg[1].toString(16)}.`));
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
onFrame(Buffer.from(jpeg));
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
/** The existing Bambu camera authentication packet, shared by snapshots and streams. */
|
|
33
|
+
export function cameraAuth(token: string): Buffer {
|
|
34
|
+
const auth = Buffer.alloc(80, 0);
|
|
35
|
+
auth.writeUInt32LE(0x40, 0); auth.writeUInt32LE(0x3000, 4);
|
|
36
|
+
auth.write("bblp", 16, 4, "ascii");
|
|
37
|
+
auth.write(token, 48, Math.min(32, Buffer.byteLength(token, "ascii")), "ascii");
|
|
38
|
+
return auth;
|
|
39
|
+
}
|
package/src/safety/print-file.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { readBoundedPrintFile, loadSafe3mfArchive } from './archive.js';
|
|
3
|
+
import { projectFilaments, type ProjectFilament } from "../ams-mapping.js";
|
|
3
4
|
import { MACHINE_LIMITS, isDualNozzleModel, normalizeMaterial, normalizeModel, validateStartupPurgeTemperature, validateTemperature } from './limits.js';
|
|
4
5
|
|
|
5
6
|
export interface PrintFileInspection {
|
|
@@ -18,6 +19,8 @@ export interface PrintFileInspection {
|
|
|
18
19
|
nozzleFlows?: string[];
|
|
19
20
|
/** Physical nozzle index for each declared filament, composed from the selected plate headers. */
|
|
20
21
|
filamentNozzleIndices?: number[];
|
|
22
|
+
/** Every project filament as project_settings.config declares it, for PrintParams.ams_mapping_info. */
|
|
23
|
+
projectFilaments?: ProjectFilament[];
|
|
21
24
|
}
|
|
22
25
|
type Metadata = Record<string, unknown>;
|
|
23
26
|
const NUMBER = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/;
|
|
@@ -405,7 +408,14 @@ export async function inspectPrintFile(filePath: string, options: {model:string;
|
|
|
405
408
|
continue;
|
|
406
409
|
}
|
|
407
410
|
if(['M104','M109','M140','M190','M141','M191'].includes(code)) {
|
|
408
|
-
const args=parameters(argumentsText,'A');
|
|
411
|
+
const args=parameters(argumentsText,code==='M104'?'AM':'A');
|
|
412
|
+
// Studio 2.9's GCodeWriter marks layer-temperature changes with bare M.
|
|
413
|
+
// Accept only its exact S-target / optional T form; targets and tool
|
|
414
|
+
// selectors still pass through the independent thermal checks below.
|
|
415
|
+
if(args.has('M')) {
|
|
416
|
+
if(!Number.isNaN(args.get('M')!) || !args.has('S') || [...args.keys()].some(key=>!'MST'.includes(key))) fail('unsupported M104 layer-change temperature parameters');
|
|
417
|
+
args.delete('M');
|
|
418
|
+
}
|
|
409
419
|
// Official A1 and A1 mini start G-code waits with M109 S<target> H<limit>
|
|
410
420
|
// (S25 H140, S100 H170, S220 H300). H is not a heater target: S stays the
|
|
411
421
|
// checked target, so H is accepted only within those vendor bounds.
|
|
@@ -548,5 +558,5 @@ export async function inspectPrintFile(filePath: string, options: {model:string;
|
|
|
548
558
|
}
|
|
549
559
|
if(!commandCount) return fail('selected G-code contains no printable commands');
|
|
550
560
|
if(!used.size) materials.forEach((_,index)=>used.add(index));
|
|
551
|
-
return {model,nozzleDiameters,materials,usedFilamentPositions:[...used].sort((a,b)=>a-b),plateInternalPath,sha256,maxNozzleTemperature,maxBedTemperature,maxChamberTemperature,bedType,selectsAms,nozzleTypes,nozzleFlows,filamentNozzleIndices};
|
|
561
|
+
return {model,nozzleDiameters,materials,usedFilamentPositions:[...used].sort((a,b)=>a-b),plateInternalPath,sha256,maxNozzleTemperature,maxBedTemperature,maxChamberTemperature,bedType,selectsAms,nozzleTypes,nozzleFlows,filamentNozzleIndices,...(Array.isArray(project.filament_type)?{projectFilaments:projectFilaments(project)}:{})};
|
|
552
562
|
}
|
|
@@ -325,12 +325,22 @@ function logHiddenNotices(raw: Record<string, any>, serial: string, model: strin
|
|
|
325
325
|
}
|
|
326
326
|
}
|
|
327
327
|
|
|
328
|
+
/**
|
|
329
|
+
* 0300_8013, "Printing was paused by the user" / "paused due to the pause command added to the
|
|
330
|
+
* printing file" (hms-messages.ts): the printer's report of a deliberate pause, which Resume clears.
|
|
331
|
+
* A paused P1S reports it as print_error, so it is not an error for resuming a paused job.
|
|
332
|
+
*/
|
|
333
|
+
export const PAUSE_NOTICE = 0x03008013;
|
|
334
|
+
export function isPauseNotice(raw: Record<string, any>, requireIdle: boolean): boolean {
|
|
335
|
+
const state = typeof raw?.gcode_state === "string" ? raw.gcode_state.toUpperCase() : "";
|
|
336
|
+
return !requireIdle && state === "PAUSE" && finiteNumber(raw?.print_error) === PAUSE_NOTICE;
|
|
337
|
+
}
|
|
328
338
|
function validateErrors(raw: Record<string,any>, requireIdle: boolean, serial: string, model: string) {
|
|
329
339
|
const state = typeof raw.gcode_state === "string" ? raw.gcode_state.toUpperCase() : "UNKNOWN";
|
|
330
340
|
const terminal = isCancelledX2DJob(raw, model) || isFinishedP1SJob(raw, model);
|
|
331
341
|
if (requireIdle && !["IDLE","FINISH"].includes(state) && !terminal) throw new Error(`Printer state ${state} is not safely idle.`);
|
|
332
342
|
if (!requireIdle && !["IDLE","FINISH","RUNNING","PAUSE"].includes(state) && !terminal) throw new Error(`Printer state ${state} is unknown or failed.`);
|
|
333
|
-
if (finiteNumber(raw.print_error) !== 0) throw new Error("Printer reports a print error or did not report its error state.");
|
|
343
|
+
if (finiteNumber(raw.print_error) !== 0 && !isPauseNotice(raw, requireIdle)) throw new Error("Printer reports a print error or did not report its error state.");
|
|
334
344
|
if (!Array.isArray(raw.hms)) throw new Error("Printer HMS error state is missing or malformed.");
|
|
335
345
|
for (const reported of raw.hms) {
|
|
336
346
|
const entry = hmsCode(reported);
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import JSZip from "jszip";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Pause-at-layer for Bambu-compatible slicer CLIs ("Add pause" in Bambu Studio
|
|
6
|
+
* and OrcaSlicer). The slicer writes the machine's own machine_pause_gcode
|
|
7
|
+
* (M400 U1 on Bambu printers) before the chosen layer, so the printer pauses
|
|
8
|
+
* by itself there and waits for Resume; no remote command is involved.
|
|
9
|
+
*
|
|
10
|
+
* The CLI takes these as --load-custom-gcodes <json> (CustomGCode::Info). It
|
|
11
|
+
* does not apply the pauses a project 3MF already carries in
|
|
12
|
+
* Metadata/custom_gcode_per_print_z.xml, so those are read here and passed the
|
|
13
|
+
* same way. A pause's print_z must be the exact top of its layer: a height
|
|
14
|
+
* between layers lands on the layer below.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** CustomGCode::Type, in the order bbs_3mf.cpp writes it as an integer. */
|
|
18
|
+
const CUSTOM_GCODE_TYPES = ["ColorChange", "PausePrint", "ToolChange", "Template", "Custom", "Unknown"] as const;
|
|
19
|
+
export type CustomGcodeType = (typeof CUSTOM_GCODE_TYPES)[number];
|
|
20
|
+
|
|
21
|
+
export interface CustomGcodeItem {
|
|
22
|
+
type: CustomGcodeType;
|
|
23
|
+
print_z: number;
|
|
24
|
+
color: string;
|
|
25
|
+
extruder: number;
|
|
26
|
+
extra: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface CustomGcodeInfo {
|
|
30
|
+
mode: string;
|
|
31
|
+
gcodes: CustomGcodeItem[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 1-based layer numbers, as Bambu Studio's and OrcaSlicer's preview slider count them. */
|
|
35
|
+
export function parsePauseLayers(value: unknown): number[] | undefined {
|
|
36
|
+
if (value === undefined || value === null || value === "") return undefined;
|
|
37
|
+
const raw = Array.isArray(value) ? value : String(value).split(/[\s,;]+/).filter(Boolean);
|
|
38
|
+
const layers = raw.map(entry => Number(entry));
|
|
39
|
+
for (const layer of layers) {
|
|
40
|
+
if (!Number.isInteger(layer) || layer < 2) {
|
|
41
|
+
throw new Error(`pause_at_layers takes whole layer numbers of 2 or more (the printer cannot pause before layer 1), not ${JSON.stringify(value)}.`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const unique = [...new Set(layers)].sort((a, b) => a - b);
|
|
45
|
+
return unique.length ? unique : undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function xmlAttr(tag: string, name: string): string | undefined {
|
|
49
|
+
const match = new RegExp(`\\b${name}="([^"]*)"`).exec(tag);
|
|
50
|
+
return match ? match[1].replace(/"/g, '"').replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&") : undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The custom G-codes (pauses, colour changes, ...) a project 3MF stores for
|
|
55
|
+
* one plate (1-based), as Bambu Studio and OrcaSlicer save them.
|
|
56
|
+
*/
|
|
57
|
+
export async function readProjectCustomGcodes(threeMfPath: string, plate = 1): Promise<CustomGcodeInfo | undefined> {
|
|
58
|
+
let zip: JSZip;
|
|
59
|
+
try {
|
|
60
|
+
zip = await JSZip.loadAsync(await fs.promises.readFile(threeMfPath));
|
|
61
|
+
} catch {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
const file = zip.file(/^Metadata\/custom_gcode_per_print_z\.xml$/i)[0];
|
|
65
|
+
if (!file) return undefined;
|
|
66
|
+
const xml = await file.async("string");
|
|
67
|
+
for (const block of xml.match(/<plate>[\s\S]*?<\/plate>/g) ?? []) {
|
|
68
|
+
const info = /<plate_info\b[^>]*>/.exec(block)?.[0];
|
|
69
|
+
if (Number(info ? xmlAttr(info, "id") : NaN) !== plate) continue;
|
|
70
|
+
const gcodes: CustomGcodeItem[] = [];
|
|
71
|
+
for (const tag of block.match(/<layer\b[^>]*>/g) ?? []) {
|
|
72
|
+
const type = CUSTOM_GCODE_TYPES[Number(xmlAttr(tag, "type"))] ?? "Unknown";
|
|
73
|
+
const printZ = Number(xmlAttr(tag, "top_z"));
|
|
74
|
+
if (!Number.isFinite(printZ)) continue;
|
|
75
|
+
gcodes.push({
|
|
76
|
+
type,
|
|
77
|
+
print_z: printZ,
|
|
78
|
+
color: xmlAttr(tag, "color") ?? "",
|
|
79
|
+
extruder: Number(xmlAttr(tag, "extruder") ?? 1) || 1,
|
|
80
|
+
// Template and Custom keep their G-code text in "extra" (CustomGCode.hpp).
|
|
81
|
+
extra: xmlAttr(tag, "extra") ?? "",
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const modeTag = /<mode\b[^>]*>/.exec(block)?.[0];
|
|
85
|
+
return { mode: (modeTag && xmlAttr(modeTag, "value")) || "SingleExtruder", gcodes };
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The plate's G-code text inside a sliced 3MF. */
|
|
91
|
+
export async function readPlateGcode(threeMfPath: string, plate = 1): Promise<string | undefined> {
|
|
92
|
+
const zip = await JSZip.loadAsync(await fs.promises.readFile(threeMfPath));
|
|
93
|
+
const exact = zip.file(`Metadata/plate_${plate}.gcode`);
|
|
94
|
+
const file = exact ?? zip.file(/^Metadata\/plate_\d+\.gcode$/)[0];
|
|
95
|
+
return file ? file.async("string") : undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Each layer's top Z in order, from the slicer's "; Z_HEIGHT:" markers. */
|
|
99
|
+
export function layerTops(gcode: string): number[] {
|
|
100
|
+
const tops: number[] = [];
|
|
101
|
+
for (const match of gcode.matchAll(/^; Z_HEIGHT: ([0-9.]+)\s*$/gm)) tops.push(Number(match[1]));
|
|
102
|
+
return tops;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The 1-based layers whose block holds a pause: the slicer's "; PAUSE_PRINTING"
|
|
107
|
+
* marker or the machine's pause G-code on its own line.
|
|
108
|
+
*/
|
|
109
|
+
export function pausedLayers(gcode: string, pauseGcode = "M400 U1"): number[] {
|
|
110
|
+
const lines = gcode.split(/\r?\n/);
|
|
111
|
+
const paused = new Set<number>();
|
|
112
|
+
let layer = 0;
|
|
113
|
+
const pauseLine = pauseGcode.trim();
|
|
114
|
+
for (const line of lines) {
|
|
115
|
+
if (line.startsWith("; Z_HEIGHT:")) { layer += 1; continue; }
|
|
116
|
+
if (layer === 0) continue;
|
|
117
|
+
const trimmed = line.trim();
|
|
118
|
+
if (trimmed === "; PAUSE_PRINTING" || (pauseLine && trimmed === pauseLine)) paused.add(layer);
|
|
119
|
+
}
|
|
120
|
+
return [...paused].sort((a, b) => a - b);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The machine_pause_gcode a sliced file was made with (its config block), else Bambu's M400 U1. */
|
|
124
|
+
export function machinePauseGcode(gcode: string): string {
|
|
125
|
+
const match = /^; machine_pause_gcode = (.*)$/m.exec(gcode);
|
|
126
|
+
const value = match?.[1]?.split("\\n")[0]?.trim();
|
|
127
|
+
return value || "M400 U1";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Project custom G-codes plus a pause before each requested layer, at that layer's exact top. */
|
|
131
|
+
export function customGcodesWithPauses(tops: number[], layers: number[], project?: CustomGcodeInfo): CustomGcodeInfo {
|
|
132
|
+
const total = tops.length;
|
|
133
|
+
const beyond = layers.filter(layer => layer > total);
|
|
134
|
+
if (beyond.length) {
|
|
135
|
+
throw new Error(`This print has ${total} layers, so it cannot pause before layer ${beyond.join(", ")}.`);
|
|
136
|
+
}
|
|
137
|
+
const pauses: CustomGcodeItem[] = layers.map(layer => ({ type: "PausePrint", print_z: tops[layer - 1], color: "", extruder: 1, extra: "" }));
|
|
138
|
+
const kept = (project?.gcodes ?? []).filter(item => !(item.type === "PausePrint" && pauses.some(p => Math.abs(p.print_z - item.print_z) < 1e-4)));
|
|
139
|
+
return {
|
|
140
|
+
mode: project?.mode || "SingleExtruder",
|
|
141
|
+
gcodes: [...kept, ...pauses].sort((a, b) => a.print_z - b.print_z),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { customGcodesWithPauses, layerTops, machinePauseGcode, pausedLayers, readPlateGcode, readProjectCustomGcodes } from "../slicer/layer-pauses.js";
|
|
1
2
|
import * as THREE from 'three';
|
|
2
3
|
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader.js';
|
|
3
4
|
import { STLExporter } from 'three/examples/jsm/exporters/STLExporter.js';
|
|
@@ -162,7 +163,7 @@ function configuredBambuProfileDirs(): string[] {
|
|
|
162
163
|
// Bambu-compatible slicer CLI flags exposed via the slice_stl tool
|
|
163
164
|
export interface BambuSliceOptions {
|
|
164
165
|
uptodate?: boolean; // --uptodate: update 3MF configs to latest presets
|
|
165
|
-
repetitions?: number; // --
|
|
166
|
+
repetitions?: number; // STL: --clone-objects N; 3MF: --repetitions N on the selected plate
|
|
166
167
|
orient?: boolean; // --orient 1: auto-orient for printability
|
|
167
168
|
arrange?: boolean; // --arrange 1: auto-arrange objects on plate
|
|
168
169
|
ensureOnBed?: boolean; // --ensure-on-bed: lift floating models onto bed
|
|
@@ -184,6 +185,8 @@ export interface BambuSliceOptions {
|
|
|
184
185
|
skipModifiedGcodes?: boolean;// --skip-modified-gcodes: ignore stale custom gcodes
|
|
185
186
|
slicePlate?: number; // --slice N: which plate (default: 0 = all)
|
|
186
187
|
processOverrides?: ProcessOverrides; // layer_height / wall_loops / sparse_infill_density over the resolved process preset
|
|
188
|
+
pauseAtLayers?: number[]; // pause before each 1-based layer (the slicer's "Add pause"); Bambu-compatible CLIs only
|
|
189
|
+
customGcodesFile?: string; // --load-custom-gcodes <json>: set by sliceSTLWithResult, not by callers
|
|
187
190
|
}
|
|
188
191
|
|
|
189
192
|
interface BambuSettingsBundle {
|
|
@@ -1740,6 +1743,54 @@ export class STLManipulator extends EventEmitter {
|
|
|
1740
1743
|
progressCallback?: ProgressCallback,
|
|
1741
1744
|
printerPreset?: string,
|
|
1742
1745
|
bambuOptions?: BambuSliceOptions
|
|
1746
|
+
): Promise<SliceOutput> {
|
|
1747
|
+
const once = (options?: BambuSliceOptions) =>
|
|
1748
|
+
this.sliceSTLOnce(stlFilePath, slicerType, slicerPath, slicerProfile, progressCallback, printerPreset, options);
|
|
1749
|
+
const layers = bambuOptions?.pauseAtLayers?.length ? bambuOptions.pauseAtLayers : undefined;
|
|
1750
|
+
const cli = slicerType === 'bambustudio' || slicerType === 'orcaslicer' || slicerType === 'orcaslicer-bambulab';
|
|
1751
|
+
const plate = bambuOptions?.slicePlate && bambuOptions.slicePlate > 0 ? bambuOptions.slicePlate : 1;
|
|
1752
|
+
// The CLI drops a project's own pauses and colour changes; carry them over.
|
|
1753
|
+
const project = cli && /\.3mf$/i.test(stlFilePath) ? await readProjectCustomGcodes(stlFilePath, plate) : undefined;
|
|
1754
|
+
if (!layers && !project?.gcodes.length) return once(bambuOptions);
|
|
1755
|
+
if (layers && !cli) {
|
|
1756
|
+
throw new Error(`Pausing at a layer needs the Bambu Studio or OrcaSlicer CLI, not ${slicerType}.`);
|
|
1757
|
+
}
|
|
1758
|
+
const customGcodesFile = path.join(this.tempDir, `custom-gcodes-${Date.now()}-${Math.random().toString(36).slice(2)}.json`);
|
|
1759
|
+
try {
|
|
1760
|
+
let tops: number[] = [];
|
|
1761
|
+
if (layers) {
|
|
1762
|
+
// A pause sits at its layer's exact top Z; learn the tops from a slice without pauses.
|
|
1763
|
+
if (progressCallback) progressCallback(10, `Finding layer heights for the pause before layer ${layers.join(", ")}`);
|
|
1764
|
+
const plain = await once({ ...bambuOptions, pauseAtLayers: undefined, customGcodesFile: undefined });
|
|
1765
|
+
const gcode = await readPlateGcode(plain.path, plate);
|
|
1766
|
+
if (!gcode) throw new Error('The slicer wrote no plate G-code, so the pause layers cannot be placed.');
|
|
1767
|
+
tops = layerTops(gcode);
|
|
1768
|
+
}
|
|
1769
|
+
const info = layers ? customGcodesWithPauses(tops, layers, project) : project!;
|
|
1770
|
+
await fs.promises.writeFile(customGcodesFile, JSON.stringify(info));
|
|
1771
|
+
const result = await once({ ...bambuOptions, customGcodesFile });
|
|
1772
|
+
if (layers) {
|
|
1773
|
+
const gcode = await readPlateGcode(result.path, plate);
|
|
1774
|
+
const placed = gcode ? pausedLayers(gcode, machinePauseGcode(gcode)) : [];
|
|
1775
|
+
const missing = layers.filter(layer => !placed.includes(layer));
|
|
1776
|
+
if (!gcode || missing.length || layerTops(gcode).length !== tops.length) {
|
|
1777
|
+
throw new Error(`The slicer did not place the pause before layer ${(missing.length ? missing : layers).join(", ")}. Nothing was sent to the printer.`);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
return result;
|
|
1781
|
+
} finally {
|
|
1782
|
+
await fs.promises.rm(customGcodesFile, { force: true });
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
private async sliceSTLOnce(
|
|
1787
|
+
stlFilePath: string,
|
|
1788
|
+
slicerType: SlicerType,
|
|
1789
|
+
slicerPath: string,
|
|
1790
|
+
slicerProfile?: string,
|
|
1791
|
+
progressCallback?: ProgressCallback,
|
|
1792
|
+
printerPreset?: string,
|
|
1793
|
+
bambuOptions?: BambuSliceOptions
|
|
1743
1794
|
): Promise<SliceOutput> {
|
|
1744
1795
|
const operationId = this.generateOperationId();
|
|
1745
1796
|
this.activeOperations.set(operationId, true);
|
|
@@ -1906,8 +1957,8 @@ export class STLManipulator extends EventEmitter {
|
|
|
1906
1957
|
"Set arrange to false, or apply the transform in the slicer and export the project."
|
|
1907
1958
|
);
|
|
1908
1959
|
}
|
|
1909
|
-
if (
|
|
1910
|
-
throw new Error("Use either repetitions or clone_objects for an STL
|
|
1960
|
+
if (!is3mf && bambuOptions?.repetitions !== undefined && bambuOptions.cloneObjects) {
|
|
1961
|
+
throw new Error("Use either repetitions or clone_objects for an STL, not both.");
|
|
1911
1962
|
}
|
|
1912
1963
|
const inputPath = meshTransform ? await this.transformMeshForCli(stlFilePath, bambuOptions!) : stlFilePath;
|
|
1913
1964
|
if (inputPath !== stlFilePath) temporaryFiles.push(inputPath);
|
|
@@ -1939,8 +1990,9 @@ export class STLManipulator extends EventEmitter {
|
|
|
1939
1990
|
if (bambuOptions?.orient !== undefined) args.push('--orient', bambuOptions.orient ? '1' : '0');
|
|
1940
1991
|
if (bambuOptions?.arrange !== undefined) args.push('--arrange', bambuOptions.arrange ? '1' : '0');
|
|
1941
1992
|
if (bambuOptions?.repetitions !== undefined) {
|
|
1942
|
-
//
|
|
1943
|
-
|
|
1993
|
+
// Both Bambu Studio and Orca count STL copies with --clone-objects.
|
|
1994
|
+
// --repetitions > 1 requires a selected existing plate and rejects --slice 0.
|
|
1995
|
+
if (!is3mf) args.push('--clone-objects', String(bambuOptions.repetitions));
|
|
1944
1996
|
else args.push('--repetitions', String(bambuOptions.repetitions));
|
|
1945
1997
|
}
|
|
1946
1998
|
if (!meshTransform) {
|
|
@@ -1956,6 +2008,7 @@ export class STLManipulator extends EventEmitter {
|
|
|
1956
2008
|
args.push('--load-defaultfila');
|
|
1957
2009
|
}
|
|
1958
2010
|
if (bambuOptions?.loadFilamentIds) args.push('--load-filament-ids', bambuOptions.loadFilamentIds);
|
|
2011
|
+
if (bambuOptions?.customGcodesFile) args.push('--load-custom-gcodes', path.resolve(bambuOptions.customGcodesFile));
|
|
1959
2012
|
|
|
1960
2013
|
args.push(path.resolve(inputPath));
|
|
1961
2014
|
// Override outputFilePath for bambustudio since it produces 3MF, not gcode
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP tool annotations (spec 2025-03-26 and later), one entry per tool, chosen from
|
|
3
|
+
* what the tool's handler actually does. Clients and Counterpart's remote endpoint
|
|
4
|
+
* read them to decide which tools a device may call, so every listed tool must have
|
|
5
|
+
* one (tests/tool-annotations.test.mjs).
|
|
6
|
+
*
|
|
7
|
+
* readOnlyHint: changes nothing outside this server (printer, files, other apps).
|
|
8
|
+
* destructiveHint: may delete, overwrite, end or irreversibly change something.
|
|
9
|
+
* idempotentHint: repeating the call with the same arguments has no further effect.
|
|
10
|
+
* openWorldHint: reaches something outside this server and its files: the printer,
|
|
11
|
+
* Bambu Cloud, a remote slicer, or another application (Blender, Fusion, Bambu Connect).
|
|
12
|
+
*/
|
|
13
|
+
export interface ToolAnnotations {
|
|
14
|
+
readOnlyHint: boolean;
|
|
15
|
+
destructiveHint: boolean;
|
|
16
|
+
idempotentHint: boolean;
|
|
17
|
+
openWorldHint: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const hints = (readOnlyHint: boolean, destructiveHint: boolean, idempotentHint: boolean, openWorldHint: boolean): ToolAnnotations =>
|
|
21
|
+
({ readOnlyHint, destructiveHint, idempotentHint, openWorldHint });
|
|
22
|
+
|
|
23
|
+
/** Reads the printer or another outside system; changes nothing. */
|
|
24
|
+
const READ_OUTSIDE = hints(true, false, true, true);
|
|
25
|
+
/** Reads local files or this server's configuration only. */
|
|
26
|
+
const READ_LOCAL = hints(true, false, true, false);
|
|
27
|
+
/** Writes a fixed-name output into this server's temporary directory, replacing an earlier output of that name; inputs untouched. */
|
|
28
|
+
const LOCAL_OUTPUT = hints(false, true, true, false);
|
|
29
|
+
/** Sets a printer setting to a given value; setting it again changes nothing. */
|
|
30
|
+
const SET_PRINTER = hints(false, false, true, true);
|
|
31
|
+
/** Starts a print job: adds a job, removes nothing, but a repeat would start another. */
|
|
32
|
+
const START_PRINT = hints(false, false, false, true);
|
|
33
|
+
/** Starts a print, auto-slicing an unsliced project first, which replaces an earlier slice output of that name. */
|
|
34
|
+
const SLICE_AND_PRINT = hints(false, true, false, true);
|
|
35
|
+
|
|
36
|
+
export const TOOL_ANNOTATIONS: Record<string, ToolAnnotations> = {
|
|
37
|
+
// Printer and cloud reads
|
|
38
|
+
get_printer_status: READ_OUTSIDE,
|
|
39
|
+
get_printer_filaments: READ_OUTSIDE,
|
|
40
|
+
resolve_3mf_ams_slots: READ_OUTSIDE, // reads the live AMS inventory
|
|
41
|
+
list_printer_files: READ_OUTSIDE,
|
|
42
|
+
counterpart_print_progress: READ_OUTSIDE,
|
|
43
|
+
// connect: true starts the bridge agent, which writes its configuration and device identity and can replace certificates.
|
|
44
|
+
bambu_network_bridge_status: hints(false, true, true, true),
|
|
45
|
+
camera_frame: READ_OUTSIDE,
|
|
46
|
+
// Open (or renew) and close the one camera connection all viewers of this printer share; no printer motion or commands.
|
|
47
|
+
camera_stream_start: hints(false, false, true, true),
|
|
48
|
+
camera_stream_stop: hints(false, false, true, true),
|
|
49
|
+
// Writes save_path, overwriting an existing file there; every call takes a new frame.
|
|
50
|
+
camera_snapshot: hints(false, true, false, true),
|
|
51
|
+
|
|
52
|
+
// Local reads
|
|
53
|
+
inspect_print_file: READ_LOCAL,
|
|
54
|
+
list_3mf_plate_objects: READ_LOCAL,
|
|
55
|
+
get_stl_info: READ_LOCAL,
|
|
56
|
+
list_templates: READ_LOCAL,
|
|
57
|
+
get_slice_settings: LOCAL_OUTPUT, // a 3MF's settings are extracted to a fixed file in the temporary directory
|
|
58
|
+
counterpart_printer_info: READ_LOCAL, // configuration and capabilities; sends nothing to the printer
|
|
59
|
+
|
|
60
|
+
// Local outputs in the server's temporary directory
|
|
61
|
+
extend_stl_base: LOCAL_OUTPUT,
|
|
62
|
+
scale_stl: LOCAL_OUTPUT,
|
|
63
|
+
rotate_stl: LOCAL_OUTPUT,
|
|
64
|
+
merge_vertices: LOCAL_OUTPUT,
|
|
65
|
+
center_model: LOCAL_OUTPUT,
|
|
66
|
+
lay_flat: LOCAL_OUTPUT,
|
|
67
|
+
// Same, but a configured orca-slicer-api slices on a remote service, a new job and output each call.
|
|
68
|
+
slice_stl: hints(false, true, false, true),
|
|
69
|
+
slice_with_template: hints(false, true, false, true),
|
|
70
|
+
// New 3MF at a caller-chosen path; never overwrites, so a repeat fails and changes nothing.
|
|
71
|
+
counterpart_make_3mf: hints(false, false, true, false),
|
|
72
|
+
// Copies into the template registry, replacing a template of the same name.
|
|
73
|
+
save_template: hints(false, true, true, false),
|
|
74
|
+
// Replaces BAMBU_DEFAULT_CONNECTION_MODE/TYPE in .env and in this process.
|
|
75
|
+
counterpart_print_route: hints(false, true, true, false),
|
|
76
|
+
|
|
77
|
+
// Print starts
|
|
78
|
+
start_print: START_PRINT,
|
|
79
|
+
start_print_job: START_PRINT,
|
|
80
|
+
print_3mf: SLICE_AND_PRINT,
|
|
81
|
+
print_3mf_bambu_network: SLICE_AND_PRINT,
|
|
82
|
+
print_collar_charm: SLICE_AND_PRINT,
|
|
83
|
+
upload_file: START_PRINT, // print: true starts the uploaded G-code; uploads never overwrite (unique names)
|
|
84
|
+
upload_gcode: hints(false, false, false, true), // a uniquely named new copy per call
|
|
85
|
+
|
|
86
|
+
// Print controls
|
|
87
|
+
pause_print: SET_PRINTER,
|
|
88
|
+
resume_print: SET_PRINTER,
|
|
89
|
+
cancel_print: hints(false, true, true, true), // ends the job; it cannot be resumed
|
|
90
|
+
skip_objects: hints(false, true, true, true), // skipped objects are not printed and cannot be restored
|
|
91
|
+
|
|
92
|
+
// Printer settings
|
|
93
|
+
set_print_speed: SET_PRINTER,
|
|
94
|
+
set_airduct_mode: SET_PRINTER,
|
|
95
|
+
set_temperature: SET_PRINTER,
|
|
96
|
+
set_fan_speed: SET_PRINTER,
|
|
97
|
+
set_light: SET_PRINTER,
|
|
98
|
+
set_ams_drying: SET_PRINTER,
|
|
99
|
+
reread_ams_rfid: hints(false, false, false, true), // moves AMS filament on every call
|
|
100
|
+
// Replaces stored AMS metadata or the calibration selection on an X2D.
|
|
101
|
+
x2d_native_control: hints(false, true, true, true),
|
|
102
|
+
clear_hms_errors: hints(false, true, true, true), // clears the printer's error record
|
|
103
|
+
delete_printer_file: hints(false, true, true, true),
|
|
104
|
+
// Only handshake and sign-in reads are allowed, but with_agent (the default) starts the bridge agent like the status probe.
|
|
105
|
+
bambu_network_call: hints(false, true, true, true),
|
|
106
|
+
// Opens the file in the Bambu Connect app; does not print.
|
|
107
|
+
bambu_connect_import_file: hints(false, false, false, true),
|
|
108
|
+
|
|
109
|
+
// Blender and Fusion
|
|
110
|
+
blender_mcp_status: READ_OUTSIDE, // connects and lists tools; edits nothing
|
|
111
|
+
blender_mcp_call: hints(false, true, false, true), // any Blender tool, including execute_blender_code
|
|
112
|
+
// The standard route preserves scene objects and needs a new output path; a configured legacy bridge promises neither.
|
|
113
|
+
blender_mcp_edit_model: hints(false, true, false, true),
|
|
114
|
+
fusion_mcp_status: hints(false, false, true, true), // launch: true starts Fusion on this computer
|
|
115
|
+
fusion_mcp_call: hints(false, true, false, true), // any Fusion tool, including execute_code
|
|
116
|
+
fusion_mcp_new_design: hints(false, false, false, true), // opens another new document
|
|
117
|
+
fusion_mcp_export: hints(false, false, true, true), // never overwrites; a repeat fails and changes nothing
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/** Assumed for a tool missing from the table (a bug the tests catch): the least a client should trust it. */
|
|
121
|
+
const UNKNOWN = hints(false, true, false, true);
|
|
122
|
+
|
|
123
|
+
/** The tool with its annotations. */
|
|
124
|
+
export function withAnnotations<T extends { name: string }>(tool: T): T & { annotations: ToolAnnotations } {
|
|
125
|
+
return { ...tool, annotations: TOOL_ANNOTATIONS[tool.name] ?? UNKNOWN };
|
|
126
|
+
}
|