-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorca_config_parser.js
More file actions
416 lines (363 loc) · 18.1 KB
/
orca_config_parser.js
File metadata and controls
416 lines (363 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
const fs = require('fs');
const path = require('path');
/**
* Парсер конфигураций OrcaSlicer для PA Test Generator
*
* ТАБЛИЦА РАЗЛИЧИЙ ПАРАМЕТРОВ МЕЖДУ PRUSA/QIDI И ORCA:
*
* Принтер (Machine/Printer):
* - start_gcode ↔ machine_start_gcode
* - end_gcode ↔ machine_end_gcode
* - retract_length ↔ retraction_length
* - retract_speed ↔ retraction_speed
* - bed_shape ↔ printable_area
* - max_print_height ↔ printable_height
*
* Филамент:
* - temperature ↔ nozzle_temperature
* - first_layer_temperature ↔ nozzle_temperature_initial_layer
* - bed_temperature ↔ hot_plate_temp
* - first_layer_bed_temperature ↔ hot_plate_temp_initial_layer
* - extrusion_multiplier ↔ filament_flow_ratio
* - max_volumetric_speed ↔ filament_max_volumetric_speed
* - disable_fan_first_layers ↔ close_fan_the_first_x_layers
*
* Печать (Process/Print):
* - first_layer_height ↔ initial_layer_print_height
* - perimeter_speed ↔ inner_wall_speed
* - external_perimeter_speed ↔ outer_wall_speed
* - infill_speed ↔ sparse_infill_speed
* - solid_infill_speed ↔ internal_solid_infill_speed
* - top_solid_infill_speed ↔ top_surface_speed
* - bridge_flow_ratio ↔ bridge_flow
* - support_material_buildplate_only ↔ support_on_build_plate_only
*/
class OrcaConfigParser {
/**
* Конструктор парсера OrcaSlicer
* @param {string} orcaPath - Путь к папке OrcaSlicer (опционально)
*/
constructor(orcaPath) {
if (!orcaPath) {
const realPath = path.join(require('os').homedir(), 'AppData', 'Roaming', 'OrcaSlicer');
const testPath = './ini_examples/orcaslicer';
orcaPath = require('fs').existsSync(realPath) ? realPath : testPath;
}
this.orcaPath = orcaPath;
this.userPath = path.join(orcaPath, 'user', 'default');
this.systemPath = path.join(orcaPath, 'system');
console.log('OrcaSlicer путь:', orcaPath);
}
/**
* Получить список пользовательских принтеров
* @returns {Array} Массив объектов с информацией о принтерах
*/
getPrinters() {
const printers = [];
const machinePath = path.join(this.userPath, 'machine');
if (!fs.existsSync(machinePath)) return printers;
const files = fs.readdirSync(machinePath)
.filter(f => f.endsWith('.json'));
files.forEach(file => {
try {
const config = JSON.parse(fs.readFileSync(path.join(machinePath, file), 'utf8'));
printers.push({
name: config.name,
inherits: config.inherits,
gcodeType: config.gcode_flavor || 'marlin',
file: path.join(machinePath, file),
print_host: config.print_host
});
} catch (e) {
console.error(`Ошибка парсинга ${file}:`, e.message);
}
});
return printers;
}
/**
* Получить список пользовательских филаментов
* @returns {Array} Массив объектов с информацией о филаментах
*/
getFilaments() {
const filaments = [];
const filamentPath = path.join(this.userPath, 'filament');
if (!fs.existsSync(filamentPath)) return filaments;
const files = fs.readdirSync(filamentPath)
.filter(f => f.endsWith('.json'));
files.forEach(file => {
try {
const config = JSON.parse(fs.readFileSync(path.join(filamentPath, file), 'utf8'));
const pressureAdvance = this.extractPressureAdvance(config);
let flowRatio = config.filament_flow_ratio?.[0] || '1.000';
if (flowRatio === '1.000' && config.inherits) {
const systemConfig = this.getSystemConfig('filament',config.inherits);
flowRatio = systemConfig?.filament_flow_ratio?.[0] || '1.0';
}
filaments.push({
name: config.name,
inherits: config.inherits,
pressureAdvance: pressureAdvance,
flowRatio: flowRatio,
maxVolumetricSpeed: config.filament_max_volumetric_speed?.[0],
nozzleTemp: config.nozzle_temperature?.[0],
bedTemp: config.hot_plate_temp?.[0],
file: path.join(filamentPath, file)
});
} catch (e) {
console.error(`Ошибка парсинга ${file}:`, e.message);
}
});
return filaments;
}
/**
* Получить список пользовательских профилей печати
* @returns {Array} Массив объектов с информацией о профилях печати
*/
getPrintProfiles() {
const profiles = [];
const processPath = path.join(this.userPath, 'process');
if (!fs.existsSync(processPath)) return profiles;
const files = fs.readdirSync(processPath)
.filter(f => f.endsWith('.json'));
files.forEach(file => {
try {
const config = JSON.parse(fs.readFileSync(path.join(processPath, file), 'utf8'));
profiles.push({
name: config.name,
inherits: config.inherits,
layerHeight: this.extractLayerHeight(config.name),
file: path.join(processPath, file)
});
} catch (e) {
console.error(`Ошибка парсинга ${file}:`, e.message);
}
});
return profiles;
}
/**
* Извлечь высоту слоя из названия профиля
* @param {string} name - Название профиля
* @returns {number} Высота слоя в мм
*/
extractLayerHeight(name) {
const match = name.match(/(\d+\.?\d*)mm/);
return match ? parseFloat(match[1]) : 0.2;
}
/**
* Извлечь значение Pressure Advance из G-code филамента
* @param {Object} config - Конфигурация филамента
* @returns {string} Значение Pressure Advance
*/
extractPressureAdvance(config) {
const startGcode = config.filament_start_gcode?.[0] || '';
const match = startGcode.match(/M900\s+K([\d.]+)/);
return match ? match[1] : '0.03';
}
/**
* Конвертировать конфигурацию принтера с нормализацией параметров
* @param {string} printerName - Имя принтера
* @returns {Object|null} Нормализованная конфигурация принтера
*/
convertPrinterConfig(printerName) {
const printers = this.getPrinters();
const printer = printers.find(p => p.name === printerName);
if (!printer) return null;
const userConfig = JSON.parse(fs.readFileSync(printer.file, 'utf8'));
const systemConfig = this.getSystemConfig('machine', printer.inherits);
const merged = { ...systemConfig, ...userConfig };
return this.convertArraysToValues(merged);
}
/**
* Конвертировать конфигурацию филамента с нормализацией параметров
* @param {string} filamentName - Имя филамента
* @returns {Object|null} Нормализованная конфигурация филамента
*/
convertFilamentConfig(filamentName) {
const filaments = this.getFilaments();
const filament = filaments.find(f => f.name === filamentName);
if (!filament) return null;
const userConfig = JSON.parse(fs.readFileSync(filament.file, 'utf8'));
const systemConfig = this.getSystemConfig('filament', filament.inherits);
const merged = { ...systemConfig, ...userConfig };
if(!merged.chamber_temperature) merged.chamber_temperature = [''];
return this.convertArraysToValues(merged);
}
/**
* Конвертировать конфигурацию процесса печати с нормализацией параметров
* @param {string} processName - Имя процесса
* @returns {Object|null} Нормализованная конфигурация процесса
*/
convertProcessConfig(processName) {
const processes = this.getPrintProfiles();
const process = processes.find(p => p.name === processName);
if (!process) return null;
const userConfig = JSON.parse(fs.readFileSync(process.file, 'utf8'));
const systemConfig = this.getSystemConfig('process', process.inherits);
const merged = { ...systemConfig, ...userConfig };
return this.convertArraysToValues(merged);
}
/**
* Преобразовать массивы с одним элементом в значения и нормализовать имена параметров
* @param {Object} config - Исходная конфигурация
* @returns {Object} Конфигурация с преобразованными массивами и нормализованными именами
*/
convertArraysToValues(config) {
const result = {};
for (const [key, value] of Object.entries(config)) {
if (Array.isArray(value) && value.length === 1) {
result[key] = value[0];
} else {
result[key] = value;
}
}
return this.normalizeOrcaToPrusaNames(result);
}
/**
* Нормализовать имена параметров OrcaSlicer в имена PrusaSlicer/QIDISlicer
* Дублирует параметры с разными именами для совместимости с генератором
* @param {Object} config - Конфигурация с параметрами OrcaSlicer
* @returns {Object} Конфигурация с дублированными параметрами
*/
normalizeOrcaToPrusaNames(config) {
// Принтер (Machine -> Printer)
if (config.machine_start_gcode) config.start_gcode = config.machine_start_gcode;
if (config.machine_end_gcode) config.end_gcode = config.machine_end_gcode;
if (config.retraction_length) config.retract_length = config.retraction_length;
if (config.retraction_speed) config.retract_speed = config.retraction_speed;
if (config.printable_area) config.bed_shape = this.convertPrintableAreaToBedShape(config.printable_area);
if (config.printable_height) config.max_print_height = config.printable_height;
// Филамент
if (config.nozzle_temperature) config.temperature = config.nozzle_temperature;
if (config.nozzle_temperature_initial_layer) config.first_layer_temperature = config.nozzle_temperature_initial_layer;
if (config.hot_plate_temp) config.bed_temperature = config.hot_plate_temp;
if (config.hot_plate_temp_initial_layer) config.first_layer_bed_temperature = config.hot_plate_temp_initial_layer;
if (config.filament_flow_ratio) config.extrusion_multiplier = config.filament_flow_ratio;
if (config.filament_max_volumetric_speed) config.max_volumetric_speed = config.filament_max_volumetric_speed;
if (config.close_fan_the_first_x_layers) config.disable_fan_first_layers = config.close_fan_the_first_x_layers;
// Печать (Process -> Print)
if (config.initial_layer_print_height) config.first_layer_height = config.initial_layer_print_height;
if (config.inner_wall_speed) config.perimeter_speed = config.inner_wall_speed;
if (config.outer_wall_speed) config.external_perimeter_speed = config.outer_wall_speed;
if (config.sparse_infill_speed) config.infill_speed = config.sparse_infill_speed;
if (config.internal_solid_infill_speed) config.solid_infill_speed = config.internal_solid_infill_speed;
if (config.top_surface_speed) config.top_solid_infill_speed = config.top_surface_speed;
if (config.bridge_flow) config.bridge_flow_ratio = config.bridge_flow;
if (config.support_on_build_plate_only) config.support_material_buildplate_only = config.support_on_build_plate_only;
return config;
}
/**
* Преобразовать printable_area OrcaSlicer в bed_shape PrusaSlicer
* @param {Array} printableArea - Массив точек области печати
* @returns {string} Строка формата bed_shape
*/
convertPrintableAreaToBedShape(printableArea) {
if (!Array.isArray(printableArea)) return '0x0,200x0,200x200,0x200';
return printableArea.join(',');
}
/**
* Получить системную конфигурацию с обработкой наследования
* @param {string} configType - Тип конфигурации (machine, filament, process)
* @param {string} inheritsName - Имя родительской конфигурации
* @returns {Object|null} Системная конфигурация или null
*/
getSystemConfig(configType, inheritsName) {
if (!inheritsName) return null;
const dirs = fs.readdirSync(this.systemPath);
for (const dir of dirs) {
if (dir === '.' || dir === '..') continue;
const configPath = path.join(this.systemPath, dir, configType, inheritsName + '.json');
if (fs.existsSync(configPath)) {
let config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
// Рекурсивная обработка наследования
if (config.inherits) {
const parentConfig = this.getSystemConfig(configType, config.inherits);
if (parentConfig) {
config = {...parentConfig, ...config};
}
}
return config;
}
}
return null;
}
/**
* Извлечь размер стола из printable_area
* @param {Array} printableArea - Массив точек области печати
* @param {string} axis - Ось ('x' или 'y')
* @returns {number} Размер по указанной оси
*/
extractBedSize(printableArea, axis) {
if (!printableArea || !Array.isArray(printableArea)) return 200;
const coords = printableArea.map(point => {
const [x, y] = point.split('x').map(Number);
return axis === 'x' ? x : y;
});
return Math.max(...coords);
}
/**
* Определить тип филамента по имени наследования
* @param {string} inherits - Имя родительского профиля
* @returns {string} Тип филамента
*/
getFilamentType(inherits) {
if (!inherits) return 'PLA';
if (inherits.includes('PLA')) return 'PLA';
if (inherits.includes('ABS')) return 'ABS';
if (inherits.includes('PETG')) return 'PETG';
if (inherits.includes('TPU')) return 'TPU';
if (inherits.includes('PA')) return 'PA';
if (inherits.includes('PC')) return 'PC';
return 'PLA';
}
/**
* Получить системное имя принтера
* @param {string} userPrinterName - Пользовательское имя принтера
* @returns {string} Системное имя принтера
*/
getSystemPrinterName(userPrinterName) {
const printers = this.getPrinters();
const printer = printers.find(p => p.name === userPrinterName);
return printer?.inherits || userPrinterName;
}
/**
* Получить совместимые филаменты для принтера
* @param {string} printerName - Имя принтера
* @returns {Array} Массив совместимых филаментов
*/
getCompatibleFilaments(printerName) {
const allFilaments = this.getFilaments();
const systemPrinterName = this.getSystemPrinterName(printerName);
const compatibleFilaments = allFilaments.filter(filament => {
const sysConfig = this.getSystemConfig('filament', filament.inherits);
return !sysConfig || this.isCompatibleWithPrinter(sysConfig, systemPrinterName);
});
return compatibleFilaments.length > 0 ? compatibleFilaments : allFilaments;
}
/**
* Получить совместимые процессы печати для принтера
* @param {string} printerName - Имя принтера
* @returns {Array} Массив совместимых процессов
*/
getCompatibleProcesses(printerName) {
const allProcesses = this.getPrintProfiles();
const systemPrinterName = this.getSystemPrinterName(printerName);
const compatibleProcesses = allProcesses.filter(process => {
const sysConfig = this.getSystemConfig('process', process.inherits);
return !sysConfig || this.isCompatibleWithPrinter(sysConfig, systemPrinterName);
});
return compatibleProcesses.length > 0 ? compatibleProcesses : allProcesses;
}
/**
* Проверить совместимость конфигурации с принтером
* @param {Object} config - Конфигурация для проверки
* @param {string} printerName - Имя принтера
* @returns {boolean} true если совместимо
*/
isCompatibleWithPrinter(config, printerName) {
if (!config.compatible_printers) return true;
const systemPrinterName = this.getSystemPrinterName(printerName);
return config.compatible_printers.includes(printerName) ||
config.compatible_printers.includes(systemPrinterName);
}
}
module.exports = OrcaConfigParser;