-
Notifications
You must be signed in to change notification settings - Fork 0
/
stl2json.js
executable file
·47 lines (40 loc) · 922 Bytes
/
stl2json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env node
var stl = require('stl');
var argv = require('optimist').argv;
var through = require('through2');
var path = require('path');
var fs = require('fs');
var parser = stl.createParseStream();
var source = process.stdin;
if (argv._.length) {
console.log(path.resolve(argv._[0]));
source = require('fs').createReadStream(
path.resolve(argv._[0])
);
}
var first = true;
source
.pipe(parser)
.pipe(through.obj(function(obj, enc, fn) {
if (obj && obj.verts) {
if (first) {
this.push('[');
first = false;
} else {
this.push(',');
}
// Flatten
if (argv.f) {
this.push(obj.verts.map(function(vert) {
return vert.join(',')
}).join(','))
} else {
this.push(JSON.stringify(obj));
}
}
fn();
}))
.pipe(process.stdout);
parser.on('end', function() {
process.stdout.write(']');
});