Injectable PCB templates

This commit is contained in:
Bán Dénes 2024-01-23 12:04:37 +01:00
parent 21e50cb11d
commit 6079aaf332
31 changed files with 277 additions and 251 deletions

2
.gitignore vendored
View file

@ -117,4 +117,4 @@ dist
# Project specific
output
temp*
temp\b

View file

@ -103,6 +103,8 @@ const inject = (type, name, value) => {
switch (type) {
case 'footprint':
return pcbs_lib.inject_footprint(name, value)
case 'template':
return pcbs_lib.inject_template(name, value)
default:
throw new Error(`Unknown injection type "${type}" with name "${name}" and value "${value}"!`)
}

View file

@ -28,6 +28,16 @@ exports.unpack = async (zip) => {
injections.push(['footprint', name, parsed])
}
// bundled pcb templates
const tpls = zip.folder('templates')
for (const tpl of tpls.file(/.*\.js$/)) {
const name = tpl.name.slice('templates/'.length).split('.')[0]
const text = await tpl.async('string')
const parsed = new Function(module_prefix + text + module_suffix)()
// TODO: some sort of template validation?
injections.push(['template', name, parsed])
}
return [config_text, injections]
}

View file

@ -7,116 +7,6 @@ const prep = require('./prepare')
const anchor = require('./anchor').parse
const filter = require('./filter').parse
const kicad_prefix = `
(kicad_pcb (version 20171130) (host pcbnew 5.1.6)
(page A3)
(title_block
(title KEYBOARD_NAME_HERE)
(rev VERSION_HERE)
(company YOUR_NAME_HERE)
)
(general
(thickness 1.6)
)
(layers
(0 F.Cu signal)
(31 B.Cu signal)
(32 B.Adhes user)
(33 F.Adhes user)
(34 B.Paste user)
(35 F.Paste user)
(36 B.SilkS user)
(37 F.SilkS user)
(38 B.Mask user)
(39 F.Mask user)
(40 Dwgs.User user)
(41 Cmts.User user)
(42 Eco1.User user)
(43 Eco2.User user)
(44 Edge.Cuts user)
(45 Margin user)
(46 B.CrtYd user)
(47 F.CrtYd user)
(48 B.Fab user)
(49 F.Fab user)
)
(setup
(last_trace_width 0.25)
(trace_clearance 0.2)
(zone_clearance 0.508)
(zone_45_only no)
(trace_min 0.2)
(via_size 0.8)
(via_drill 0.4)
(via_min_size 0.4)
(via_min_drill 0.3)
(uvia_size 0.3)
(uvia_drill 0.1)
(uvias_allowed no)
(uvia_min_size 0.2)
(uvia_min_drill 0.1)
(edge_width 0.05)
(segment_width 0.2)
(pcb_text_width 0.3)
(pcb_text_size 1.5 1.5)
(mod_edge_width 0.12)
(mod_text_size 1 1)
(mod_text_width 0.15)
(pad_size 1.524 1.524)
(pad_drill 0.762)
(pad_to_mask_clearance 0.05)
(aux_axis_origin 0 0)
(visible_elements FFFFFF7F)
(pcbplotparams
(layerselection 0x010fc_ffffffff)
(usegerberextensions false)
(usegerberattributes true)
(usegerberadvancedattributes true)
(creategerberjobfile true)
(excludeedgelayer true)
(linewidth 0.100000)
(plotframeref false)
(viasonmask false)
(mode 1)
(useauxorigin false)
(hpglpennumber 1)
(hpglpenspeed 20)
(hpglpendiameter 15.000000)
(psnegative false)
(psa4output false)
(plotreference true)
(plotvalue true)
(plotinvisibletext false)
(padsonsilk false)
(subtractmaskfromsilk false)
(outputformat 1)
(mirror false)
(drillshape 1)
(scaleselection 1)
(outputdirectory ""))
)
`
const kicad_suffix = `
)
`
const kicad_netclass = `
(net_class Default "This is the default net class."
(clearance 0.2)
(trace_width 0.25)
(via_dia 0.8)
(via_drill 0.4)
(uvia_dia 0.3)
(uvia_drill 0.1)
__ADD_NET
)
`
const makerjs2kicad = exports._makerjs2kicad = (model, layer) => {
const grs = []
const xy = val => `${val[0]} ${-val[1]}`
@ -148,11 +38,16 @@ const makerjs2kicad = exports._makerjs2kicad = (model, layer) => {
}
const footprint_types = require('./footprints')
const template_types = require('./templates')
exports.inject_footprint = (name, fp) => {
footprint_types[name] = fp
}
exports.inject_template = (name, t) => {
template_types[name] = t
}
const xy_obj = (x, y) => {
return {
x,
@ -300,8 +195,9 @@ exports.parse = (config, points, outlines, units) => {
for (const [pcb_name, pcb_config] of Object.entries(pcbs)) {
// config sanitization
a.unexpected(pcb_config, `pcbs.${pcb_name}`, ['outlines', 'footprints', 'references'])
a.unexpected(pcb_config, `pcbs.${pcb_name}`, ['outlines', 'footprints', 'references', 'template', 'params'])
const references = a.sane(pcb_config.references || false, `pcbs.${pcb_name}.references`, 'boolean')()
const template = template_types[a.in(pcb_config.template || 'kicad5', `pcbs.${pcb_name}.template`, Object.keys(template_types))]
// outline conversion
if (a.type(pcb_config.outlines)() == 'array') {
@ -358,28 +254,19 @@ exports.parse = (config, points, outlines, units) => {
// finalizing nets
const nets_arr = []
const add_nets_arr = []
for (const [net, index] of Object.entries(nets)) {
nets_arr.push(`(net ${index} "${net}")`)
add_nets_arr.push(`(add_net "${net}")`)
nets_arr.push(net_obj(net, index))
}
const netclass = kicad_netclass.replace('__ADD_NET', add_nets_arr.join('\n'))
const nets_text = nets_arr.join('\n')
const footprint_text = footprints.join('\n')
const outline_text = Object.values(kicad_outlines).join('\n')
const personalized_prefix = kicad_prefix
.replace('KEYBOARD_NAME_HERE', pcb_name)
.replace('VERSION_HERE', config.meta && config.meta.version || 'v1.0.0')
.replace('YOUR_NAME_HERE', config.meta && config.meta.author || 'Unknown')
results[pcb_name] = `
${personalized_prefix}
${nets_text}
${netclass}
${footprint_text}
${outline_text}
${kicad_suffix}
`
results[pcb_name] = template({
name: pcb_name,
version: config.meta && config.meta.version || 'v1.0.0',
author: config.meta && config.meta.author || 'Unknown',
nets: nets_arr,
footprints: footprints,
outlines: kicad_outlines,
custom: pcb_config.params
})
}
return results

3
src/templates/index.js Normal file
View file

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

121
src/templates/kicad5.js Normal file
View file

@ -0,0 +1,121 @@
module.exports = 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)
(page A3)
(title_block
(title ${params.name})
(rev ${params.version})
(company ${params.author})
)
(general
(thickness 1.6)
)
(layers
(0 F.Cu signal)
(31 B.Cu signal)
(32 B.Adhes user)
(33 F.Adhes user)
(34 B.Paste user)
(35 F.Paste user)
(36 B.SilkS user)
(37 F.SilkS user)
(38 B.Mask user)
(39 F.Mask user)
(40 Dwgs.User user)
(41 Cmts.User user)
(42 Eco1.User user)
(43 Eco2.User user)
(44 Edge.Cuts user)
(45 Margin user)
(46 B.CrtYd user)
(47 F.CrtYd user)
(48 B.Fab user)
(49 F.Fab user)
)
(setup
(last_trace_width 0.25)
(trace_clearance 0.2)
(zone_clearance 0.508)
(zone_45_only no)
(trace_min 0.2)
(via_size 0.8)
(via_drill 0.4)
(via_min_size 0.4)
(via_min_drill 0.3)
(uvia_size 0.3)
(uvia_drill 0.1)
(uvias_allowed no)
(uvia_min_size 0.2)
(uvia_min_drill 0.1)
(edge_width 0.05)
(segment_width 0.2)
(pcb_text_width 0.3)
(pcb_text_size 1.5 1.5)
(mod_edge_width 0.12)
(mod_text_size 1 1)
(mod_text_width 0.15)
(pad_size 1.524 1.524)
(pad_drill 0.762)
(pad_to_mask_clearance 0.05)
(aux_axis_origin 0 0)
(visible_elements FFFFFF7F)
(pcbplotparams
(layerselection 0x010fc_ffffffff)
(usegerberextensions false)
(usegerberattributes true)
(usegerberadvancedattributes true)
(creategerberjobfile true)
(excludeedgelayer true)
(linewidth 0.100000)
(plotframeref false)
(viasonmask false)
(mode 1)
(useauxorigin false)
(hpglpennumber 1)
(hpglpenspeed 20)
(hpglpendiameter 15.000000)
(psnegative false)
(psa4output false)
(plotreference true)
(plotvalue true)
(plotinvisibletext false)
(padsonsilk false)
(subtractmaskfromsilk false)
(outputformat 1)
(mirror false)
(drillshape 1)
(scaleselection 1)
(outputdirectory ""))
)
${net_text}
(net_class Default "This is the default net class."
(clearance 0.2)
(trace_width 0.25)
(via_dia 0.8)
(via_drill 0.4)
(uvia_dia 0.3)
(uvia_drill 0.1)
${netclass_text}
)
${footprint_text}
${outline_text}
)
`
}

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net_class Default "This is the default net class."
(clearance 0.2)
@ -109,4 +109,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net_class Default "This is the default net class."
(clearance 0.2)
@ -109,4 +109,3 @@
)

View file

@ -0,0 +1 @@
Custom template override. The secret is 42.

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net_class Default "This is the default net class."
(clearance 0.2)
@ -112,11 +112,10 @@
(fp_text reference "I1" (at 0 0) (layer F.SilkS) hide (effects (font (size 1.27 1.27) (thickness 0.15))))
)
(gr_line (start -9 9) (end 9 9) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start -9 9) (end 9 9) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start 9 9) (end 9 -9) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start 9 -9) (end -9 -9) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start -9 -9) (end -9 9) (angle 90) (layer Edge.Cuts) (width 0.15))
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net_class Default "This is the default net class."
(clearance 0.2)
@ -109,4 +109,3 @@
)

Binary file not shown.

View file

@ -11,3 +11,8 @@ pcbs:
injected:
what: injected
where: matrix
custom_template:
outlines.edge.outline: box
template: custom_template
params:
secret: 42

View file

@ -0,0 +1,3 @@
module.exports = params => {
return `Custom template override. The secret is ${params.custom.secret}.`
}

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "from")
(net 2 "to")
@ -172,4 +172,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "from")
(net 2 "to")
@ -357,4 +357,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "from")
(net 2 "to")
@ -332,4 +332,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "from")
(net 2 "to")
@ -151,4 +151,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "from")
(net 2 "to")
@ -357,4 +357,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "net")
(net_class Default "This is the default net class."
@ -223,4 +223,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "RAW")
(net 2 "GND")
(net 3 "RST")
@ -316,4 +316,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "from")
(net 2 "to")
(net 3 "pos")
@ -526,4 +526,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "A")
(net 2 "B")
(net 3 "C")
@ -225,4 +225,3 @@
)

View file

@ -139,4 +139,8 @@ exports.inject = (ergogen) => {
return `references ${p.ref_hide ? 'hidden' : 'shown'}`
}
})
ergogen.inject('template', 'template_test', params => {
return `Custom template override. The secret is ${params.custom.secret}.`
})
}

View file

@ -5,7 +5,7 @@ const glob = require('glob')
const u = require('../src/utils')
const a = require('../src/assert')
const ergogen = require('../src/ergogen')
require('./helpers/mock_footprints').inject(ergogen)
require('./helpers/mock').inject(ergogen)
let what = process.env.npm_config_what
const dump = process.env.npm_config_dump

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net 1 "P1")
(net 2 "T4_1")
(net 3 "T4_2")
@ -234,7 +234,7 @@
)
(gr_line (start -9.5 9.5) (end 9.5 9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start -9.5 9.5) (end 9.5 9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start 9.5 9.5) (end 9.5 -9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start 9.5 -9.5) (end -9.5 -9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start -9.5 -9.5) (end -9.5 9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
@ -245,4 +245,3 @@
)

View file

@ -0,0 +1,6 @@
points.zones.matrix:
pcbs:
main:
template: template_test
params:
secret: 42

View file

@ -0,0 +1 @@
Custom template override. The secret is 42.

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net_class Default "This is the default net class."
(clearance 0.2)
@ -105,7 +105,7 @@
)
(gr_line (start -5.7 9.5) (end 5.699999999999999 9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start -5.7 9.5) (end 5.699999999999999 9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start 9.5 5.7) (end 9.5 -5.699999999999999) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start 5.699999999999999 -9.5) (end -5.7 -9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
(gr_line (start -9.5 -5.699999999999999) (end -9.5 5.7) (angle 90) (layer Edge.Cuts) (width 0.15))
@ -129,4 +129,3 @@
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net_class Default "This is the default net class."
(clearance 0.2)
@ -104,9 +104,8 @@
(add_net "")
)
references hidden
references hidden
)

View file

@ -92,7 +92,7 @@
(outputdirectory ""))
)
(net 0 "")
(net 0 "")
(net_class Default "This is the default net class."
(clearance 0.2)
@ -104,9 +104,8 @@
(add_net "")
)
references shown
references shown
)