forked from ergogen/ergogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.js
82 lines (73 loc) · 2.07 KB
/
io.js
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
const yaml = require('js-yaml')
const makerjs = require('makerjs')
const jscad = require('@jscad/openjscad')
const u = require('./utils')
const a = require('./assert')
const kle = require('./kle')
exports.interpret = (raw, logger) => {
let config = raw
let format = 'OBJ'
if (a.type(raw)() == 'string') {
try {
config = yaml.safeLoad(raw)
format = 'YAML'
} catch (yamlex) {
try {
config = new Function(raw)()
a.assert(
a.type(config)() == 'object',
'Input JS Code doesn\'t resolve into an object!'
)
format = 'JS'
} catch (codeex) {
logger('YAML exception:', yamlex)
logger('Code exception:', codeex)
throw new Error('Input is not valid YAML, JSON, or JS Code!')
}
}
}
try {
// assume it's KLE and try to convert it
config = kle.convert(config, logger)
format = 'KLE'
} catch (kleex) {
// nope... nevermind
}
if (a.type(config)() != 'object') {
throw new Error('Input doesn\'t resolve into an object!')
}
if (!Object.keys(config).length) {
throw new Error('Input appears to be empty!')
}
return [config, format]
}
exports.twodee = (model, debug) => {
const assembly = makerjs.model.originate({
models: {
export: u.deepcopy(model)
},
units: 'mm'
})
const result = {
dxf: makerjs.exporter.toDXF(assembly),
}
if (debug) {
result.yaml = assembly
result.svg = makerjs.exporter.toSVG(assembly)
}
return result
}
exports.threedee = async (script, debug) => {
const compiled = await new Promise((resolve, reject) => {
jscad.compile(script, {}).then(compiled => {
resolve(compiled)
})
})
const result = {
stl: jscad.generateOutput('stla', compiled).asBuffer().toString()
}
if (debug) {
result.jscad = script
}
return result
}