forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (55 loc) · 2 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
JSCAD Geometry to STL Format Serialization
## License
Copyright (c) 2018-2019 JSCAD Organization https://github.com/jscad
All code released under MIT license
Notes:
1) geom2 conversion to:
none
2) geom3 conversion to:
STL mesh
3) path2 conversion to:
none
*/
/**
* Serializer of JSCAD geometries to STL mesh.
* @module io/stl-serializer
* @example
* const { serializer, mimeType } = require('@jscad/stl-serializer')
*/
const { geometries, modifiers } = require('@jscad/modeling')
const { flatten, toArray } = require('@jscad/array-utils')
const { serializeBinary } = require('./CSGToStlb')
const { serializeText } = require('./CSGToStla')
const mimeType = 'application/sla'
/**
* Serialize the give objects to STL mesh.
* @param {Object} options - options for serialization
* @param {String} [options.binary='true'] - target format for data
* @param {Function} [options.statusCallback] - call back function for progress ({ progress: 0-100 })
* @param {...Object} objects - objects to serialize as STL
* @returns {Array} serialized contents with one STL mesh (either string or binary data)
* @alias module:io/stl-serializer.serialize
* @example
* const geometry = primitives.cube()
* const stlData = serializer({binary: false}, geometry)
*/
const serialize = (options, ...objects) => {
const defaults = {
binary: true,
statusCallback: null
}
options = Object.assign({}, defaults, options)
objects = flatten(objects)
// convert only 3D geometries
let objects3d = objects.filter((object) => geometries.geom3.isA(object))
if (objects3d.length === 0) throw new Error('only 3D geometries can be serialized to STL')
if (objects.length !== objects3d.length) console.warn('some objects could not be serialized to STL')
// convert to triangles
objects3d = toArray(modifiers.generalize({ snap: true, triangulate: true }, objects3d))
return options.binary ? serializeBinary(objects3d, options) : serializeText(objects3d, options)
}
module.exports = {
mimeType,
serialize
}