Created
July 9, 2015 07:10
-
-
Save tcr3dr/375d3bd4f8a406e5103d to your computer and use it in GitHub Desktop.
OK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs') | |
var gerberToSvg = require('gerber-to-svg'); | |
var stroke = require('./stroke') | |
var _ = require('underscore') | |
var gerberString = fs.readFileSync(__dirname + '/../accel-mma84/render/accel-mma84-F_Cu.gbr', 'utf-8') | |
var svgObj = gerberToSvg(gerberString, { object: true } ) | |
var paths = (svgObj.svg._[1].g._.filter(function (a) { | |
return 'path' in a | |
})) | |
var newpaths = [] | |
var regions = paths.map(function (p) { | |
var q = null; | |
for (var i = 0; i < p.path.d.length; i++) { | |
if (p.path.d[i] == 'M') { | |
if (q) { | |
newpaths.push(q); | |
} | |
q = JSON.parse(JSON.stringify(p)); | |
q.path.d = ['M']; | |
} else { | |
q.path.d.push(p.path.d[i]); | |
} | |
} | |
if (q) { | |
newpaths.push(q); | |
} | |
return p; | |
}) | |
var vertices = [] | |
var regions = []; | |
var polygons = []; | |
var clipper = require('clipper') | |
svgObj.svg._[1].g._ = [].concat(newpaths, svgObj.svg._[1].g._.filter(function (a) { | |
return !('path' in a) | |
}).map(function (u) { | |
u.use.fill = 'rgba(0, 0, 0, 0.5)' | |
return u; | |
})); | |
newpaths.filter(function (p) { | |
var poly = stroke([p.path.d[1], p.path.d[2]], [p.path.d[4], p.path.d[5]], p.path['stroke-width'] / 2) | |
regions.push([[vertices.length, vertices.length+1, vertices.length+2, vertices.length+3]]) | |
vertices = vertices.concat(poly); | |
polygons.push(_.flatten(poly)) | |
// p.path.d = 'M ' + poly[0].join(' ') + ' ' + | |
// 'L ' + poly[1].join(' ') + ' ' + | |
// 'L ' + poly[2].join(' ') + ' ' + | |
// 'L ' + poly[3].join(' ') + ' '; | |
// 'L ' + poly[0].join(' ') + ' '; | |
// delete p.path['stroke-width']; | |
// p.path.fill = 'green' | |
p.path['stroke'] = 'red'; | |
return p; | |
}) | |
var pir = require('point-in-region'); | |
var classifyPoint = pir(vertices, regions) | |
svgObj.svg.width = '100%' | |
svgObj.svg.height = '100%' | |
function spread (res, l) { | |
function alive (p) { | |
p.path['stroke'] = 'green' | |
p.path['data-title'] = l.name + ' / ' + l.net; | |
} | |
alive(newpaths[res]); | |
polygons.forEach(function (polymatch, i) { | |
var clip = clipper.begin(); | |
clip.setFillTypes(clipper.PolyFillType.POSITIVE, clipper.PolyFillType.POSITIVE); | |
clip.addSubjectPath(polygons[res], true); | |
clip.addClipPath(polymatch); | |
var output = clip.intersection()[0]; | |
if (output.length) { | |
// console.log('reviving', output); | |
if (newpaths[i].path.stroke != 'green') { | |
alive(newpaths[i]) | |
spread(i, l); | |
} | |
} | |
}) | |
} | |
var d356 = fs.readFileSync(__dirname + '/../accel-mma84/accel-mma84.d356', 'utf-8') | |
d356.split(/\n/).filter(function (l) { | |
return l[0] == '3'; | |
}).map(function (l) { | |
// 327SDA IC1 -3 A01X+053667Y-039174X0417Y0256R000S2 | |
return { | |
net: l.slice(3, 20).replace(/\s+$/, ''), | |
name: l.slice(20, 26).replace(/\s+$/, ''), | |
x: parseInt(l.slice(42, 42 + 7))*2.54, | |
y: parseInt(l.slice(50, 50 + 7))*2.54, | |
full: l, | |
} | |
}).filter(function (l) { | |
var res = classifyPoint([l.x, l.y]) | |
if (res != -1) { | |
spread(res, l); | |
// fix each point | |
// [ | |
// [newpaths[res].path.d[1], newpaths[res].path.d[2]], | |
// [newpaths[res].path.d[4], newpaths[res].path.d[5]] | |
// ].forEach(function (v) { | |
// var o = -1; | |
// var r2 = regions.slice(); | |
// do { | |
// var classif = pir(vertices, r2) | |
// o = classif(v); | |
// if (o != -1) { | |
// console.log('revived'); | |
// alive(newpaths[o]) | |
// r2[o] = [[0]] | |
// } | |
// } while (o != -1); | |
// console.log('done'); | |
// }) | |
} else { | |
// console.log(l) | |
} | |
}) | |
// console.log(vertices) | |
// console.log(classifyPoint([135882.5, -105000])) | |
var builder = require('gerber-to-svg/lib/obj-to-xml'); | |
var out = builder(svgObj, { | |
pretty: true | |
}) | |
fs.writeFileSync(__dirname + '/f_cu.svg', out, 'utf-8'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery.min.js"></script> | |
<style> | |
#frames iframe { | |
border: 0; | |
position: absolute; | |
top: 0; left: 0; | |
pointer-events: none; | |
} | |
#frames { | |
width: 800px; height: 400px; | |
margin-right: 1em; | |
position: relative; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>schematic viewer demo</h1> | |
<div id="frames" style="background: #666"> | |
<iframe src="edge.svg" width="400" height="400" style="left: 400px; background: #888;"></iframe> | |
<div style="-webkit-filter: hue-rotate(-120deg);"> | |
<iframe src="b_cu.svg" width="400" height="400" name='b_cu' style="left: 400px;"></iframe> | |
</div> | |
<iframe src="edge.svg" width="400" height="400"></iframe> | |
<iframe src="f_silk.svg" width="400" height="400" style="opacity: 0.4;"></iframe> | |
<iframe src="f_cu.svg" width="400" height="400" name='f_cu'></iframe> | |
</div> | |
<h2 id="target"></h2> | |
<script> | |
function magic (name) { | |
var frame = window.frames[name]; | |
return frame.document; | |
} | |
$('#frames').on('mousemove', function (e) { | |
var toptext = null; | |
['f_cu', 'b_cu'].some(function (name) { | |
var s = magic(name); | |
var path = s.elementFromPoint(e.clientX - $('iframe[name=' + name + ']').offset().left, e.clientY - $('iframe[name=' + name + ']').offset().top); | |
if (toptext) { | |
var text = null; | |
} else { | |
var text = $(path).attr('data-title') | |
} | |
toptext = toptext || text || ''; | |
}); | |
[magic('f_cu'), magic('b_cu')].some(function (s) { | |
$('path', s).each(function () { | |
if (toptext && $(this).attr('data-title').replace(/.*\//) == toptext.replace(/.*\//)) { | |
$(this).attr('stroke', '#00cc00'); | |
} else { | |
$(this).attr('stroke', 'green'); | |
} | |
}) | |
}) | |
$('#target').text((toptext || '').split(/\s+/).reverse().join(' ')); | |
}) | |
magic('b_cu'); | |
magic('f_cu'); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "svg", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function offset(o, dx, dy, width) { | |
return [o[0] - dx*width, o[1] + dy*width] | |
} | |
function stroke (a, b, width) { | |
a = a.slice() | |
b = b.slice() | |
// console.log(a, b); | |
var dx = (b[1] - a[1]) | |
var dy = (b[0] - a[0]) | |
var len = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)) | |
dx /= len; | |
dy /= len; | |
a[0] -= dy/2*width; | |
a[1] -= dx/2*width; | |
b[0] += dy/2*width; | |
b[1] += dx/2*width; | |
var A = offset(a, dx, dy, width); | |
var B = offset(a, dx, dy, -width); | |
var C = offset(b, dx, dy, -width); | |
var D = offset(b, dx, dy, width); | |
return [A, B, C, D]; | |
// console.log( | |
// 'M', A.join(' '), | |
// 'L', B.join(' '), | |
// 'L', C.join(' '), | |
// 'L', D.join(' '), | |
// 'L', A.join(' ')); | |
} | |
// var a = [2,2]; | |
// var b = [5,0]; | |
// console.log('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="1000" width="1000" viewBox="0 0 10 10">') | |
// console.log('<path d="') | |
// stroke(a, b); | |
// console.log('" />') | |
// console.log('<path d="M ' + a.join(' ') + ' L' + b.join(' ') + '" strokeWidth=".5" stroke="red" />') | |
// console.log('</svg>') | |
module.exports = stroke; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment