tracespace / gerber-to-svg

gerber-to-svg development moved to tracespace/tracespace
https://github.com/tracespace/tracespace
MIT License
81 stars 17 forks source link

infinite loop if file ends without M02 #1

Closed mcous closed 10 years ago

mcous commented 10 years ago

Get the parser to return a file end flag or something

henrikekblad commented 8 years ago

Hmm.. I'm getting a "Unable to generate board file: Error: Error at line 119 - end of file encountered before required M02 command" on this file. Seems to have M02 at the end...

henrikekblad_issue-1.txt

Edit by @mcous: Remove paste and add link to file

mcous commented 8 years ago

I just ran this file in the command line and on svgerber and didn't have any issues. Seeing as the file is 6443 lines long and you're seeing the error at line 119, there could be a problem with whatever is grabbing the string to enter into gerber-to-svg.

Off the top of my head, is the file coming from a request that could be getting chunked up? Or maybe from a FileReader that isn't done reading yet?

henrikekblad commented 8 years ago

Hmm.. you might be onto something.. I'm using promises to read the file (after beeing downloaded and unzipped). Might be an issue with this.. will do some more tests.

var Promise = require("bluebird");
var fs = Promise.promisifyAll(require("fs-extra")); 
let content = yield fs.readFileAsync(filename, "utf-8");

I'll.. let you know when resolved.

mcous commented 8 years ago

Certainly seems like it could cause problems. You could try doing it with the normal fs.readFile callback to check that it works, and then building up to the promise wrapping and fs-extra.

If you're in need of asynchronicity, you could also play around with the release-candidate branch. I haven't finished tying up all the loose ends after a big refactor (still working on step repeats, pretty printing, and getting the API completely sorted), but it does let you use it with fs.createReadStream. Details here: https://github.com/mcous/gerber-to-svg/blob/release-candidate/API.md

henrikekblad commented 8 years ago

Nice,

Is pcb-stackup compatible with the release candidate?

_sort-layers seems to blow here:

for (i = 0, len = layers.length; i < len; i++) {
      ly = layers[i];
      svg = ly.svg;
      children = svg.svg._;              <------- HERE 
      type = ly.type;
      props = layerProps(svg);
      gType = genericType(type);
2015-12-10T09:19:49.888Z - info: (myproject/controllers/create.js:297) TypeError: Cannot read property '_' of undefined
    at sortLayers (/Users/hek/git/openhardware/myproject/node_modules/pcb-stackup/lib/_sort-layers.js:47:25)

When running this snippet on release-candidate

var idLayer = require('pcb-stackup/lib/layer-types').identify;
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require("fs-extra")); 
var fsOrig = Promise.promisifyAll(require("fs")); 
var gerberToSvg = Promise.promisify(require('gerber-to-svg'));

let generateBoardsG = function*(id) {
    let imagesDir = __dirname + "/../public/uploads/"+id+"/image/";
    let boardDir = __dirname + "/../public/uploads/"+id+"/design/";
    yield fs.mkdirpAsync(imagesDir);
            let warnings = [];
    return yield fs.readdirAsync(boardDir).filter(function(filename) {
        return idLayer(filename) !== "drw";
    }).map(function(filename) {
        return fs.readFileAsync(boardDir+filename, {encoding: "utf8"}).then(function(content) {
            let layerType = idLayer(filename)
            console.log(filename);
            return gerberToSvg(content, {id: "oh", object: true, drill: (layerType === 'drl'), warnArr: warnings}).then(function (layer) {
                return {type: layerType, svg: layer};
            });
        }).catch(function(err) {
            console.log(filename);
            console.log(err.stack);
        });
    }).then(function(layers) {
        let insert = function (orig, find, insert) {
           var pos = orig.indexOf(find); 
           return [orig.slice(0, pos), insert, orig.slice(pos)].join('');
        };

        let style = 
        '<style type="text/css">\
            .oh_board-fr4 { color: #696969; }\
            .oh_board-cu { color: #d3d3d3; }\
            .oh_board-cf { color: #daa520; }\
            .oh_board-sm { color: #006400; opacity: 0.75; }\
            .oh_board-ss { color: #ffffff; }\
            .oh_board-sp { color: #c0c0c0; }\
            .oh_board-out { color: #000000; }\
        </style>';
        let stackup = pcbStackup(layers, 'oh');
        let top = insert(gerberToSvg(stackup.top), "<defs>", style);
        let bottom = insert(gerberToSvg(stackup.bottom), "<defs>", style);
       yield fs.writeFileAsync(Path.join(imagesDir, "generatedTop.svg"), top);
       yield fs.writeFileAsync(Path.join(imagesDir, "generatedBottom.svg"), bottom);
        let path = "/uploads/"+id+"/";
        return [{type:"image", path:path+"generatedTop.svg"}, {type:"image", path:path+"generatedBottom.svg"}];
    }).catch(function (err) {
        console.log(err.stack);
        console.log(err);
    })
};
henrikekblad commented 8 years ago

Hmm.. looks like gerberToSvg doesn't respect the {object:true} options any longer.

mcous commented 8 years ago

Ah oops, please forgive my 2AM brain for leaving that (admittedly important) part out! Object mode has been dropped, and pcb-stackup will need to be completely re-written. So if you're using pcb-stackup, the RC of gerber-to-svg isn't an option right now. I won't release gerber-to-svg v1.0.0 until I have pcb-stackup ready for the new version, though.

henrikekblad commented 8 years ago

Ok,

Too bad.. The parsing went much better with the release-candidate.. Hmm.. Will the merging of layers be more complicated now in the stacker?

mcous commented 8 years ago

Actually, it will be way simpler! Simple string concatenation instead of a bunch of array parsing and nested objects. I also get to throw out most of board shape module because release-candidate reorganizes paths for smaller file sizes. I'm hoping to get some time to work on it in the coming weeks (in a sprint at the real job right now).

mcous commented 8 years ago

Oh and there won't be any need to throw it back into gerber-to-svg at the end.

henrikekblad commented 8 years ago

Ok, sounds promising. I'll test anything you throw at me :)

mcous commented 8 years ago

:+1: sweet I'll be sure to let you know when I've got stuff up