mcp-3d-printer-server 1.0.0 → 1.0.2

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 (42) hide show
  1. package/dist/3mf_parser.d.ts +3 -0
  2. package/dist/3mf_parser.js +216 -0
  3. package/dist/index.d.ts +2 -0
  4. package/dist/index.js +1018 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/printers/bambu.d.ts +42 -0
  7. package/dist/printers/bambu.js +284 -0
  8. package/dist/printers/creality.d.ts +10 -0
  9. package/dist/printers/creality.js +90 -0
  10. package/dist/printers/duet.d.ts +10 -0
  11. package/dist/printers/duet.js +66 -0
  12. package/dist/printers/klipper.d.ts +10 -0
  13. package/dist/printers/klipper.js +62 -0
  14. package/dist/printers/octoprint.d.ts +10 -0
  15. package/dist/printers/octoprint.js +97 -0
  16. package/dist/printers/printer-factory.d.ts +9 -0
  17. package/dist/printers/printer-factory.js +33 -0
  18. package/dist/printers/prusa.d.ts +10 -0
  19. package/dist/printers/prusa.js +93 -0
  20. package/dist/printers/repetier.d.ts +10 -0
  21. package/dist/printers/repetier.js +59 -0
  22. package/dist/stl/stl-manipulator.d.ts +140 -0
  23. package/dist/stl/stl-manipulator.js +1081 -0
  24. package/dist/test/stl-manipulator-test.d.ts +2 -0
  25. package/dist/test/stl-manipulator-test.js +91 -0
  26. package/dist/types.d.ts +75 -0
  27. package/dist/types.js +26 -0
  28. package/package.json +9 -3
  29. package/src/printers/bambu.ts +8 -3
  30. package/.dockerignore +0 -52
  31. package/.env.example +0 -17
  32. package/Dockerfile +0 -31
  33. package/docker-compose.yml +0 -31
  34. package/scripts/docker-run.sh +0 -25
  35. package/scripts/mcp-inspector.js +0 -49
  36. package/test/sample_cube.stl +0 -86
  37. package/test/sample_cube_extended.stl +0 -172
  38. package/test/sample_cube_modified.stl +0 -86
  39. package/test/sample_cube_rotated.stl +0 -86
  40. package/test/sample_cube_scaled.stl +0 -86
  41. package/test/sample_cube_translated.stl +0 -86
  42. package/tsconfig.json +0 -16
@@ -0,0 +1,3 @@
1
+ import { ThreeMFData } from './types.js';
2
+ export { ThreeMFData };
3
+ export declare function parse3MF(filePath: string): Promise<ThreeMFData>;
@@ -0,0 +1,216 @@
1
+ import JSZip from 'jszip';
2
+ import { parseStringPromise } from 'xml2js';
3
+ import * as fs from 'fs/promises';
4
+ // Parses Bambu Studio's JSON config data
5
+ function parseBambuJSONConfig(jsonData) {
6
+ const config = {};
7
+ const amsMapping = {};
8
+ // Map known keys, converting types as needed
9
+ if (jsonData.layer_height !== undefined)
10
+ config.layer_height = parseFloat(jsonData.layer_height);
11
+ if (jsonData.initial_layer_print_height !== undefined)
12
+ config.first_layer_height = parseFloat(jsonData.initial_layer_print_height);
13
+ if (jsonData.sparse_infill_density !== undefined)
14
+ config.sparse_infill_density = parseFloat(jsonData.sparse_infill_density);
15
+ if (jsonData.sparse_infill_pattern !== undefined)
16
+ config.sparse_infill_pattern = String(jsonData.sparse_infill_pattern);
17
+ if (jsonData.enable_support !== undefined)
18
+ config.support_enabled = String(jsonData.enable_support) === '1' || String(jsonData.enable_support).toLowerCase() === 'true';
19
+ if (jsonData.support_type !== undefined)
20
+ config.support_type = String(jsonData.support_type);
21
+ if (jsonData.support_angle !== undefined)
22
+ config.support_threshold_angle = parseFloat(jsonData.support_angle);
23
+ if (jsonData.raft_layers !== undefined)
24
+ config.raft_layers = parseInt(String(jsonData.raft_layers), 10);
25
+ if (jsonData.brim_width !== undefined)
26
+ config.brim_width = parseFloat(jsonData.brim_width);
27
+ if (jsonData.wall_loops !== undefined)
28
+ config.wall_loops = parseInt(String(jsonData.wall_loops), 10);
29
+ if (jsonData.top_shell_layers !== undefined)
30
+ config.top_shell_layers = parseInt(String(jsonData.top_shell_layers), 10);
31
+ if (jsonData.bottom_shell_layers !== undefined)
32
+ config.bottom_shell_layers = parseInt(String(jsonData.bottom_shell_layers), 10);
33
+ // Temperatures (handle potential arrays)
34
+ if (Array.isArray(jsonData.nozzle_temperature) && jsonData.nozzle_temperature.length > 0) {
35
+ config.nozzle_temperature = jsonData.nozzle_temperature.map((t) => parseFloat(String(t)));
36
+ }
37
+ else if (jsonData.nozzle_temperature !== undefined) {
38
+ config.nozzle_temperature = [parseFloat(String(jsonData.nozzle_temperature))];
39
+ }
40
+ // Bed Temp (prefer initial layer temp)
41
+ let bedTempKey = jsonData.hot_plate_temp_initial_layer !== undefined ? 'hot_plate_temp_initial_layer' : 'hot_plate_temp';
42
+ if (jsonData[bedTempKey] !== undefined) {
43
+ if (Array.isArray(jsonData[bedTempKey]) && jsonData[bedTempKey].length > 0) {
44
+ config.bed_temperature = parseFloat(String(jsonData[bedTempKey][0]));
45
+ }
46
+ else {
47
+ config.bed_temperature = parseFloat(String(jsonData[bedTempKey]));
48
+ }
49
+ }
50
+ // Filaments and Flow (handle potential arrays)
51
+ if (Array.isArray(jsonData.filament_type) && jsonData.filament_type.length > 0) {
52
+ config.filament_type = jsonData.filament_type.map(String);
53
+ }
54
+ if (Array.isArray(jsonData.filament_flow_ratio) && jsonData.filament_flow_ratio.length > 0) {
55
+ config.flow_ratio = jsonData.filament_flow_ratio.map((f) => parseFloat(String(f)));
56
+ }
57
+ // AMS Mapping (derive from filament_settings_id order)
58
+ if (Array.isArray(jsonData.filament_settings_id) && jsonData.filament_settings_id.length > 0) {
59
+ jsonData.filament_settings_id.forEach((filamentId, index) => {
60
+ // Map filament ID (e.g., "Generic PLA @BBL P1P") to its index (assumed AMS slot)
61
+ if (filamentId) { // Ensure filamentId is not empty
62
+ amsMapping[filamentId] = index;
63
+ }
64
+ });
65
+ if (Object.keys(amsMapping).length > 0) {
66
+ config.ams_mapping = amsMapping;
67
+ }
68
+ }
69
+ // Store remaining keys found in the config
70
+ for (const key in jsonData) {
71
+ if (!config.hasOwnProperty(key)) {
72
+ // Simple assignment for remaining keys, preserving original type if possible
73
+ config[key] = jsonData[key];
74
+ }
75
+ }
76
+ return config;
77
+ }
78
+ async function parseBambuConfig(zip) {
79
+ // Look for common Bambu config file names
80
+ const potentialFiles = ['Metadata/project_settings.config', 'Metadata/model_settings.config', 'Metadata/slice_info.config'];
81
+ let configFile = null;
82
+ let configContent = '';
83
+ for (const name of potentialFiles) {
84
+ const file = zip.file(name);
85
+ if (file) {
86
+ configFile = file;
87
+ configContent = await file.async('string');
88
+ console.log(`Found Bambu config file: ${configFile.name}`);
89
+ break; // Use the first one found (project usually has the most)
90
+ }
91
+ }
92
+ if (configFile && configContent) {
93
+ try {
94
+ // Attempt to parse as JSON
95
+ const jsonData = JSON.parse(configContent);
96
+ const parsedConfig = parseBambuJSONConfig(jsonData);
97
+ console.log('Bambu config parsed successfully as JSON.');
98
+ return parsedConfig;
99
+ }
100
+ catch (jsonError) {
101
+ console.warn(`Failed to parse ${configFile.name} as JSON: ${jsonError.message}. Attempting INI parse as fallback...`);
102
+ // Fallback: Try parsing as INI if JSON fails (though unlikely based on sample)
103
+ try {
104
+ const parsedConfig = parseFallbackINIConfig(configContent); // Keep a simple INI parser as fallback
105
+ console.log('Bambu config parsed successfully as INI (fallback).');
106
+ return parsedConfig;
107
+ }
108
+ catch (iniError) {
109
+ console.error(`Error parsing Bambu config ${configFile.name} as INI (fallback):`, iniError);
110
+ return {};
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ console.log('No Bambu-specific config file found in Metadata directory.');
116
+ return {};
117
+ }
118
+ }
119
+ // Fallback INI parser (Simplified version of the previous one)
120
+ function parseFallbackINIConfig(configContent) {
121
+ const config = {};
122
+ const lines = configContent.split(/\r?\n/);
123
+ for (const line of lines) {
124
+ const trimmedLine = line.trim();
125
+ if (!trimmedLine || trimmedLine.startsWith('#') || trimmedLine.startsWith(';'))
126
+ continue;
127
+ const equalsIndex = trimmedLine.indexOf('=');
128
+ if (equalsIndex === -1)
129
+ continue;
130
+ const key = trimmedLine.substring(0, equalsIndex).trim();
131
+ const value = trimmedLine.substring(equalsIndex + 1).trim();
132
+ // Simple assignment - no type conversion here for fallback
133
+ config[key] = value;
134
+ }
135
+ return config;
136
+ }
137
+ async function parse3DModelConfig(zip) {
138
+ const modelFile = zip.file('3D/3dmodel.model');
139
+ if (!modelFile) {
140
+ throw new Error('3D/3dmodel.model not found in 3MF archive');
141
+ }
142
+ const modelContent = await modelFile.async('string');
143
+ try {
144
+ const parsedXml = await parseStringPromise(modelContent, {
145
+ explicitArray: false,
146
+ mergeAttrs: true,
147
+ charkey: 'value'
148
+ });
149
+ const modelData = parsedXml.model;
150
+ if (!modelData) {
151
+ throw new Error('Invalid 3dmodel.model format: <model> tag not found.');
152
+ }
153
+ const metadata = { unit: modelData.unit || 'millimeter' };
154
+ if (modelData.metadata) {
155
+ const metas = Array.isArray(modelData.metadata) ? modelData.metadata : [modelData.metadata];
156
+ metas.forEach((meta) => {
157
+ if (meta.name && meta.value !== undefined) {
158
+ metadata[meta.name] = String(meta.value);
159
+ }
160
+ });
161
+ }
162
+ const objects = [];
163
+ if (modelData.resources?.object) {
164
+ const resources = Array.isArray(modelData.resources.object) ? modelData.resources.object : [modelData.resources.object];
165
+ resources.forEach((objResource) => {
166
+ objects.push({
167
+ id: objResource.id,
168
+ name: objResource.name,
169
+ type: objResource.type || 'model'
170
+ // Actual mesh data (vertices, triangles) is nested deeper if needed
171
+ });
172
+ });
173
+ }
174
+ const buildItems = [];
175
+ if (modelData.build?.item) {
176
+ const items = Array.isArray(modelData.build.item) ? modelData.build.item : [modelData.build.item];
177
+ items.forEach((item) => {
178
+ buildItems.push({
179
+ objectId: item.objectid,
180
+ transform: item.transform
181
+ });
182
+ });
183
+ }
184
+ console.log('3dmodel.model parsed successfully.');
185
+ return { metadata, objects, build: { items: buildItems } };
186
+ }
187
+ catch (error) {
188
+ console.error('Error parsing 3dmodel.model XML:', error);
189
+ throw new Error(`Failed to parse 3dmodel.model: ${error.message}`);
190
+ }
191
+ }
192
+ export async function parse3MF(filePath) {
193
+ console.log(`Parsing 3MF file: ${filePath}`);
194
+ try {
195
+ const data = await fs.readFile(filePath);
196
+ const zip = await JSZip.loadAsync(data);
197
+ // List files for debugging
198
+ // const fileList = Object.keys(zip.files);
199
+ // console.log('Files in 3MF:', fileList);
200
+ const { metadata, objects, build } = await parse3DModelConfig(zip);
201
+ const bambuConfig = await parseBambuConfig(zip);
202
+ // Combine data into the final structure
203
+ const combinedData = {
204
+ metadata,
205
+ objects,
206
+ build,
207
+ slicerConfig: bambuConfig
208
+ };
209
+ console.log('3MF parsing completed.');
210
+ return combinedData;
211
+ }
212
+ catch (error) {
213
+ console.error(`Error parsing 3MF file ${filePath}:`, error);
214
+ throw new Error(`Failed to parse 3MF file: ${error.message}`);
215
+ }
216
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};