Verified Commit 6a0aa52f authored by Isabella Skořepová's avatar Isabella Skořepová
Browse files

imported kicad_pcb conversion script

parent 1ec9e4f9
......@@ -3,3 +3,5 @@
*.b#*
*.s#*
.mayaSwatches
.idea
node_modules
\ No newline at end of file
#!/bin/node
const {
load, save,
} = require('./kicad-pcb')
const fs = require('fs')
const util = require('util')
if(process.argv.length < 3 || !/\.kicad_pcb$/.exec(process.argv[2])) {
console.log('Specify .kicad_pcb as first argument');
process.exit(13)
}
const replace = (replacers) => (data) => {
const out = []
for(const el of data) {
if(Array.isArray(el) && replacers[el[0]]) {
out.push(replacers[el[0]](el))
} else {
out.push(el)
}
}
return out
}
const toObj = (a) => {
const obj = {}
a.slice(1).forEach(el => {
obj[el[0]] = el.slice(1)
})
return obj
}
// "meat" of the program - this specifies how the conversion is done
const replacers = {
zone: replace({
connect_pads: replace({
clearance: a => ['clearance', '0.1524'],
}),
}),
via: a => {
const {
at, size, drill, net, tstamp,
} = toObj(a)
if(net[0] !== '0') return a
return [ 'module',
'VIA-'+drill[0]+'_'+size[0]+'mm',
[ 'layer', 'Top' ],
[ 'tedit', ...tstamp ],
[ 'tstamp', ...tstamp ],
[ 'at', ...at ],
[ 'pad',
'1',
'thru_hole',
'circle',
[ 'at', '0', '0' ],
[ 'size', size[0], size[0] ],
[ 'drill', ...drill ],
[ 'layers', '*.Cu' ],
[ 'net', '1', 'GND' ],
[ 'zone_connect', '2' ] ] ]
},
module: a => {
const model = (arg) => {
let {
path,
at = ['0', '0', '0'],
scale = ['1', '1', '1'],
rotate = ['0', '0', '0'],
} = typeof arg === 'string' ? { path: arg } : arg
return [...a,
[
'model',
'${KISYS3DMOD}/'+path,
['at', ['xyz', ...at]],
['scale', ['xyz', ...scale]],
['rotate', ['xyz', ...rotate]],
]
]
}
const map = {
C0805: 'Capacitors_SMD.3dshapes/C_0805.wrl',
R0805: 'Resistors_SMD.3dshapes/R_0805.wrl',
CHIPLED_0805: {
path: 'LEDs.3dshapes/LED_0805.wrl',
at: [0, 0.007874015748031498, 0],
rotate: [0, 0, 90],
},
'CHIP-LED0805': {
path: 'LEDs.3dshapes/LED_0805.wrl',
at: [0, 0.007874015748031498, 0],
rotate: [0, 0, 90],
},
SOT23: 'TO_SOT_Packages_SMD.3dshapes/SOT-23.wrl',
'SOT-23': {
path: 'TO_SOT_Packages_SMD.3dshapes/SOT-23.wrl',
rotate: [0,0,-180],
},
'SOT23-BEC': 'TO_SOT_Packages_SMD.3dshapes/SOT-23.wrl',
'502774-0891': {
path: 'conn_misc/microsd_socket.wrl',
at: [-0.1161417322834646, 0.3248031496062992, 0],
rotate: [0, 0, -180]
},
}
if(map[a[1]]) return model(map[a[1]])
return a
}
}
const log = (a) => console.log(a) || a
load(process.argv[2])
.then(data => {
if(data[0] !== 'kicad_pcb') throw new Error('not kicad_pcb')
return data
})
// replacing
.then(replace(replacers))
// write
.then(data => save(process.argv[3] || process.argv[2].replace(/\.kicad_pcb$/, '_converted.kicad_pcb'), data))
.then(() => {
console.log('done')
})
.catch(e => {
console.error(e)
})
const fs = require('fs')
const parse = require('s-expression')
const stringify = (expr, p = '') => {
try {
if(expr instanceof String) {
return '"'+expr.replace(/\n/g,'\\n').replace(/"/g, '\\"')+'"'
}
if(typeof expr === 'number') {
return expr+''
}
if(typeof expr === 'string') {
if(/[\(\)]/.exec(expr) || !expr) return stringify(new String(expr))
return expr
}
// "leaf" s-expression
if(expr.filter(t => typeof t !== 'string' && typeof t !== 'number').length == 0) {
return '('+expr.map(stringify).reduce((a,b) => a+' '+b)+')'
}
return '('
+ expr
.map((t,i) => stringify(t,p+' '))
.reduce((a,b) => a+'\n '+p+b)
+ `\n${p})`
} catch(e) {
if(!e.rethrown) e.rethrown = 1
else e.rethrown++
if(e.rethrown <= 2) {
console.log('Error while parsing ',expr)
}
throw e
}
}
const load = (fname) => new Promise((res, rej) => {
fs.readFile(fname, 'utf8', (err, data) => {
if(err) rej(err)
try {
const parsed = parse(data)
res(parsed)
} catch(e) {
rej(e)
}
})
})
const save = (fname, obj) => new Promise((res, rej) => {
try {
const data = stringify(obj)
fs.writeFile(fname, data, 'utf8', (err) => {
if(err) {
rej(e)
} else {
res()
}
})
} catch(e) {
rej(e)
}
})
module.exports = {
stringify,
parse,
load,
save,
}
{
"flags": [],
"linkedModules": [],
"topLevelPatters": [
"s-expression@^3.0.3"
],
"lockfileEntries": {
"s-expression@^3.0.3": "https://registry.yarnpkg.com/s-expression/-/s-expression-3.0.3.tgz#80309bfea6bcf80960b61bb22f4498d17860799d"
},
"files": []
}
\ No newline at end of file
{
"dependencies": {
"s-expression": "^3.0.3"
},
"name": "conversion_script",
"version": "1.0.0",
"main": "index.js",
"license": "MIT"
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
s-expression@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/s-expression/-/s-expression-3.0.3.tgz#80309bfea6bcf80960b61bb22f4498d17860799d"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment