Ergogen reorg

This commit is contained in:
Bán Dénes 2020-06-16 22:24:46 +02:00
parent cd1e10293c
commit 0ab5a246e5
14 changed files with 2289 additions and 309 deletions

71
src/cli.js Normal file
View file

@ -0,0 +1,71 @@
const fs = require('fs-extra')
const path = require('path')
const yaml = require('js-yaml')
const yargs = require('yargs')
const points_lib = require('../helpers/points')
const outline_lib = require('./outline')
const args = yargs
.option('config', {
alias: 'c',
demandOption: true,
describe: 'Config yaml file',
type: 'string'
})
.option('output', {
alias: 'o',
default: path.resolve('output'),
describe: 'Output folder',
type: 'string'
})
.option('debug', {
default: false,
hidden: true,
type: 'boolean'
})
.option('outline', {
default: true,
describe: 'Generate 2D outlines',
type: 'boolean'
})
.option('pcb', {
default: false,
describe: 'Generate PCB draft',
type: 'boolean'
})
.option('case', {
default: false,
describe: 'Generate case files',
type: 'boolean'
})
.argv
if (!args.outline && !args.pcb && !args.case) {
yargs.showHelp('log')
console.log('Nothing to do...')
process.exit(0)
}
const config = yaml.load(fs.readFileSync(args.c).toString())
const points = points_lib.parse(config)
if (args.debug) {
points_lib.dump(points)
}
if (args.outline) {
outline_lib.draw(points, config)
}
console.log('Done.')
// exports.dump_model = (model, file='model', json=false) => {
// const assembly = m.model.originate({
// models: deepcopy(model),
// units: 'mm'
// })
// if (json) fs.writeFileSync(`${file}.json`, JSON.stringify(assembly, null, ' '))
// fs.writeFileSync(`${file}.dxf`, m.exporter.toDXF(assembly))
// }

3
src/ergogen.js Normal file
View file

@ -0,0 +1,3 @@
module.exports = {
points: require('./points')
}

256
src/outline.js Normal file
View file

@ -0,0 +1,256 @@
const m = require('makerjs')
const fs = require('fs-extra')
const assert = require('assert').strict
const u = require('./utils')
const outline = (points, config) => {
assert.ok(config.outline)
const footprint = config.outline.footprint || 18
const corner = config.outline.corner || 0
const global_bind = config.outline.bind || 5
let glue = {paths: {}}
if (config.outline.glue) {
const glue_conf = config.outline.glue
const internal_part = (line) => {
// taking the middle part only, so that we don't interfere with corner rounding
return u.line(m.point.middle(line, 0.4), m.point.middle(line, 0.6))
}
const get_line = (def={}) => {
const point = points[def.key]
if (!point) throw new Error(`Point ${def.key} not found...`)
const rect = m.model.originate(point.rect(footprint))
line = rect.paths[def.line || 'top']
return internal_part(line)
}
assert.ok(glue_conf.top)
const tll = get_line(glue_conf.top.left)
const trl = get_line(glue_conf.top.right)
const tip = m.path.converge(tll, trl)
const tlp = u.eq(tll.origin, tip) ? tll.end : tll.origin
const trp = u.eq(trl.origin, tip) ? trl.end : trl.origin
assert.ok(glue_conf.bottom)
const bll = get_line(glue_conf.bottom.left)
const brl = get_line(glue_conf.bottom.right)
const bip = m.path.converge(bll, brl)
const blp = u.eq(bll.origin, bip) ? bll.end : bll.origin
const brp = u.eq(brl.origin, bip) ? brl.end : brl.origin
const left_waypoints = []
const right_waypoints = []
for (const w of glue_conf.waypoints || []) {
const percent = w.percent / 100
const center_x = tip[0] + percent * (bip[0] - tip[0])
const center_y = tip[1] + percent * (bip[1] - tip[1])
const left_x = center_x - (w.left || w.width / 2)
const right_x = center_x + (w.right || w.width / 2)
left_waypoints.push([left_x, center_y])
right_waypoints.unshift([right_x, center_y])
}
const waypoints =
[trp, tip, tlp]
.concat(left_waypoints)
.concat([blp, bip, brp])
.concat(right_waypoints)
glue = u.poly(waypoints)
}
// TODO
jsu = require('util')
let i = 0
const keys = {}
let left_keys = {}
let right_keys = {}
for (const zone of Object.values(config.zones)) {
// interate cols in reverse order so they can
// always overlap with the growing middle patch
for (const col of zone.columns.slice().reverse()) {
for (const [pname, p] of Object.entries(points)) {
if (p.meta.col.name != col.name) continue
let from_x = -footprint / 2, to_x = footprint / 2
let from_y = -footprint / 2, to_y = footprint / 2
const bind = p.meta.row.bind || p.meta.col.bind || global_bind
const mirrored = p.meta.mirrored
const bind_x = p.meta.row.bind_x || p.meta.col.bind_x
if ((bind_x == 'left' && !mirrored) || (bind_x == 'right' && mirrored) || bind_x == 'both') {
from_x -= bind
}
if ((bind_x == 'right' && !mirrored) || (bind_x == 'left' && mirrored) || bind_x == 'both') {
to_x += bind
}
const bind_y = p.meta.row.bind_y || p.meta.col.bind_y
if (bind_y == 'down' || bind_y == 'both') {
from_y -= bind
}
if (bind_y == 'up' || bind_y == 'both') {
to_y += bind
}
let key = new m.models.RoundRectangle(to_x - from_x, to_y - from_y, corner)
key = m.model.moveRelative(key, [from_x, from_y])
key = p.position(key)
// console.log(i+1, pname, jsu.inspect(key, true, null, true))
// if (i == 7) throw 7
keys[pname] = u.deepcopy(p.position(u.rect(14, 14, [-7, -7])))
if (mirrored) {
// TODO running into the problem again where the right side doesn't combine properly
// have to debug this at a lower level, it might be a bug in the makerjs source :/
// first step is to export these inspections and create a minimal reproduction
// if that fails as well, I have to dive into the combineUnion code...
// if (pname === 'mirror_inner_top') {
// u.dump_model({a: right_keys, b: key}, `debug_bad`, true)
// }
right_keys = m.model.combineUnion(key, right_keys)
u.dump_model({a: glue, c: right_keys}, `right_${++i}`)
} else {
// if (pname === 'inner_top') {
// u.dump_model({a: left_keys, b: key}, `debug_good`, true)
// }
left_keys = m.model.combineUnion(key, left_keys)
u.dump_model({a: glue, b: left_keys}, `left_${++i}`)
}
// u.dump_model({a: glue}, `glue_${i++}`)
}
}
}
u.dump_model({a: glue, b: left_keys, c: {models: right_keys}}, `all_before`)
glue = m.model.combineUnion(glue, left_keys)
u.dump_model({a: glue, b: left_keys, c: {models: right_keys}}, `all_after_left`)
glue = m.model.combineUnion(glue, right_keys)
u.dump_model({a: glue, b: {models: keys}}, `fullll`)
// glue = m.model.outline(glue, expansion)
// const keys = {}
// // let i = 1
// for (const zone of Object.values(config.zones)) {
// // interate cols in reverse order so they can
// // always overlap with the growing middle patch
// for (const col of zone.columns.slice().reverse()) {
// for (const [pname, p] of Object.entries(points)) {
// if (p.meta.col.name != col.name) continue
// // let key = new m.models.RoundRectangle(footprint, footprint, corner)
// let key = u.rect(footprint, footprint)
// key = m.model.moveRelative(key, [-footprint/2, -footprint/2])
// key = p.position(key)
// keys[pname] = u.deepcopy(key)
// key = m.model.outline(key, expansion)
// glue = m.model.combineUnion(glue, key)
// // u.dump_model(keys, `keys_${i}`)
// // u.dump_model({a: glue}, `glue_${i++}`)
// }
// }
// }
// u.dump_model({a: glue, b: {models: keys}}, `all`)
// glue = m.model.outline(glue, expansion, 1, true)
// u.dump_model({a: glue}, `glue_post`)
// u.dump_model({
// a: {
// models: {a: glue},
// // paths: {
// // tll: tll,
// // trl: trl,
// // tip: u.circle(tip, 1),
// // tlp: u.circle(tlp, 1),
// // trp: u.circle(trp, 1),
// // bll: bll,
// // brl: brl,
// // bip: u.circle(bip, 1),
// // blp: u.circle(blp, 1),
// // brp: u.circle(brp, 1),
// // }
// },
// b: { models: keys }
// }, 'valami', true)
// throw 3
// let tl = m.model.moveRelative(m.model.rotate(u.rect(a_lot, size, [-size/2, -size/2]), tlp.r), tlp.p)
// let tr = m.model.moveRelative(m.model.rotate(u.rect(a_lot, size, [-a_lot+size/2, -size/2]), trp.r), trp.p)
// tl = m.model.originate(tl)
// tr = m.model.originate(tr)
// let top_intersect = m.path.intersection(tl.paths.top, tr.paths.top).intersectionPoints
// if (!top_intersect) {
// throw new Error('Top intersect')
// }
// console.log(tlp.p, tl, tl.paths.top, ',,,', trp.p, tr, tr.paths.top, top_intersect)
// // create the two bottoms
// assert.ok(config.bottom.left)
// assert.ok(config.bottom.right)
// const blp = points[config.bottom.left]
// const brp = points[config.bottom.right]
// // create middle "patch"
// const tll = new m.paths.Line(tlp.p, tlp.add([a_lot, 0]).rotate(tlp.r, tlp.p).p)
// const trl = new m.paths.Line(trp.p, trp.add([a_lot, 0]).rotate(trp.r, trp.p).p)
// const bll = new m.paths.Line(blp.p, blp.add([a_lot, 0]).rotate(blp.r, blp.p).p)
// const brl = new m.paths.Line(brp.p, brp.add([a_lot, 0]).rotate(brp.r, brp.p).p)
// const bottom_intersect = m.path.intersection(bll, brl).intersectionPoints[0]
// console.log(tll, trl, top_intersect)
// throw 2
// for (const p of Object.values(points)) {
// const r = new m.models.RoundRectangle(size, size, config.corner || 0)
// }
}
exports.draw = (points, config) => {
// const lefts = {}
// const rights = {}
// for (const [k, p] of Object.entries(points)) {
// if (p.meta.mirrored) {
// rights[k] = p
// } else {
// lefts[k] = p
// }
// }
// TODO this is just a test
outline(points, config)
}

71
src/point.js Normal file
View file

@ -0,0 +1,71 @@
const m = require('makerjs')
const u = require('./utils')
class Point {
constructor(x=0, y=0, r=0, meta={}) {
if (Array.isArray(x)) {
this.x = x[0]
this.y = x[1]
this.r = 0
this.meta = {}
} else {
this.x = x
this.y = y
this.r = r
this.meta = meta
}
}
get p() {
return [this.x, this.y]
}
set p(val) {
[this.x, this.y] = val
}
add(a) {
const res = this.clone()
res.x += a[0]
res.y += a[1]
return res
}
shift(s) {
this.x += s[0]
this.y += s[1]
return this
}
rotate(angle, origin=[0, 0]) {
this.p = m.point.rotate(this.p, angle, origin)
this.r += angle
return this
}
mirror(x) {
this.x = 2 * x - this.x
this.r = -this.r
return this
}
clone() {
return new Point(
this.x,
this.y,
this.r,
u.deepcopy(this.meta)
)
}
position(model) {
return m.model.moveRelative(m.model.rotate(model, this.r), this.p)
}
rect(size=14) {
let rect = u.rect(size, size, [-size/2, -size/2], this.meta.mirrored)
return this.position(rect)
}
}
module.exports = Point

128
src/points.js Normal file
View file

@ -0,0 +1,128 @@
const m = require('makerjs')
const u = require('./utils')
const push_rotation = (list, angle, origin) => {
let candidate = origin
for (const r of list) {
candidate = m.point.rotate(candidate, r.angle, r.origin)
}
list.push({
angle: angle,
origin: candidate
})
}
const render_zone = (cols, rows, anchor=new Point(), reverse=false) => {
const sign = reverse ? -1 : 1
const points = {}
const rotations = []
// transferring the anchor rotation to "real" rotations
rotations.push({
angle: anchor.r,
origin: anchor.p
})
for (const col of cols) {
anchor.y += col.stagger || 0
const col_anchor = anchor.clone()
// clear potential rotations, as they will get re-applied anyway
// and we don't want to apply them twice...
col_anchor.r = 0
// combine row data from zone-wide defs and col-specific defs
const col_specific = col.rows || []
const zone_wide = rows || []
const actual_rows = []
for (let i = 0; i < zone_wide.length && i < col_specific.length; ++i) {
actual_rows.push(Object.assign({}, zone_wide[i], col_specific[i]))
}
for (const row of actual_rows) {
let point = col_anchor.clone()
for (const r of rotations) {
point.rotate(r.angle, r.origin)
}
point.r += col.angle || 0
const name = `${col.name}_${row.name}`
point.meta = {col, row, name}
points[name] = point
col_anchor.y += row.padding || 19
}
if (col.rotate) {
push_rotation(
rotations,
col.rotate,
anchor.add(col.origin || [0, 0]).p
)
}
anchor.x += sign * (col.padding || 19)
}
return points
}
const anchor = (raw, points={}) => {
let a = new Point()
if (raw) {
if (raw.ref && points[raw.ref]) {
a = points[raw.ref].clone()
}
if (raw.shift) {
a.x += raw.shift[0]
a.y += raw.shift[1]
}
a.r += raw.angle || 0
}
return a
}
exports.parse = (config) => {
let points = {}
for (const zone of Object.values(config.zones)) {
points = Object.assign(points, render_zone(
zone.columns || [],
zone.rows || [{name: 'default'}],
anchor(zone.anchor, points),
!!zone.reverse
))
}
if (config.angle) {
for (const p of Object.values(points)) {
p.rotate(config.angle)
}
}
if (config.mirror) {
let axis = anchor(config.mirror, points).x
axis += (config.mirror.distance || 0) / 2
const mirrored_points = {}
for (const [name, p] of Object.entries(points)) {
if (p.meta.col.asym == 'left' || p.meta.row.asym == 'left') continue
const mp = p.clone().mirror(axis)
mp.meta.mirrored = true
delete mp.meta.asym
mirrored_points[`mirror_${name}`] = mp
if (p.meta.col.asym == 'right' || p.meta.row.asym == 'right') {
p.meta.col.skip = true
}
}
Object.assign(points, mirrored_points)
}
const filtered = {}
for (const [k, p] of Object.entries(points)) {
if (p.meta.col.skip || p.meta.row.skip) continue
filtered[k] = p
}
return filtered
}

44
src/utils.js Normal file
View file

@ -0,0 +1,44 @@
const m = require('makerjs')
exports.deepcopy = (value) => JSON.parse(JSON.stringify(value))
const eq = exports.eq = (a=[], b=[]) => {
return a[0] === b[0] && a[1] === b[1]
}
const line = exports.line = (a, b) => {
return new m.paths.Line(a, b)
}
exports.circle = (p, r) => {
return new m.paths.Circle(p, r)
}
exports.rect = (w, h, o=[0, 0], mirrored=false) => {
const res = {
top: line([0, h], [w, h]),
right: line([w, h], [w, 0]),
bottom: line([w, 0], [0, 0]),
left: line([0, 0], [0, h])
}
if (mirrored) {
for (const segment of Object.values(res)) {
[segment.origin, segment.end] = [segment.end, segment.origin]
}
}
return m.model.move({paths: res}, o)
}
exports.poly = (arr) => {
let counter = 0
let prev = arr[arr.length - 1]
const res = {
paths: {}
}
for (const p of arr) {
if (eq(prev, p)) continue
res.paths['p' + (++counter)] = line(prev, p)
prev = p
}
return res
}