Skip to content
This repository has been archived by the owner on Jul 24, 2022. It is now read-only.

support for a status callback #49

Merged
merged 6 commits into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/amf-deserializer/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const createObject = require('./objectBuilder')
const parse = require('./parse')

const translate = function (src, filename, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
filename = filename || 'amf'
const defaults = {pxPmm: require('./constants').pxPmm, version: '0.0.0', addMetaData: true}
options = Object.assign({}, defaults, options)
Expand All @@ -20,8 +21,13 @@ const translate = function (src, filename, options) {
if (!amfObj) {
throw new Error('AMF parsing failed, no valid AMF data retrieved')
}

options && options.statusCallback && options.statusCallback({progress: 50})

const scadCode = codify(amfObj, {amfMaterials, amfTextures, amfConstels})
code += scadCode

options && options.statusCallback && options.statusCallback({progress: 100})
return code
}

Expand Down
9 changes: 7 additions & 2 deletions packages/amf-serializer/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { ensureManifoldness } = require('@jscad/io-utils')
const mimeType = 'application/amf+xml'

function serialize (CSG, m) {
function serialize (CSG, m, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
CSG = ensureManifoldness(CSG)
var result = '<?xml version="1.0" encoding="UTF-8"?>\n<amf' + (m && m.unit ? ' unit="+m.unit"' : '') + '>\n'
for (var k in m) {
Expand All @@ -17,7 +18,7 @@ function serialize (CSG, m) {
result += '</vertices>\n'

var n = 0
CSG.polygons.map(function (p) { // then we dump all polygons
CSG.polygons.map(function (p, i) { // then we dump all polygons
result += '<volume>\n'
if (p.vertices.length < 3) {
return
Expand All @@ -42,9 +43,13 @@ function serialize (CSG, m) {
}
n += p.vertices.length
result += '</volume>\n'
options && options.statusCallback && options.statusCallback({progress: 100 * i / CSG.polygons.length})
})
result += '</mesh>\n</object>\n'
result += '</amf>\n'

options && options.statusCallback && options.statusCallback({progress: 100})

return [result]
}

Expand Down
11 changes: 7 additions & 4 deletions packages/dxf-serializer/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const mimeType = 'application/dxf'

function serialize (cagObject) {
function serialize (cagObject, options) {
var paths = cagObject.getOutlinePaths()
return PathsToDxf(paths)
return PathsToDxf(paths, options)
}

function PathsToDxf (paths) {
function PathsToDxf (paths, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
var str = '999\nDXF generated by OpenJsCad\n'
str += ' 0\nSECTION\n 2\nHEADER\n'
str += ' 0\nENDSEC\n'
Expand All @@ -22,7 +23,7 @@ function PathsToDxf (paths) {
str += ' 0\nSECTION\n 2\nBLOCKS\n'
str += ' 0\nENDSEC\n'
str += ' 0\nSECTION\n 2\nENTITIES\n'
paths.map(function (path) {
paths.map(function (path, i) {
var numpointsClosed = path.points.length + (path.closed ? 1 : 0)
str += ' 0\nLWPOLYLINE\n 8\nOpenJsCad\n 90\n' + numpointsClosed + '\n 70\n' + (path.closed ? 1 : 0) + '\n'
for (var pointindex = 0; pointindex < numpointsClosed; pointindex++) {
Expand All @@ -31,8 +32,10 @@ function PathsToDxf (paths) {
var point = path.points[pointindexwrapped]
str += ' 10\n' + point.x + '\n 20\n' + point.y + '\n 30\n0.0\n'
}
options && options.statusCallback && options.statusCallback({progress: 100 * i / paths.length})
})
str += ' 0\nENDSEC\n 0\nEOF\n'
options && options.statusCallback && options.statusCallback({progress: 100})
return [str]
}

Expand Down
4 changes: 4 additions & 0 deletions packages/gcode-deserializer/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

function deserialize (gcode, filename, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
// http://reprap.org/wiki/G-code
const defaults = {version: '0.0.0', addMetaData: true, output: 'jscad'}
options = Object.assign({}, defaults, options)
Expand Down Expand Up @@ -83,6 +84,7 @@ function deserialize (gcode, filename, options) {
lpos.E = pos.E
}
ld = d
options && options.statusCallback && options.statusCallback({progress: 100 * i / l.length})
}

let code = addMetaData ? `//
Expand All @@ -99,6 +101,8 @@ function deserialize (gcode, filename, options) {
`
// if(err) src += "// WARNING: import errors: "+err+" (some triangles might be misaligned or missing)\n";

options && options.statusCallback && options.statusCallback({progress: 100})

return code
}

Expand Down
3 changes: 3 additions & 0 deletions packages/json-deserializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ function toSource (obj) {
// fn (optional) original filename of JSON source
//
function deserialize (src, fn, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
fn = fn || 'amf'
const defaults = {version: '0.0.0'}
options = Object.assign({}, defaults, options)
const {version} = options

// convert the JSON into an anonymous object
var obj = JSON.parse(src)
options && options.statusCallback && options.statusCallback({progress: 50})
// convert the internal objects to JSCAD code
var code = ''
code += '//\n'
Expand All @@ -99,6 +101,7 @@ function deserialize (src, fn, options) {
code += 'function main() {\n'
code += toSource(obj)
code += '};\n'
options && options.statusCallback && options.statusCallback({progress: 100})
return code
};

Expand Down
15 changes: 10 additions & 5 deletions packages/json-serializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@ const { ensureManifoldness } = require('@jscad/io-utils')

const mimeType = 'application/json'

function fromCAG (CAG) {
function fromCAG (CAG, options) {
let str = '{ "type": "cag","sides": ['
let comma = ''
CAG.sides.map(
function (side) {
function (side, i) {
str += comma
str += JSON.stringify(side)
comma = ','
options && options.statusCallback && options.statusCallback({progress: 100 * i / CAG.sides.length})
}
)
str += '] }'
return [str]
}

function fromCSG (CSG) {
function fromCSG (CSG, options) {
let str = '{ "type": "csg","polygons": ['
let comma = ''
CSG.polygons.map(
function (polygon) {
function (polygon, i) {
str += comma
str += JSON.stringify(polygon)
comma = ','
options && options.statusCallback && options.statusCallback({progress: 100 * i / CSG.polygons.length})
}
)
str += '],'
Expand All @@ -34,7 +36,10 @@ function fromCSG (CSG) {
}

function serialize (data, options) {
return 'sides' in data ? fromCAG(data) : fromCSG(ensureManifoldness(data))
options && options.statusCallback && options.statusCallback({progress: 0})
const result = 'sides' in data ? fromCAG(data, options) : fromCSG(ensureManifoldness(data), options)
options && options.statusCallback && options.statusCallback({progress: 100})
return result
}

module.exports = {
Expand Down
10 changes: 7 additions & 3 deletions packages/obj-deserializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ const {CSG} = require('@jscad/csg')
* @return {CSG/string} either a CAG/CSG object or a string (jscad script)
*/
function deserialize (input, filename, options) { // http://en.wikipedia.org/wiki/Wavefront_.obj_file
options && options.statusCallback && options.statusCallback({progress: 0})
const defaults = {version: '0.0.0', addMetaData: true, output: 'jscad'}
options = Object.assign({}, defaults, options)
const {output} = options

const {positions, faces} = getPositionsAndFaces(input)
return output === 'jscad' ? stringify({positions, faces, options}) : objectify({positions, faces, options})
const {positions, faces} = getPositionsAndFaces(input, options)
const result = output === 'jscad' ? stringify({positions, faces, options}) : objectify({positions, faces, options})
options && options.statusCallback && options.statusCallback({progress: 100})
return result
}

const getPositionsAndFaces = data => {
const getPositionsAndFaces = (data, options) => {
let lines = data.split(/\n/)
let positions = []
let faces = []
Expand Down Expand Up @@ -53,6 +56,7 @@ const getPositionsAndFaces = data => {
} else {
// vn vt and all others disregarded
}
options && options.statusCallback && options.statusCallback({progress: 90 * i / lines.length}) //getPositionsAndFaces is 90% of total
}
return {positions, faces}
}
Expand Down
10 changes: 9 additions & 1 deletion packages/stl-deserializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const echo = console.info
* @return {CSG/string} either a CAG/CSG object or a string (jscad script)
*/
function deserialize (stl, filename, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
const defaults = {version: '0.0.0', addMetaData: true, output: 'jscad'}
options = Object.assign({}, defaults, options)
const {version, output, addMetaData} = options
Expand All @@ -30,14 +31,21 @@ function deserialize (stl, filename, options) {

stl = isBinary && isBuffer(stl) ? bufferToBinaryString(stl) : stl

options && options.statusCallback && options.statusCallback({progress: 33})

const elementFormatterJscad = ({vertices, triangles, normals, colors, index}) => `// object #${index}: triangles: ${triangles.length}\n${vt2jscad(vertices, triangles, null)}`
const elementFormatterCSG = ({vertices, triangles, normals, colors}) => polyhedron({ points: vertices, polygons: triangles })

options && options.statusCallback && options.statusCallback({progress: 66})

const deserializer = isBinary ? deserializeBinarySTL : deserializeAsciiSTL
const elementFormatter = output === 'jscad' ? elementFormatterJscad : elementFormatterCSG
const outputFormatter = output === 'jscad' ? formatAsJscad : formatAsCsg

return outputFormatter(deserializer(stl, filename, version, elementFormatter), addMetaData, version, filename)
const result = outputFormatter(deserializer(stl, filename, version, elementFormatter), addMetaData, version, filename)

options && options.statusCallback && options.statusCallback({progress: 100})
return result

/*
if (err) src += '// WARNING: import errors: ' + err + ' (some triangles might be misaligned or missing)\n'
Expand Down
7 changes: 5 additions & 2 deletions packages/stl-serializer/CSGToStla.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

function serialize (CSG) {
function serialize (CSG, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
var result = 'solid csg.js\n'
CSG.polygons.map(function (p) {
CSG.polygons.map(function (p, i) {
result += CSGPolygontoStlString(p)
options && options.statusCallback && options.statusCallback({progress: 100 * i / CSG.polygons.length})
})
result += 'endsolid csg.js\n'
options && options.statusCallback && options.statusCallback({progress: 100})
return [result]
}

Expand Down
7 changes: 5 additions & 2 deletions packages/stl-serializer/CSGToStlb.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

// see http://en.wikipedia.org/wiki/STL_%28file_format%29#Binary_STL
function serialize (CSG) {
function serialize (CSG, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
// first check if the host is little-endian:
var buffer = new ArrayBuffer(4)
var int32buffer = new Int32Array(buffer, 0, 1)
Expand Down Expand Up @@ -35,7 +36,7 @@ function serialize (CSG) {
// and one uint16:
var triangleUint16array = new Uint16Array(triangleBuffer, 48, 1)
var byteoffset = 0
CSG.polygons.map(function (p) {
CSG.polygons.map(function (p, i) {
var numvertices = p.vertices.length
for (var i = 0; i < numvertices - 2; i++) {
var normal = p.plane.normal
Expand All @@ -55,7 +56,9 @@ function serialize (CSG) {
allTrianglesBufferAsInt8.set(triangleBufferAsInt8, byteoffset)
byteoffset += 50
}
options && options.statusCallback && options.statusCallback({progress: 100 * i / CSG.polygons.length})
})
options && options.statusCallback && options.statusCallback({progress: 100})
return [headerarray.buffer, ar1.buffer, allTrianglesBuffer]// 'blobable array'
/* return new Blob([headerarray.buffer, ar1.buffer, allTrianglesBuffer], {
type: mimeType
Expand Down
14 changes: 13 additions & 1 deletion packages/stl-serializer/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const serializer = require('./index.js')
test('serialize csg to stl (binary)', function (t) {
const input = new CSG.cube()
const observed = serializer.serialize(input, {binary: true})

// TODO: VERY shallow testing ... improve
t.deepEqual(observed[0].byteLength, 80)
t.deepEqual(observed[1].byteLength, 4)
Expand All @@ -18,3 +18,15 @@ test('serialize csg to stl (ascii)', function (t) {
const observed = serializer.serialize(input, {binary: false})
t.deepEqual(observed, expected)
})

test('progress status callback', function (t) {
const input = new CSG.cube()
const progresses = [];
const statusCallback = function (statusObj) {
progresses.push(statusObj.progress);
};
const observed = serializer.serialize(input, {statusCallback: statusCallback})

t.deepEqual(0, progresses[0]);
t.deepEqual(100, progresses[progresses.length - 1]);
})
12 changes: 11 additions & 1 deletion packages/svg-deserializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const deserialize = function (input, filename, options) {
* @return a CAG (2D CSG) object
*/
function deserializeToCSG (src, filename, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
filename = filename || 'svg'
const defaults = {pxPmm: require('./constants').pxPmm, version: '0.0.0', addMetaData: true}
options = Object.assign({}, defaults, options)
Expand All @@ -60,7 +61,12 @@ function deserializeToCSG (src, filename, options) {
throw new Error('SVG parsing failed, no valid svg data retrieved')
}

return objectify(svgObj)
options && options.statusCallback && options.statusCallback({progress: 50})

const result = objectify(svgObj)

options && options.statusCallback && options.statusCallback({progress: 100})
return result
}

/**
Expand All @@ -75,6 +81,7 @@ function deserializeToCSG (src, filename, options) {
* @return a CAG (2D CSG) object
*/
function translate (src, filename, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
filename = filename || 'svg'
const defaults = {pxPmm: require('./constants').pxPmm, version: '0.0.0', addMetaData: true}
options = Object.assign({}, defaults, options)
Expand All @@ -94,9 +101,12 @@ function translate (src, filename, options) {
throw new Error('SVG parsing failed, no valid svg data retrieved')
}

options && options.statusCallback && options.statusCallback({progress: 50})

const scadCode = codify(svgObj)
code += scadCode

options && options.statusCallback && options.statusCallback({progress: 100})
return code
}

Expand Down
10 changes: 7 additions & 3 deletions packages/svg-serializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ function dPath (path, xoffset, yoffset) {
return str
}

function PathsToSvg (paths, bounds) {
function PathsToSvg (paths, bounds, options) {
// calculate offsets in order to create paths orientated from the 0,0 axis
var xoffset = 0 - bounds[0].x
var yoffset = 0 - bounds[0].y

return paths.reduce(function (res, path) {
return paths.reduce(function (res, path, i) {
options && options.statusCallback && options.statusCallback({progress: 100 * i / paths.length})
return res.concat([['path', {d: dPath(path, xoffset, yoffset)}]])
}, ['g'])
}

function serialize (cagObject) {
function serialize (cagObject, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
var decimals = 1000

// mirror the CAG about the X axis in order to generate paths into the POSITIVE direction
Expand Down Expand Up @@ -60,6 +62,8 @@ function serialize (cagObject) {
svg += '<!-- Generated by OpenJSCAD.org -->\n'
svg += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">\n'
svg += stringify(body)

options && options.statusCallback && options.statusCallback({progress: 100})
return svg
}

Expand Down
Loading