stl-exporter
Version:
Convert face-vertex, or polygon meshes to ascii and binary STLs
71 lines (59 loc) • 2.97 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var AsciiSerializer, createBeautifiedFacetString, createFacetString, stream,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
stream = require('stream');
createFacetString = function(face) {
var normal, vertices;
normal = face.normal, vertices = face.vertices;
return ("facet normal " + normal.x + " " + normal.y + " " + normal.z + "\n") + 'outer loop\n' + ("vertex " + vertices[0].x + " " + vertices[0].y + " " + vertices[0].z + "\n") + ("vertex " + vertices[1].x + " " + vertices[1].y + " " + vertices[1].z + "\n") + ("vertex " + vertices[2].x + " " + vertices[2].y + " " + vertices[2].z + "\n") + 'endloop\n' + 'endfacet\n';
};
createBeautifiedFacetString = function(face) {
var normal, vertices;
normal = face.normal, vertices = face.vertices;
return ("facet normal " + normal.x + " " + normal.y + " " + normal.z + "\n") + '\touter loop\n' + ("\t\tvertex " + vertices[0].x + " " + vertices[0].y + " " + vertices[0].z + "\n") + ("\t\tvertex " + vertices[1].x + " " + vertices[1].y + " " + vertices[1].z + "\n") + ("\t\tvertex " + vertices[2].x + " " + vertices[2].y + " " + vertices[2].z + "\n") + '\tendloop\n' + 'endfacet\n';
};
AsciiSerializer = (function(superClass) {
extend(AsciiSerializer, superClass);
function AsciiSerializer(options) {
var base, base1;
this.options = options != null ? options : {};
if ((base = this.options).writableObjectMode == null) {
base.writableObjectMode = true;
}
if ((base1 = this.options).readableObjectMode == null) {
base1.readableObjectMode = false;
}
AsciiSerializer.__super__.constructor.call(this, this.options);
this.internalBuffer = [];
this.name = '';
}
AsciiSerializer.prototype._flush = function(done) {
this.push("endsolid " + this.name + "\n");
return done();
};
AsciiSerializer.prototype._transform = function(chunk, encoding, done) {
var data;
this.internalBuffer = this.internalBuffer.concat(chunk.split('\n'));
while (data = this.internalBuffer.shift()) {
data = JSON.parse(data);
if (data.hasOwnProperty('name')) {
this.name = data.name;
this.push("solid " + this.name + "\n");
continue;
}
if (data.hasOwnProperty('vertices')) {
if (this.options.beautify) {
this.push(createBeautifiedFacetString(data));
} else {
this.push(createFacetString(data));
}
}
}
return done();
};
return AsciiSerializer;
})(stream.Transform);
module.exports = AsciiSerializer;
}).call(this);