From de7441d632e7430e07a78e7528001f2dcc8eb02f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1n=20D=C3=A9nes?= Date: Fri, 29 Mar 2024 20:02:17 +0100 Subject: [PATCH] Outsource kicad outline conversion to pcb templates --- src/pcbs.js | 34 +----------- src/templates/kicad5.js | 49 +++++++++++++++--- test/fixtures/bundle.zip | Bin 1425 -> 1456 bytes .../bundle/templates/custom_template.js | 7 ++- test/helpers/mock.js | 7 ++- test/unit/internals.js | 6 +-- 6 files changed, 57 insertions(+), 46 deletions(-) diff --git a/src/pcbs.js b/src/pcbs.js index 9fd9160..a50d039 100644 --- a/src/pcbs.js +++ b/src/pcbs.js @@ -7,36 +7,6 @@ const prep = require('./prepare') const anchor = require('./anchor').parse const filter = require('./filter').parse -const makerjs2kicad = exports._makerjs2kicad = (model, layer) => { - const grs = [] - const xy = val => `${val[0]} ${-val[1]}` - m.model.walk(model, { - onPath: wp => { - const p = wp.pathContext - switch (p.type) { - case 'line': - grs.push(`(gr_line (start ${xy(p.origin)}) (end ${xy(p.end)}) (angle 90) (layer ${layer}) (width 0.15))`) - break - case 'arc': - const arc_center = p.origin - const angle_start = p.startAngle > p.endAngle ? p.startAngle - 360 : p.startAngle - const angle_diff = Math.abs(p.endAngle - angle_start) - const arc_end = m.point.rotate(m.point.add(arc_center, [p.radius, 0]), angle_start, arc_center) - grs.push(`(gr_arc (start ${xy(arc_center)}) (end ${xy(arc_end)}) (angle ${-angle_diff}) (layer ${layer}) (width 0.15))`) - break - case 'circle': - const circle_center = p.origin - const circle_end = m.point.add(circle_center, [p.radius, 0]) - grs.push(`(gr_circle (center ${xy(circle_center)}) (end ${xy(circle_end)}) (layer ${layer}) (width 0.15))`) - break - default: - throw new Error(`Can't convert path type "${p.type}" to kicad!`) - } - } - }) - return grs.join('\n') -} - const footprint_types = require('./footprints') const template_types = require('./templates') @@ -208,7 +178,7 @@ exports.parse = (config, points, outlines, units) => { for (const [outline_name, outline] of Object.entries(config_outlines)) { const ref = a.in(outline.outline, `pcbs.${pcb_name}.outlines.${outline_name}.outline`, Object.keys(outlines)) const layer = a.sane(outline.layer || 'Edge.Cuts', `pcbs.${pcb_name}.outlines.${outline_name}.outline`, 'string')() - kicad_outlines[outline_name] = makerjs2kicad(outlines[ref], layer) + kicad_outlines[outline_name] = template.convert_outline(outlines[ref], layer) } // making a global net index registry @@ -258,7 +228,7 @@ exports.parse = (config, points, outlines, units) => { nets_arr.push(net_obj(net, index)) } - results[pcb_name] = template({ + results[pcb_name] = template.body({ name: pcb_name, version: config.meta && config.meta.version || 'v1.0.0', author: config.meta && config.meta.author || 'Unknown', diff --git a/src/templates/kicad5.js b/src/templates/kicad5.js index 694a5bf..dae42a7 100644 --- a/src/templates/kicad5.js +++ b/src/templates/kicad5.js @@ -1,11 +1,45 @@ -module.exports = params => { +const m = require('makerjs') - const net_text = params.nets.join('\n') - const netclass_text = params.nets.map(net => `(add_net "${net.name}")`).join('\n') - const footprint_text = params.footprints.join('\n') - const outline_text = Object.values(params.outlines).join('\n') - - return ` +module.exports = { + + convert_outline: (model, layer) => { + const grs = [] + const xy = val => `${val[0]} ${-val[1]}` + m.model.walk(model, { + onPath: wp => { + const p = wp.pathContext + switch (p.type) { + case 'line': + grs.push(`(gr_line (start ${xy(p.origin)}) (end ${xy(p.end)}) (angle 90) (layer ${layer}) (width 0.15))`) + break + case 'arc': + const arc_center = p.origin + const angle_start = p.startAngle > p.endAngle ? p.startAngle - 360 : p.startAngle + const angle_diff = Math.abs(p.endAngle - angle_start) + const arc_end = m.point.rotate(m.point.add(arc_center, [p.radius, 0]), angle_start, arc_center) + grs.push(`(gr_arc (start ${xy(arc_center)}) (end ${xy(arc_end)}) (angle ${-angle_diff}) (layer ${layer}) (width 0.15))`) + break + case 'circle': + const circle_center = p.origin + const circle_end = m.point.add(circle_center, [p.radius, 0]) + grs.push(`(gr_circle (center ${xy(circle_center)}) (end ${xy(circle_end)}) (layer ${layer}) (width 0.15))`) + break + default: + throw new Error(`Can't convert path type "${p.type}" to kicad!`) + } + } + }) + return grs.join('\n') + }, + + body: params => { + + const net_text = params.nets.join('\n') + const netclass_text = params.nets.map(net => `(add_net "${net.name}")`).join('\n') + const footprint_text = params.footprints.join('\n') + const outline_text = Object.values(params.outlines).join('\n') + + return ` (kicad_pcb (version 20171130) (host pcbnew 5.1.6) @@ -118,4 +152,5 @@ module.exports = params => { ` +} } \ No newline at end of file diff --git a/test/fixtures/bundle.zip b/test/fixtures/bundle.zip index 8c7ca8987f8de784966a72ed336ebc81d4f11403..a4ca4d68d3aa7a8f0e142b87ce03eb36e033a1fd 100644 GIT binary patch delta 334 zcmbQpy@7jz5lbjHd)h>^uPpv=+0rKKGX{VKHa9amGeU$;G06&d&#R4KfP#fUT82Sk zvK^~Z{c~RSG#Dy!j3(h`2tFJt}K);mC#twbDmob8d6#Ph_cJ zkz3Zbp?u=Jhks?xFS7{R;_f$T;{v}I8GQ0ypPne4W1CgDFJb4xZLe3v=j=0nG|g(id?>v+THdIlFO?irpU3En)t3gd}G-+<^XSY z4l|al57j`YE}Xo8*_9RK$&Hi$vq(*5VX=aFRdn({X12+(EUsWL1N{yX;F^4t#T26A fAB#LM$ma;7v{=n}VEzTslXF<**$x4{%)kHuDr*G1ePvl$yFPVtB8%wc`%D~@eHe{d>}P-lgf~xPG+<=;3RE`v z6qBrQLcDnd0~F){X&DBE$+4_T^&kZ>Tw1}+z{v7~nSlXJsP;H=H5dpmzu%|Ce*QP} z-XE;;2`d>{vrm_4H+^|8c2?71X3LIg&4;XAxArieT*dWOWuZnBNj$ev$^tSVFuc%PP+c@)F1lh!LKwW;`&DfPLM~D$lkU=obbC E0IJ1ycmMzZ diff --git a/test/fixtures/bundle/templates/custom_template.js b/test/fixtures/bundle/templates/custom_template.js index 8361655..d3217b8 100644 --- a/test/fixtures/bundle/templates/custom_template.js +++ b/test/fixtures/bundle/templates/custom_template.js @@ -1,3 +1,6 @@ -module.exports = params => { - return `Custom template override. The secret is ${params.custom.secret}.` +module.exports = { + convert_outline: () => {}, + body: params => { + return `Custom template override. The secret is ${params.custom.secret}.` + } } \ No newline at end of file diff --git a/test/helpers/mock.js b/test/helpers/mock.js index 0ae67c9..507ad2d 100644 --- a/test/helpers/mock.js +++ b/test/helpers/mock.js @@ -140,7 +140,10 @@ exports.inject = (ergogen) => { } }) - ergogen.inject('template', 'template_test', params => { - return `Custom template override. The secret is ${params.custom.secret}.` + ergogen.inject('template', 'template_test', { + convert_outline: () => {}, + body: params => { + return `Custom template override. The secret is ${params.custom.secret}.` + } }) } \ No newline at end of file diff --git a/test/unit/internals.js b/test/unit/internals.js index 2449d34..e610fb8 100644 --- a/test/unit/internals.js +++ b/test/unit/internals.js @@ -1,15 +1,15 @@ const m = require('makerjs') -const pcb_lib = require('../../src/pcbs') +const kicad5 = require('../../src/templates/kicad5') const ergogen = require('../../src/ergogen') describe('Internals', function() { - it('makerjs2kicad', function() { + it('pcb outline conversion', function() { // warn on unknown path type sinon.stub(m.model, 'walk').callsFake(function(model, config) { config.onPath({pathContext: {type: 'nonexistent'}}) }) - pcb_lib._makerjs2kicad.bind(this).should.throw("Can't convert path type") + kicad5.convert_outline.bind(this).should.throw("Can't convert path type") }) it('injection', function() {