@jscad/amf-serializer
Advanced tools
Comparing version 1.0.0-pre-V2.0 to 2.0.0-alpha.5
@@ -6,2 +6,71 @@ # Change Log | ||
# [2.0.0-alpha.5](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/amf-serializer@2.0.0-alpha.4...@jscad/amf-serializer@2.0.0-alpha.5) (2020-09-28) | ||
**Note:** Version bump only for package @jscad/amf-serializer | ||
# [2.0.0-alpha.4](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/amf-serializer@2.0.0-alpha.3...@jscad/amf-serializer@2.0.0-alpha.4) (2020-09-19) | ||
**Note:** Version bump only for package @jscad/amf-serializer | ||
# [2.0.0-alpha.3](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/amf-serializer@2.0.0-alpha.2...@jscad/amf-serializer@2.0.0-alpha.3) (2020-09-08) | ||
**Note:** Version bump only for package @jscad/amf-serializer | ||
# [2.0.0-alpha.2](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/amf-serializer@2.0.0-alpha.1...@jscad/amf-serializer@2.0.0-alpha.2) (2020-09-02) | ||
### Bug Fixes | ||
* **all:** update dependencies ([d8c713a](https://github.com/jscad/OpenJSCAD.org/commit/d8c713a933b97a6d179ed3d3e923e188e334f99e)) | ||
# [2.0.0-alpha.1](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/amf-serializer@2.0.0-alpha.0...@jscad/amf-serializer@2.0.0-alpha.1) (2020-08-19) | ||
**Note:** Version bump only for package @jscad/amf-serializer | ||
# 2.0.0-alpha.0 (2020-08-13) | ||
### Features | ||
* **amf-serializer:** overhaul for V2 (#467) | ||
<a name="0.2.0"></a> | ||
# [0.2.0](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.1.2...@jscad/amf-serializer@0.2.0) (2018-11-25) | ||
<a name="0.1.2"></a> | ||
## [0.1.2](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.1.1...@jscad/amf-serializer@0.1.2) (2018-09-02) | ||
### Bug Fixes | ||
* **svg deserializer:** fixed svg-deserializer to work with Inkscape files ([#72](https://github.com/jscad/io/issues/72)) ([f35ea5e](https://github.com/jscad/io/commit/f35ea5e)) | ||
<a name="0.1.1"></a> | ||
@@ -8,0 +77,0 @@ ## [0.1.1](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.1.0...@jscad/amf-serializer@0.1.1) (2017-12-14) |
128
index.js
@@ -11,7 +11,7 @@ /* | ||
Notes: | ||
1) CAG conversion to: | ||
1) geom2 conversion to: | ||
none | ||
2) CSG conversion to: | ||
2) geom3 conversion to: | ||
mesh | ||
3) Path2D conversion to: | ||
3) path2 conversion to: | ||
none | ||
@@ -23,7 +23,8 @@ | ||
const {isCSG} = require('@jscad/csg') | ||
const {ensureManifoldness} = require('@jscad/io-utils') | ||
const {toArray} = require('@jscad/io-utils/arrays') | ||
const stringify = require('onml/lib/stringify') | ||
// const { ensureManifoldness } = require('@jscad/io-utils') | ||
const { geometries, utils } = require('@jscad/modeling') | ||
const mimeType = 'application/amf+xml' | ||
@@ -36,17 +37,3 @@ | ||
*/ | ||
const serialize = (...params) => { | ||
let options = {} | ||
let objects | ||
if (params.length === 0) { | ||
throw new Error('no arguments supplied to serialize function !') | ||
} else if (params.length === 1) { | ||
// assumed to be object(s) | ||
objects = Array.isArray(params[0]) ? params[0] : params | ||
} else if (params.length > 1) { | ||
options = params[0] | ||
objects = params[1] | ||
} | ||
// make sure we always deal with arrays of objects as inputs | ||
objects = toArray(objects) | ||
const serialize = (options, ...objects) => { | ||
const defaults = { | ||
@@ -59,6 +46,14 @@ statusCallback: null, | ||
options.statusCallback && options.statusCallback({progress: 0}) | ||
objects = utils.flatten(objects) | ||
// convert only 3D geometries | ||
const objects3d = objects.filter((object) => geometries.geom3.isA(object)) | ||
if (objects3d.length === 0) throw new Error('only 3D geometries can be serialized to AMF') | ||
if (objects.length !== objects3d.length) console.warn('some objects could not be serialized to AMF') | ||
options.statusCallback && options.statusCallback({ progress: 0 }) | ||
// construct the contents of the XML | ||
var body = ['amf', | ||
let body = ['amf', | ||
{ | ||
@@ -68,11 +63,11 @@ unit: options.unit, | ||
}, | ||
['metadata', {type: 'author'}, 'Created using JSCAD'] | ||
['metadata', { type: 'author' }, 'Created using JSCAD'] | ||
] | ||
body = body.concat(translateObjects(objects, options)) | ||
body = body.concat(translateObjects(objects3d, options)) | ||
// convert the contents to AMF (XML) format | ||
var amf = `<?xml version="1.0" encoding="UTF-8"?> | ||
${stringify(body)}` | ||
const amf = `<?xml version="1.0" encoding="UTF-8"?> | ||
${stringify(body, 2)}` | ||
options && options.statusCallback && options.statusCallback({progress: 100}) | ||
options && options.statusCallback && options.statusCallback({ progress: 100 }) | ||
@@ -83,8 +78,11 @@ return [amf] | ||
const translateObjects = (objects, options) => { | ||
let contents = [] | ||
objects.forEach(function (object, i) { | ||
if (isCSG(object) && object.polygons.length > 0) { | ||
object = ensureManifoldness(object) | ||
options.id = i | ||
contents.push(convertCSG(object, options)) | ||
const contents = [] | ||
objects.forEach((object, i) => { | ||
if (geometries.geom3.isA(object)) { | ||
const polygons = geometries.geom3.toPolygons(object) | ||
if (polygons.length > 0) { | ||
// TODO object = ensureManifoldness(object) | ||
options.id = i | ||
contents.push(convertToObject(object, options)) | ||
} | ||
} | ||
@@ -95,4 +93,4 @@ }) | ||
const convertCSG = (object, options) => { | ||
var contents = ['object', {id: options.id}, convertToMesh(object, options)] | ||
const convertToObject = (object, options) => { | ||
const contents = ['object', { id: options.id }, convertToMesh(object, options)] | ||
return contents | ||
@@ -102,3 +100,3 @@ } | ||
const convertToMesh = (object, options) => { | ||
var contents = ['mesh', {}, convertToVertices(object, options)] | ||
let contents = ['mesh', {}, convertToVertices(object, options)] | ||
contents = contents.concat(convertToVolumes(object, options)) | ||
@@ -109,10 +107,11 @@ return contents | ||
/* | ||
* This section converts each CSG object to a list of vertex / coordinates | ||
* This section converts each 3D geometry to a list of vertex / coordinates | ||
*/ | ||
const convertToVertices = (object, options) => { | ||
var contents = ['vertices', {}] | ||
const contents = ['vertices', {}] | ||
let vertices = [] | ||
object.polygons.forEach(function (polygon) { | ||
const vertices = [] | ||
const polygons = geometries.geom3.toPolygons(object) | ||
polygons.forEach((polygon) => { | ||
for (let i = 0; i < polygon.vertices.length; i++) { | ||
@@ -127,3 +126,3 @@ vertices.push(convertToVertex(polygon.vertices[i], options)) | ||
const convertToVertex = (vertex, options) => { | ||
let contents = ['vertex', {}, convertToCoordinates(vertex, options)] | ||
const contents = ['vertex', {}, convertToCoordinates(vertex, options)] | ||
return contents | ||
@@ -133,4 +132,3 @@ } | ||
const convertToCoordinates = (vertex, options) => { | ||
let position = vertex.pos | ||
let contents = ['coordinates', {}, ['x', {}, position._x], ['y', {}, position._y], ['z', {}, position._z]] | ||
const contents = ['coordinates', {}, ['x', {}, vertex[0]], ['y', {}, vertex[1]], ['z', {}, vertex[2]]] | ||
return contents | ||
@@ -140,10 +138,12 @@ } | ||
/* | ||
* This section converts each CSG object to a list of volumes consisting of indexes into the list of vertices | ||
* This section converts each 3D geometry to a list of volumes consisting of indexes into the list of vertices | ||
*/ | ||
const convertToVolumes = (object, options) => { | ||
let contents = [] | ||
let n = 0 | ||
const objectcolor = convertColor(object.color) | ||
const polygons = geometries.geom3.toPolygons(object) | ||
let n = 0 | ||
object.polygons.forEach(function (polygon) { | ||
const contents = [] | ||
polygons.forEach((polygon) => { | ||
if (polygon.vertices.length < 3) { | ||
@@ -154,7 +154,10 @@ return | ||
let volume = ['volume', {}] | ||
let color = convertToColor(polygon, options) | ||
let triangles = convertToTriangles(polygon, n) | ||
const polycolor = convertToColor(polygon, options) | ||
const triangles = convertToTriangles(polygon, n) | ||
if (color) { | ||
volume.push(color) | ||
if (polycolor) { | ||
volume.push(polycolor) | ||
} else | ||
if (objectcolor) { | ||
volume.push(objectcolor) | ||
} | ||
@@ -170,10 +173,4 @@ volume = volume.concat(triangles) | ||
const convertToColor = (polygon, options) => { | ||
let color = null | ||
if (polygon.shared && polygon.shared.color) { | ||
color = polygon.shared.color | ||
} else if (polygon.color) { | ||
color = polygon.color | ||
} | ||
if (color != null) { | ||
const convertColor = (color) => { | ||
if (color) { | ||
if (color.length < 4) color.push(1.0) | ||
@@ -185,8 +182,13 @@ return ['color', {}, ['r', {}, color[0]], ['g', {}, color[1]], ['b', {}, color[2]], ['a', {}, color[3]]] | ||
const convertToColor = (polygon, options) => { | ||
const color = polygon.color | ||
return convertColor(color) | ||
} | ||
const convertToTriangles = (polygon, index) => { | ||
let contents = [] | ||
const contents = [] | ||
// making sure they are all triangles (triangular polygons) | ||
for (var i = 0; i < polygon.vertices.length - 2; i++) { | ||
let triangle = ['triangle', {}, ['v1', {}, index], ['v2', {}, (index + i + 1)], ['v3', {}, (index + i + 2)]] | ||
for (let i = 0; i < polygon.vertices.length - 2; i++) { | ||
const triangle = ['triangle', {}, ['v1', {}, index], ['v2', {}, (index + i + 1)], ['v3', {}, (index + i + 2)]] | ||
contents.push(triangle) | ||
@@ -193,0 +195,0 @@ } |
{ | ||
"name": "@jscad/amf-serializer", | ||
"version": "1.0.0-pre-V2.0", | ||
"description": "Amf serializer for jscad project", | ||
"repository": "https://github.com/jscad/io", | ||
"version": "2.0.0-alpha.5", | ||
"description": "AMF serializer for JSCAD project", | ||
"repository": "https://github.com/jscad/OpenJSCAD.org", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "ava './test.js' --verbose --timeout 20000", | ||
"coverage": "nyc --all --reporter=html --reporter=text npm test", | ||
"test": "ava --verbose --timeout 2m 'tests/**/*.test.js'", | ||
"release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", | ||
@@ -30,3 +31,3 @@ "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", | ||
"jscad", | ||
"csg", | ||
"export", | ||
"serializer", | ||
@@ -37,9 +38,10 @@ "amf" | ||
"dependencies": { | ||
"@jscad/csg": "^0.5.3", | ||
"@jscad/io-utils": "^1.0.0-pre-V2.0", | ||
"onml": "^0.4.1" | ||
"@jscad/io-utils": "2.0.0-alpha.4", | ||
"@jscad/modeling": "2.0.0-alpha.4", | ||
"onml": "1.2.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.19.1" | ||
} | ||
"ava": "3.10.0" | ||
}, | ||
"gitHead": "28d9addc9037a36b4c8e26f1db4d2d1d044a71db" | ||
} |
## @jscad/amf-serializer | ||
> amf serializer for the jscad project (from CSG) | ||
> Serializer of JSCAD geometries to AMF shapes | ||
[![npm version](https://badge.fury.io/js/%40jscad%2Famf-serializer.svg)](https://badge.fury.io/js/%40jscad%2Famf-serializer) | ||
[![NPM version](https://badge.fury.io/js/%40jscad%2Famf-serializer.svg)](https://badge.fury.io/js/%40jscad%2Famf-serializer) | ||
[![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/amf-serializer) | ||
@@ -10,6 +10,8 @@ | ||
This serializer outputs a 'blobable' array of data (from a CSG object) | ||
ie an array that can either be passed directly to a Blob (`new Blob(blobable)`) | ||
or converted to a Node.js buffer. | ||
This serializer outputs a 'blobable' array of data from one or more JSCAD geometries. Only XML output is supported. | ||
The array of data can either be used to create a Blob (`new Blob(blobable)`), or converted to a Node.js buffer. | ||
The serialization of the following geometries are possible. | ||
- serialization of 3D geometry (geom3) to AMF geometry (non-overlapping volumes) | ||
## Table of Contents | ||
@@ -19,6 +21,5 @@ | ||
- [Usage](#usage) | ||
- [Contribute](#contribute) | ||
- [Contributing](#contributing) | ||
- [License](#license) | ||
## Installation | ||
@@ -32,7 +33,6 @@ | ||
```javascript | ||
const amfSerializer = require('@jscad/amf-serializer') | ||
const rawData = amfSerializer.serialize(CSGObject) | ||
const rawData = amfSerializer.serialize({unit: inch}, geometry) | ||
@@ -44,15 +44,22 @@ //in browser (with browserify etc) | ||
## Contributing | ||
## Contribute | ||
The various JSCAD packages and all source code are part of the JSCAD Organization, and maintained by a group of volunteers. | ||
We welcome and encourage anyone to pitch in but please take a moment to read the following guidelines. | ||
For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) | ||
* If you want to submit a bug report please make sure to follow the [Reporting Issues](https://github.com/jscad/OpenJSCAD.org/wiki/Reporting-Issues) guide. Bug reports are accepted as [Issues](https://github.com/jscad/OpenJSCAD.org/issues/) via GitHub. | ||
PRs accepted. | ||
* If you want to submit a change or a patch, please read the [Contributing Guide](https://github.com/jscad/OpenJSCAD.org/blob/master/CONTRIBUTING.md) . New contributions are accepted as [Pull Requests](https://github.com/jscad/OpenJSCAD.org/pulls/) via GithHub. | ||
* We only accept bug reports and pull requests on **GitHub**. | ||
* If you have a question about how to use JSCAD, then please start a conversation at the [JSCAD User Group](https://jscad.xyz/forum). You might find the answer in the [JSCAD.org User Guide](https://openjscad.org/dokuwiki/doku.php). | ||
* If you have a change or new feature in mind, please start a conversation with the [Core Developers](https://jscad.xyz/forum) and start contributing changes. | ||
Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. | ||
## License | ||
[The MIT License (MIT)](./LICENSE) | ||
[The MIT License (MIT)](https://github.com/jscad/OpenJSCAD.org/blob/master/LICENSE) | ||
(unless specified otherwise) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
25145
6
62
711
2
+ Added@jscad/io-utils@2.0.0-alpha.4(transitive)
+ Added@jscad/modeling@2.0.0-alpha.4(transitive)
+ Addedonml@1.2.0(transitive)
- Removed@jscad/csg@^0.5.3
- Removed@jscad/csg@0.5.4(transitive)
- Removed@jscad/io-utils@1.0.0-pre-V2.0(transitive)
- Removedonml@0.4.5(transitive)
Updatedonml@1.2.0