Bringing points stuff up to spec

This commit is contained in:
Bán Dénes 2020-06-26 21:00:10 +02:00
parent 0ab5a246e5
commit a5e686b059
11 changed files with 474 additions and 108 deletions

View file

@ -1,11 +1,28 @@
#!/usr/bin/env node
const m = require('makerjs')
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 u = require('./utils')
const points_lib = require('./points')
const outline_lib = require('./outline')
const dump_model = (model, file='model') => {
const assembly = m.model.originate({
models: u.deepcopy(model),
units: 'mm'
})
fs.mkdirpSync(path.dirname(`${file}.dxf`))
fs.writeFileSync(`${file}.dxf`, m.exporter.toDXF(assembly))
if (args.debug) {
fs.writeJSONSync(`${file}.json`, assembly, {spaces: 4})
}
}
const args = yargs
.option('config', {
alias: 'c',
@ -24,48 +41,20 @@ const args = yargs
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)
}
fs.mkdirpSync(args.o)
const config = yaml.load(fs.readFileSync(args.c).toString())
const points = points_lib.parse(config)
const config_parser = args.c.endsWith('.yaml') ? yaml.load : JSON.parse
const config = config_parser(fs.readFileSync(args.c).toString())
const points = points_lib.parse(config.points)
if (args.debug) {
points_lib.dump(points)
}
if (args.outline) {
outline_lib.draw(points, config)
fs.writeJSONSync(path.join(args.o, 'points.json'), points, {spaces: 4})
const size = 14
const rect = u.rect(size, size, [-size/2, -size/2])
const points_demo = outline_lib.layout(points, rect)
dump_model(points_demo, path.join(args.o, 'points_demo'))
}
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))
// }

View file

@ -1,23 +1,25 @@
const m = require('makerjs')
const fs = require('fs-extra')
const assert = require('assert').strict
const u = require('./utils')
const layout = exports.layout = (points, shape) => {
const shapes = {}
for (const [pname, p] of Object.entries(points)) {
shapes[pname] = p.position(u.deepcopy(shape))
}
return {layout: {models: shapes}}
}
const outline = exports._outline = (points, config={}) => params => {
const outline = (points, config) => {
let size = params.size || [18, 18]
if (!Array.isArray(size)) size = [size, size]
const corner = params.corner || 0
assert.ok(config.outline)
const footprint = config.outline.footprint || 18
const corner = config.outline.corner || 0
const global_bind = config.outline.bind || 5
const global_bind = config.bind || 5
let glue = {paths: {}}
if (config.outline.glue) {
const glue_conf = config.outline.glue
if (config.glue) {
const internal_part = (line) => {
// taking the middle part only, so that we don't interfere with corner rounding
@ -25,23 +27,33 @@ const outline = (points, config) => {
}
const get_line = (def={}) => {
const point = points[def.key]
if (!point) throw new Error(`Point ${def.key} not found...`)
const ref = points[def.ref]
if (!ref) throw new Error(`Point ${def.ref} not found...`)
let from = [0, 0]
let to = [ref.meta.mirrored ? -1 : 1, 0]
// todo: position according to point to get the lines...
let point = ref.clone().shift(def.shift || [0, 0])
point.rotate(def.rotate || 0, point.add(def.origin || [0, 0]))
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)
assert.ok(config.glue.top)
const tll = get_line(config.glue.top.left)
const trl = get_line(config.glue.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)
assert.ok(config.glue.bottom)
const bll = get_line(config.glue.bottom.left)
const brl = get_line(config.glue.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
@ -49,7 +61,7 @@ const outline = (points, config) => {
const left_waypoints = []
const right_waypoints = []
for (const w of glue_conf.waypoints || []) {
for (const w of config.glue.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])

View file

@ -1,7 +1,7 @@
const m = require('makerjs')
const u = require('./utils')
class Point {
module.exports = class Point {
constructor(x=0, y=0, r=0, meta={}) {
if (Array.isArray(x)) {
this.x = x[0]
@ -67,5 +67,3 @@ class Point {
return this.position(rect)
}
}
module.exports = Point

View file

@ -1,7 +1,37 @@
const m = require('makerjs')
const u = require('./utils')
const Point = require('./point')
const push_rotation = (list, angle, origin) => {
const extend_pair = exports._extend_pair = (to, from) => {
const to_type = u.type(to)
const from_type = u.type(from)
if (!from && ['array', 'object'].includes(to_type)) return to
if (to_type != from_type) return from
if (from_type == 'object') {
const res = {}
for (const key of Object.keys(from)) {
res[key] = extend_pair(to[key], from[key])
}
return res
} else if (from_type == 'array') {
const res = u.deepcopy(to)
for (const [i, val] of from.entries()) {
res[i] = extend_pair(res[i], val)
}
return res
} else return from
}
const extend = exports._extend = (...args) => {
let res = args[0]
for (const arg of args) {
if (res == arg) continue
res = extend_pair(res, arg)
}
return res
}
const push_rotation = exports._push_rotation = (list, angle, origin) => {
let candidate = origin
for (const r of list) {
candidate = m.point.rotate(candidate, r.angle, r.origin)
@ -12,9 +42,8 @@ const push_rotation = (list, angle, origin) => {
})
}
const render_zone = (cols, rows, anchor=new Point(), reverse=false) => {
const render_zone = exports._render_zone = (cols, rows, zone_wide_key, anchor) => {
const sign = reverse ? -1 : 1
const points = {}
const rotations = []
@ -24,7 +53,7 @@ const render_zone = (cols, rows, anchor=new Point(), reverse=false) => {
origin: anchor.p
})
for (const col of cols) {
for (const [colname, col] of Object.entries(cols)) {
anchor.y += col.stagger || 0
const col_anchor = anchor.clone()
@ -33,26 +62,36 @@ const render_zone = (cols, rows, anchor=new Point(), reverse=false) => {
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]))
const col_specific_rows = col.rows || {}
const zone_wide_rows = rows || {}
const actual_rows = col_specific_rows || zone_wide_rows
// get key config through the 4-level extension
const keys = []
for (const row of Object.keys(actual_rows)) {
const key = extend(zone_wide_key, col.key || {}, zone_wide_rows[row] || {}, col_specific_rows[row] || {})
key.col = col
key.row = row
key.name = `${colname}_${row}`
keys.push(key)
}
for (const row of actual_rows) {
// lay out keys
for (const key of keys) {
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
if (key.rotate) {
point.rotate(key.rotate, point.add(key.origin || [0, 0]).p)
}
point.meta = key
points[key.name] = point
col_anchor.y += row.padding || 19
col_anchor.y += key.padding || 19
}
// apply col-level rotation for the next columns
if (col.rotate) {
push_rotation(
rotations,
@ -61,23 +100,23 @@ const render_zone = (cols, rows, anchor=new Point(), reverse=false) => {
)
}
anchor.x += sign * (col.padding || 19)
anchor.x += col.spread || 19
}
return points
}
const anchor = (raw, points={}) => {
const anchor = exports._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.x += raw.shift[0] || 0
a.y += raw.shift[1] || 0
}
a.r += raw.angle || 0
a.r += raw.rotate || 0
}
return a
}
@ -90,29 +129,32 @@ exports.parse = (config) => {
points = Object.assign(points, render_zone(
zone.columns || [],
zone.rows || [{name: 'default'}],
anchor(zone.anchor, points),
!!zone.reverse
zone.key || {},
anchor(zone.anchor, points)
))
}
if (config.angle) {
if (config.rotate) {
for (const p of Object.values(points)) {
p.rotate(config.angle)
p.rotate(config.rotate)
}
}
if (config.mirror) {
let axis = anchor(config.mirror, points).x
axis += (config.mirror.distance || 0) / 2
let axis = config.mirror.axis
if (!axis) {
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
if (p.meta.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
if (p.meta.asym == 'right') {
p.meta.skip = true
}
}
Object.assign(points, mirrored_points)
@ -120,7 +162,7 @@ exports.parse = (config) => {
const filtered = {}
for (const [k, p] of Object.entries(points)) {
if (p.meta.col.skip || p.meta.row.skip) continue
if (p.meta.skip) continue
filtered[k] = p
}

View file

@ -1,7 +1,19 @@
const m = require('makerjs')
exports.assert = (exp, msg) => {
if (!exp) {
throw new Error(msg)
}
}
exports.deepcopy = (value) => JSON.parse(JSON.stringify(value))
exports.type = (val) => {
if (Array.isArray(val)) return 'array'
if (val === null) return 'null'
return typeof val
}
const eq = exports.eq = (a=[], b=[]) => {
return a[0] === b[0] && a[1] === b[1]
}