This repository was archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
121 lines (105 loc) · 2.91 KB
/
index.ts
File metadata and controls
121 lines (105 loc) · 2.91 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
/* eslint-disable camelcase */
/**
* @fileoverview Cura WASM Definitions
*/
//Imports
import {clone, merge} from 'lodash';
import {extruders, printers} from './definitions/index';
import type {Extruder, Printer, ExtruderID, PrinterID, CombinedDefinition} from './types';
//Primary definitions are bundled with "cura-wasm" itself
const primaryExtruders = ['fdmextruder'];
const primaryPrinters = ['fdmextruder', 'fdmprinter'];
/**
* Recursively resolve an extruder (Squash all depended-on extruders together)
* @param id The ID of the extruder
*/
export const resolveExtruder = (id: ExtruderID): Extruder =>
{
//Don't resolve primary extruders (They're bundled with Cura WASM)
if (primaryExtruders.includes(id))
{
return null;
}
else
{
//Get the extruder
const extruder = <Extruder>clone(extruders[id]);
//Resolve parent definitions
if (extruder != null && extruder.inherits != null)
{
//Get parent extruder
const parent = resolveExtruder(extruder.inherits);
//Delete secondary inherited extruders (Because we're resolving them)
if (!primaryExtruders.includes(extruder.inherits))
{
delete extruder.inherits;
}
//Merge parent and current extruder
return merge(parent, extruder);
}
else
{
return extruder;
}
}
};
/**
* Recursively resolve a definition (Squash all depended-on defintions together)
* @param id The ID of the printer
*/
export const resolvePrinter = (id: PrinterID): Printer =>
{
//Don't resolve primary definitions (They're are bundled with Cura WASM)
if (primaryPrinters.includes(id))
{
return null;
}
else
{
//Get the printer
const printer = <Printer>clone(printers[id]);
//Resolve parent definitions
if (printer != null && printer.inherits != null)
{
const parent = resolvePrinter(printer.inherits);
//Delete secondary inherited definitions (Because we're resolving them)
if (!primaryPrinters.includes(printer.inherits))
{
delete printer.inherits;
}
return merge(parent, printer);
}
else
{
return printer;
}
}
};
/**
* Resolve an extruder and a printer
* @param id The ID of the printer
*/
export const resolveDefinition = (id: PrinterID): CombinedDefinition =>
{
//Get and resolve the printer
const printer = resolvePrinter(id);
//Get and resolve the extruders
let extruders: Extruder[] = [];
if (printer != null && printer.metadata.machine_extruder_trains != null)
{
extruders = Object.values(printer.metadata.machine_extruder_trains)
.map(extruder => resolveExtruder(extruder));
//Generate new machine extruder trains
const extruderTrains = {};
for (const key in extruders)
{
extruderTrains[key] = `extruder-${key}`;
}
//Overwrite the extruder
printer.metadata.machine_extruder_trains = extruderTrains;
}
return {
extruders,
printer
};
};