Basic KLE support
This commit is contained in:
parent
d09b3fdf38
commit
1cb9fdc3c2
5 changed files with 154 additions and 77 deletions
78
src/io.js
Normal file
78
src/io.js
Normal file
|
@ -0,0 +1,78 @@
|
|||
const json5 = require('json5')
|
||||
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
|
||||
let format
|
||||
if (a.type(raw)() != 'object') {
|
||||
try {
|
||||
config = yaml.safeLoad(raw)
|
||||
format = 'YAML'
|
||||
} catch (yamlex) {
|
||||
try {
|
||||
config = json5.parse(raw)
|
||||
format = 'JSON'
|
||||
} catch (jsonex) {
|
||||
try {
|
||||
config = new Function(raw)()
|
||||
format = 'JS Code'
|
||||
} catch (codeex) {
|
||||
logger('YAML exception:', yamlex)
|
||||
logger('JSON exception:', jsonex)
|
||||
logger('Code exception:', codeex)
|
||||
throw new Error('Input is not valid YAML, JSON, or JS Code!')
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!config) {
|
||||
throw new Error('Input appears to be empty!')
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// assume it's KLE and try to convert it
|
||||
// if it's not, it'll throw anyway
|
||||
return kle.convert(config, logger)
|
||||
} catch (kleex) {
|
||||
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.json = 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()
|
||||
}
|
||||
if (debug) {
|
||||
result.jscad = script
|
||||
}
|
||||
return result
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue