CLI and output restructure, SVG/DXF/STL integration
This commit is contained in:
parent
c49881c2b4
commit
42a3e2de55
7 changed files with 730 additions and 154 deletions
113
src/cli.js
Normal file → Executable file
113
src/cli.js
Normal file → Executable file
|
@ -1,26 +1,15 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
// libs
|
||||
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const yaml = require('js-yaml')
|
||||
const yargs = require('yargs')
|
||||
|
||||
// internals
|
||||
|
||||
const io = require('./io')
|
||||
const ergogen = require('./ergogen')
|
||||
const pkg = require('../package.json')
|
||||
|
||||
// command line args
|
||||
|
||||
const args = yargs
|
||||
.option('config', {
|
||||
alias: 'c',
|
||||
demandOption: true,
|
||||
describe: 'Config yaml/json file',
|
||||
type: 'string'
|
||||
})
|
||||
.option('output', {
|
||||
alias: 'o',
|
||||
default: path.resolve('output'),
|
||||
|
@ -40,60 +29,100 @@ const args = yargs
|
|||
})
|
||||
.argv
|
||||
|
||||
if (args.clean) fs.removeSync(args.o)
|
||||
fs.mkdirpSync(args.o)
|
||||
// config reading
|
||||
|
||||
// config parsing
|
||||
const config_file = args._[0]
|
||||
if (!config_file) {
|
||||
console.error('Usage: ergogen <config_file> [options]')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
let config_text
|
||||
try {
|
||||
config_text = fs.readFileSync(args.c).toString()
|
||||
config_text = fs.readFileSync(config_file).toString()
|
||||
} catch (err) {
|
||||
throw new Error(`Could not read file "${args.c}": ${err}`)
|
||||
console.error(`Could not read config file "${config_file}": ${err}`)
|
||||
process.exit(2)
|
||||
}
|
||||
|
||||
const is_yaml = args.c.endsWith('.yaml') || args.c.endsWith('.yml')
|
||||
const config_parser = is_yaml ? yaml.load : JSON.parse
|
||||
let config
|
||||
try {
|
||||
config = config_parser(config_text)
|
||||
} catch (err) {
|
||||
throw new Error(`Malformed input within "${args.c}": ${err}`)
|
||||
}
|
||||
const title_suffix = args.debug ? ' (Debug Mode)' : ''
|
||||
console.log(`Ergogen v${pkg.version} CLI${title_suffix}`)
|
||||
console.log()
|
||||
|
||||
;(async () => {
|
||||
|
||||
// processing
|
||||
|
||||
const results = ergogen.process(config, args.debug, s => console.log(s))
|
||||
let results
|
||||
try {
|
||||
results = await ergogen.process(config_text, args.debug, s => console.log(s))
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
process.exit(3)
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
const single = (data, rel) => {
|
||||
if (!data) return
|
||||
const abs = path.join(args.o, rel)
|
||||
fs.mkdirpSync(path.dirname(abs))
|
||||
if (abs.endsWith('.json')) {
|
||||
fs.writeJSONSync(abs, data, {spaces: 4})
|
||||
} else if (abs.endsWith('.yaml')) {
|
||||
fs.writeFileSync(abs, yaml.dump(data, {indent: 4}))
|
||||
} else {
|
||||
fs.writeFileSync(abs, data)
|
||||
}
|
||||
}
|
||||
|
||||
const composite = (data, rel) => {
|
||||
if (!data) return
|
||||
const abs = path.join(args.o, rel)
|
||||
if (data.json) {
|
||||
fs.mkdirpSync(path.dirname(abs))
|
||||
fs.writeJSONSync(abs + '.json', data.json, {spaces: 4})
|
||||
}
|
||||
for (const format of ['svg', 'dxf', 'jscad', 'stl']) {
|
||||
if (data[format]) {
|
||||
fs.mkdirpSync(path.dirname(abs))
|
||||
fs.writeFileSync(abs + '.' + format, data[format])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// output
|
||||
|
||||
console.log('Writing output to disk...')
|
||||
|
||||
if (args.debug) {
|
||||
io.dump_model(results.demo, path.join(args.o, 'points/demo'), args.debug)
|
||||
fs.writeJSONSync(path.join(args.o, 'points/data.json'), results.points, {spaces: 4})
|
||||
if (args.clean) {
|
||||
console.log('Cleaning output folder...')
|
||||
fs.removeSync(args.o)
|
||||
}
|
||||
|
||||
console.log('Writing output to disk...')
|
||||
fs.mkdirpSync(args.o)
|
||||
|
||||
single(results.raw, 'raw.txt')
|
||||
single(results.canonical, 'canonical.yaml')
|
||||
single(results.units, 'units.json')
|
||||
|
||||
single(results.points, 'points/points.json')
|
||||
composite(results.demo, 'points/demo')
|
||||
|
||||
for (const [name, outline] of Object.entries(results.outlines)) {
|
||||
io.dump_model(outline, path.join(args.o, `outlines/${name}`), args.debug)
|
||||
composite(outline, `outlines/${name}`)
|
||||
}
|
||||
|
||||
for (const [name, _case] of Object.entries(results.cases)) {
|
||||
const file = path.join(args.o, `cases/${name}.jscad`)
|
||||
fs.mkdirpSync(path.dirname(file))
|
||||
fs.writeFileSync(file, _case)
|
||||
composite(_case, `cases/${name}`)
|
||||
}
|
||||
|
||||
for (const [name, pcb] of Object.entries(results.pcbs)) {
|
||||
const file = path.join(args.o, `pcbs/${name}.kicad_pcb`)
|
||||
fs.mkdirpSync(path.dirname(file))
|
||||
fs.writeFileSync(file, pcb)
|
||||
}
|
||||
|
||||
if (args.debug) {
|
||||
fs.writeJSONSync(path.join(args.o, 'results.json'), results, {spaces: 4})
|
||||
single(pcb, `pcbs/${name}.kicad_pcb`)
|
||||
}
|
||||
|
||||
// goodbye
|
||||
|
||||
console.log('Done.')
|
||||
console.log()
|
||||
|
||||
})()
|
||||
|
|
|
@ -1,3 +1,11 @@
|
|||
const yaml = require('js-yaml')
|
||||
const json5 = require('json5')
|
||||
const makerjs = require('makerjs')
|
||||
const jscad = require('@jscad/openjscad')
|
||||
|
||||
const a = require('./assert')
|
||||
const u = require('./utils')
|
||||
|
||||
const prepare = require('./prepare')
|
||||
const units_lib = require('./units')
|
||||
const points_lib = require('./points')
|
||||
|
@ -7,16 +15,79 @@ const pcbs_lib = require('./pcbs')
|
|||
|
||||
const noop = () => {}
|
||||
|
||||
const 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
|
||||
}
|
||||
|
||||
const 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
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
version: '__ergogen_version',
|
||||
process: (config, debug=false, logger=noop) => {
|
||||
process: async (raw, debug=false, logger=noop) => {
|
||||
|
||||
logger('Preparing input...')
|
||||
const prefix = 'Interpreting format... '
|
||||
let config = raw
|
||||
if (a.type(raw)() != 'object') {
|
||||
try {
|
||||
config = yaml.safeLoad(raw)
|
||||
logger(prefix + 'YAML detected.')
|
||||
} catch (yamlex) {
|
||||
try {
|
||||
config = json5.parse(raw)
|
||||
logger(prefix + 'JSON detected.')
|
||||
} catch (jsonex) {
|
||||
try {
|
||||
config = new Function(raw)()
|
||||
logger(prefix + 'JS code detected.')
|
||||
} 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!')
|
||||
}
|
||||
}
|
||||
|
||||
logger('Preprocessing input...')
|
||||
config = prepare.unnest(config)
|
||||
config = prepare.inherit(config)
|
||||
const results = {}
|
||||
if (debug) {
|
||||
results.raw = raw
|
||||
results.canonical = config
|
||||
}
|
||||
|
||||
// parsing units
|
||||
logger('Calculating variables...')
|
||||
const units = units_lib.parse(config)
|
||||
if (debug) {
|
||||
|
@ -24,10 +95,13 @@ module.exports = {
|
|||
}
|
||||
|
||||
logger('Parsing points...')
|
||||
if (!config.points) {
|
||||
throw new Error('Input does not contain any points!')
|
||||
}
|
||||
const points = points_lib.parse(config.points, units)
|
||||
if (debug) {
|
||||
results.points = points
|
||||
results.demo = points_lib.visualize(points)
|
||||
results.demo = twodee(points_lib.visualize(points), debug)
|
||||
}
|
||||
|
||||
logger('Generating outlines...')
|
||||
|
@ -35,15 +109,15 @@ module.exports = {
|
|||
results.outlines = {}
|
||||
for (const [name, outline] of Object.entries(outlines)) {
|
||||
if (!debug && name.startsWith('_')) continue
|
||||
results.outlines[name] = outline
|
||||
results.outlines[name] = twodee(outline, debug)
|
||||
}
|
||||
|
||||
logger('Extruding cases...')
|
||||
const cases = cases_lib.parse(config.cases || {}, outlines, units)
|
||||
results.cases = {}
|
||||
for (const [case_name, case_text] of Object.entries(cases)) {
|
||||
for (const [case_name, case_script] of Object.entries(cases)) {
|
||||
if (!debug && case_name.startsWith('_')) continue
|
||||
results.cases[case_name] = case_text
|
||||
results.cases[case_name] = await threedee(case_script, debug)
|
||||
}
|
||||
|
||||
logger('Scaffolding PCBs...')
|
||||
|
@ -56,6 +130,5 @@ module.exports = {
|
|||
|
||||
return results
|
||||
},
|
||||
visualize: points_lib.visualize,
|
||||
inject_footprint: pcbs_lib.inject_footprint
|
||||
}
|
21
src/io.js
21
src/io.js
|
@ -1,21 +0,0 @@
|
|||
const m = require('makerjs')
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
|
||||
const u = require('./utils')
|
||||
|
||||
exports.dump_model = (model, file='model', debug=false) => {
|
||||
const assembly = m.model.originate({
|
||||
models: {
|
||||
export: u.deepcopy(model)
|
||||
},
|
||||
units: 'mm'
|
||||
})
|
||||
|
||||
fs.mkdirpSync(path.dirname(`${file}.dxf`))
|
||||
fs.writeFileSync(`${file}.dxf`, m.exporter.toDXF(assembly))
|
||||
if (debug) {
|
||||
fs.writeFileSync(`${file}.svg`, m.exporter.toSVG(assembly))
|
||||
fs.writeJSONSync(`${file}.json`, assembly, {spaces: 4})
|
||||
}
|
||||
}
|
|
@ -168,11 +168,13 @@ const footprint = exports._footprint = (config, name, points, point, net_indexer
|
|||
// connecting other, non-net, non-anchor parameters
|
||||
parsed_params.param = {}
|
||||
for (const [param_name, param_value] of Object.entries(prep.extend(fp.params || {}, params))) {
|
||||
let value = a.sane(param_value, `${name}.nets.${param_name}`, 'string')()
|
||||
let value = param_value
|
||||
if (a.type(value)() == 'string' && value.startsWith('=') && point) {
|
||||
const indirect = value.substring(1)
|
||||
value = point.meta[indirect]
|
||||
value = a.sane(value, `${name}.params.${param} --> ${point.meta.name}.${indirect}`, 'string')()
|
||||
if (value === undefined) {
|
||||
throw new Error(`Indirection "${name}.params.${param}" --> "${point.meta.name}.${indirect}" to undefined value!`)
|
||||
}
|
||||
}
|
||||
parsed_params.param[param_name] = value
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue