Skip to content

Commit

Permalink
Merge pull request #212 from kaosat-dev/node-x3d-support
Browse files Browse the repository at this point in the history
Enhancements for exporting X3D from NODEJS based scripts.
  • Loading branch information
z3dev authored Jan 13, 2017
2 parents 4a9f456 + 678cfa7 commit 3871b49
Show file tree
Hide file tree
Showing 8 changed files with 6,565 additions and 74 deletions.
2,169 changes: 2,166 additions & 3 deletions dist/cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cli.js.map

Large diffs are not rendered by default.

2,269 changes: 2,206 additions & 63 deletions dist/index.js

Large diffs are not rendered by default.

2,169 changes: 2,166 additions & 3 deletions dist/module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/module.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"openscad-openjscad-translator": "0.0.7",
"sax": "^1.2.1",
"underscore": "^1.8.3",
"webworkify": "^1.4.0"
"webworkify": "^1.4.0",
"xmldom": "^0.1.27"
},
"devDependencies": {
"ava": "^0.15.2",
Expand Down
9 changes: 7 additions & 2 deletions src/io/writers/CSGToX3D.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import makeBlob from '../../utils/Blob'
const Blob = makeBlob()

import xmldom from 'xmldom'
const XMLSerializer = xmldom.XMLSerializer
// NOTE: might be useful :https://github.com/jindw/xmldom/pull/152/commits/be5176ece6fa1591daef96a5f361aaacaa445175

export default function toX3D (CSG) {
const DOMImplementation = typeof document !== 'undefined' ? document.implementation : new xmldom.DOMImplementation()
// materialPolygonLists
// key: a color string (e.g. "0 1 1" for yellow)
// value: an array of strings specifying polygons of this color
Expand Down Expand Up @@ -48,9 +53,9 @@ export default function toX3D (CSG) {
})

// create output document
var docType = document.implementation.createDocumentType('X3D',
var docType = DOMImplementation.createDocumentType('X3D',
'ISO//Web3D//DTD X3D 3.1//EN', 'http://www.web3d.org/specifications/x3d-3.1.dtd')
var exportDoc = document.implementation.createDocument(null, 'X3D', docType)
var exportDoc = DOMImplementation.createDocument(null, 'X3D', docType)
exportDoc.insertBefore(
exportDoc.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"'),
exportDoc.doctype)
Expand Down
16 changes: 16 additions & 0 deletions src/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,19 @@ test('generateOutput(amf)', t => {
t.is(size, 385246)// FIXME: verify: original value was 385255
})
})

test('generateOutput(x3d)', t => {
const {generateOutput} = openjscad
// FIXME : create a fake csgObject rather than using output from another function
const inputPath = path.resolve(__dirname, '../examples/logo.jscad')
const script = fs.readFileSync(inputPath, 'UTF8')

return openjscad.compile(script, {})
.then(function (input) {
const output = generateOutput('x3d', input)
const {type, encoding, size} = output // FIXME for some reason this fails ?t.is(output.encoding, 'foo' when falsy)
t.is(type, 'model/x3d+xml')
t.is(encoding, 'utf8')
t.is(size, 44384)
})
})

0 comments on commit 3871b49

Please sign in to comment.