├── .gitignore ├── .npmrc ├── .travis.yml ├── CONTRIBUTING.md ├── README.md ├── lerna.json ├── package.json ├── packages ├── amf-deserializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── constants.js │ ├── deserialize.js │ ├── helpers.js │ ├── index.js │ ├── objectBuilder.js │ ├── package.json │ ├── parse.js │ ├── test.js │ └── translate.js ├── amf-serializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── index.js │ ├── package.json │ └── test.js ├── dxf-deserializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── DxfReader.js │ ├── README.md │ ├── autocad.js │ ├── index.js │ ├── instantiate.js │ ├── notes.txt │ ├── package.json │ ├── test │ │ ├── test-2d-entities.js │ │ ├── test-2d-translation.js │ │ ├── test-3d-entities.js │ │ ├── test-3d-translation.js │ │ ├── test-DxfReader.js │ │ ├── test-dxf-versions.js │ │ └── test-dxf.js │ └── translate.js ├── dxf-serializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── autocad_AC2017.js │ ├── index.js │ ├── package.json │ └── tests │ │ ├── testCagToDxf.js │ │ ├── testCsgToDxf.js │ │ └── testPathToDxf.js ├── gcode-deserializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── index.js │ ├── package.json │ └── test.js ├── io-utils │ ├── .npmrc │ ├── BinaryReader.js │ ├── CHANGELOG.md │ ├── README.md │ ├── arrays.js │ ├── ensureManifoldness.js │ ├── index.js │ ├── makeBlob.js │ ├── package.json │ └── test.js ├── io │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── index.js │ └── package.json ├── json-deserializer │ ├── .npmrc │ ├── CAGFromJson.js │ ├── CHANGELOG.md │ ├── CSGFromJson.js │ ├── README.md │ ├── index.js │ ├── package.json │ └── test.js ├── json-serializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── index.js │ ├── package.json │ └── test.js ├── obj-deserializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── index.js │ ├── package.json │ ├── test.js │ └── vt2jscad.js ├── scad-deserializer │ ├── LICENSE │ ├── README.md │ ├── openscad-parser.jison │ ├── package.json │ ├── src │ │ ├── ArgContainer.js │ │ ├── ArgsContainer.js │ │ ├── CSGModule.js │ │ ├── ChildModule.js │ │ ├── Context.js │ │ ├── ControlModules.js │ │ ├── Expression.js │ │ ├── FunctionDef.js │ │ ├── Globals.js │ │ ├── IfElseModuleInstantiation.js │ │ ├── ImportModule.js │ │ ├── Module.js │ │ ├── ModuleAdaptor.js │ │ ├── ModuleInstantiation.js │ │ ├── OpenjscadSolidFactory.js │ │ ├── OpenjscadSolidFactorySingleton.js │ │ ├── OpenscadOpenjscadParser.js │ │ ├── PrimitiveModules.js │ │ ├── Range.js │ │ ├── StlDecoder.js │ │ ├── TransformModules.js │ │ ├── openscad-parser-ext.js │ │ ├── openscad-parser-support.js │ │ ├── openscad-parser.js │ │ ├── parserCJS.js │ │ ├── parserMaker.js │ │ └── parserWrapper.js │ └── tests │ │ ├── 2d_primitives.js │ │ ├── 2d_primitives │ │ ├── squareEx1.jscad │ │ └── squareEx1.scad │ │ ├── 2d_to_3d_extrusion.js │ │ ├── 2d_to_3d_extrusion │ │ ├── linearExtrudeEx1.jscad │ │ ├── linearExtrudeEx1.scad │ │ ├── linearExtrudeEx2.jscad │ │ ├── linearExtrudeEx2.scad │ │ ├── linearExtrudeEx3.jscad │ │ ├── linearExtrudeEx3.scad │ │ ├── linearExtrudeEx4.jscad │ │ ├── linearExtrudeEx4.scad │ │ ├── linearExtrudeEx5.jscad │ │ ├── linearExtrudeEx5.scad │ │ ├── linearExtrudeEx6.jscad │ │ ├── linearExtrudeEx6.scad │ │ ├── linearExtrudeEx7.jscad │ │ └── linearExtrudeEx7.scad │ │ ├── all-tests.js │ │ ├── conditional_and_iterator_functions.js │ │ ├── conditional_and_iterator_functions │ │ ├── assignStatementEx1.jscad │ │ ├── assignStatementEx1.scad │ │ ├── forLoopEx1.jscad │ │ ├── forLoopEx1.scad │ │ ├── forLoopEx2a.jscad │ │ ├── forLoopEx2a.scad │ │ ├── forLoopEx2b.jscad │ │ ├── forLoopEx2b.scad │ │ ├── forLoopEx3.jscad │ │ ├── forLoopEx3.scad │ │ ├── forLoopEx4.jscad │ │ ├── forLoopEx4.scad │ │ ├── ifStatementEx1.jscad │ │ ├── ifStatementEx1.scad │ │ ├── intersectionForLoopEx1.jscad │ │ ├── intersectionForLoopEx1.scad │ │ ├── intersectionForLoopEx2.jscad │ │ └── intersectionForLoopEx2.scad │ │ ├── examples.js │ │ ├── examples │ │ ├── example001.jscad │ │ ├── example001.scad │ │ ├── example002.jscad │ │ ├── example002.scad │ │ ├── example003.jscad │ │ ├── example003.scad │ │ ├── example004.jscad │ │ ├── example004.scad │ │ ├── example005.jscad │ │ ├── example005.scad │ │ ├── example006.jscad │ │ ├── example006.scad │ │ ├── example007.dxf │ │ ├── example007.scad │ │ ├── example008.dxf │ │ ├── example008.scad │ │ ├── example009.dxf │ │ ├── example009.scad │ │ ├── example010.dat │ │ ├── example010.scad │ │ ├── example011.jscad │ │ ├── example011.scad │ │ ├── example012.scad │ │ ├── example012.stl │ │ ├── example013.dxf │ │ ├── example013.scad │ │ ├── example014.jscad │ │ ├── example014.scad │ │ ├── example015.scad │ │ ├── example016.scad │ │ ├── example016.stl │ │ ├── example017.dxf │ │ ├── example017.scad │ │ ├── example018.jscad │ │ ├── example018.scad │ │ ├── example019.jscad │ │ ├── example019.scad │ │ ├── example020.scad │ │ ├── example021.scad │ │ ├── example022.scad │ │ └── example023.scad │ │ ├── general.js │ │ ├── helpers.js │ │ ├── index.html │ │ ├── math_ops.scad │ │ ├── modules.js │ │ ├── modules │ │ ├── modulesChildEx1.jscad │ │ ├── modulesChildEx1.scad │ │ ├── modulesChildrenEx1.jscad │ │ ├── modulesChildrenEx1.scad │ │ ├── modulesEx1.jscad │ │ ├── modulesEx1.scad │ │ ├── modulesParametersEx1.jscad │ │ └── modulesParametersEx1.scad │ │ ├── primitive_solids.js │ │ ├── primitive_solids │ │ ├── cubeEx1.jscad │ │ ├── cubeEx1.scad │ │ ├── cubeEx2.jscad │ │ ├── cubeEx2.scad │ │ ├── cylinderEx1.jscad │ │ ├── cylinderEx1.scad │ │ ├── cylinderEx2.jscad │ │ ├── cylinderEx2.scad │ │ ├── cylinderEx3.jscad │ │ ├── cylinderEx3.scad │ │ ├── cylinderEx4.jscad │ │ ├── cylinderEx4.scad │ │ ├── cylinderEx5.jscad │ │ ├── cylinderEx5.scad │ │ ├── polyhedronEx1.jscad │ │ ├── polyhedronEx1.scad │ │ ├── polyhedronEx2.jscad │ │ ├── polyhedronEx2.scad │ │ ├── sphereEx1.jscad │ │ ├── sphereEx1.scad │ │ ├── sphereEx2.jscad │ │ └── sphereEx2.scad │ │ ├── submodule_tests.js │ │ ├── submodule_tests │ │ ├── nestedSubmoduleEx1.jscad │ │ ├── nestedSubmoduleEx1.scad │ │ ├── nestedSubmoduleEx2.jscad │ │ ├── nestedSubmoduleEx2.scad │ │ ├── transformedSubmoduleEx1.jscad │ │ ├── transformedSubmoduleEx1.scad │ │ ├── transformedSubmoduleEx2.jscad │ │ ├── transformedSubmoduleEx2.scad │ │ ├── transformedSubmoduleEx3.jscad │ │ └── transformedSubmoduleEx3.scad │ │ ├── test.scad │ │ ├── testharness.js │ │ ├── transformations.js │ │ ├── transformations │ │ ├── colorEx1.jscad │ │ ├── colorEx1.scad │ │ ├── colorEx2.jscad │ │ ├── colorEx2.scad │ │ ├── mirrorEx1.jscad │ │ ├── mirrorEx1.scad │ │ ├── multmatrixEx1.jscad │ │ ├── multmatrixEx1.scad │ │ ├── multmatrixEx2.jscad │ │ ├── multmatrixEx2.scad │ │ ├── rotateEx1.jscad │ │ ├── rotateEx1.scad │ │ ├── rotateEx2.jscad │ │ ├── rotateEx2.scad │ │ ├── scaleEx1.jscad │ │ ├── scaleEx1.scad │ │ ├── scaleEx2.jscad │ │ ├── scaleEx2.scad │ │ ├── translateEx1.jscad │ │ └── translateEx1.scad │ │ ├── undef_tests.scad │ │ └── vector_matrix_math.scad ├── stl-deserializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── index.js │ ├── package.json │ ├── test │ │ ├── pr2_head_tilt_vertices.json │ │ └── test.js │ └── vt2jscad.js ├── stl-serializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── CSGToStla.js │ ├── CSGToStlb.js │ ├── README.md │ ├── index.js │ ├── package.json │ └── test.js ├── svg-deserializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── constants.js │ ├── helpers.js │ ├── index.js │ ├── package.json │ ├── shapesMapCsg.js │ ├── shapesMapJscad.js │ ├── svgElementHelpers.js │ └── test.js ├── svg-serializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── index.js │ ├── package.json │ └── test.js └── x3d-serializer │ ├── .npmrc │ ├── CHANGELOG.md │ ├── README.md │ ├── index.js │ ├── package.json │ └── test.js └── test └── helpers └── nearlyEqual.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | package-lock.json 4 | *.log 5 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6" 4 | - '8' 5 | - '9' 6 | sudo: false 7 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## io 2 | 3 | ## Input Output Format Handling Library - Contributing Guide 4 | 5 | This library is part of the JSCAD Organization, and is maintained by a group of volunteers. We welcome and encourage anyone to pitch in but please take a moment to read the following guidelines. 6 | 7 | * If you want to submit a bug report please make sure to follow the [Reporting Issues](https://github.com/jscad/csg.js/wiki/Reporting-Issues) guide. Bug reports are accepted as [Issues](https://github.com/jscad/io/issues/) via GitHub. 8 | 9 | * If you want to submit a change or a patch, please read the contents below on how to make changes. New contributions are accepted as [Pull Requests](https://github.com/jscad/io/pulls/) via GithHub. 10 | 11 | * We only accept bug reports and pull requests on **GitHub**. 12 | 13 | * If you have a question about how to use CSG.js, then please start a conversation at the [OpenJSCAD.org User Group](https://plus.google.com/communities/114958480887231067224). You might find the answer in the [OpenJSCAD.org User Guide](https://github.com/Spiritdude/OpenJSCAD.org/wiki/User-Guide). 14 | 15 | * If you have a change or new feature in mind, please start a conversation with the [Core Developers](https://plus.google.com/communities/114958480887231067224) and start contributing changes. 16 | 17 | Thanks! 18 | 19 | The JSCAD Organization 20 | 21 | ## Making Changes 22 | 23 | First, we suggest that you fork this GIT repository. This will keep your changes separate from the fast lane. And make pull requests easier. 24 | 25 | Once forked, clone your copy of the CSG library. 26 | ``` 27 | git clone https://github.com/myusername/io.git 28 | cd io 29 | ``` 30 | 31 | **We suggest downloading NPM. This will allow you to generate API documents, and run test suites.** 32 | 33 | Once you have NPM, install all the dependencies for development. 34 | ``` 35 | npm install 36 | ``` 37 | And, run the test cases to verify the library is functional. 38 | ``` 39 | npm test 40 | ``` 41 | 42 | The project is structured as: 43 | - packages/* : package directories 44 | - */index.js : source code for each package 45 | - io/index.js : source code for main io library 46 | 47 | You can now make changes to the library, as well as the test suites. 48 | 49 | If you intend to contribute changes back to JSCAD then please be sure to create test cases. 50 | 51 | Done? Great! Push the changes back to the fork. 52 | ``` 53 | git commit changed-file 54 | git add test/new-test-suite.js 55 | git commit test/new-test-suite.js 56 | git push 57 | ``` 58 | Finally, you can review your changes via GitHub, and create a pull request. 59 | 60 | TIPS for successful pull requests: 61 | - Commit often, and comment well 62 | - Follow the [JavaScript Standard Style](https://github.com/feross/standard) 63 | - Create test cases for all changes 64 | - Verify that all tests suites pass 65 | 66 | WOW! Thanks for the cool code. 67 | 68 | -------------------------------------------------------------------------------- /lerna.json: -------------------------------------------------------------------------------- 1 | { 2 | "lerna": "2.1.2", 3 | "packages": [ 4 | "packages/*" 5 | ], 6 | "version": "independent", 7 | "command": { 8 | "init": { 9 | "exact": true 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/io", 3 | "private": "true", 4 | "version": "0.1.0", 5 | "description": "Input , output and formats handling for jscad project", 6 | "repository": "https://github.com/jscad/io", 7 | "scripts": { 8 | "test": "lerna bootstrap && lerna run test", 9 | "lint": "eslint packages/**/*.js", 10 | "publish": "git checkout master && git pull origin master && npm t && lerna publish --conventional-commits", 11 | "publish-dryrun": "git checkout master && git pull origin master && npm t && lerna publish --conventional-commits --skip-git --skip-npm" 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openscad", 29 | "openjscad", 30 | "jscad", 31 | "parametric", 32 | "modeling", 33 | "cad", 34 | "io", 35 | "formats", 36 | "parser", 37 | "writer", 38 | "serializer", 39 | "deserializer" 40 | ], 41 | "license": "MIT", 42 | "devDependencies": { 43 | "@jscad/sample-files": "github:jscad/sample-files", 44 | "ava": "^0.25.0", 45 | "eslint": "^4.9.0", 46 | "eslint-config-standard": "^10.2.1", 47 | "eslint-plugin-import": "^2.8.0", 48 | "eslint-plugin-node": "^5.2.0", 49 | "eslint-plugin-promise": "^3.6.0", 50 | "eslint-plugin-standard": "^3.0.1", 51 | "lerna": "2.1.2" 52 | }, 53 | "eslintConfig": { 54 | "extends": "standard" 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /packages/amf-deserializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/amf-deserializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | ## [0.2.3](https://github.com/jscad/io/compare/@jscad/amf-deserializer@0.2.2...@jscad/amf-deserializer@0.2.3) (2018-11-25) 8 | 9 | 10 | 11 | 12 | **Note:** Version bump only for package @jscad/amf-deserializer 13 | 14 | 15 | ## [0.2.2](https://github.com/jscad/io/compare/@jscad/amf-deserializer@0.2.1...@jscad/amf-deserializer@0.2.2) (2018-11-22) 16 | 17 | 18 | 19 | 20 | **Note:** Version bump only for package @jscad/amf-deserializer 21 | 22 | 23 | ## [0.2.1](https://github.com/jscad/io/compare/@jscad/amf-deserializer@0.2.0...@jscad/amf-deserializer@0.2.1) (2017-12-14) 24 | 25 | 26 | 27 | 28 | **Note:** Version bump only for package @jscad/amf-deserializer 29 | 30 | 31 | # [0.2.0](https://github.com/jscad/io/compare/@jscad/amf-deserializer@0.1.0...@jscad/amf-deserializer@0.2.0) (2017-11-29) 32 | 33 | 34 | ### Features 35 | 36 | * add support for a status callback for de/serialization progress ([#49](https://github.com/jscad/io/issues/49)) ([f457cdb](https://github.com/jscad/io/commit/f457cdb)) 37 | 38 | 39 | 40 | 41 | 42 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/amf-deserializer@0.0.4...@jscad/amf-deserializer@0.1.0) (2017-11-14) 43 | 44 | 45 | ### Features 46 | 47 | * **deserializers:** added csg output capabilities to amf & obj deserializers & basic tests ([#47](https://github.com/jscad/io/issues/47)) ([abf3040](https://github.com/jscad/io/commit/abf3040)) 48 | 49 | 50 | 51 | 52 | 53 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/amf-deserializer@0.0.3...@jscad/amf-deserializer@0.0.4) (2017-11-04) 54 | 55 | 56 | 57 | 58 | **Note:** Version bump only for package @jscad/amf-deserializer 59 | 60 | 61 | ## [0.0.3](https://github.com/jscad/io/compare/@jscad/amf-deserializer@0.0.2...@jscad/amf-deserializer@0.0.3) (2017-10-10) 62 | 63 | 64 | 65 | 66 | **Note:** Version bump only for package @jscad/amf-deserializer 67 | 68 | 69 | ## 0.0.2 (2017-10-10) 70 | 71 | 72 | 73 | 74 | **Note:** Version bump only for package @jscad/amf-deserializer 75 | -------------------------------------------------------------------------------- /packages/amf-deserializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/amf-deserializer 2 | 3 | > amf deserializer for the jscad project 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Famf-deserializer.svg)](https://badge.fury.io/js/%40jscad%2Famf-deserializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/amf-deserializer) 7 | 8 | ## Overview 9 | 10 | This deserializer converts raw amf data to jscad code (that can be evaluated to CSG/CAG). 11 | 12 | ## Table of Contents 13 | 14 | - [Installation](#installation) 15 | - [Usage](#usage) 16 | - [Contribute](#contribute) 17 | - [License](#license) 18 | 19 | 20 | ## Installation 21 | 22 | ``` 23 | npm install @jscad/amf-deserializer 24 | ``` 25 | 26 | ## Usage 27 | 28 | 29 | ```javascript 30 | const amfDeSerializer = require('@jscad/amf-deserializer') 31 | 32 | const rawData = fs.readFileSync('PATH/TO/file.amf') 33 | const csgData = amfDeSerializer.deserialize(rawData, undefined, {output: 'csg'}) 34 | ``` 35 | 36 | 37 | ## Contribute 38 | 39 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 40 | 41 | PRs accepted. 42 | 43 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 44 | 45 | 46 | ## License 47 | 48 | [The MIT License (MIT)](./LICENSE) 49 | (unless specified otherwise) 50 | -------------------------------------------------------------------------------- /packages/amf-deserializer/constants.js: -------------------------------------------------------------------------------- 1 | const inchMM = (1 / 0.039370) // used for scaling AMF (inch) to CAG coordinates(MM) 2 | 3 | module.exports = {inchMM} 4 | -------------------------------------------------------------------------------- /packages/amf-deserializer/deserialize.js: -------------------------------------------------------------------------------- 1 | const {CSG} = require('@jscad/csg') 2 | 3 | const createObject = require('./objectBuilder') 4 | const parse = require('./parse') 5 | 6 | const deserializeToCSG = function (src, filename, options) { 7 | filename = filename || 'amf' 8 | const defaults = {pxPmm: require('./constants').pxPmm, version: '0.0.0', addMetaData: true} 9 | options = Object.assign({}, defaults, options) 10 | const {pxPmm} = options 11 | 12 | // parse the AMF data 13 | const {amfObj, amfMaterials, amfTextures, amfConstels} = parse(src, pxPmm) 14 | if (!amfObj) { 15 | throw new Error('AMF parsing failed, no valid amf data retrieved') 16 | } 17 | 18 | return objectify(amfObj, {amfMaterials, amfTextures, amfConstels}) 19 | } 20 | 21 | const objectify = (amf, data) => { 22 | let objects = amf.objects 23 | const csgs = objects.map((object, index) => object.type === 'object' ? createObject(object, index, data, {amf, csg: true}) : undefined) 24 | return new CSG().union(csgs) 25 | } 26 | 27 | module.exports = deserializeToCSG 28 | -------------------------------------------------------------------------------- /packages/amf-deserializer/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | ## License 3 | 4 | Copyright (c) 2016 Z3 Development https://github.com/z3dev 5 | Copyright (c) 2013-2016 by Rene K. Mueller 6 | Copyright (c) 2016 by Z3D Development 7 | 8 | All code released under MIT license 9 | 10 | History: 11 | 2016/06/27: 0.5.1: rewrote using SAX XML parser, enhanced for multiple objects, materials, units by Z3Dev 12 | 2013/04/11: 0.018: added alpha support to AMF export 13 | 14 | */ 15 | 16 | // ////////////////////////////////////////// 17 | // 18 | // AMF is a language for describing three-dimensional graphics in XML 19 | // See http://www.astm.org/Standards/ISOASTM52915.htm 20 | // See http://amf.wikispaces.com/ 21 | // 22 | // ////////////////////////////////////////// 23 | const translate = require('./translate') 24 | const deserializeToCSG = require('./deserialize') 25 | 26 | /** 27 | * Parse the given AMF source (xml) and return either a JSCAD script or a CSG/CAG object 28 | * @param {string} input amf data 29 | * @param {string} filename (optional) original filename of AMF source 30 | * @param {object} options options (optional) anonymous object with: 31 | * @param {string} [options.version='0.0.0'] version number to add to the metadata 32 | * @param {boolean} [options.addMetadata=true] toggle injection of metadata (producer, date, source) at the start of the file 33 | * @param {string} [options.output='jscad'] {String} either jscad or csg to set desired output 34 | * @return {CSG/string} either a CAG/CSG object or a string (jscad script) 35 | */ 36 | const deserialize = function (input, filename, options) { 37 | const defaults = { 38 | output: 'jscad' 39 | } 40 | options = Object.assign({}, defaults, options) 41 | return options.output === 'jscad' ? translate(input, filename, options) : deserializeToCSG(input, filename, options) 42 | } 43 | 44 | module.exports = { 45 | deserialize 46 | } 47 | -------------------------------------------------------------------------------- /packages/amf-deserializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/amf-deserializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Amf deserializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 10000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "deserializer", 32 | "amf" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "^0.7.0", 37 | "sax": "^1.2.1" 38 | }, 39 | "devDependencies": { 40 | "ava": "^0.25.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /packages/amf-deserializer/translate.js: -------------------------------------------------------------------------------- 1 | const createObject = require('./objectBuilder') 2 | const parse = require('./parse') 3 | 4 | const translate = function (src, filename, options) { 5 | options && options.statusCallback && options.statusCallback({progress: 0}) 6 | filename = filename || 'amf' 7 | const defaults = {pxPmm: require('./constants').pxPmm, version: '0.0.0', addMetaData: true} 8 | options = Object.assign({}, defaults, options) 9 | const {version, pxPmm, addMetaData} = options 10 | 11 | // parse the AMF source 12 | const {amfObj, amfMaterials, amfTextures, amfConstels} = parse(src, pxPmm) 13 | // convert the internal objects to JSCAD code 14 | let code = addMetaData ? `// 15 | // producer: OpenJSCAD.org ${version} AMF deserializer 16 | // date: ${new Date()} 17 | // source: ${filename} 18 | // 19 | ` : '' 20 | 21 | if (!amfObj) { 22 | throw new Error('AMF parsing failed, no valid AMF data retrieved') 23 | } 24 | 25 | options && options.statusCallback && options.statusCallback({progress: 50}) 26 | 27 | const scadCode = codify(amfObj, {amfMaterials, amfTextures, amfConstels}) 28 | code += scadCode 29 | 30 | options && options.statusCallback && options.statusCallback({progress: 100}) 31 | return code 32 | } 33 | 34 | // 35 | // convert the internal repreentation into JSCAD code 36 | // 37 | function codify (amf, data) { 38 | if (amf.type !== 'amf' || (!amf.objects)) throw new Error('AMF malformed') 39 | let code = '' 40 | 41 | // hack due to lack of this in array map() 42 | let objects = amf.objects 43 | let materials = data.amfMaterials 44 | 45 | // convert high level definitions 46 | function createDefinition (obj, didx) { 47 | // console.log(materials.length); 48 | switch (obj.type) { 49 | case 'object': 50 | code += createObject(obj, didx, data, {csg: false}) 51 | break 52 | case 'metadata': 53 | break 54 | case 'material': 55 | break 56 | default: 57 | console.log('Warning: unknown definition: ' + obj.type) 58 | break 59 | } 60 | } 61 | 62 | // start everthing 63 | code = `// Objects : ${objects.length} 64 | // Materials: ${materials.length} 65 | 66 | // helper functions 67 | ` 68 | 69 | if (amf.scale !== 1.0) { 70 | code += 'let SCALE = ' + amf.scale + '; // scaling units (' + amf.unit + ')\n' 71 | code += 'let VV = function(x,y,z) { return new CSG.Vertex(new CSG.Vector3D(x*SCALE,y*SCALE,z*SCALE)); };\n' 72 | } else { 73 | code += 'let VV = function(x,y,z) { return new CSG.Vertex(new CSG.Vector3D(x,y,z)); };\n' 74 | } 75 | code += `let PP = function(a) { return new CSG.Polygon(a); }; 76 | 77 | function main() { 78 | let csgs = []; 79 | ` 80 | for (let i = 0; i < objects.length; i++) { 81 | let obj = objects[i] 82 | if (obj.type === 'object') { 83 | code += ' csgs.push(createObject' + obj.id + '());\n' 84 | } 85 | } 86 | code += ` return union(csgs); 87 | } 88 | 89 | ` 90 | 91 | objects.map(createDefinition, data) 92 | return code 93 | } 94 | 95 | module.exports = translate 96 | -------------------------------------------------------------------------------- /packages/amf-serializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/amf-serializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | # [0.2.0](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.1.2...@jscad/amf-serializer@0.2.0) (2018-11-25) 8 | 9 | 10 | 11 | 12 | ## [0.1.2](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.1.1...@jscad/amf-serializer@0.1.2) (2018-09-02) 13 | 14 | 15 | ### Bug Fixes 16 | 17 | * **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)) 18 | 19 | 20 | 21 | 22 | 23 | ## [0.1.1](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.1.0...@jscad/amf-serializer@0.1.1) (2017-12-14) 24 | 25 | 26 | 27 | 28 | **Note:** Version bump only for package @jscad/amf-serializer 29 | 30 | 31 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.0.5...@jscad/amf-serializer@0.1.0) (2017-11-29) 32 | 33 | 34 | ### Features 35 | 36 | * add support for a status callback for de/serialization progress ([#49](https://github.com/jscad/io/issues/49)) ([f457cdb](https://github.com/jscad/io/commit/f457cdb)) 37 | 38 | 39 | 40 | 41 | 42 | ## [0.0.5](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.0.4...@jscad/amf-serializer@0.0.5) (2017-11-04) 43 | 44 | 45 | 46 | 47 | **Note:** Version bump only for package @jscad/amf-serializer 48 | 49 | 50 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.0.3...@jscad/amf-serializer@0.0.4) (2017-10-30) 51 | 52 | 53 | 54 | 55 | **Note:** Version bump only for package @jscad/amf-serializer 56 | 57 | 58 | ## [0.0.3](https://github.com/jscad/io/compare/@jscad/amf-serializer@0.0.2...@jscad/amf-serializer@0.0.3) (2017-10-10) 59 | 60 | 61 | 62 | 63 | **Note:** Version bump only for package @jscad/amf-serializer 64 | 65 | 66 | ## 0.0.2 (2017-10-10) 67 | 68 | 69 | 70 | 71 | **Note:** Version bump only for package @jscad/amf-serializer 72 | -------------------------------------------------------------------------------- /packages/amf-serializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/amf-serializer 2 | 3 | > amf serializer for the jscad project (from CSG) 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Famf-serializer.svg)](https://badge.fury.io/js/%40jscad%2Famf-serializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/amf-serializer) 7 | 8 | ## Overview 9 | 10 | This serializer outputs a 'blobable' array of data (from a CSG object) 11 | ie an array that can either be passed directly to a Blob (`new Blob(blobable)`) 12 | or converted to a Node.js buffer. 13 | 14 | ## Table of Contents 15 | 16 | - [Installation](#installation) 17 | - [Usage](#usage) 18 | - [Contribute](#contribute) 19 | - [License](#license) 20 | 21 | 22 | ## Installation 23 | 24 | ``` 25 | npm install @jscad/amf-serializer 26 | ``` 27 | 28 | ## Usage 29 | 30 | 31 | ```javascript 32 | const amfSerializer = require('@jscad/amf-serializer') 33 | 34 | const rawData = amfSerializer.serialize(CSGObject) 35 | 36 | //in browser (with browserify etc) 37 | const blob = new Blob(rawData) 38 | 39 | ``` 40 | 41 | 42 | ## Contribute 43 | 44 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 45 | 46 | PRs accepted. 47 | 48 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 49 | 50 | 51 | ## License 52 | 53 | [The MIT License (MIT)](./LICENSE) 54 | (unless specified otherwise) 55 | -------------------------------------------------------------------------------- /packages/amf-serializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/amf-serializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Amf serializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 20000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "serializer", 32 | "amf" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "0.7.0", 37 | "@jscad/io-utils": "^1.0.0-alpha.d0fb3056", 38 | "onml": "^0.4.1" 39 | }, 40 | "devDependencies": { 41 | "@jscad/csg": "0.7.0", 42 | "ava": "^0.25.0" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /packages/dxf-deserializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/dxf-deserializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | ## [0.1.2](https://github.com/jscad/io/compare/@jscad/dxf-deserializer@0.1.1...@jscad/dxf-deserializer@0.1.2) (2018-11-22) 8 | 9 | 10 | 11 | 12 | **Note:** Version bump only for package @jscad/dxf-deserializer 13 | 14 | 15 | ## [0.1.1](https://github.com/jscad/io/compare/@jscad/dxf-deserializer@0.1.0...@jscad/dxf-deserializer@0.1.1) (2018-05-24) 16 | 17 | 18 | ### Bug Fixes 19 | 20 | * **dxf-deserializer:** fix layers/ labels ([#65](https://github.com/jscad/io/issues/65)) ([2804e3b](https://github.com/jscad/io/commit/2804e3b)) 21 | 22 | 23 | 24 | 25 | 26 | # 0.1.0 (2018-02-24) 27 | 28 | 29 | ### Features 30 | 31 | * **DXF Deserializer:** Adds dxf deserializer and all associated code ([ae4a6c0](https://github.com/jscad/io/commit/ae4a6c0)), closes [#10](https://github.com/jscad/io/issues/10) 32 | -------------------------------------------------------------------------------- /packages/dxf-deserializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/dxf-deserializer 2 | 3 | > DXF deserializer for the JSCAD project 4 | 5 | [![GitHub version](https://badge.fury.io/gh/jscad%40jscad%2Fdxf-deserializer.svg)](https://badge.fury.io/gh/jscad%40jscad%2Fdxf-deserializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/dxf-deserializer) 7 | 8 | ## Overview 9 | 10 | This deserializer converts raw DXF data to JSCAD code fragments, or converts raw DXF data directy to CSG library objects. When converting unknown DXF data, converting to JSCAD code fragments will provide more information. 11 | 12 | ### DXF Implementation Notes 13 | 14 | The [DXF(tm) file structure](http://docs.autodesk.com/ACD/2014/ENU/files/GUID-73E9E797-3BAA-4795-BBD8-4CE7A03E93CF.htm) changes continously as AutoDesk releases new features and fixes. So, you can imagine that after 20 years, the contents of DXF files are pretty messed up. Expect the worst. 15 | 16 | **NOTE: At this time, only ASCII DXF files are supported. BINARY DXF files are not supported.** 17 | 18 | This deserializer converts only what is required by JSCAD libraries and applications. Full document conversion is NOT supported, so don't even try. However, conversion of the following DXF entities are possible: 19 | 20 | | DXF Entity | CSG Object | Notes | 21 | | --------------- | ---------- | ------ | 22 | | 3DFACE | to CSG | | 23 | | ARC | to Path2D | | 24 | | CIRCLE | to CAG | Start and stop angles are ignored | 25 | | ELLIPSE | to CAG | Start and stop angles are ignored | 26 | | LINE | to Line2D or Line3D | This will change in the future | 27 | | LWPOLYLINE | to Path2D or CAG | Conversion to CAG if LWPOLYLINE is closed | 28 | | MESH | to CSG | | 29 | | POLYLINE (line) | to Path2D or CAG | Conversion to CAG if POLYLINE is closed | 30 | | POLYLINE (mesh) | to CSG | | 31 | | POLYLINE (face) | to CSG | | 32 | 33 | In addition, colors are converted using the AutoCad standard color index (256 colors). 34 | 35 | Finally, there are many applications that can save to DXF formats. All testing is performed with files from AutoCad, period. 36 | 37 | ## Table of Contents 38 | 39 | - [Installation](#installation) 40 | - [Usage](#usage) 41 | - [Contribute](#contribute) 42 | - [License](#license) 43 | 44 | 45 | ## Installation 46 | 47 | ``` 48 | npm install @jscad/dxf-deserializer 49 | ``` 50 | 51 | ## Usage 52 | 53 | ```javascript 54 | const deSerializer = require('@jscad/dxf-deserializer') 55 | 56 | const rawData = fs.readFileSync('PATH/TO/file.dxf') 57 | const jscadCode = deSerializer(rawData) 58 | 59 | ``` 60 | 61 | ## Contribute 62 | 63 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 64 | 65 | PRs accepted. 66 | 67 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 68 | 69 | ## License 70 | 71 | [The MIT License (MIT)](./LICENSE) 72 | -------------------------------------------------------------------------------- /packages/dxf-deserializer/notes.txt: -------------------------------------------------------------------------------- 1 | 3D Only Entities 2 | ================ 3 | + 3DFACE (DXF) 4 | + MESH (DXF) 5 | SOLID (DXF) 6 | 7 | 2D / 3D Entities 8 | ================ 9 | + ARC (DXF) 10 | + CIRCLE (DXF) 11 | + ELLIPSE (DXF) 12 | HATCH (DXF) 13 | HELIX (DXF) 14 | + LINE (DXF) 15 | + LWPOLYLINE (DXF) 16 | MLINE (DXF) 17 | + POLYLINE (DXF) 18 | 19 | 2D Only Entities 20 | ================ 21 | SPLINE (DXF) - a (rational) spline with ControlPoints, Degrees, Knots and Weights. 22 | 23 | Intermediate Entities 24 | ===================== 25 | + SEQEND (DXF) 26 | + VERTEX (DXF) 27 | 28 | Unknown / Unsupported Entites 29 | ============================= 30 | - 3DSOLID (DXF) - AA library objects in encoded binary format 31 | ACAD_PROXY_ENTITY (DXF) 32 | ATTDEF (DXF) - annotation 33 | ATTRIB (DXF) - annotation 34 | BODY (DXF) 35 | DIMENSION (DXF) - annotation 36 | IMAGE (DXF) 37 | INSERT (DXF) - annotation 38 | LEADER (DXF) - annotation 39 | LIGHT (DXF) 40 | MLEADERSTYLE (DXF) 41 | MLEADER (DXF) - annotation 42 | MTEXT (DXF) - annotation 43 | OLEFRAME (DXF) 44 | OLE2FRAME (DXF) 45 | POINT (DXF) 46 | RAY (DXF) - a ray with starting point and direction, extending to infinity on one end. 47 | REGION (DXF) 48 | SHAPE (DXF) 49 | SUN (DXF) 50 | SURFACE (DXF) 51 | TABLE (DXF) 52 | TEXT (DXF) - annotation 53 | TOLERANCE (DXF) - annotation 54 | VIEWPORT (DXF) 55 | UNDERLAY (DXF) - underlay (image) of document 56 | WIPEOUT (DXF) 57 | XLINE (DXF) - a line with starting point and direction, extending to infinity on both ends. 58 | 59 | SECTION (DXF) 60 | TRACE (DXF) - Four points defining the corners of the trace 61 | -------------------------------------------------------------------------------- /packages/dxf-deserializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/dxf-deserializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "DXF deserializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test/*.js' --verbose --timeout 20000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "z3dev", 16 | "url": "http://www.z3d.jp" 17 | }, 18 | { 19 | "name": "Mark 'kaosat-dev' Moissette", 20 | "url": "http://kaosat.net" 21 | } 22 | ], 23 | "keywords": [ 24 | "openjscad", 25 | "jscad", 26 | "csg", 27 | "deserializer", 28 | "dxf" 29 | ], 30 | "license": "MIT", 31 | "dependencies": { 32 | "@jscad/csg": "^0.7.0" 33 | }, 34 | "devDependencies": { 35 | "ava": "^0.25.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /packages/dxf-deserializer/test/test-DxfReader.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('ava') 4 | const { CSG, CAG } = require('@jscad/csg') 5 | 6 | const dxf = require('../DxfReader') 7 | 8 | const samples = path.resolve('../../node_modules/@jscad/sample-files') 9 | 10 | // 11 | // Test suite for DXF reader 12 | // 13 | test('DXF Reader', t => { 14 | const dxfPath = path.resolve(samples, 'dxf/jscad/circle10.dxf') 15 | t.deepEqual(true, fs.existsSync(dxfPath)) 16 | 17 | let src = fs.readFileSync(dxfPath, 'UTF8') 18 | let reader = dxf.reader(src) 19 | 20 | t.is(typeof reader, 'object') 21 | }) 22 | -------------------------------------------------------------------------------- /packages/dxf-deserializer/test/test-dxf-versions.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('ava') 4 | const { CSG, CAG } = require('@jscad/csg') 5 | 6 | const { nearlyEqual } = require( '../../../test/helpers/nearlyEqual' ) 7 | 8 | const { deserialize } = require( '../index' ) 9 | 10 | const samples = path.resolve('../../node_modules/@jscad/sample-files') 11 | 12 | // 13 | // Test suite for DXF deserialization (import) 14 | // 15 | test('ASCII DXF R13 to Object Conversion', t => { 16 | const dxfPath = path.resolve(samples, 'dxf/ezdxf/small_r13.dxf') 17 | t.deepEqual(true, fs.existsSync(dxfPath)) 18 | 19 | let dxf = fs.readFileSync(dxfPath, 'UTF8') 20 | let objs = deserialize(dxf,'aaa',{output: 'csg'}) 21 | 22 | t.true(Array.isArray(objs)) 23 | t.is(objs.length, 16) 24 | 25 | t.true(objs[0] instanceof CSG.Path2D) 26 | t.true(objs[15] instanceof CSG.Path2D) 27 | }) 28 | 29 | test('ASCII DXF R14 to Object Conversion', t => { 30 | const dxfPath = path.resolve(samples, 'dxf/ezdxf/small_r14.dxf') 31 | t.deepEqual(true, fs.existsSync(dxfPath)) 32 | 33 | let dxf = fs.readFileSync(dxfPath, 'UTF8') 34 | let objs = deserialize(dxf,'aaa',{output: 'csg'}) 35 | 36 | t.true(Array.isArray(objs)) 37 | t.is(objs.length, 0) 38 | }) 39 | 40 | test('ASCII DXF ANSI to Object Conversion', t => { 41 | const dxfPath = path.resolve(samples, 'dxf/ezdxf/ansi_pattern.dxf') 42 | t.deepEqual(true, fs.existsSync(dxfPath)) 43 | 44 | let dxf = fs.readFileSync(dxfPath, 'UTF8') 45 | let objs = deserialize(dxf,'aaa',{output: 'csg'}) 46 | 47 | t.true(Array.isArray(objs)) 48 | t.is(objs.length, 1) 49 | 50 | t.true(objs[0] instanceof CAG) 51 | }) 52 | 53 | test('ASCII DXF ISO to Object Conversion', t => { 54 | const dxfPath = path.resolve(samples, 'dxf/ezdxf/iso_pattern.dxf') 55 | t.deepEqual(true, fs.existsSync(dxfPath)) 56 | 57 | let dxf = fs.readFileSync(dxfPath, 'UTF8') 58 | let objs = deserialize(dxf,'aaa',{output: 'csg'}) 59 | 60 | t.true(Array.isArray(objs)) 61 | t.is(objs.length, 14) 62 | 63 | t.true(objs[0] instanceof CAG) 64 | t.true(objs[13] instanceof CAG) 65 | }) 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /packages/dxf-deserializer/test/test-dxf.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('ava') 4 | const { CSG, CAG } = require('@jscad/csg') 5 | 6 | const { nearlyEqual } = require( '../../../test/helpers/nearlyEqual' ) 7 | 8 | const { deserialize } = require( '../index' ) 9 | 10 | const samples = path.resolve('../../node_modules/@jscad/sample-files') 11 | 12 | // 13 | // Test suite for DXF deserialization (import) 14 | // 15 | test('ASCII DXF from Bourke 3D Entities to Object Conversion', t => { 16 | //const dxfPath = path.resolve(__dirname, '../../../../sample-files/dxf/bourke/3d-entities.dxf') 17 | const dxfPath = path.resolve(samples, 'dxf/bourke/3d-entities.dxf') 18 | t.deepEqual(true, fs.existsSync(dxfPath)) 19 | 20 | let dxf = fs.readFileSync(dxfPath, 'UTF8') 21 | let objs = deserialize(dxf,'aaa',{output: 'objects'}) 22 | 23 | // expect one layer, containing 2 objects (CSG, and Line3D) 24 | t.true(Array.isArray(objs)) 25 | t.is(objs.length,2) 26 | }) 27 | 28 | -------------------------------------------------------------------------------- /packages/dxf-serializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/dxf-serializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/dxf-serializer 2 | 3 | > DXF serializer for the JSCAD project (from CSG) 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fdxf-serializer.svg)](https://badge.fury.io/js/%40jscad%2Fdxf-serializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/dxf-serializer) 7 | 8 | ## Overview 9 | 10 | This serializer outputs a 'blobable' array of data (from one or more CSG library objects). 11 | The array of data can either be used to create a Blob (`new Blob(blobable)`), or converted to a Node.js buffer. 12 | 13 | The serialization of the following objects are possible. 14 | - CAG serialization to DXF LWPOLYLINE or POLYLINE 15 | - CSG serialization to DXF 3DFACE(s) 16 | - CSG.Path2D serialization to DXF LWPOLYLINE or POLYINE 17 | 18 | ## Table of Contents 19 | 20 | - [Installation](#installation) 21 | - [Usage](#usage) 22 | - [Contribute](#contribute) 23 | - [License](#license) 24 | 25 | 26 | ## Installation 27 | 28 | ``` 29 | npm install @jscad/dxf-serializer 30 | ``` 31 | 32 | ## Usage 33 | 34 | 35 | ```javascript 36 | const dxfSerializer = require('@jscad/dxf-serializer') 37 | 38 | const rawData = dxfSerializer.serialize(CSGObject) 39 | 40 | //in browser (with browserify etc) 41 | const blob = new Blob(rawData) 42 | 43 | ``` 44 | 45 | 46 | ## Contribute 47 | 48 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 49 | 50 | PRs accepted. 51 | 52 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 53 | 54 | 55 | ## License 56 | 57 | [The MIT License (MIT)](./LICENSE) 58 | (unless specified otherwise) 59 | -------------------------------------------------------------------------------- /packages/dxf-serializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/dxf-serializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Dxf serializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './tests/' --verbose --timeout 60000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "serializer", 32 | "dxf" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "^0.7.0", 37 | "@jscad/io-utils": "^1.0.0-alpha.d0fb3056" 38 | }, 39 | "devDependencies": { 40 | "ava": "^0.25.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /packages/gcode-deserializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/gcode-deserializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | ## [0.2.1](https://github.com/jscad/io/compare/@jscad/gcode-deserializer@0.2.0...@jscad/gcode-deserializer@0.2.1) (2018-11-25) 8 | 9 | 10 | 11 | 12 | **Note:** Version bump only for package @jscad/gcode-deserializer 13 | 14 | 15 | # [0.2.0](https://github.com/jscad/io/compare/@jscad/gcode-deserializer@0.1.0...@jscad/gcode-deserializer@0.2.0) (2017-11-29) 16 | 17 | 18 | ### Features 19 | 20 | * add support for a status callback for de/serialization progress ([#49](https://github.com/jscad/io/issues/49)) ([f457cdb](https://github.com/jscad/io/commit/f457cdb)) 21 | 22 | 23 | 24 | 25 | 26 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/gcode-deserializer@0.0.4...@jscad/gcode-deserializer@0.1.0) (2017-11-14) 27 | 28 | 29 | ### Features 30 | 31 | * **deserializers:** added csg output capabilities to amf & obj deserializers & basic tests ([#47](https://github.com/jscad/io/issues/47)) ([abf3040](https://github.com/jscad/io/commit/abf3040)) 32 | 33 | 34 | 35 | 36 | 37 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/gcode-deserializer@0.0.3...@jscad/gcode-deserializer@0.0.4) (2017-11-04) 38 | 39 | 40 | 41 | 42 | **Note:** Version bump only for package @jscad/gcode-deserializer 43 | 44 | 45 | ## [0.0.3](https://github.com/jscad/io/compare/@jscad/gcode-deserializer@0.0.2...@jscad/gcode-deserializer@0.0.3) (2017-10-10) 46 | 47 | 48 | 49 | 50 | **Note:** Version bump only for package @jscad/gcode-deserializer 51 | 52 | 53 | ## 0.0.2 (2017-10-10) 54 | 55 | 56 | 57 | 58 | **Note:** Version bump only for package @jscad/gcode-deserializer 59 | -------------------------------------------------------------------------------- /packages/gcode-deserializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/gcode-deserializer 2 | 3 | > gcode deserializer for the jscad project 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fgcode-deserializer.svg)](https://badge.fury.io/js/%40jscad%2Fgcode-deserializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/gcode-deserializer) 7 | 8 | ## Overview 9 | 10 | This deserializer converts raw gcode data to jscad code (that can be evaluated to CSG/CAG). 11 | 12 | ## Table of Contents 13 | 14 | - [Installation](#installation) 15 | - [Usage](#usage) 16 | - [Contribute](#contribute) 17 | - [License](#license) 18 | 19 | 20 | ## Installation 21 | 22 | ``` 23 | npm install @jscad/gcode-deserializer 24 | ``` 25 | 26 | ## Usage 27 | 28 | 29 | ```javascript 30 | const gcodeDeSerializer = require('@jscad/gcode-deserializer') 31 | 32 | const rawData = fs.readFileSync('PATH/TO/file.gcode') 33 | const csgData = gcodeDeSerializer(rawData) 34 | 35 | ``` 36 | 37 | 38 | ## Contribute 39 | 40 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 41 | 42 | PRs accepted. 43 | 44 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 45 | 46 | 47 | ## License 48 | 49 | [The MIT License (MIT)](./LICENSE) 50 | (unless specified otherwise) 51 | -------------------------------------------------------------------------------- /packages/gcode-deserializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/gcode-deserializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Gcode deserializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 10000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "deserializer", 32 | "gcode" 33 | ], 34 | "license": "MIT", 35 | "devDependencies": { 36 | "ava": "^0.25.0" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /packages/gcode-deserializer/test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('ava') 4 | const deserializer = require('./index.js') 5 | 6 | const filesPath = path.resolve('../../node_modules/@jscad/sample-files') // require.resolve('@jscad/sample-files') 7 | const polygonsFromCsg = csg => csg.polygons.map(x => x.vertices.map(vert => ([vert.pos.x, vert.pos.y, vert.pos.z]))) 8 | 9 | test.failing('translate simple gcode file to jscad code (NOT IMPLEMENTED!!!)', function (t) { 10 | const inputPath = path.resolve(filesPath, 'gcode/cube_10mm.gcode') 11 | const inputFile = fs.readFileSync(inputPath, 'utf8') 12 | const expected = `` 13 | 14 | const observed = deserializer.deserialize(inputFile, undefined, {output: 'jscad', addMetaData: false}) 15 | t.deepEqual(observed, expected) 16 | }) 17 | 18 | test.failing('deserialize simple gcode to cag/csg objects (NOT IMPLEMENTED!!!)', function (t) { 19 | const inputPath = path.resolve(filesPath, 'gcode/cube_10mm.gcode') 20 | const inputFile = fs.readFileSync(inputPath, 'utf8') 21 | 22 | const observed = deserializer.deserialize(inputFile, undefined, {output: 'csg', addMetaData: false}) 23 | t.deepEqual(observed.polygons.length, 6) 24 | 25 | const observedVertices = polygonsFromCsg(observed) 26 | const expectedVertices = [ [ [ 1, 1, 0 ], [ 1, 0, 0 ], [ 0, 0, 0 ], [ 0, 1, 0 ] ], 27 | [ [ 0, 1, 1 ], [ 0, 1, 0 ], [ 0, 0, 0 ], [ 0, 0, 1 ] ], 28 | [ [ 1, 1, 1 ], [ 1, 1, 0 ], [ 0, 1, 0 ], [ 0, 1, 1 ] ], 29 | [ [ 1, 1, 0 ], [ 1, 1, 1 ], [ 1, 0, 1 ], [ 1, 0, 0 ] ], 30 | [ [ 1, 0, 0 ], [ 1, 0, 1 ], [ 0, 0, 1 ], [ 0, 0, 0 ] ], 31 | [ [ 1, 0, 1 ], [ 1, 1, 1 ], [ 0, 1, 1 ], [ 0, 0, 1 ] ] ] 32 | t.deepEqual(observedVertices, expectedVertices) 33 | }) 34 | -------------------------------------------------------------------------------- /packages/io-utils/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/io-utils/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | ## [0.1.3](https://github.com/jscad/io/compare/@jscad/io-utils@0.1.2...@jscad/io-utils@0.1.3) (2018-09-02) 8 | 9 | 10 | ### Bug Fixes 11 | 12 | * **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)) 13 | 14 | 15 | 16 | 17 | 18 | ## [0.1.2](https://github.com/jscad/io/compare/@jscad/io-utils@0.1.1...@jscad/io-utils@0.1.2) (2017-11-04) 19 | 20 | 21 | 22 | 23 | **Note:** Version bump only for package @jscad/io-utils 24 | 25 | 26 | ## [0.1.1](https://github.com/jscad/io/compare/@jscad/io-utils@0.1.0...@jscad/io-utils@0.1.1) (2017-10-30) 27 | 28 | 29 | 30 | 31 | **Note:** Version bump only for package @jscad/io-utils 32 | 33 | 34 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/io-utils@0.0.4...@jscad/io-utils@0.1.0) (2017-10-15) 35 | 36 | 37 | ### Features 38 | 39 | * **stl-deserializer:** add ability to deserialize stl to csg ([#32](https://github.com/jscad/io/issues/32)) ([a90dcf4](https://github.com/jscad/io/commit/a90dcf4)) 40 | 41 | 42 | 43 | 44 | 45 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/io-utils@0.0.3...@jscad/io-utils@0.0.4) (2017-10-10) 46 | 47 | 48 | 49 | 50 | **Note:** Version bump only for package @jscad/io-utils 51 | 52 | 53 | ## 0.0.3 (2017-10-10) 54 | 55 | 56 | 57 | 58 | **Note:** Version bump only for package @jscad/io-utils 59 | -------------------------------------------------------------------------------- /packages/io-utils/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/io-utils 2 | 3 | > input/output handling utilities 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fio-utils.svg)](https://badge.fury.io/js/%40jscad%2Fio-utils) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/io-utils) 7 | 8 | ## Overview 9 | 10 | This contains following utilities: 11 | 12 | - makeBlob : converts arrays of raw data output by the various serializers into 13 | a Blob that can also be converted to Node.js buffer 14 | 15 | ## Table of Contents 16 | 17 | - [Installation](#installation) 18 | - [Usage](#usage) 19 | - [Contribute](#contribute) 20 | - [License](#license) 21 | 22 | 23 | ## Installation 24 | 25 | ``` 26 | npm install @jscad/io-utils 27 | ``` 28 | 29 | ## Usage 30 | 31 | 32 | ```javascript 33 | const {makeBlob} = require('@jscad/io-utils') 34 | const stlSerializer = require('@jscad/stl-serializer') 35 | 36 | const rawData = stlSerializer(CSGObject) 37 | 38 | const blob = new makeBlob(rawData) 39 | //get a Node.js buffer 40 | const buffer = blob.asBuffer() 41 | 42 | ``` 43 | 44 | 45 | ## Contribute 46 | 47 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 48 | 49 | PRs accepted. 50 | 51 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 52 | 53 | 54 | ## License 55 | 56 | [The MIT License (MIT)](./LICENSE) 57 | (unless specified otherwise) 58 | -------------------------------------------------------------------------------- /packages/io-utils/arrays.js: -------------------------------------------------------------------------------- 1 | // TODO !! same as in @jscad/core/arrays, dedupe !! 2 | /* converts input data to array if it is not already an array */ 3 | const toArray = data => { 4 | if (!data) return [] 5 | if (data.constructor !== Array) return [data] 6 | return data 7 | } 8 | 9 | const head = (array) => { 10 | if (array === undefined || null) { 11 | return undefined 12 | } 13 | if (array.length === 0) { 14 | return undefined 15 | } 16 | return array[0] 17 | } 18 | 19 | const flatten = list => list.reduce( 20 | (a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), [] 21 | ) 22 | 23 | // helper function to retrieve the nth element of an array 24 | const nth = (index, data) => { 25 | if (!data) { 26 | return undefined 27 | } 28 | if (data.length < index) { 29 | return undefined 30 | } 31 | return data[index] 32 | } 33 | 34 | module.exports = {toArray, head, flatten, nth} 35 | -------------------------------------------------------------------------------- /packages/io-utils/ensureManifoldness.js: -------------------------------------------------------------------------------- 1 | /** 2 | * wrapper around internal csg methods (in case they change) to make sure 3 | * it resuts in a manifold mesh 4 | * @constructor 5 | * @param {string} title - The title of the book. 6 | * @return {csg} 7 | */ 8 | function ensureManifoldness (input) { 9 | const transform = input => { 10 | input = 'reTesselated' in input ? input.reTesselated() : input 11 | input = 'fixTJunctions' in input ? input.fixTJunctions() : input // fixTJunctions also calls this.canonicalized() so no need to do it twice 12 | return input 13 | } 14 | 15 | return input.constructor !== Array ? transform(input) : input.map(transform) 16 | } 17 | 18 | module.exports = ensureManifoldness 19 | -------------------------------------------------------------------------------- /packages/io-utils/index.js: -------------------------------------------------------------------------------- 1 | const makeBlob = require('./makeBlob') 2 | const BinaryReader = require('./BinaryReader') 3 | const ensureManifoldness = require('./ensureManifoldness') 4 | module.exports = {makeBlob, BinaryReader, ensureManifoldness} 5 | -------------------------------------------------------------------------------- /packages/io-utils/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/io-utils", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Utilities for input , output and formats handling for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 10000", 9 | "release-patch": "git checkout master && npm version patch ; git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor ; git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major ; git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openscad", 29 | "openjscad", 30 | "jscad", 31 | "cad", 32 | "io", 33 | "formats", 34 | "serializer", 35 | "deserializer" 36 | ], 37 | "license": "MIT", 38 | "devDependencies": { 39 | "@jscad/csg": "0.7.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /packages/io-utils/test.js: -------------------------------------------------------------------------------- 1 | const test = require('ava') 2 | const {CSG} = require('@jscad/csg') 3 | const ensureManifoldness = require('./ensureManifoldness.js') 4 | 5 | // NOTE : this test is BAD WAY, and too dependent on the current implementation , we need actual checks 6 | // such as https://gamedev.stackexchange.com/questions/61878/how-check-if-an-arbitrary-given-mesh-is-a-single-closed-mesh 7 | // https://stackoverflow.com/questions/761026/is-a-closed-polygonal-mesh-flipped 8 | // https://blender.stackexchange.com/questions/20956/is-there-a-way-to-check-a-mesh-for-problems 9 | // https://pypi.python.org/pypi/trimesh/2.2.8 10 | test('ensureManifoldness of csg objects (single input)', function (t) { 11 | const input = new CSG.cube() 12 | t.deepEqual(input.isCanonicalized, false) 13 | t.deepEqual(input.isRetesselated, false) 14 | const observed = ensureManifoldness(input) 15 | t.deepEqual(observed.isCanonicalized, true) 16 | t.deepEqual(observed.isRetesselated, true) 17 | }) 18 | 19 | test('ensureManifoldness of csg objects (array of inputs)', function (t) { 20 | const input = [ 21 | new CSG.cube(), 22 | new CSG.sphere(), 23 | new CSG.cube() 24 | ] 25 | input.map(x => { 26 | t.deepEqual(x.isCanonicalized, false) 27 | t.deepEqual(x.isRetesselated, false) 28 | }) 29 | const observed = ensureManifoldness(input) 30 | observed.map(x => { 31 | t.deepEqual(x.isCanonicalized, true) 32 | t.deepEqual(x.isRetesselated, true) 33 | }) 34 | }) 35 | -------------------------------------------------------------------------------- /packages/io/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/io/README.md: -------------------------------------------------------------------------------- 1 | ## io 2 | 3 | ## input output formats handling for the jscad project 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fio.svg)](https://badge.fury.io/js/%40jscad%2Fio) 6 | 7 | ## Overview 8 | 9 | This package is a metapackage and includes all the input/output format handling for the jscad projects, and can also be used separately. 10 | 11 | ### Inputs / deserializers 12 | 13 | ie: file data => jscad code (that can be evaluated to CSG/CAG) 14 | > note : currently serializers & deserializers are NOT symetrical as deserializers 15 | do not generate CSG/CAG objects 16 | 17 | Following formats are supported as inputs 18 | - [AMF](https://github.com/jscad/io/blob/master/packages/amf-deserializer) 19 | - [DXF](https://github.com/jscad/io/blob/master/packages/dxf-deserializer) 20 | - [gcode](https://github.com/jscad/io/blob/master/packages/gcode-deserializer) 21 | - [JSON](https://github.com/jscad/io/blob/master/packages/json-deserializer) 22 | - [OBJ](https://github.com/jscad/io/blob/master/packages/obj-deserializer) 23 | - [STL (binary, ASCII)](https://github.com/jscad/io/blob/master/packages/stl-deserializer) 24 | - [SVG](https://github.com/jscad/io/blob/master/packages/svg-deserializer) 25 | 26 | ### Outputs/ serializers 27 | 28 | ie: CSG/CAG => blob 29 | 30 | Following formats are supported as outputs 31 | - [AMF](https://github.com/jscad/io/blob/master/packages/amf-serializer) 32 | - [DXF](https://github.com/jscad/io/blob/master/packages/dxf-serializer) 33 | - [JSON](https://github.com/jscad/io/blob/master/packages/json-serializer) 34 | - [STL (binary, ASCII)](https://github.com/jscad/io/blob/master/packages/stl-serializer) 35 | - [SVG](https://github.com/jscad/io/blob/master/packages/svg-serializer) 36 | - [X3D](https://github.com/jscad/io/blob/master/packages/x3d-serializer) 37 | 38 | 39 | ## Table of Contents 40 | 41 | - [Installation](#installation) 42 | - [Usage](#usage) 43 | - [Contribute](#contribute) 44 | - [License](#license) 45 | 46 | 47 | ## Installation 48 | 49 | ``` 50 | npm install @jscad/io 51 | ``` 52 | 53 | ## Usage 54 | 55 | - as Node module : 56 | 57 | ``` 58 | const io = require('@jscad/io') 59 | ``` 60 | 61 | 62 | ## Contribute 63 | 64 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 65 | 66 | PRs accepted. 67 | 68 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 69 | 70 | 71 | ## License 72 | 73 | [The MIT License (MIT)](https://github.com/jscad/io/blob/master/LICENSE) 74 | (unless specified otherwise) 75 | -------------------------------------------------------------------------------- /packages/io/index.js: -------------------------------------------------------------------------------- 1 | const { makeBlob } = require('@jscad/io-utils') 2 | 3 | const amfSerializer = require('@jscad/amf-serializer') 4 | const dxfSerializer = require('@jscad/dxf-serializer') 5 | const jsonSerializer = require('@jscad/json-serializer') 6 | const stlSerializer = require('@jscad/stl-serializer') 7 | const svgSerializer = require('@jscad/svg-serializer') 8 | const x3dSerializer = require('@jscad/x3d-serializer') 9 | 10 | const amfDeSerializer = require('@jscad/amf-deserializer') 11 | const dxfDeSerializer = require('@jscad/dxf-deserializer') 12 | const gcodeDeSerializer = require('@jscad/gcode-deserializer') 13 | const jsonDeSerializer = require('@jscad/json-deserializer') 14 | const objDeSerializer = require('@jscad/obj-deserializer') 15 | // const scadDeSerializer = require('@jscad/scad-deserializer') //FIXME: upgrade, fix before re-enabling 16 | const stlDeSerializer = require('@jscad/stl-deserializer') 17 | const svgDeSerializer = require('@jscad/svg-deserializer') 18 | 19 | module.exports = { 20 | makeBlob, 21 | amfSerializer, 22 | dxfSerializer, 23 | jsonSerializer, 24 | stlSerializer, 25 | svgSerializer, 26 | x3dSerializer, 27 | 28 | amfDeSerializer, 29 | dxfDeSerializer, 30 | gcodeDeSerializer, 31 | jsonDeSerializer, 32 | objDeSerializer, 33 | // scadDeSerializer, //FIXME: upgrade, fix before re-enabling 34 | stlDeSerializer, 35 | svgDeSerializer 36 | } 37 | /* export {makeBlob} from './utils/Blob' 38 | 39 | import * as CAGToDxf from './serializers/CAGToDxf' 40 | import * as CAGToJson from './serializers/CAGToJson' 41 | import * as CAGToSvg from './serializers/CAGToSvg' 42 | import * as CSGToAMF from './serializers/CSGToAMF' 43 | import * as CSGToJson from './serializers/CSGToJson' 44 | import * as CSGToStla from './serializers/CSGToStla' 45 | import * as CSGToStlb from './serializers/CSGToStlb' 46 | import * as CSGToX3D from './serializers/CSGToX3D' 47 | 48 | export {CAGToDxf, CAGToJson, CAGToSvg, CSGToAMF, CSGToJson, CSGToStla, CSGToStlb, CSGToX3D} 49 | 50 | export {parseAMF} from './deserializers/parseAMF' 51 | export {parseGCode} from './deserializers/parseGCode' 52 | export {parseJSON} from './deserializers/parseJSON' 53 | export {parseOBJ} from './deserializers/parseOBJ' 54 | export {parseSTL} from './deserializers/parseSTL' 55 | export {parseSVG} from './deserializers/parseSVG' */ 56 | -------------------------------------------------------------------------------- /packages/io/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/io", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Meta package for input , output and formats handling for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "release-patch": "git checkout master && npm version patch ; git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 9 | "release-minor": "git checkout master && npm version minor ; git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-major": "git checkout master && npm version major ; git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 11 | }, 12 | "contributors": [ 13 | { 14 | "name": "Rene K. Mueller", 15 | "url": "http://renekmueller.com" 16 | }, 17 | { 18 | "name": "z3dev", 19 | "url": "http://www.z3d.jp" 20 | }, 21 | { 22 | "name": "Mark 'kaosat-dev' Moissette", 23 | "url": "http://kaosat.net" 24 | } 25 | ], 26 | "keywords": [ 27 | "openscad", 28 | "openjscad", 29 | "jscad", 30 | "parametric", 31 | "modeling", 32 | "cad", 33 | "io", 34 | "formats", 35 | "parser", 36 | "writer", 37 | "serializer", 38 | "deserializer" 39 | ], 40 | "license": "MIT", 41 | "dependencies": { 42 | "@jscad/amf-deserializer": "1.0.0-alpha.d0fb3056", 43 | "@jscad/amf-serializer": "1.0.0-alpha.d0fb3056", 44 | "@jscad/dxf-deserializer": "1.0.0-alpha.d0fb3056", 45 | "@jscad/dxf-serializer": "1.0.0-alpha.d0fb3056", 46 | "@jscad/gcode-deserializer": "1.0.0-alpha.d0fb3056", 47 | "@jscad/io-utils": "1.0.0-alpha.d0fb3056", 48 | "@jscad/json-deserializer": "1.0.0-alpha.d0fb3056", 49 | "@jscad/json-serializer": "1.0.0-alpha.d0fb3056", 50 | "@jscad/obj-deserializer": "1.0.0-alpha.d0fb3056", 51 | "@jscad/stl-deserializer": "1.0.0-alpha.d0fb3056", 52 | "@jscad/stl-serializer": "1.0.0-alpha.d0fb3056", 53 | "@jscad/svg-deserializer": "1.0.0-alpha.d0fb3056", 54 | "@jscad/svg-serializer": "1.0.0-alpha.d0fb3056", 55 | "@jscad/x3d-serializer": "1.0.0-alpha.d0fb3056" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /packages/json-deserializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/json-deserializer/CAGFromJson.js: -------------------------------------------------------------------------------- 1 | // import { CSG, CAG } from '@jscad/csg' 2 | const {CSG, CAG} = require('@jscad/csg') 3 | 4 | // convert the given (anonymous JSON) object into CAG 5 | // Note: Any issues during conversion will result in exceptions 6 | function deserialize (o) { 7 | // verify the object IS convertable 8 | if (o.type === 'cag') { 9 | Object.setPrototypeOf(o, CAG.prototype) 10 | o.sides.map(function (side) { 11 | Object.setPrototypeOf(side, CAG.Side.prototype) 12 | Object.setPrototypeOf(side.vertex0, CAG.Vertex.prototype) 13 | Object.setPrototypeOf(side.vertex1, CAG.Vertex.prototype) 14 | Object.setPrototypeOf(side.vertex0.pos, CSG.Vector2D.prototype) 15 | Object.setPrototypeOf(side.vertex1.pos, CSG.Vector2D.prototype) 16 | } 17 | ) 18 | } 19 | return o 20 | } 21 | 22 | module.exports = { 23 | deserialize 24 | } 25 | -------------------------------------------------------------------------------- /packages/json-deserializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | ## [0.1.2](https://github.com/jscad/io/compare/@jscad/json-deserializer@0.1.1...@jscad/json-deserializer@0.1.2) (2018-09-02) 8 | 9 | 10 | ### Bug Fixes 11 | 12 | * **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)) 13 | 14 | 15 | 16 | 17 | 18 | ## [0.1.1](https://github.com/jscad/io/compare/@jscad/json-deserializer@0.1.0...@jscad/json-deserializer@0.1.1) (2017-12-14) 19 | 20 | 21 | 22 | 23 | **Note:** Version bump only for package @jscad/json-deserializer 24 | 25 | 26 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/json-deserializer@0.0.4...@jscad/json-deserializer@0.1.0) (2017-11-29) 27 | 28 | 29 | ### Features 30 | 31 | * add support for a status callback for de/serialization progress ([#49](https://github.com/jscad/io/issues/49)) ([f457cdb](https://github.com/jscad/io/commit/f457cdb)) 32 | 33 | 34 | 35 | 36 | 37 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/json-deserializer@0.0.3...@jscad/json-deserializer@0.0.4) (2017-11-04) 38 | 39 | 40 | 41 | 42 | **Note:** Version bump only for package @jscad/json-deserializer 43 | 44 | 45 | ## [0.0.3](https://github.com/jscad/io/compare/@jscad/json-deserializer@0.0.2...@jscad/json-deserializer@0.0.3) (2017-10-10) 46 | 47 | 48 | 49 | 50 | **Note:** Version bump only for package @jscad/json-deserializer 51 | 52 | 53 | ## 0.0.2 (2017-10-10) 54 | 55 | 56 | 57 | 58 | **Note:** Version bump only for package @jscad/json-deserializer 59 | -------------------------------------------------------------------------------- /packages/json-deserializer/CSGFromJson.js: -------------------------------------------------------------------------------- 1 | const {CSG} = require('@jscad/csg') 2 | 3 | // convert the given (anonymous JSON) object into CSG 4 | // Note: Any issues during conversion will result in exceptions 5 | function parse (o) { 6 | // verify the object IS convertable 7 | if (o.type === 'csg') { 8 | Object.setPrototypeOf(o, CSG.prototype) 9 | o.polygons.map(function (p) { 10 | Object.setPrototypeOf(p, CSG.Polygon.prototype) 11 | p.vertices.map(function (v) { 12 | Object.setPrototypeOf(v, CSG.Vertex.prototype) 13 | Object.setPrototypeOf(v.pos, CSG.Vector3D.prototype) 14 | }) 15 | Object.setPrototypeOf(p.shared, CSG.Polygon.Shared.prototype) 16 | Object.setPrototypeOf(p.plane, CSG.Plane.prototype) 17 | Object.setPrototypeOf(p.plane.normal, CSG.Vector3D.prototype) 18 | }) 19 | o.properties = new CSG.Properties() 20 | } 21 | return o 22 | } 23 | 24 | module.exports = { 25 | parse 26 | } 27 | -------------------------------------------------------------------------------- /packages/json-deserializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/json-deserializer 2 | 3 | > json deserializer for the jscad project 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fjson-deserializer.svg)](https://badge.fury.io/js/%40jscad%2Fjson-deserializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/json-deserializer) 7 | 8 | ## Overview 9 | 10 | This deserializer converts raw json data to jscad code (that can be evaluated to CSG/CAG). 11 | 12 | ## Table of Contents 13 | 14 | - [Installation](#installation) 15 | - [Usage](#usage) 16 | - [Contribute](#contribute) 17 | - [License](#license) 18 | 19 | 20 | ## Installation 21 | 22 | ``` 23 | npm install @jscad/json-deserializer 24 | ``` 25 | 26 | ## Usage 27 | 28 | 29 | ```javascript 30 | const jsonDeSerializer = require('@jscad/json-deserializer') 31 | 32 | const rawData = fs.readFileSync('PATH/TO/file.json') 33 | const csgData = jsonDeSerializer.deserialize(rawData, undefined, {output: 'csg'}) 34 | 35 | ``` 36 | 37 | 38 | ## Contribute 39 | 40 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 41 | 42 | PRs accepted. 43 | 44 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 45 | 46 | 47 | ## License 48 | 49 | [The MIT License (MIT)](./LICENSE) 50 | (unless specified otherwise) 51 | -------------------------------------------------------------------------------- /packages/json-deserializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/json-deserializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Json deserializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 20000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "deserializer", 32 | "json" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "0.7.0" 37 | }, 38 | "devDependencies": { 39 | "ava": "^0.25.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /packages/json-deserializer/test.js: -------------------------------------------------------------------------------- 1 | const test = require('ava') 2 | const deserializer = require('./index.js') 3 | 4 | test.todo('add some actual tests later') 5 | -------------------------------------------------------------------------------- /packages/json-serializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/json-serializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | # [0.2.0](https://github.com/jscad/io/compare/@jscad/json-serializer@0.1.2...@jscad/json-serializer@0.2.0) (2018-11-25) 8 | 9 | 10 | ## [0.1.2](https://github.com/jscad/io/compare/@jscad/json-serializer@0.1.1...@jscad/json-serializer@0.1.2) (2018-09-02) 11 | 12 | 13 | ### Bug Fixes 14 | 15 | * **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)) 16 | 17 | 18 | 19 | 20 | 21 | ## [0.1.1](https://github.com/jscad/io/compare/@jscad/json-serializer@0.1.0...@jscad/json-serializer@0.1.1) (2017-12-14) 22 | 23 | 24 | 25 | 26 | **Note:** Version bump only for package @jscad/json-serializer 27 | 28 | 29 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/json-serializer@0.0.5...@jscad/json-serializer@0.1.0) (2017-11-29) 30 | 31 | 32 | ### Features 33 | 34 | * add support for a status callback for de/serialization progress ([#49](https://github.com/jscad/io/issues/49)) ([f457cdb](https://github.com/jscad/io/commit/f457cdb)) 35 | 36 | 37 | 38 | 39 | 40 | ## [0.0.5](https://github.com/jscad/io/compare/@jscad/json-serializer@0.0.4...@jscad/json-serializer@0.0.5) (2017-11-04) 41 | 42 | 43 | 44 | 45 | **Note:** Version bump only for package @jscad/json-serializer 46 | 47 | 48 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/json-serializer@0.0.3...@jscad/json-serializer@0.0.4) (2017-10-30) 49 | 50 | 51 | 52 | 53 | **Note:** Version bump only for package @jscad/json-serializer 54 | 55 | 56 | ## [0.0.3](https://github.com/jscad/io/compare/@jscad/json-serializer@0.0.2...@jscad/json-serializer@0.0.3) (2017-10-10) 57 | 58 | 59 | 60 | 61 | **Note:** Version bump only for package @jscad/json-serializer 62 | 63 | 64 | ## 0.0.2 (2017-10-10) 65 | 66 | 67 | 68 | 69 | **Note:** Version bump only for package @jscad/json-serializer 70 | -------------------------------------------------------------------------------- /packages/json-serializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/json-serializer 2 | 3 | > json serializer for the jscad project (from CSG) 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fjson-serializer.svg)](https://badge.fury.io/js/%40jscad%2Fjson-serializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/json-serializer) 7 | 8 | ## Overview 9 | 10 | This serializer outputs a 'blobable' array of data (from a CSG object) 11 | ie an array that can either be passed directly to a Blob (`new Blob(blobable)`) 12 | or converted to a Node.js buffer. 13 | 14 | ## Table of Contents 15 | 16 | - [Installation](#installation) 17 | - [Usage](#usage) 18 | - [Contribute](#contribute) 19 | - [License](#license) 20 | 21 | 22 | ## Installation 23 | 24 | ``` 25 | npm install @jscad/json-serializer 26 | ``` 27 | 28 | ## Usage 29 | 30 | 31 | ```javascript 32 | const jsonSerializer = require('@jscad/json-serializer') 33 | 34 | const rawData = jsonSerializer.serialize(CSGObject) 35 | 36 | //in browser (with browserify etc) 37 | const blob = new Blob(rawData) 38 | 39 | ``` 40 | 41 | 42 | ## Contribute 43 | 44 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 45 | 46 | PRs accepted. 47 | 48 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 49 | 50 | 51 | ## License 52 | 53 | [The MIT License (MIT)](./LICENSE) 54 | (unless specified otherwise) 55 | -------------------------------------------------------------------------------- /packages/json-serializer/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | JSCAD Object to JSON Format Serialization 3 | 4 | ## License 5 | 6 | Copyright (c) 2018 JSCAD Organization https://github.com/jscad 7 | 8 | All code released under MIT license 9 | 10 | Notes: 11 | 1) CAG conversion to: 12 | none 13 | 2) CSG conversion to: 14 | JSON 15 | 3) Path2D conversion to: 16 | none 17 | */ 18 | 19 | const { ensureManifoldness } = require('@jscad/io-utils') 20 | const {toArray} = require('@jscad/io-utils/arrays') 21 | const { isCSG, isCAG } = require('@jscad/csg') 22 | 23 | const mimeType = 'application/json' 24 | 25 | function fromCAG (CAG, options) { 26 | let str = '{ "type": "cag","sides": [' 27 | let comma = '' 28 | CAG.sides.map( 29 | function (side, i) { 30 | str += comma 31 | str += JSON.stringify(side) 32 | comma = ',' 33 | } 34 | ) 35 | str += '] }' 36 | return str 37 | } 38 | 39 | function fromCSG (CSG, options) { 40 | let str = '{ "type": "csg","polygons": [' 41 | let comma = '' 42 | CSG.polygons.map( 43 | function (polygon, i) { 44 | str += comma 45 | str += JSON.stringify(polygon) 46 | comma = ',' 47 | } 48 | ) 49 | str += '],' 50 | str += '"isCanonicalized": ' + JSON.stringify(CSG.isCanonicalized) + ',' 51 | str += '"isRetesselated": ' + JSON.stringify(CSG.isRetesselated) 52 | str += '}' 53 | return str 54 | } 55 | 56 | function serialize (...params) { 57 | let options = {} 58 | let objects 59 | if (params.length === 0) { 60 | throw new Error('no arguments supplied to serialize function !') 61 | } else if (params.length === 1) { 62 | // assumed to be object(s) 63 | objects = Array.isArray(params[0]) ? params[0] : params 64 | } else if (params.length > 1) { 65 | options = params[0] 66 | objects = params[1] 67 | } 68 | // make sure we always deal with arrays of objects as inputs 69 | objects = toArray(objects) 70 | 71 | const defaults = { 72 | statusCallback: null 73 | } 74 | options = Object.assign({}, defaults, options) 75 | 76 | options.statusCallback && options.statusCallback({progress: 0}) 77 | 78 | let contents = [] 79 | objects.forEach(function (object, i) { 80 | if (isCSG(object) && object.polygons.length > 0) { 81 | let data = ensureManifoldness(object) 82 | contents.push(fromCSG(data, options)) 83 | } 84 | if (isCAG(object) && object.sides.length > 0) { 85 | contents.push(fromCAG(object, options)) 86 | } 87 | options.statusCallback && options.statusCallback({progress: 100 * i / objects.length}) 88 | }) 89 | 90 | options.statusCallback && options.statusCallback({progress: 100}) 91 | return contents 92 | } 93 | 94 | module.exports = { 95 | serialize, 96 | mimeType 97 | } 98 | -------------------------------------------------------------------------------- /packages/json-serializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/json-serializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "json serializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 10000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "serializer", 32 | "json" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "0.7.0", 37 | "@jscad/io-utils": "^1.0.0-alpha.d0fb3056" 38 | }, 39 | "devDependencies": { 40 | "ava": "^0.25.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /packages/obj-deserializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/obj-deserializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | ## [0.2.3](https://github.com/jscad/io/compare/@jscad/obj-deserializer@0.2.2...@jscad/obj-deserializer@0.2.3) (2018-11-25) 8 | 9 | 10 | 11 | 12 | **Note:** Version bump only for package @jscad/obj-deserializer 13 | 14 | 15 | ## [0.2.2](https://github.com/jscad/io/compare/@jscad/obj-deserializer@0.2.1...@jscad/obj-deserializer@0.2.2) (2018-11-22) 16 | 17 | 18 | 19 | 20 | **Note:** Version bump only for package @jscad/obj-deserializer 21 | 22 | 23 | ## [0.2.1](https://github.com/jscad/io/compare/@jscad/obj-deserializer@0.2.0...@jscad/obj-deserializer@0.2.1) (2017-12-14) 24 | 25 | 26 | 27 | 28 | **Note:** Version bump only for package @jscad/obj-deserializer 29 | 30 | 31 | # [0.2.0](https://github.com/jscad/io/compare/@jscad/obj-deserializer@0.1.0...@jscad/obj-deserializer@0.2.0) (2017-11-29) 32 | 33 | 34 | ### Features 35 | 36 | * add support for a status callback for de/serialization progress ([#49](https://github.com/jscad/io/issues/49)) ([f457cdb](https://github.com/jscad/io/commit/f457cdb)) 37 | 38 | 39 | 40 | 41 | 42 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/obj-deserializer@0.0.4...@jscad/obj-deserializer@0.1.0) (2017-11-14) 43 | 44 | 45 | ### Features 46 | 47 | * **deserializers:** added csg output capabilities to amf & obj deserializers & basic tests ([#47](https://github.com/jscad/io/issues/47)) ([abf3040](https://github.com/jscad/io/commit/abf3040)) 48 | 49 | 50 | 51 | 52 | 53 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/obj-deserializer@0.0.3...@jscad/obj-deserializer@0.0.4) (2017-11-04) 54 | 55 | 56 | 57 | 58 | **Note:** Version bump only for package @jscad/obj-deserializer 59 | 60 | 61 | ## [0.0.3](https://github.com/jscad/io/compare/@jscad/obj-deserializer@0.0.2...@jscad/obj-deserializer@0.0.3) (2017-10-10) 62 | 63 | 64 | 65 | 66 | **Note:** Version bump only for package @jscad/obj-deserializer 67 | 68 | 69 | ## 0.0.2 (2017-10-10) 70 | 71 | 72 | 73 | 74 | **Note:** Version bump only for package @jscad/obj-deserializer 75 | -------------------------------------------------------------------------------- /packages/obj-deserializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/obj-deserializer 2 | 3 | > obj deserializer for the jscad project 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fobj-deserializer.svg)](https://badge.fury.io/js/%40jscad%2Fobj-deserializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/obj-deserializer) 7 | 8 | ## Overview 9 | 10 | This deserializer converts raw obj data to jscad code (that can be evaluated to CSG/CAG). 11 | 12 | ## Table of Contents 13 | 14 | - [Installation](#installation) 15 | - [Usage](#usage) 16 | - [Contribute](#contribute) 17 | - [License](#license) 18 | 19 | 20 | ## Installation 21 | 22 | ``` 23 | npm install @jscad/obj-deserializer 24 | ``` 25 | 26 | ## Usage 27 | 28 | 29 | ```javascript 30 | const objDeSerializer = require('@jscad/obj-deserializer') 31 | 32 | const rawData = fs.readFileSync('PATH/TO/file.obj') 33 | const csgData = objDeSerializer.deserialize(rawData, undefined, {output: 'csg'}) 34 | 35 | ``` 36 | 37 | 38 | ## Contribute 39 | 40 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 41 | 42 | PRs accepted. 43 | 44 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 45 | 46 | 47 | ## License 48 | 49 | [The MIT License (MIT)](./LICENSE) 50 | (unless specified otherwise) 51 | -------------------------------------------------------------------------------- /packages/obj-deserializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/obj-deserializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Obj deserializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 10000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "deserializer", 32 | "obj" 33 | ], 34 | "license": "MIT", 35 | "devDependencies": { 36 | "ava": "^0.25.0" 37 | }, 38 | "dependencies": { 39 | "@jscad/csg": "^0.7.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /packages/obj-deserializer/test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('ava') 4 | const deserializer = require('./index.js') 5 | 6 | const filesPath = path.resolve('../../node_modules/@jscad/sample-files') // require.resolve('@jscad/sample-files') 7 | const polygonsFromCsg = csg => csg.polygons.map(x => x.vertices.map(vert => ([vert.pos.x, vert.pos.y, vert.pos.z]))) 8 | 9 | test('translate simple obj file to jscad code', function (t) { 10 | const inputPath = path.resolve(filesPath, 'obj/cube.obj') 11 | const inputFile = fs.readFileSync(inputPath, 'utf8') 12 | 13 | const expected = `// objects: 1 14 | // object #1: polygons: 6 15 | function main() { return 16 | polyhedron({ points: [ 17 | [-0.5,-0.5,0.5], 18 | [-0.5,-0.5,-0.5], 19 | [-0.5,0.5,-0.5], 20 | [-0.5,0.5,0.5], 21 | [0.5,-0.5,0.5], 22 | [0.5,-0.5,-0.5], 23 | [0.5,0.5,-0.5], 24 | [0.5,0.5,0.5]], 25 | polygons: [ 26 | [3,2,1,0], 27 | [1,5,4,0], 28 | [2,6,5,1], 29 | [7,6,2,3], 30 | [4,7,3,0], 31 | [5,6,7,4]] }) 32 | 33 | } 34 | ` 35 | 36 | const observed = deserializer.deserialize(inputFile, undefined, {output: 'jscad', addMetaData: false}) 37 | t.deepEqual(observed, expected) 38 | }) 39 | 40 | test('deserialize simple obj to cag/csg objects', function (t) { 41 | const inputPath = path.resolve(filesPath, 'obj/cube.obj') 42 | const inputFile = fs.readFileSync(inputPath, 'utf8') 43 | 44 | const observed = deserializer.deserialize(inputFile, undefined, {output: 'csg', addMetaData: false}) 45 | t.deepEqual(observed.polygons.length, 6) 46 | 47 | const observedVertices = polygonsFromCsg(observed) 48 | const expectedVertices = [ [ [ -0.5, -0.5, 0.5 ], 49 | [ -0.5, -0.5, -0.5 ], 50 | [ -0.5, 0.5, -0.5 ], 51 | [ -0.5, 0.5, 0.5 ] ], 52 | [ [ -0.5, -0.5, 0.5 ], 53 | [ 0.5, -0.5, 0.5 ], 54 | [ 0.5, -0.5, -0.5 ], 55 | [ -0.5, -0.5, -0.5 ] ], 56 | [ [ -0.5, -0.5, -0.5 ], 57 | [ 0.5, -0.5, -0.5 ], 58 | [ 0.5, 0.5, -0.5 ], 59 | [ -0.5, 0.5, -0.5 ] ], 60 | [ [ -0.5, 0.5, 0.5 ], 61 | [ -0.5, 0.5, -0.5 ], 62 | [ 0.5, 0.5, -0.5 ], 63 | [ 0.5, 0.5, 0.5 ] ], 64 | [ [ -0.5, -0.5, 0.5 ], 65 | [ -0.5, 0.5, 0.5 ], 66 | [ 0.5, 0.5, 0.5 ], 67 | [ 0.5, -0.5, 0.5 ] ], 68 | [ [ 0.5, -0.5, 0.5 ], 69 | [ 0.5, 0.5, 0.5 ], 70 | [ 0.5, 0.5, -0.5 ], 71 | [ 0.5, -0.5, -0.5 ] ] ] 72 | t.deepEqual(observedVertices, expectedVertices) 73 | }) 74 | -------------------------------------------------------------------------------- /packages/obj-deserializer/vt2jscad.js: -------------------------------------------------------------------------------- 1 | // positions, triangles, normals and colors 2 | function vt2jscad (positions, triangles, normals, colors) { 3 | let src = '' 4 | src += 'polyhedron({ points: [\n ' 5 | for (let i = 0, j = 0; i < positions.length; i++) { 6 | if (j++) src += ',\n ' 7 | src += '[' + positions[i] + ']' // .join(", "); 8 | } 9 | src += '],\n polygons: [\n ' 10 | for (let i = 0, j = 0; i < triangles.length; i++) { 11 | if (j++) src += ',\n ' 12 | src += '[' + triangles[i] + ']' // .join(', '); 13 | } 14 | if (colors && triangles.length === colors.length) { 15 | src += '],\n\tcolors: [\n ' 16 | for (let i = 0, j = 0; i < colors.length; i++) { 17 | if (j++) src += ',\n ' 18 | src += '[' + colors[i] + ']' // .join(', '); 19 | } 20 | } 21 | src += '] })\n' 22 | return src 23 | } 24 | 25 | module.exports = { 26 | vt2jscad 27 | } 28 | -------------------------------------------------------------------------------- /packages/scad-deserializer/LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2017 @jscad 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /packages/scad-deserializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/scad-deserializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Translates OpenSCAD syntax (http://www.openscad.org/) into OpenJsCAD syntax (http://joostn.github.com/OpenJsCad/).", 5 | "main": "src/parserWrapper.js", 6 | "browser": { 7 | "lapack": false 8 | }, 9 | "scripts": { 10 | "test": "node tests/all-tests", 11 | "build-old": "browserify src/parserWrapper.js --ignore lapack --standalone openscadOpenJscadParser -o dist/web-built.js", 12 | "build-min-old": "browserify src/parserWrapper.js --ignore lapack --standalone openscadOpenJscadParser | uglifyjs -c > dist/web-built.min.js", 13 | "build-all-old": "npm run build && npm run build-min", 14 | "build-parser": "node src/parserMaker", 15 | "release-patch": "git checkout master && npm run build-all; npm version patch ; git commit -a -m 'chore(dist): built release'; git push origin master --tags ", 16 | "release-minor": "git checkout master && npm run build-all; npm version minor ; git commit -a -m 'chore(dist): built release'; git push origin master --tags ", 17 | "release-major": "git checkout master && npm run build-all; npm version major ; git commit -a -m 'chore(dist): built release'; git push origin master --tags " 18 | }, 19 | "directories": { 20 | "test": "tests" 21 | }, 22 | "dependencies": { 23 | "lodash": "^4.17.4", 24 | "sylvester": "github:kaosat-dev/node-sylvester" 25 | }, 26 | "devDependencies": { 27 | "jison": "^0.4.17", 28 | "test": "~0.6.0" 29 | }, 30 | "repository": "https://github.com/jscad/io", 31 | "bugs": { 32 | "url": "https://github.com/jscad/io/issues" 33 | }, 34 | "keywords": [ 35 | "openscad", 36 | "openjscad", 37 | "jscad", 38 | "scad", 39 | "deserializer", 40 | "translator" 41 | ], 42 | "author": { 43 | "name": "Gary Hodgson", 44 | "email": "contact@garyhodgson.com", 45 | "url": "http://garyhodgson.com" 46 | }, 47 | "contributors": [ 48 | { 49 | "name": "z3dev", 50 | "url": "http://www.z3d.jp" 51 | }, 52 | { 53 | "name": "Mark 'kaosat-dev' Moissette", 54 | "url": "http://kaosat.net" 55 | } 56 | ], 57 | "license": "MIT" 58 | } 59 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/ArgContainer.js: -------------------------------------------------------------------------------- 1 | module.exports = function () { 2 | this.argname 3 | this.argexpr 4 | } 5 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/ArgsContainer.js: -------------------------------------------------------------------------------- 1 | module.exports = function () { 2 | this.argnames = [] 3 | this.argexpr = [] 4 | } 5 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/CSGModule.js: -------------------------------------------------------------------------------- 1 | var Context = require('./Context') 2 | var Globals = require('./Globals') 3 | var _ = require('lodash') 4 | 5 | function CSGModule (factory, csgOperation) { 6 | this.csgOperation = csgOperation 7 | this.factory = factory 8 | }; 9 | 10 | CSGModule.prototype.evaluate = function (parentContext, inst) { 11 | var context = new Context(parentContext) 12 | 13 | var childModules = [] 14 | 15 | for (var i = 0; i < inst.children.length; i++) { 16 | var childInst = inst.children[i] 17 | childInst.argvalues = [] 18 | _.each(childInst.argexpr, function (expr, index, list) { 19 | childInst.argvalues.push(expr.evaluate(context)) 20 | }) 21 | 22 | var childAdaptor = this.factory.getAdaptor(childInst) 23 | var evaluatedChild = childAdaptor.evaluate(parentContext, childInst) 24 | if (evaluatedChild !== undefined) { 25 | childModules.push(evaluatedChild) 26 | } 27 | }; 28 | 29 | if (childModules.length <= 1) { 30 | return childModules[0] 31 | } else { 32 | return childModules[0] + '.' + this.csgOperation + '([' + childModules.slice(1).join(',\n') + '])' 33 | } 34 | } 35 | 36 | module.exports = CSGModule 37 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/ChildModule.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash') 2 | var Context = require('./Context') 3 | var Globals = require('./Globals') 4 | 5 | function Child (factory) { 6 | this.factory = factory 7 | }; 8 | 9 | Child.prototype.evaluate = function (parentContext, inst) { 10 | inst.argvalues = [] 11 | _.each(inst.argexpr, function (expr, index, list) { 12 | inst.argvalues.push(expr.evaluate(parentContext)) 13 | }) 14 | 15 | var context = Context.newContext(parentContext, [], [], inst) 16 | 17 | var childIndex = 0 18 | if (inst.argvalues[0] !== undefined) { 19 | childIndex = inst.argvalues[0] 20 | } 21 | 22 | var evaluatedChildren = [] 23 | 24 | for (var i = Globals.context_stack.length - 1; i >= 0; i--) { 25 | var ctx = Globals.context_stack[i] 26 | 27 | if (ctx.inst_p !== undefined) { 28 | if (childIndex < ctx.inst_p.children.length) { 29 | var childInst = ctx.inst_p.children[childIndex] 30 | 31 | _.each(childInst.argexpr, function (expr, index, list) { 32 | childInst.argvalues.push(expr.evaluate(ctx.inst_p.ctx)) 33 | }) 34 | 35 | var childAdaptor = this.factory.getAdaptor(childInst) 36 | evaluatedChildren.push(childAdaptor.evaluate(ctx.inst_p.ctx, childInst)) 37 | } 38 | return evaluatedChildren 39 | } 40 | ctx = ctx.parentContext 41 | }; 42 | 43 | return undefined 44 | } 45 | 46 | module.exports = Child 47 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/FunctionDef.js: -------------------------------------------------------------------------------- 1 | var Context = require('./Context') 2 | 3 | function FunctionDef () { 4 | this.argnames = [] 5 | this.argexpr = [] 6 | this.expr 7 | } 8 | 9 | FunctionDef.prototype.evaluate = function (parentContext, call_argnames, call_argvalues) { 10 | var context = new Context(parentContext) 11 | context.args(this.argnames, this.argexpr, call_argnames, call_argvalues) 12 | 13 | if (this.expr !== undefined) { 14 | return this.expr.evaluate(context) 15 | } 16 | 17 | return undefined 18 | } 19 | 20 | module.exports = FunctionDef 21 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/Globals.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash') 2 | 3 | var singleLineModuleRegex = /(module\s*\w*\([^\)]*\)[\w\n]*)([^{};]*);/gm 4 | var singleLineModuleReplacement = '$1 {$2;};' 5 | var multiLineCommentRegex = /((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/gm 6 | 7 | function stripString (s) { 8 | if (/^\".*\"$/.test(s)) { 9 | return s.match(/^\"(.*)\"$/)[1] 10 | } else { 11 | return s 12 | } 13 | } 14 | 15 | function convertForStrFunction (val) { 16 | if (_.isString(val)) { 17 | return stripString(val) 18 | } 19 | 20 | if (_.isArray(val)) { 21 | var mapped = _.map(val, function (value, key, list) { 22 | return convertForStrFunction(value) 23 | }) 24 | 25 | return '[' + mapped.join(',') + ']' 26 | } 27 | 28 | return val 29 | } 30 | 31 | function preParse (text) { 32 | return text 33 | .replace(multiLineCommentRegex, '') 34 | .replace(singleLineModuleRegex, singleLineModuleReplacement) 35 | } 36 | 37 | module.exports = { 38 | DEFAULT_RESOLUTION: 16, 39 | DEFAULT_2D_RESOLUTION: 16, 40 | FN_DEFAULT: 0, 41 | FS_DEFAULT: 2.0, 42 | FA_DEFAULT: 12.0, 43 | module_stack: [], 44 | context_stack: [], 45 | stripString: stripString, 46 | convertForStrFunction: convertForStrFunction, 47 | preParse: preParse, 48 | importedObjectRegex: /import\([^\"]*\"([^\)]*)\"[,]?.*\);?/gm, 49 | usedLibraryRegex: /use <([^>]*)>;?/gm, 50 | includedLibraryRegex: /include <([^>]*)>;?/gm 51 | } 52 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/IfElseModuleInstantiation.js: -------------------------------------------------------------------------------- 1 | var ModuleInstantiation = require('./ModuleInstantiation') 2 | 3 | function IfElseModuleInstantiation () { 4 | ModuleInstantiation.call(this) 5 | this.name = 'if' 6 | this.else_children = [] 7 | } 8 | 9 | IfElseModuleInstantiation.prototype = new ModuleInstantiation() 10 | IfElseModuleInstantiation.prototype.constructor = IfElseModuleInstantiation 11 | 12 | module.exports = IfElseModuleInstantiation 13 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/ImportModule.js: -------------------------------------------------------------------------------- 1 | var Context = require('./Context') 2 | var Globals = require('./Globals') 3 | var StlDecoder = require('./StlDecoder') 4 | 5 | function Import (factory) { 6 | this.factory = factory 7 | } 8 | 9 | Import.prototype.evaluate = function (parentContext, inst) { 10 | var context = new Context(parentContext) 11 | 12 | var argnames = ['file', 'filename', 'convexity'] 13 | var argexpr = [] 14 | 15 | context.args(argnames, argexpr, inst.argnames, inst.argvalues) 16 | 17 | var filename = Context.contextVariableLookup(context, 'file', null) || Context.contextVariableLookup(context, 'filename', null) 18 | 19 | var convexity = Context.contextVariableLookup(context, 'convexity', 5) 20 | 21 | var importCache = Context.contextVariableLookup(context, 'importCache', {}) 22 | 23 | var fileContents = importCache[filename] 24 | 25 | if (fileContents !== undefined) { 26 | var stlDecoder = new StlDecoder(atob(fileContents)) 27 | stlDecoder.decode() 28 | return stlDecoder.getCSGString() 29 | } 30 | 31 | return undefined 32 | } 33 | 34 | module.Exports = Import 35 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/Module.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash') 2 | var Context = require('./Context') 3 | var Globals = require('./Globals') 4 | 5 | 6 | function Module(name) { 7 | this.name = name; 8 | this.children = []; 9 | this.assignments_var = {}; 10 | this.functions = {}; 11 | this.modules = []; 12 | this.argnames = []; 13 | this.argexpr = []; 14 | }; 15 | 16 | Module.prototype.evaluate = function(parentContext, inst) { 17 | var lines = []; 18 | 19 | var context = new Context(parentContext); 20 | 21 | if (parentContext === undefined){ 22 | context.setVariable("$fn", Globals.DEFAULT_RESOLUTION); 23 | context.setVariable("$fs", 2.0); 24 | context.setVariable("$fa", 12.0); 25 | } 26 | 27 | if (inst !== undefined) { 28 | context.args(this.argnames, this.argexpr, inst.argnames, inst.argvalues); 29 | context.setVariable("$children", inst.children.length); 30 | } 31 | 32 | context.inst_p = inst; 33 | context.functions_p = this.functions; 34 | context.modules_p = this.modules; 35 | _.each(this.assignments_var, function(value, key, list) { 36 | context.setVariable(key, value.evaluate(context)); 37 | }); 38 | 39 | var controlChildren = _.filter(this.children, function(child){ 40 | return child && child.name == "echo"; 41 | }); 42 | 43 | _.each(controlChildren, function(child, index, list) { 44 | child.evaluate(context); 45 | }); 46 | 47 | var nonControlChildren = _.reject(this.children, function(child){ 48 | return !child || child.name == "echo"; 49 | }); 50 | 51 | var evaluatedLines = []; 52 | _.each(nonControlChildren, function(child, index, list) { 53 | var evaluatedChild = child.evaluate(context) 54 | if (evaluatedChild == undefined || (_.isArray(evaluatedChild) && _.isEmpty(evaluatedChild))){ 55 | // ignore 56 | } else { 57 | evaluatedLines.push(evaluatedChild); 58 | } 59 | }); 60 | 61 | var cleanedLines = _.compact(evaluatedLines); 62 | if (cleanedLines.length == 1){ 63 | lines.push(cleanedLines[0]); 64 | } else if (cleanedLines.length > 1){ 65 | lines.push(_.head(cleanedLines)+".union([" +_.tail(cleanedLines)+"])"); 66 | } 67 | 68 | return lines; 69 | }; 70 | 71 | module.exports = Module 72 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/ModuleAdaptor.js: -------------------------------------------------------------------------------- 1 | function ModuleAdaptor () {} 2 | 3 | ModuleAdaptor.prototype.evaluate = function (parentContext, inst) { 4 | inst.isSubmodule = true 5 | return parentContext.evaluateModule(inst) 6 | } 7 | 8 | module.exports = ModuleAdaptor 9 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/ModuleInstantiation.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash') 2 | var OpenjscadSolidFactorySingleton = require('./OpenjscadSolidFactorySingleton') 3 | 4 | function ModuleInstantiation () { 5 | this.name 6 | this.argnames = [] 7 | this.argvalues = [] 8 | this.argexpr = [] 9 | this.children = [] 10 | this.isSubmodule = false 11 | this.context 12 | } 13 | 14 | ModuleInstantiation.prototype.evaluate = function (context) { 15 | var evaluatedModule 16 | 17 | // NOTE: not sure how we should handle this in javascript ... is it necessary? 18 | // if (this.context === null) { 19 | // console.log("WARNING: Ignoring recursive module instantiation of ", this.name) 20 | // } else { 21 | var that = this 22 | 23 | this.argvalues = [] 24 | 25 | _.each(this.argexpr, function (expr, index, list) { 26 | that.argvalues.push(expr.evaluate(context)) 27 | }) 28 | 29 | that.context = context 30 | 31 | evaluatedModule = context.evaluateModule(that, OpenjscadSolidFactorySingleton.getInstance()) 32 | 33 | that.context = null 34 | that.argvalues = [] 35 | 36 | // } 37 | return evaluatedModule 38 | } 39 | 40 | ModuleInstantiation.prototype.evaluateChildren = function (context) { 41 | var childModules = [] 42 | 43 | for (var i = 0; i < this.children.length; i++) { 44 | var childInst = this.children[i] 45 | 46 | var evaluatedChild = childInst.evaluate(context) 47 | if (evaluatedChild !== undefined) { 48 | childModules.push(evaluatedChild) 49 | } 50 | } 51 | 52 | return childModules 53 | } 54 | 55 | module.exports = ModuleInstantiation 56 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/OpenjscadSolidFactory.js: -------------------------------------------------------------------------------- 1 | var Context = require('./Context') 2 | var Globals = require('./Globals') 3 | var PrimitiveModules = require('./PrimitiveModules') 4 | var TransformModules = require('./TransformModules') 5 | var ControlModules = require('./ControlModules') 6 | var CSGModule = require('./CSGModule') 7 | 8 | var ChildModule = require('./ChildModule') 9 | var ModuleAdaptor = require('./ModuleAdaptor') 10 | var ImportModule = require('./ImportModule') 11 | 12 | function OpenjscadSolidFactory () {} 13 | 14 | OpenjscadSolidFactory.prototype.getAdaptor = function (args) { 15 | switch (args.name) { 16 | case 'cube': 17 | return new PrimitiveModules.Cube() 18 | case 'sphere': 19 | return new PrimitiveModules.Sphere() 20 | case 'cylinder': 21 | return new PrimitiveModules.Cylinder() 22 | case 'polyhedron': 23 | return new PrimitiveModules.Polyhedron() 24 | case 'circle': 25 | return new PrimitiveModules.Circle() 26 | case 'square': 27 | return new PrimitiveModules.Square() 28 | case 'polygon': 29 | return new PrimitiveModules.Polygon() 30 | case 'union': 31 | return new CSGModule(this, 'union') 32 | case 'difference': 33 | return new CSGModule(this, 'subtract') 34 | case 'intersect': 35 | case 'intersection': 36 | return new CSGModule(this, 'intersect') 37 | case 'translate': 38 | return new TransformModules.Translate(this) 39 | case 'scale': 40 | return new TransformModules.Scale(this) 41 | case 'rotate': 42 | return new TransformModules.Rotate(this) 43 | case 'mirror': 44 | return new TransformModules.Mirror(this) 45 | case 'linear_extrude': 46 | return new TransformModules.Extrude(this) 47 | case 'color': 48 | return new TransformModules.Color(this) 49 | case 'multmatrix': 50 | return new TransformModules.Multimatrix(this) 51 | case 'render': 52 | case 'assign': // Note: assign does the same as render in this case - re-evaluate the arguments and process the children. 53 | return new TransformModules.Render(this) 54 | case 'echo': 55 | return new ControlModules.Echo(this) 56 | case 'for': 57 | return new ControlModules.ForLoopStatement(this, { csgOp: 'union' }) 58 | case 'intersection_for': 59 | return new ControlModules.ForLoopStatement(this, { csgOp: 'intersect' }) 60 | case 'if': 61 | return new ControlModules.IfStatement(this) 62 | case 'import': 63 | return new ImportModule(this) 64 | case 'child': 65 | return new ChildModule(this) 66 | default: 67 | return new ModuleAdaptor() 68 | } 69 | } 70 | 71 | module.exports = OpenjscadSolidFactory 72 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/OpenjscadSolidFactorySingleton.js: -------------------------------------------------------------------------------- 1 | var OpenjscadSolidFactory = require('./OpenjscadSolidFactory') 2 | var factory = new OpenjscadSolidFactory() 3 | 4 | module.exports = { 5 | getInstance: function () { 6 | return factory 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/OpenscadOpenjscadParser.js: -------------------------------------------------------------------------------- 1 | var requirejs = require('requirejs') 2 | 3 | requirejs.config({ 4 | baseUrl: __dirname, 5 | paths: { 6 | lib: '../lib' 7 | }, 8 | nodeRequire: require 9 | }) 10 | 11 | var parser = requirejs('openscad-parser') 12 | var Globals = requirejs('Globals') 13 | var parser_support = requirejs('openscad-parser-support') 14 | var us = requirejs('lib/underscore') 15 | 16 | module.exports = { 17 | parse: function (text) { 18 | if (parser.yy === undefined) { 19 | parser.yy = {} 20 | } 21 | 22 | var openSCADText = Globals.preParse(text) 23 | 24 | var openJSCADResult = parser.parse(openSCADText) 25 | 26 | return openJSCADResult.lines.join('\n') 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/Range.js: -------------------------------------------------------------------------------- 1 | function Range (begin, step, end) { 2 | this.begin = begin 3 | this.step = step 4 | this.end = end 5 | } 6 | 7 | module.exports = Range 8 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/openscad-parser-ext.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash') 2 | var Module = require('./Module') 3 | var Context = require('./Context') 4 | var Globals = require('./Globals') 5 | var FunctionDef = require('./FunctionDef') 6 | var support = require('./openscad-parser-support') 7 | 8 | var currmodule = new Module('root') 9 | 10 | function resetModule () { 11 | currmodule = new Module('root') 12 | Globals.context_stack = [] 13 | Globals.module_stack = [] 14 | } 15 | 16 | function processModule (yy) { 17 | var lines = [] 18 | lines.push('function main(){') 19 | lines.push('\n') 20 | 21 | var context = undefined 22 | if (yy.context !== undefined) { 23 | context = yy.context 24 | } else { 25 | context = new Context() 26 | } 27 | 28 | if (yy.importCache !== undefined) { 29 | context.setVariable('importCache', yy.importCache) 30 | } 31 | 32 | var res = currmodule.evaluate(context) 33 | 34 | var evaluatedLines = _.flatten(res) 35 | if (evaluatedLines.length == 1) { 36 | lines.push('return ' + evaluatedLines[0] + ';') 37 | } else if (evaluatedLines.length > 1) { 38 | lines.push('return ' + _.first(evaluatedLines) + '.union([') 39 | lines.push(_.tail(evaluatedLines)) 40 | lines.push(']);') 41 | } 42 | lines.push('};') 43 | 44 | var x = { lines: lines, context: Globals.context_stack[Globals.context_stack.length - 1] } 45 | resetModule() 46 | 47 | return x 48 | } 49 | 50 | function stashModule (newName, newArgNames, newArgExpr) { 51 | var p_currmodule = currmodule 52 | Globals.module_stack.push(currmodule) 53 | 54 | currmodule = new Module(newName) 55 | 56 | p_currmodule.modules.push(currmodule) 57 | 58 | currmodule.argnames = newArgNames 59 | currmodule.argexpr = newArgExpr 60 | } 61 | 62 | function popModule () { 63 | if (Globals.module_stack.length > 0) { 64 | currmodule = Globals.module_stack.pop() 65 | } 66 | } 67 | 68 | function addModuleChild (child) { 69 | currmodule.children.push(child) 70 | } 71 | 72 | function addModuleAssignmentVar (name, value) { 73 | currmodule.assignments_var[name] = value 74 | } 75 | 76 | function addModuleFunction (name, expr, argnames, argexpr) { 77 | var func = new FunctionDef() 78 | func.argnames = argnames 79 | func.argexpr = argexpr 80 | func.expr = expr 81 | currmodule.functions[name] = func 82 | } 83 | 84 | module.exports = { 85 | processModule: processModule, 86 | stashModule: stashModule, 87 | popModule: popModule, 88 | addModuleChild: addModuleChild, 89 | addModuleAssignmentVar: addModuleAssignmentVar, 90 | addModuleFunction: addModuleFunction 91 | } 92 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/parserMaker.js: -------------------------------------------------------------------------------- 1 | const Parser = require('jison').Parser 2 | const fs = require('fs') 3 | const path = require('path') 4 | 5 | const grammar = fs.readFileSync('./openscad-parser.jison', 'utf8') 6 | const parser = new Parser(grammar, {type: 'LALR'}) 7 | 8 | // generate source, ready to be written to disk 9 | let parserSource = parser.generate() 10 | 11 | // we add our extra variables to the base parser 12 | parserSource = `const ext = require('./openscad-parser-ext') 13 | const ArgContainer = require('./ArgContainer') 14 | const ArgsContainer = require('./ArgsContainer') 15 | const Expression = require('./Expression') 16 | const ModuleInstantiation = require('./ModuleInstantiation') 17 | const IfElseModuleInstantiation = require('./IfElseModuleInstantiation') 18 | ${parserSource}` 19 | fs.writeFileSync(path.join(__dirname, 'parserCJS.js'), parserSource) 20 | 21 | // ext, ArgContainer, ArgsContainer, Expression, ModuleInstantiation, IfElseModuleInstantiation 22 | // openscad-parser-ext", "ArgContainer", "ArgsContainer", "Expression", "ModuleInstantiation", "IfElseModuleInstantiation 23 | -------------------------------------------------------------------------------- /packages/scad-deserializer/src/parserWrapper.js: -------------------------------------------------------------------------------- 1 | var parser = require('./parserCJS') 2 | var Globals = require('./Globals') 3 | 4 | module.exports = { 5 | parse: function (text) { 6 | if (parser.yy === undefined) { 7 | parser.yy = {} 8 | } 9 | 10 | var openSCADText = Globals.preParse(text) 11 | var openJSCADResult = parser.parse(openSCADText) 12 | return openJSCADResult.lines.join('\n') 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_primitives.js: -------------------------------------------------------------------------------- 1 | const check = require('./helpers').check 2 | const filedir = '2d_primitives/' 3 | 4 | exports['test Square'] = function () { 5 | check(filedir, 'squareEx1') 6 | } 7 | if (module === require.main) require('test').run(exports) 8 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_primitives/squareEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CAG.rectangle({center: [0,0], radius: [1,1]}); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_primitives/squareEx1.scad: -------------------------------------------------------------------------------- 1 | square ([2,2],center =true); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion.js: -------------------------------------------------------------------------------- 1 | const check = require('./helpers').check 2 | const filedir = '2d_to_3d_extrusion/' 3 | 4 | exports['test Linear Extrude'] = function () { 5 | check(filedir, 'linearExtrudeEx1') 6 | check(filedir, 'linearExtrudeEx2') 7 | check(filedir, 'linearExtrudeEx3') 8 | check(filedir, 'linearExtrudeEx4') 9 | check(filedir, 'linearExtrudeEx5') 10 | check(filedir, 'linearExtrudeEx6') 11 | check(filedir, 'linearExtrudeEx7') 12 | } 13 | 14 | if (module === require.main) require('test').run(exports) 15 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CAG.circle({center: [0,0], radius: 1, resolution: 5}).translate([2,0,0]).extrude({offset: [0, 0, 10], twistangle: 0,twiststeps: 2}).translate([0,0,-5]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx1.scad: -------------------------------------------------------------------------------- 1 | linear_extrude(height = 10, center = true, convexity = 10, twist = 0) 2 | translate([2, 0, 0]) 3 | circle(r = 1); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CAG.circle({center: [0,0], radius: 1, resolution: 5}).translate([2,0,0]).extrude({offset: [0, 0, 10], twistangle: 100,twiststeps: 8}).translate([0,0,-5]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx2.scad: -------------------------------------------------------------------------------- 1 | linear_extrude(height = 10, center = true, convexity = 10, twist = -100) 2 | translate([2, 0, 0]) 3 | circle(r = 1); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx3.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CAG.circle({center: [0,0], radius: 1, resolution: 5}).translate([2,0,0]).extrude({offset: [0, 0, 10], twistangle: -100,twiststeps: 8}).translate([0,0,-5]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx3.scad: -------------------------------------------------------------------------------- 1 | linear_extrude(height = 10, center = true, convexity = 10, twist = 100) 2 | translate([2, 0, 0]) 3 | circle(r = 1); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx4.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CAG.circle({center: [0,0], radius: 1, resolution: 5}).translate([2,0,0]).extrude({offset: [0, 0, 10], twistangle: 500,twiststeps: 41}).translate([0,0,-5]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx4.scad: -------------------------------------------------------------------------------- 1 | linear_extrude(height = 10, center = true, convexity = 10, twist = -500) 2 | translate([2, 0, 0]) 3 | circle(r = 1); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx5.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CAG.circle({center: [0,0], radius: 1, resolution: 5}).translate([2,0,0]).extrude({offset: [0, 0, 10], twistangle: 500,twiststeps: 41}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx5.scad: -------------------------------------------------------------------------------- 1 | linear_extrude(height = 10, center = false, convexity = 10, twist = -500) 2 | translate([2, 0, 0]) 3 | circle(r = 1); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx6.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CAG.circle({center: [0,0], radius: 1, resolution: 5}).translate([2,0,0]).extrude({offset: [0, 0, 10], twistangle: -360,twiststeps: 100}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx6.scad: -------------------------------------------------------------------------------- 1 | linear_extrude(height = 10, center = false, convexity = 10, twist = 360, slices = 100) 2 | translate([2, 0, 0]) 3 | circle(r = 1); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx7.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CAG.circle({center: [0,0], radius: 1, resolution: 100}).translate([2,0,0]).extrude({offset: [0, 0, 10], twistangle: -360,twiststeps: 100}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/2d_to_3d_extrusion/linearExtrudeEx7.scad: -------------------------------------------------------------------------------- 1 | linear_extrude(height = 10, center = false, convexity = 10, twist = 360, $fn = 100) 2 | translate([2, 0, 0]) 3 | circle(r = 1); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/all-tests.js: -------------------------------------------------------------------------------- 1 | exports.test1 = require('./2d_to_3d_extrusion') 2 | exports.test2 = require('./conditional_and_iterator_functions') 3 | exports.test5 = require('./modules') 4 | exports.test6 = require('./primitive_solids') 5 | exports.test7 = require('./submodule_tests') 6 | exports.test8 = require('./transformations') 7 | exports.test9 = require('./2d_primitives') 8 | 9 | if (require.main === module) { 10 | require('test').run(exports) 11 | } 12 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert') 2 | const check = require('./helpers').check 3 | const filedir = 'conditional_and_iterator_functions/' 4 | 5 | exports['test for loop'] = function () { 6 | check(filedir, 'forLoopEx1') 7 | check(filedir, 'forLoopEx2a') 8 | check(filedir, 'forLoopEx2b') 9 | check(filedir, 'forLoopEx3') 10 | check(filedir, 'forLoopEx4') 11 | } 12 | 13 | exports['test intersection_for loop'] = function () { 14 | check(filedir, 'intersectionForLoopEx1') 15 | check(filedir, 'intersectionForLoopEx2') 16 | } 17 | 18 | exports['test if statement'] = function () { 19 | check(filedir, 'ifStatementEx1') 20 | } 21 | 22 | /* 23 | exports['test assign statement'] = function () { 24 | assert.todo('todo ') 25 | }*/ 26 | 27 | if (module === require.main) require('test').run(exports) 28 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/assignStatementEx1.jscad: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscad/io/f58d6f4e8a7ef698c0cede08b54df27e26dd6f84/packages/scad-deserializer/tests/conditional_and_iterator_functions/assignStatementEx1.jscad -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/assignStatementEx1.scad: -------------------------------------------------------------------------------- 1 | for (i = [10:50]) 2 | { 3 | assign (angle = i*360/20, distance = i*10, r = i*2) 4 | { 5 | rotate(angle, [1, 0, 0]) 6 | translate([0, distance, 0]) 7 | sphere(r = r); 8 | } 9 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/forLoopEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0.5,0.5,0.5],radius: [0.5,0.5,0.5], resolution: 16}).translate([0,0,-1]).union([CSG.cube({center: [0.5,0.5,0.5],radius: [0.5,0.5,0.5], resolution: 16}).translate([0,0,1])]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/forLoopEx1.scad: -------------------------------------------------------------------------------- 1 | for (z = [-1, 1]) // two iterations, z = -1, z = 1 2 | { 3 | translate([0, 0, z]) 4 | cube(size = 1, center = false); 5 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/forLoopEx2a.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.sphere({center: [0,0,0], radius: 1, resolution: 5}).translate([0,10,0]).transform(CSG.Matrix4x4.rotation([0,0,0], [1,0,0], 0)).union([CSG.sphere({center: [0,0,0], radius: 1, resolution: 5}).translate([0,10,0]).transform(CSG.Matrix4x4.rotation([0,0,0], [1,0,0], 60))]).union([CSG.sphere({center: [0,0,0], radius: 1, resolution: 5}).translate([0,10,0]).transform(CSG.Matrix4x4.rotation([0,0,0], [1,0,0], 120))]).union([CSG.sphere({center: [0,0,0], radius: 1, resolution: 5}).translate([0,10,0]).transform(CSG.Matrix4x4.rotation([0,0,0], [1,0,0], 180))]).union([CSG.sphere({center: [0,0,0], radius: 1, resolution: 5}).translate([0,10,0]).transform(CSG.Matrix4x4.rotation([0,0,0], [1,0,0], 240))]).union([CSG.sphere({center: [0,0,0], radius: 1, resolution: 5}).translate([0,10,0]).transform(CSG.Matrix4x4.rotation([0,0,0], [1,0,0], 300))]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/forLoopEx2a.scad: -------------------------------------------------------------------------------- 1 | for ( i = [0 : 5] ) 2 | { 3 | rotate( i * 360 / 6, [1, 0, 0]) 4 | translate([0, 10, 0]) 5 | sphere(r = 1); 6 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/forLoopEx2b.scad: -------------------------------------------------------------------------------- 1 | // Note: The middle parameter in the range designation 2 | // ('0.2' in this case) is the 'increment-by' value 3 | // Warning: Depending on the 'increment-by' value, the 4 | // real end value will be smaller than the given one. 5 | for ( i = [0 : 0.2 : 5] ) 6 | { 7 | rotate( i * 360 / 6, [1, 0, 0]) 8 | translate([0, 10, 0]) 9 | sphere(r = 1); 10 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/forLoopEx3.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(0).rotateY(0).rotateZ(0).union([CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(10).rotateY(20).rotateZ(300)]).union([CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(200).rotateY(40).rotateZ(57)]).union([CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(20).rotateY(88).rotateZ(57)]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/forLoopEx3.scad: -------------------------------------------------------------------------------- 1 | for(i = [ [ 0, 0, 0], 2 | [ 10, 20, 300], 3 | [200, 40, 57], 4 | [ 20, 88, 57] ]) 5 | { 6 | rotate(i) 7 | cube([100, 20, 20], center = true); 8 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/forLoopEx4.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [25,7.5,5], resolution: 16}).translate([0,0,0]).union([CSG.cube({center: [0,0,0],radius: [25,7.5,5], resolution: 16}).translate([10,12,10])]).union([CSG.cube({center: [0,0,0],radius: [25,7.5,5], resolution: 16}).translate([20,24,20])]).union([CSG.cube({center: [0,0,0],radius: [25,7.5,5], resolution: 16}).translate([30,36,30])]).union([CSG.cube({center: [0,0,0],radius: [25,7.5,5], resolution: 16}).translate([20,48,40])]).union([CSG.cube({center: [0,0,0],radius: [25,7.5,5], resolution: 16}).translate([10,60,50])]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/forLoopEx4.scad: -------------------------------------------------------------------------------- 1 | for(i = [ [ 0, 0, 0], 2 | [10, 12, 10], 3 | [20, 24, 20], 4 | [30, 36, 30], 5 | [20, 48, 40], 6 | [10, 60, 50] ]) 7 | { 8 | translate(i) 9 | cube([50, 15, 10], center = true); 10 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/ifStatementEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0.5,0.5,0.5],radius: [0.5,0.5,0.5], resolution: 16}); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/ifStatementEx1.scad: -------------------------------------------------------------------------------- 1 | x=2; 2 | y=1; 3 | if (x > y) 4 | { 5 | cube(size = 1, center = false); 6 | } else { 7 | cube(size = 2, center = true); 8 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/intersectionForLoopEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){return CSG.sphere({center: [0,0,0], radius: 12, resolution: 30}).translate([5,0,0]).rotateX(0).rotateY(0).rotateZ(60).intersect([CSG.sphere({center: [0,0,0], radius: 12, resolution: 30}).translate([5,0,0]).rotateX(0).rotateY(0).rotateZ(120)]).intersect([CSG.sphere({center: [0,0,0], radius: 12, resolution: 30}).translate([5,0,0]).rotateX(0).rotateY(0).rotateZ(180)]).intersect([CSG.sphere({center: [0,0,0], radius: 12, resolution: 30}).translate([5,0,0]).rotateX(0).rotateY(0).rotateZ(240)]).intersect([CSG.sphere({center: [0,0,0], radius: 12, resolution: 30}).translate([5,0,0]).rotateX(0).rotateY(0).rotateZ(300)]).intersect([CSG.sphere({center: [0,0,0], radius: 12, resolution: 30}).translate([5,0,0]).rotateX(0).rotateY(0).rotateZ(360)]);}; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/intersectionForLoopEx1.scad: -------------------------------------------------------------------------------- 1 | intersection_for(n = [1 : 6]) 2 | { 3 | rotate([0, 0, n * 60]) 4 | { 5 | translate([5,0,0]) 6 | sphere(r=12); 7 | } 8 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/intersectionForLoopEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){return CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(0).rotateY(0).rotateZ(0).intersect([CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(10).rotateY(20).rotateZ(300)]).intersect([CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(200).rotateY(40).rotateZ(57)]).intersect([CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(20).rotateY(88).rotateZ(57)]);}; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/conditional_and_iterator_functions/intersectionForLoopEx2.scad: -------------------------------------------------------------------------------- 1 | intersection_for(i = [ [ 0, 0, 0], 2 | [ 10, 20, 300], 3 | [200, 40, 57], 4 | [ 20, 88, 57] ]) 5 | { 6 | rotate(i) 7 | cube([100, 20, 20], center = true); 8 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const assert = require('assert') 3 | const parser = require('../src/parser') 4 | 5 | function check (testFileName) { 6 | var test = fs.readFileSync('examples/' + testFileName + '.scad', 'utf8') 7 | var expected = fs.readFileSync('examples/' + testFileName + '.jscad', 'utf8').replace(/\n/g, '') 8 | var actual = parser.parse(test).lines.join('').replace(/\n/g, '') 9 | // assert.equal(actual, expected, console.log("\nExpected:\n" + expected + "\n\nActual:\n" + actual + "\n")) 10 | assert.equal(actual, expected) 11 | } 12 | 13 | exports['test example001'] = function () { check('example001'); } 14 | exports['test example002'] = function () { check('example002'); } 15 | exports['test example003'] = function () { check('example003'); } 16 | exports['test example004'] = function () { check('example004'); } 17 | exports['test example005'] = function () { check('example005'); } 18 | exports['test example006'] = function () { check('example006'); } 19 | // exports["test example007"] = function() { check("example007"); } 20 | // exports["test example008"] = function() { check("example008"); } 21 | // exports["test example009"] = function() { check("example009"); } 22 | // exports["test example010"] = function() { check("example010"); } 23 | exports['test example011'] = function () { check('example011'); } 24 | // exports["test example012"] = function() { check("example012"); } 25 | // exports["test example013"] = function() { check("example013"); } 26 | exports['test example014'] = function () { check('example014'); } 27 | // exports["test example015"] = function() { check("example015"); } 28 | // exports["test example016"] = function() { check("example016"); } 29 | // exports["test example017"] = function() { check("example017"); } 30 | exports['test example018'] = function () { check('example018'); } 31 | exports['test example019'] = function () { check('example019'); } 32 | // exports["test example020"] = function() { check("example020"); } 33 | // exports["test example021"] = function() { check("example021"); } 34 | // exports["test example022"] = function() { check("example022"); } 35 | // exports["test example023"] = function() { check("example023"); } 36 | 37 | if (module === require.main) require('test').run(exports) 38 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example001.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.sphere({center: [0,0,0], radius: 25, resolution: 30}).subtract([CSG.cylinder({start: [0,0,-31.25], end: [0,0,31.25],radiusStart: 12.5, radiusEnd: 12.5, resolution: 30}).transform(CSG.Matrix4x4.rotation([0,0,0], [0,0,1], 90)), 5 | CSG.cylinder({start: [0,0,-31.25], end: [0,0,31.25],radiusStart: 12.5, radiusEnd: 12.5, resolution: 30}).transform(CSG.Matrix4x4.rotation([0,0,0], [1,0,0], 90)), 6 | CSG.cylinder({start: [0,0,-31.25], end: [0,0,31.25],radiusStart: 12.5, radiusEnd: 12.5, resolution: 30}).transform(CSG.Matrix4x4.rotation([0,0,0], [0,1,0], 90))]); 7 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example001.scad: -------------------------------------------------------------------------------- 1 | 2 | module example001() 3 | { 4 | function r_from_dia(d) = d / 2; 5 | 6 | module rotcy(rot, r, h) { 7 | rotate(90, rot) 8 | cylinder(r = r, h = h, center = true); 9 | } 10 | 11 | difference() { 12 | sphere(r = r_from_dia(size)); 13 | rotcy([0, 0, 0], cy_r, cy_h); 14 | rotcy([1, 0, 0], cy_r, cy_h); 15 | rotcy([0, 1, 0], cy_r, cy_h); 16 | } 17 | 18 | size = 50; 19 | hole = 25; 20 | 21 | cy_r = r_from_dia(hole); 22 | cy_h = r_from_dia(size * 2.5); 23 | } 24 | 25 | example001(); 26 | 27 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example002.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [15,15,15], resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [7.5,7.5,25], resolution: 16}).translate([0,0,-25])]).subtract([CSG.cube({center: [0,0,0],radius: [25,5,5], resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [5,25,5], resolution: 16}), 5 | CSG.cube({center: [0,0,0],radius: [5,5,25], resolution: 16})])]).intersect([CSG.cylinder({start: [0,0,-25], end: [0,0,25],radiusStart: 20, radiusEnd: 5, resolution: 30}).translate([0,0,5])]); 6 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example002.scad: -------------------------------------------------------------------------------- 1 | 2 | module example002() 3 | { 4 | intersection() { 5 | difference() { 6 | union() { 7 | cube([30, 30, 30], center = true); 8 | translate([0, 0, -25]) 9 | cube([15, 15, 50], center = true); 10 | } 11 | union() { 12 | cube([50, 10, 10], center = true); 13 | cube([10, 50, 10], center = true); 14 | cube([10, 10, 50], center = true); 15 | } 16 | } 17 | translate([0, 0, 5]) 18 | cylinder(h = 50, r1 = 20, r2 = 5, center = true); 19 | } 20 | } 21 | 22 | example002(); 23 | 24 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example003.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [15,15,15], resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [20,7.5,7.5], resolution: 16}), 5 | CSG.cube({center: [0,0,0],radius: [7.5,20,7.5], resolution: 16}), 6 | CSG.cube({center: [0,0,0],radius: [7.5,7.5,20], resolution: 16})]).subtract([CSG.cube({center: [0,0,0],radius: [25,5,5], resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [5,25,5], resolution: 16}), 7 | CSG.cube({center: [0,0,0],radius: [5,5,25], resolution: 16})])]); 8 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example003.scad: -------------------------------------------------------------------------------- 1 | 2 | module example003() 3 | { 4 | difference() { 5 | union() { 6 | cube([30, 30, 30], center = true); 7 | cube([40, 15, 15], center = true); 8 | cube([15, 40, 15], center = true); 9 | cube([15, 15, 40], center = true); 10 | } 11 | union() { 12 | cube([50, 10, 10], center = true); 13 | cube([10, 50, 10], center = true); 14 | cube([10, 10, 50], center = true); 15 | } 16 | } 17 | } 18 | 19 | example003(); 20 | 21 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example004.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [15,15,15], resolution: 16}).subtract([CSG.sphere({center: [0,0,0], radius: 20, resolution: 30})]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example004.scad: -------------------------------------------------------------------------------- 1 | 2 | module example004() 3 | { 4 | difference() { 5 | cube(30, center = true); 6 | sphere(20); 7 | } 8 | } 9 | 10 | example004(); 11 | 12 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example005.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,0], end: [0,0,50],radiusStart: 100, radiusEnd: 100, resolution: 30}).subtract([CSG.cylinder({start: [0,0,0], end: [0,0,50],radiusStart: 80, radiusEnd: 80, resolution: 30}).translate([0,0,10]), 5 | CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).translate([100,0,35])]).translate([0,0,-120]).union([CSG.cylinder({start: [0,0,0], end: [0,0,200],radiusStart: 10, radiusEnd: 10, resolution: 30}).translate([0,80,0]).union([CSG.cylinder({start: [0,0,0], end: [0,0,200],radiusStart: 10, radiusEnd: 10, resolution: 30}).translate([69.28203230275508,40.00000000000001,0])]).union([CSG.cylinder({start: [0,0,0], end: [0,0,200],radiusStart: 10, radiusEnd: 10, resolution: 30}).translate([69.2820323027551,-39.999999999999986,0])]).union([CSG.cylinder({start: [0,0,0], end: [0,0,200],radiusStart: 10, radiusEnd: 10, resolution: 30}).translate([9.796850830579018e-15,-80,0])]).union([CSG.cylinder({start: [0,0,0], end: [0,0,200],radiusStart: 10, radiusEnd: 10, resolution: 30}).translate([-69.28203230275507,-40.000000000000036,0])]).union([CSG.cylinder({start: [0,0,0], end: [0,0,200],radiusStart: 10, radiusEnd: 10, resolution: 30}).translate([-69.28203230275508,40.00000000000001,0])]).translate([0,0,-120]),CSG.cylinder({start: [0,0,0], end: [0,0,80],radiusStart: 120, radiusEnd: 0, resolution: 30}).translate([0,0,200]).translate([0,0,-120])]); 6 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example005.scad: -------------------------------------------------------------------------------- 1 | 2 | module example005() 3 | { 4 | translate([0, 0, -120]) { 5 | difference() { 6 | cylinder(h = 50, r = 100); 7 | translate([0, 0, 10]) cylinder(h = 50, r = 80); 8 | translate([100, 0, 35]) cube(50, center = true); 9 | } 10 | for (i = [0:5]) { 11 | echo(360*i/6, sin(360*i/6)*80, cos(360*i/6)*80); 12 | translate([sin(360*i/6)*80, cos(360*i/6)*80, 0 ]) 13 | cylinder(h = 200, r=10); 14 | } 15 | translate([0, 0, 200]) 16 | cylinder(h = 80, r1 = 120, r2 = 0); 17 | } 18 | } 19 | 20 | example005(); 21 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example006.scad: -------------------------------------------------------------------------------- 1 | 2 | module example006() 3 | { 4 | module edgeprofile() 5 | { 6 | render(convexity = 2) difference() { 7 | cube([20, 20, 150], center = true); 8 | translate([-10, -10, 0]) 9 | cylinder(h = 80, r = 10, center = true); 10 | translate([-10, -10, +40]) 11 | sphere(r = 10); 12 | translate([-10, -10, -40]) 13 | sphere(r = 10); 14 | } 15 | } 16 | 17 | difference() 18 | { 19 | cube(100, center = true); 20 | for (rot = [ [0, 0, 0], [1, 0, 0], [0, 1, 0] ]) { 21 | rotate(90, rot) 22 | for (p = [[+1, +1, 0], [-1, +1, 90], [-1, -1, 180], [+1, -1, 270]]) { 23 | translate([ p[0]*50, p[1]*50, 0 ]) 24 | rotate(p[2], [0, 0, 1]) 25 | edgeprofile(); 26 | } 27 | } 28 | for (i = [ 29 | [ 0, 0, [ [0, 0] ] ], 30 | [ 90, 0, [ [-20, -20], [+20, +20] ] ], 31 | [ 180, 0, [ [-20, -25], [-20, 0], [-20, +25], [+20, -25], [+20, 0], [+20, +25] ] ], 32 | [ 270, 0, [ [0, 0], [-25, -25], [+25, -25], [-25, +25], [+25, +25] ] ], 33 | [ 0, 90, [ [-25, -25], [0, 0], [+25, +25] ] ], 34 | [ 0, -90, [ [-25, -25], [+25, -25], [-25, +25], [+25, +25] ] ] 35 | ]) { 36 | rotate(i[0], [0, 0, 1]) rotate(i[1], [1, 0, 0]) translate([0, -50, 0]) 37 | for (j = i[2]) 38 | translate([j[0], 0, j[1]]) sphere(10); 39 | } 40 | } 41 | } 42 | 43 | example006(); 44 | 45 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example007.scad: -------------------------------------------------------------------------------- 1 | 2 | module cutout() 3 | { 4 | intersection() 5 | { 6 | rotate(90, [1, 0, 0]) 7 | translate([0, 0, -50]) 8 | linear_extrude(height = 100, convexity = 1) 9 | import(file = "example007.dxf", layer = "cutout1"); 10 | 11 | rotate(90, [0, 0, 1]) 12 | rotate(90, [1, 0, 0]) 13 | translate([0, 0, -50]) 14 | linear_extrude(height = 100, convexity = 2) 15 | import(file = "example007.dxf", layer = "cutout2"); 16 | } 17 | } 18 | 19 | module clip() 20 | { 21 | difference() { 22 | // NB! We have to use the deprecated module here since the "dorn" 23 | // layer contains an open polyline, which is not yet supported 24 | // by the import() module. 25 | rotate_extrude( 26 | file = "example007.dxf", 27 | layer="dorn", 28 | convexity = 3); 29 | for (r = [0, 90]) 30 | rotate(r, [0, 0, 1]) 31 | cutout(); 32 | } 33 | } 34 | 35 | module cutview() 36 | { 37 | difference() 38 | { 39 | difference() 40 | { 41 | translate([0, 0, -10]) 42 | clip(); 43 | 44 | rotate(20, [0, 0, 1]) 45 | rotate(-20, [0, 1, 0]) 46 | translate([18, 0, 0]) 47 | cube(30, center = true); 48 | } 49 | 50 | # render(convexity = 5) intersection() 51 | { 52 | translate([0, 0, -10]) 53 | clip(); 54 | 55 | rotate(20, [0, 0, 1]) 56 | rotate(-20, [0, 1, 0]) 57 | translate([18, 0, 0]) 58 | cube(30, center = true); 59 | } 60 | } 61 | } 62 | 63 | translate([0, 0, -10]) 64 | clip(); 65 | 66 | // cutview(); 67 | 68 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example008.scad: -------------------------------------------------------------------------------- 1 | 2 | difference() 3 | { 4 | intersection() 5 | { 6 | translate([ -25, -25, -25]) 7 | linear_extrude(height = 50, convexity = 3) 8 | import(file = "example008.dxf", layer = "G"); 9 | 10 | rotate(90, [1, 0, 0]) 11 | translate([ -25, -125, -25]) 12 | linear_extrude(height = 50, convexity = 3) 13 | import(file = "example008.dxf", layer = "E"); 14 | 15 | rotate(90, [0, 1, 0]) 16 | translate([ -125, -125, -25]) 17 | linear_extrude(height = 50, convexity = 3) 18 | import(file = "example008.dxf", layer = "B"); 19 | } 20 | 21 | intersection() 22 | { 23 | translate([ -125, -25, -26]) 24 | linear_extrude(height = 52, convexity = 1) 25 | import(file = "example008.dxf", layer = "X"); 26 | 27 | rotate(90, [0, 1, 0]) 28 | translate([ -125, -25, -26]) 29 | linear_extrude(height = 52, convexity = 1) 30 | import(file = "example008.dxf", layer = "X"); 31 | } 32 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example009.scad: -------------------------------------------------------------------------------- 1 | 2 | bodywidth = dxf_dim(file = "example009.dxf", name = "bodywidth"); 3 | fanwidth = dxf_dim(file = "example009.dxf", name = "fanwidth"); 4 | platewidth = dxf_dim(file = "example009.dxf", name = "platewidth"); 5 | fan_side_center = dxf_cross(file = "example009.dxf", layer = "fan_side_center"); 6 | fanrot = dxf_dim(file = "example009.dxf", name = "fanrot"); 7 | 8 | % linear_extrude(height = bodywidth, center = true, convexity = 10) 9 | import(file = "example009.dxf", layer = "body"); 10 | 11 | % for (z = [+(bodywidth/2 + platewidth/2), 12 | -(bodywidth/2 + platewidth/2)]) 13 | { 14 | translate([0, 0, z]) 15 | linear_extrude(height = platewidth, center = true, convexity = 10) 16 | import(file = "example009.dxf", layer = "plate"); 17 | } 18 | 19 | intersection() 20 | { 21 | linear_extrude(height = fanwidth, center = true, convexity = 10, twist = -fanrot) 22 | import(file = "example009.dxf", layer = "fan_top"); 23 | 24 | // NB! We have to use the deprecated module here since the "fan_side" 25 | // layer contains an open polyline, which is not yet supported 26 | // by the import() module. 27 | rotate_extrude(file = "example009.dxf", layer = "fan_side", 28 | origin = fan_side_center, convexity = 10); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example010.scad: -------------------------------------------------------------------------------- 1 | 2 | // example010.dat generated using octave: 3 | // d = (sin(1:0.2:10)' * cos(1:0.2:10)) * 10; 4 | // save("example010.dat", "d"); 5 | 6 | intersection() 7 | { 8 | surface(file = "example010.dat", 9 | center = true, convexity = 5); 10 | 11 | rotate(45, [0, 0, 1]) 12 | surface(file = "example010.dat", 13 | center = true, convexity = 5); 14 | } 15 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example011.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.fromPolygons([new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([0,0,10])),new CSG.Vertex(new CSG.Vector3D([10,-10,0])),new CSG.Vertex(new CSG.Vector3D([10,10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([0,0,10])),new CSG.Vertex(new CSG.Vector3D([-10,-10,0])),new CSG.Vertex(new CSG.Vector3D([10,-10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([0,0,10])),new CSG.Vertex(new CSG.Vector3D([-10,10,0])),new CSG.Vertex(new CSG.Vector3D([-10,-10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([0,0,10])),new CSG.Vertex(new CSG.Vector3D([10,10,0])),new CSG.Vertex(new CSG.Vector3D([-10,10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([-10,10,0])),new CSG.Vertex(new CSG.Vector3D([10,10,0])),new CSG.Vertex(new CSG.Vector3D([10,-10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([-10,10,0])),new CSG.Vertex(new CSG.Vector3D([10,-10,0])),new CSG.Vertex(new CSG.Vector3D([-10,-10,0]))])]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example011.scad: -------------------------------------------------------------------------------- 1 | polyhedron( 2 | points=[ 3 | [10,10,0], 4 | [10,-10,0], 5 | [-10,-10,0], 6 | [-10,10,0], 7 | [0,0,10] 8 | ], 9 | triangles=[ 10 | [0,1,4], 11 | [1,2,4], 12 | [2,3,4], 13 | [3,0,4], 14 | [1,0,3], 15 | [2,1,3] 16 | ] 17 | ); 18 | 19 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example012.scad: -------------------------------------------------------------------------------- 1 | 2 | // example012.stl is Mblock.stl, (c) 2009 Will Langford 3 | // licensed under the Creative Commons - GNU GPL license. 4 | // http://www.thingiverse.com/thing:753 5 | 6 | difference() 7 | { 8 | sphere(20); 9 | 10 | translate([ -2.92, 0.5, +20 ]) rotate([180, 0, 180]) 11 | import("example012.stl", convexity = 5); 12 | } 13 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example013.scad: -------------------------------------------------------------------------------- 1 | 2 | intersection() 3 | { 4 | linear_extrude(height = 100, center = true, convexity= 3) 5 | import(file = "example013.dxf"); 6 | rotate([0, 90, 0]) 7 | linear_extrude(height = 100, center = true, convexity= 3) 8 | import(file = "example013.dxf"); 9 | rotate([90, 0, 0]) 10 | linear_extrude(height = 100, center = true, convexity= 3) 11 | import(file = "example013.dxf"); 12 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example014.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(0).rotateY(0).rotateZ(0).intersect([CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(10).rotateY(20).rotateZ(300)]).intersect([CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(200).rotateY(40).rotateZ(57)]).intersect([CSG.cube({center: [0,0,0],radius: [50,10,10], resolution: 16}).rotateX(20).rotateY(88).rotateZ(57)]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example014.scad: -------------------------------------------------------------------------------- 1 | 2 | intersection_for(i = [ 3 | [0, 0, 0], 4 | [10, 20, 300], 5 | [200, 40, 57], 6 | [20, 88, 57] 7 | ]) 8 | rotate(i) cube([100, 20, 20], center = true); 9 | 10 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example015.scad: -------------------------------------------------------------------------------- 1 | 2 | module shape() 3 | { 4 | difference() 5 | { 6 | translate([ -35, -35 ]) intersection() 7 | { 8 | union() { 9 | difference() { 10 | square(100, true); 11 | square(50, true); 12 | } 13 | translate([ 50, 50 ]) 14 | square(15, true); 15 | } 16 | rotate(45) translate([ 0, -15 ]) square([ 100, 30 ]); 17 | } 18 | 19 | rotate(-45) scale([ 0.7, 1.3 ]) circle(5); 20 | } 21 | 22 | import(file = "example009.dxf", layer = "body", 23 | convexity = 6, scale=2); 24 | } 25 | 26 | // linear_extrude(convexity = 10, center = true) 27 | shape(); 28 | 29 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example016.scad: -------------------------------------------------------------------------------- 1 | 2 | // example016.stl is derived from Mblock.stl 3 | // (c) 2009 Will Langford licensed under 4 | // the Creative Commons - GNU GPL license. 5 | // http://www.thingiverse.com/thing:753 6 | // 7 | // Jonas Pfeil converted the file to binary 8 | // STL and duplicated its content. 9 | 10 | module blk1() { 11 | cube([ 65, 28, 28 ], center = true); 12 | } 13 | 14 | module blk2() { 15 | difference() { 16 | translate([ 0, 0, 7.5 ]) 17 | cube([ 60, 28, 14 ], center = true); 18 | cube([ 8, 32, 32 ], center = true); 19 | } 20 | } 21 | 22 | module chop() { 23 | translate([ -14, 0, 0 ]) 24 | import(file = "example016.stl", convexity = 12); 25 | } 26 | 27 | difference() { 28 | blk1(); 29 | for (alpha = [0, 90, 180, 270]) { 30 | rotate(alpha, [ 1, 0, 0]) render(convexity = 12) 31 | difference() { 32 | blk2(); 33 | chop(); 34 | } 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example016.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jscad/io/f58d6f4e8a7ef698c0cede08b54df27e26dd6f84/packages/scad-deserializer/tests/examples/example016.stl -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example018.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | return CSG.cube({center: [0,0,0],radius: [30,30,30], resolution: 16}).translate([-150,0,0]).union([CSG.cylinder({start: [0,0,-25], end: [0,0,25],radiusStart: 30, radiusEnd: 30, resolution: 30}).translate([-50,0,0])]).union([CSG.cube({center: [0,0,0],radius: [22.5,22.5,22.5], resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(45).rotateY(0).rotateZ(0),CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(0).rotateY(45).rotateZ(0),CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(0).rotateY(0).rotateZ(45)]).translate([50,0,0])]).union([CSG.sphere({center: [0,0,0], radius: 30, resolution: 30}).translate([150,0,0])]).translate([0,-150,0]).union([CSG.cylinder({start: [0,0,-25], end: [0,0,25],radiusStart: 30, radiusEnd: 30, resolution: 30}).translate([-150,0,0]).union([CSG.cube({center: [0,0,0],radius: [22.5,22.5,22.5], resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(45).rotateY(0).rotateZ(0),CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(0).rotateY(45).rotateZ(0),CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(0).rotateY(0).rotateZ(45)]).translate([-50,0,0])]).union([CSG.sphere({center: [0,0,0], radius: 30, resolution: 30}).translate([50,0,0])]).union([CSG.cube({center: [0,0,0],radius: [30,30,30], resolution: 16}).translate([150,0,0])]).translate([0,-50,0])]).union([CSG.cube({center: [0,0,0],radius: [22.5,22.5,22.5], resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(45).rotateY(0).rotateZ(0),CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(0).rotateY(45).rotateZ(0),CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(0).rotateY(0).rotateZ(45)]).translate([-150,0,0]).union([CSG.sphere({center: [0,0,0], radius: 30, resolution: 30}).translate([-50,0,0])]).union([CSG.cube({center: [0,0,0],radius: [30,30,30], resolution: 16}).translate([50,0,0])]).union([CSG.cylinder({start: [0,0,-25], end: [0,0,25],radiusStart: 30, radiusEnd: 30, resolution: 30}).translate([150,0,0])]).translate([0,50,0])]).union([CSG.sphere({center: [0,0,0], radius: 30, resolution: 30}).translate([-150,0,0]).union([CSG.cube({center: [0,0,0],radius: [30,30,30], resolution: 16}).translate([-50,0,0])]).union([CSG.cylinder({start: [0,0,-25], end: [0,0,25],radiusStart: 30, radiusEnd: 30, resolution: 30}).translate([50,0,0])]).union([CSG.cube({center: [0,0,0],radius: [22.5,22.5,22.5], resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(45).rotateY(0).rotateZ(0),CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(0).rotateY(45).rotateZ(0),CSG.cube({center: [0,0,0],radius: [25,25,25], resolution: 16}).rotateX(0).rotateY(0).rotateZ(45)]).translate([150,0,0])]).translate([0,150,0])]); 4 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example018.scad: -------------------------------------------------------------------------------- 1 | 2 | module step(len, mod) 3 | { 4 | for (i = [0:$children-1]) 5 | translate([ len*(i - ($children-1)/2), 0, 0 ]) child((i+mod) % $children); 6 | } 7 | 8 | for (i = [1:4]) 9 | { 10 | translate([0, -250+i*100, 0]) step(100, i) 11 | { 12 | sphere(30); 13 | cube(60, true); 14 | cylinder(r = 30, h = 50, center = true); 15 | 16 | union() { 17 | cube(45, true); 18 | rotate([45, 0, 0]) cube(50, true); 19 | rotate([0, 45, 0]) cube(50, true); 20 | rotate([0, 0, 45]) cube(50, true); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example019.scad: -------------------------------------------------------------------------------- 1 | 2 | function get_cylinder_h(p) = lookup(p, [ 3 | [ -200, 5 ], 4 | [ -50, 20 ], 5 | [ -20, 18 ], 6 | [ +80, 25 ], 7 | [ +150, 2 ] 8 | ]); 9 | 10 | for (i = [-100:5:+100]) { 11 | // echo(i, get_cylinder_h(i)); 12 | translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h = get_cylinder_h(i)*3); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example020.scad: -------------------------------------------------------------------------------- 1 | 2 | module screw(type = 2, r1 = 15, r2 = 20, n = 7, h = 100, t = 8) 3 | { 4 | linear_extrude(height = h, twist = 360*t/n, convexity = t) 5 | difference() { 6 | circle(r2); 7 | for (i = [0:n-1]) { 8 | if (type == 1) rotate(i*360/n) polygon([ 9 | [ 2*r2, 0 ], 10 | [ r2, 0 ], 11 | [ r1*cos(180/n), r1*sin(180/n) ], 12 | [ r2*cos(360/n), r2*sin(360/n) ], 13 | [ 2*r2*cos(360/n), 2*r2*sin(360/n) ], 14 | ]); 15 | if (type == 2) rotate(i*360/n) polygon([ 16 | [ 2*r2, 0 ], 17 | [ r2, 0 ], 18 | [ r1*cos(90/n), r1*sin(90/n) ], 19 | [ r1*cos(180/n), r1*sin(180/n) ], 20 | [ r2*cos(270/n), r2*sin(270/n) ], 21 | [ 2*r2*cos(270/n), 2*r2*sin(270/n) ], 22 | ]); 23 | } 24 | } 25 | } 26 | 27 | module nut(type = 2, r1 = 16, r2 = 21, r3 = 30, s = 6, n = 7, h = 100/5, t = 8/5) 28 | { 29 | difference() { 30 | cylinder($fn = s, r = r3, h = h); 31 | translate([ 0, 0, -h/2 ]) screw(type, r1, r2, n, h*2, t*2); 32 | } 33 | } 34 | 35 | module spring(r1 = 100, r2 = 10, h = 100, hr = 12) 36 | { 37 | stepsize = 1/16; 38 | module segment(i1, i2) { 39 | alpha1 = i1 * 360*r2/hr; 40 | alpha2 = i2 * 360*r2/hr; 41 | len1 = sin(acos(i1*2-1))*r2; 42 | len2 = sin(acos(i2*2-1))*r2; 43 | if (len1 < 0.01) 44 | polygon([ 45 | [ cos(alpha1)*r1, sin(alpha1)*r1 ], 46 | [ cos(alpha2)*(r1-len2), sin(alpha2)*(r1-len2) ], 47 | [ cos(alpha2)*(r1+len2), sin(alpha2)*(r1+len2) ] 48 | ]); 49 | if (len2 < 0.01) 50 | polygon([ 51 | [ cos(alpha1)*(r1+len1), sin(alpha1)*(r1+len1) ], 52 | [ cos(alpha1)*(r1-len1), sin(alpha1)*(r1-len1) ], 53 | [ cos(alpha2)*r1, sin(alpha2)*r1 ], 54 | ]); 55 | if (len1 >= 0.01 && len2 >= 0.01) 56 | polygon([ 57 | [ cos(alpha1)*(r1+len1), sin(alpha1)*(r1+len1) ], 58 | [ cos(alpha1)*(r1-len1), sin(alpha1)*(r1-len1) ], 59 | [ cos(alpha2)*(r1-len2), sin(alpha2)*(r1-len2) ], 60 | [ cos(alpha2)*(r1+len2), sin(alpha2)*(r1+len2) ] 61 | ]); 62 | } 63 | linear_extrude(height = 100, twist = 180*h/hr, 64 | $fn = (hr/r2)/stepsize, convexity = 5) { 65 | for (i = [ stepsize : stepsize : 1+stepsize/2 ]) 66 | segment(i-stepsize, min(i, 1)); 67 | } 68 | } 69 | 70 | translate([ -30, 0, 0 ]) 71 | screw(); 72 | 73 | translate([ 30, 0, 0 ]) 74 | nut(); 75 | 76 | spring(); 77 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example021.scad: -------------------------------------------------------------------------------- 1 | 2 | module thing() 3 | { 4 | $fa = 30; 5 | difference() { 6 | sphere(r = 25); 7 | cylinder(h = 62.5, r1 = 12.5, r2 = 6.25, center = true); 8 | rotate(90, [ 1, 0, 0 ]) cylinder(h = 62.5, 9 | r1 = 12.5, r2 = 6.25, center = true); 10 | rotate(90, [ 0, 1, 0 ]) cylinder(h = 62.5, 11 | r1 = 12.5, r2 = 6.25, center = true); 12 | } 13 | } 14 | 15 | module demo_proj() 16 | { 17 | linear_extrude(center = true, height = 0.5) projection(cut = false) thing(); 18 | % thing(); 19 | } 20 | 21 | module demo_cut() 22 | { 23 | for (i=[-20:5:+20]) { 24 | rotate(-30, [ 1, 1, 0 ]) translate([ 0, 0, -i ]) 25 | linear_extrude(center = true, height = 0.5) projection(cut = true) 26 | translate([ 0, 0, i ]) rotate(+30, [ 1, 1, 0 ]) thing(); 27 | } 28 | % thing(); 29 | } 30 | 31 | translate([ -30, 0, 0 ]) demo_proj(); 32 | translate([ +30, 0, 0 ]) demo_cut(); 33 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example022.scad: -------------------------------------------------------------------------------- 1 | // size is a vector [w, h, d] 2 | module roundedBox(size, radius, sidesonly) 3 | { 4 | rot = [ [0,0,0], [90,0,90], [90,90,0] ]; 5 | if (sidesonly) { 6 | cube(size - [2*radius,0,0], true); 7 | cube(size - [0,2*radius,0], true); 8 | for (x = [radius-size[0]/2, -radius+size[0]/2], 9 | y = [radius-size[1]/2, -radius+size[1]/2]) { 10 | translate([x,y,0]) cylinder(r=radius, h=size[2], center=true); 11 | } 12 | } 13 | else { 14 | cube([size[0], size[1]-radius*2, size[2]-radius*2], center=true); 15 | cube([size[0]-radius*2, size[1], size[2]-radius*2], center=true); 16 | cube([size[0]-radius*2, size[1]-radius*2, size[2]], center=true); 17 | 18 | for (axis = [0:2]) { 19 | for (x = [radius-size[axis]/2, -radius+size[axis]/2], 20 | y = [radius-size[(axis+1)%3]/2, -radius+size[(axis+1)%3]/2]) { 21 | rotate(rot[axis]) 22 | translate([x,y,0]) 23 | cylinder(h=size[(axis+2)%3]-2*radius, r=radius, center=true); 24 | } 25 | } 26 | for (x = [radius-size[0]/2, -radius+size[0]/2], 27 | y = [radius-size[1]/2, -radius+size[1]/2], 28 | z = [radius-size[2]/2, -radius+size[2]/2]) { 29 | translate([x,y,z]) sphere(radius); 30 | } 31 | } 32 | } 33 | 34 | translate([-15,0,0])roundedBox([20,30,40], 5, true); 35 | translate([15,0,0]) roundedBox([20,30,40], 5, false); 36 | 37 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/examples/example023.scad: -------------------------------------------------------------------------------- 1 | // Example combining MCAD/fonts.scad with search() function. 2 | 3 | use 4 | 5 | thisFont=8bit_polyfont(); 6 | x_shift=thisFont[0][0]; 7 | y_shift=thisFont[0][1]; 8 | 9 | hours=["one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"]; 10 | 11 | module clock_hour_words(word_offset=20.0,word_height=2.0) { 12 | for(i=[0:(len(hours)-1)]) assign( hourHandAngle=(i+1)*360/len(hours), theseIndicies=search(hours[i],thisFont[2],1,1) ) { 13 | rotate(90-hourHandAngle) translate([word_offset,0]) 14 | for( j=[0:(len(theseIndicies)-1)] ) translate([j*x_shift,-y_shift/2]) { 15 | linear_extrude(height=word_height) polygon(points=thisFont[2][theseIndicies[j]][6][0],paths=thisFont[2][theseIndicies[j]][6][1]); 16 | } 17 | } 18 | } 19 | 20 | clock_hour_words(word_offset=16.0,word_height=5.0); 21 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/helpers.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const assert = require('assert') 4 | const parser = require(path.join(__dirname, '../src/parserWrapper')) 5 | 6 | function check (filedir, testFileName) { 7 | var test = fs.readFileSync(path.join(__dirname, `${filedir}${testFileName}.scad`), 'utf8') 8 | var expected = fs.readFileSync(path.join(__dirname, `${filedir}${testFileName}.jscad`), 'utf8').replace(/\n/g, '') 9 | var actual = parser.parse(test).replace(/\n/g, '') 10 | assert.equal(actual, expected) 11 | } 12 | 13 | module.exports = {check} 14 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | test 4 | 5 | 6 | 7 |
8 | function r_from_dia(d) = d / 2; 9 | 10 | module rotcy(rot, r, h) { 11 | rotate(90, rot) 12 | cylinder(r = r, h = h, center = true); 13 | } 14 | 15 | difference() { 16 | sphere(r = r_from_dia(size)); 17 | rotcy([0, 0, 0], cy_r, cy_h); 18 | rotcy([1, 0, 0], cy_r, cy_h); 19 | rotcy([0, 1, 0], cy_r, cy_h); 20 | } 21 | 22 | size = 50; 23 | hole = 25; 24 | 25 | cy_r = r_from_dia(hole); 26 | cy_h = r_from_dia(size * 2.5); 27 |
28 | 29 | 30 | 31 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/math_ops.scad: -------------------------------------------------------------------------------- 1 | echo( "Random Vector: ",rands(5,15,4,42)); 2 | echo( "abs: ",abs(-9)); 3 | echo( "sign: ",sign(9)); 4 | echo( "sign: ",sign(-9)); 5 | echo( "cos: ",cos(9)); 6 | echo( "sin: ",sin(9)); 7 | echo( "tan: ",tan(9)); 8 | echo( "acos: ",acos(0.1)); 9 | echo( "asin: ",asin(0.1)); 10 | echo( "atan: ",atan(9)); 11 | echo( "atan2: ",atan2(1,2)); 12 | 13 | echo( "min: ",min(1,2)); 14 | echo( "max: ",max(1,2)); 15 | echo( "pow: ",pow(1,2)); 16 | echo( "sqrt: ",sqrt(2)); 17 | echo( "ln: ",ln(1)); 18 | 19 | echo( "ceil: ",ceil(2.5)); 20 | echo( "ceil: ",ceil(-2.5)); 21 | echo( "floor: ",floor(2.5)); 22 | echo( "floor: ",floor(-2.5)); 23 | echo( "log: ",log(10)); 24 | echo( "log: ",log(10,2)); 25 | echo( "exp: ",exp(2)); 26 | 27 | echo("sign"); 28 | echo(sign(-5.0)); 29 | echo(sign(0)); 30 | echo(sign(8.0)); 31 | 32 | echo("round"); 33 | echo(round(2.5));// = x+1. 34 | echo(round(2.49));// = x. 35 | echo(round(-(2.5)));// = -(x+1). 36 | echo(round(-(2.49)));// = -x. 37 | 38 | echo(round(5.4));// //-> 5 39 | echo(round(5.5));// //-> 6 40 | echo(round(5.6));// //-> 6 41 | 42 | sphere(); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/modules.js: -------------------------------------------------------------------------------- 1 | const check = require('./helpers').check 2 | 3 | const filedir = 'modules/' 4 | 5 | exports['test modules'] = function () { 6 | check(filedir, 'modulesEx1') 7 | } 8 | 9 | exports['test modules child'] = function () { 10 | check(filedir, 'modulesChildEx1') 11 | } 12 | 13 | exports['test modules children'] = function () { 14 | check(filedir, 'modulesChildrenEx1') 15 | } 16 | 17 | exports['test modules parameters'] = function () { 18 | check(filedir, 'modulesParametersEx1') 19 | } 20 | 21 | if (module === require.main) require('test').run(exports) 22 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/modules/modulesChildEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.sphere({center: [0,0,0], radius: 30, resolution: 30}).translate([0,0,0]).union([CSG.sphere({center: [0,0,0], radius: 30, resolution: 30}).translate([65,0,0])]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/modules/modulesChildEx1.scad: -------------------------------------------------------------------------------- 1 | module lineup(num, space) { 2 | for (i = [0 : num-1]) 3 | translate([ space*i, 0, 0 ]) child(0); 4 | } 5 | 6 | lineup(2, 65) sphere(30); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/modules/modulesChildrenEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.sphere({center: [0,0,0], radius: 30, resolution: 30}).scale([10,1,1]).union([CSG.cube({center: [5,5,5],radius: [5,5,5], resolution: 16}).scale([10,1,1])]).union([CSG.cylinder({start: [0,0,0], end: [0,0,50],radiusStart: 10, radiusEnd: 10, resolution: 30}).scale([10,1,1])]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/modules/modulesChildrenEx1.scad: -------------------------------------------------------------------------------- 1 | module elongate() { 2 | for (i = [0 : $children-1]) 3 | scale([10 , 1, 1 ]) child(i); 4 | } 5 | 6 | elongate() { sphere(30); cube([10,10,10]); cylinder(r=10,h=50); } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/modules/modulesEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,-50], end: [0,0,50],radiusStart: 10, radiusEnd: 10, resolution: 30}).translate([0,0,0]).transform(CSG.Matrix4x4.rotation([0,0,0], [1,0,0], 90)); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/modules/modulesEx1.scad: -------------------------------------------------------------------------------- 1 | module hole(distance, rot, size) { 2 | rotate(a = rot, v = [1, 0, 0]) { 3 | translate([0, distance, 0]) { 4 | cylinder(r = size, h = 100, center = true); 5 | } 6 | } 7 | } 8 | hole(0, 90, 10); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/modules/modulesParametersEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0.5,0.5,0.5],radius: [0.5,0.5,0.5], resolution: 16}).translate([0,-1,0]).setColor(1,0,0).union([(new CSG.Path2D([[0,0],[0,1],[0.5,1.5],[1,1],[1,0]],true)).innerToCAG().extrude({offset: [0, 0, 1], twistangle: 0,twiststeps: 2}).rotateX(90).rotateY(0).rotateZ(0).setColor(1,0,0).translate([2,0,0]), 5 | CSG.sphere({center: [0,0,0], radius: 0.5, resolution: 20}).translate([0.5,0.5,1]).union([CSG.cube({center: [0.5,0.5,0.5],radius: [0.5,0.5,0.5], resolution: 16})]).translate([0,-1,0]).setColor(0,1,0).translate([4,0,0]), 6 | (new CSG.Path2D([[0,0],[0,1],[0.5,1.5],[1,1],[1,0]],true)).innerToCAG().extrude({offset: [0, 0, 1], twistangle: 0,twiststeps: 2}).rotateX(90).rotateY(0).rotateZ(0).setColor(0,0,1).translate([6,0,0]), 7 | (new CSG.Path2D([[0,0],[0,1],[0.5,1.5],[1,1],[1,0]],true)).innerToCAG().extrude({offset: [0, 0, 1], twistangle: 0,twiststeps: 2}).rotateX(90).rotateY(0).rotateZ(0).setColor(0,0,0).translate([8,0,0]), 8 | CSG.sphere({center: [0,0,0], radius: 0.5, resolution: 20}).translate([0.5,0.5,1]).union([CSG.cube({center: [0.5,0.5,0.5],radius: [0.5,0.5,0.5], resolution: 16})]).translate([0,-1,0]).setColor(1,0,0).translate([10,0,0]), 9 | CSG.cube({center: [0.5,0.5,0.5],radius: [0.5,0.5,0.5], resolution: 16}).translate([0,-1,0]).setColor(0,0.5,0.5).translate([12,0,0])]); 10 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/modules/modulesParametersEx1.scad: -------------------------------------------------------------------------------- 1 | module house(roof="flat",paint=[1,0,0]){ 2 | color(paint) 3 | if(roof=="flat"){ 4 | translate([0,-1,0]) 5 | cube(); 6 | } else if(roof=="pitched"){ 7 | rotate([90,0,0]) 8 | linear_extrude(height=1) 9 | polygon(points=[[0,0],[0,1],[0.5,1.5],[1,1],[1,0]],paths=[ [0,1,2,3,4] ]); 10 | } else if(roof=="domical"){ 11 | translate([0,-1,0]) 12 | union(){ 13 | translate([0.5,0.5,1]) sphere(r=0.5,$fn=20); 14 | cube(); 15 | } 16 | } 17 | } 18 | 19 | union(){ 20 | house(); 21 | translate([2,0,0]) house("pitched"); 22 | translate([4,0,0]) house("domical",[0,1,0]); 23 | translate([6,0,0]) house(roof="pitched",paint=[0,0,1]); 24 | translate([8,0,0]) house(paint=[0,0,0],roof="pitched"); 25 | translate([10,0,0]) house(roof="domical"); 26 | translate([12,0,0]) house(paint=[0,0.5,0.5]); 27 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids.js: -------------------------------------------------------------------------------- 1 | const check = require('./helpers').check 2 | const filedir = 'primitive_solids/' 3 | 4 | exports['test cube'] = function () { 5 | check(filedir, 'cubeEx1') 6 | check(filedir, 'cubeEx2') 7 | } 8 | 9 | exports['test sphere'] = function () { 10 | check(filedir, 'sphereEx1') 11 | check(filedir, 'sphereEx2') 12 | } 13 | 14 | exports['test cylinder'] = function () { 15 | check(filedir, 'cylinderEx1') 16 | check(filedir, 'cylinderEx2') 17 | check(filedir, 'cylinderEx3') 18 | check(filedir, 'cylinderEx5') 19 | } 20 | 21 | exports['test cylinder additional parameters'] = function () { 22 | check(filedir, 'cylinderEx4') 23 | } 24 | 25 | exports['test polyhedron'] = function () { 26 | check(filedir, 'polyhedronEx1') 27 | check(filedir, 'polyhedronEx2') 28 | } 29 | 30 | if (module === require.main) require('test').run(exports) 31 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cubeEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0.5,0.5,0.5],radius: [0.5,0.5,0.5], resolution: 16}); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cubeEx1.scad: -------------------------------------------------------------------------------- 1 | cube(size = 1, center = false); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cubeEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [0.5,1,1.5], resolution: 16}); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cubeEx2.scad: -------------------------------------------------------------------------------- 1 | cube(size = [1,2,3], center = true); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,0], end: [0,0,10],radiusStart: 10, radiusEnd: 20, resolution: 30}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx1.scad: -------------------------------------------------------------------------------- 1 | cylinder(h = 10, r1 = 10, r2 = 20, center = false); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,-5], end: [0,0,5],radiusStart: 20, radiusEnd: 10, resolution: 30}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx2.scad: -------------------------------------------------------------------------------- 1 | cylinder(h = 10, r1 = 20, r2 = 10, center = true); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx3.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,0], end: [0,0,10],radiusStart: 20, radiusEnd: 20, resolution: 30}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx3.scad: -------------------------------------------------------------------------------- 1 | cylinder(h = 10, r=20); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx4.jscad: -------------------------------------------------------------------------------- 1 | function main(){return CSG.cylinder({start: [0,0,0], end: [0,0,10],radiusStart: 20, radiusEnd: 20, resolution: 21});}; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx4.scad: -------------------------------------------------------------------------------- 1 | cylinder(h = 10, r=20, $fs=6); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx5.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,0], end: [0,0,10],radiusStart: 20, radiusEnd: 20, resolution: 100}); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/cylinderEx5.scad: -------------------------------------------------------------------------------- 1 | cylinder(h = 10, r=20, $fn=100); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/polyhedronEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.fromPolygons([new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([0,0,10])),new CSG.Vertex(new CSG.Vector3D([10,-10,0])),new CSG.Vertex(new CSG.Vector3D([10,10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([0,0,10])),new CSG.Vertex(new CSG.Vector3D([-10,-10,0])),new CSG.Vertex(new CSG.Vector3D([10,-10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([0,0,10])),new CSG.Vertex(new CSG.Vector3D([-10,10,0])),new CSG.Vertex(new CSG.Vector3D([-10,-10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([0,0,10])),new CSG.Vertex(new CSG.Vector3D([10,10,0])),new CSG.Vertex(new CSG.Vector3D([-10,10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([-10,10,0])),new CSG.Vertex(new CSG.Vector3D([10,10,0])),new CSG.Vertex(new CSG.Vector3D([10,-10,0]))]),new CSG.Polygon([new CSG.Vertex(new CSG.Vector3D([-10,10,0])),new CSG.Vertex(new CSG.Vector3D([10,-10,0])),new CSG.Vertex(new CSG.Vector3D([-10,-10,0]))])]); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/polyhedronEx1.scad: -------------------------------------------------------------------------------- 1 | polyhedron( 2 | points=[ [10,10,0],[10,-10,0],[-10,-10,0],[-10,10,0], // the four points at base 3 | [0,0,10] ], // the apex point 4 | triangles=[ [0,1,4],[1,2,4],[2,3,4],[3,0,4], // each triangle side 5 | [1,0,3],[2,1,3] ] // two triangles for square base 6 | ); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/polyhedronEx2.scad: -------------------------------------------------------------------------------- 1 | polyhedron 2 | (points = [ 3 | [0, -10, 60], [0, 10, 60], [0, 10, 0], [0, -10, 0], [60, -10, 60], [60, 10, 60], 4 | [10, -10, 50], [10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], [30, 10, 50] 5 | ], 6 | triangles = [ 7 | [0,3,2], [0,2,1], [4,0,5], [5,0,1], [5,2,4], [4,2,3], 8 | [6,8,9], [6,7,8], [6,10,11],[6,11,7], [10,8,11], 9 | [10,9,8], [3,0,9], [9,0,6], [10,6, 0],[0,4,10], 10 | [3,9,10], [3,10,4], [1,7,11], [1,11,5], [1,8,7], 11 | [2,8,1], [8,2,11], [5,11,2] 12 | ] 13 | ); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/sphereEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.sphere({center: [0,0,0], radius: 1, resolution: 5}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/sphereEx1.scad: -------------------------------------------------------------------------------- 1 | sphere(r = 1); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/sphereEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.sphere({center: [0,0,0], radius: 2, resolution: 100}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/primitive_solids/sphereEx2.scad: -------------------------------------------------------------------------------- 1 | // this will create a high resolution sphere with a 2mm radius 2 | sphere(2, $fn=100); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests.js: -------------------------------------------------------------------------------- 1 | const check = require('./helpers').check 2 | const filedir = 'submodule_tests/' 3 | 4 | exports['test transformed submodule'] = function () { 5 | check(filedir, 'transformedSubmoduleEx1') 6 | } 7 | 8 | exports['test transformed submodule with extra line'] = function () { 9 | check(filedir, 'transformedSubmoduleEx2') 10 | } 11 | 12 | exports['test transformed submodule with color mod'] = function () { 13 | check(filedir, 'transformedSubmoduleEx3') 14 | } 15 | 16 | exports['test nested submodules'] = function () { 17 | check(filedir, 'nestedSubmoduleEx1') 18 | check(filedir, 'nestedSubmoduleEx2') 19 | } 20 | 21 | if (module === require.main) require('test').run(exports) 22 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/nestedSubmoduleEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [0.5,1,0.5], resolution: 16}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/nestedSubmoduleEx1.scad: -------------------------------------------------------------------------------- 1 | module mycube2(a,b) { 2 | cube(size=[a, b, 1], center=true); 3 | } 4 | 5 | module mycube(a,b,c) { 6 | mycube2(a,b); 7 | } 8 | 9 | mycube(1,2,3); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/nestedSubmoduleEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [0.5,1,1], resolution: 16}); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/nestedSubmoduleEx2.scad: -------------------------------------------------------------------------------- 1 | module mycube2(a,b) { 2 | module innercube(q1,q2){ 3 | cube(size=[q1, q2, 2], center=true); 4 | } 5 | innercube(a,b); 6 | } 7 | 8 | module mycube1(a,b,c) { 9 | mycube2(a,b); 10 | } 11 | 12 | mycube1(1,2,3); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/transformedSubmoduleEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,-50], end: [0,0,50],radiusStart: 5, radiusEnd: 5, resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [5,5,5], resolution: 16})]).translate([25,0,0]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/transformedSubmoduleEx1.scad: -------------------------------------------------------------------------------- 1 | module hole(size) { 2 | cylinder(r = size, h = 100, center = true); 3 | cube(size=[10, 10, 10], center=true); 4 | } 5 | 6 | translate([25, 0, 0]) { 7 | hole(5); 8 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/transformedSubmoduleEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,-50], end: [0,0,50],radiusStart: 5, radiusEnd: 5, resolution: 16}).union([CSG.cube({center: [0,0,0],radius: [5,5,5], resolution: 16})]).translate([25,0,0]).union([CSG.cube({center: [0,0,0],radius: [5,5,5], resolution: 16})]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/transformedSubmoduleEx2.scad: -------------------------------------------------------------------------------- 1 | module hole(size) { 2 | cylinder(r = size, h = 100, center = true); 3 | cube(size=[10, 10, 10], center=true); 4 | } 5 | 6 | translate([25, 0, 0]) { 7 | hole(5); 8 | } 9 | 10 | cube(size=[10, 10, 10], center=true); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/transformedSubmoduleEx3.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,-50], end: [0,0,50],radiusStart: 5, radiusEnd: 5, resolution: 16}).translate([25,0,0]).union([CSG.cube({center: [0,0,0],radius: [5,5,5], resolution: 16}).setColor(1,0,0).translate([25,0,0])]); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/submodule_tests/transformedSubmoduleEx3.scad: -------------------------------------------------------------------------------- 1 | module hole(size) { 2 | cylinder(r = size, h = 100, center = true); 3 | } 4 | 5 | translate([25, 0, 0]) { 6 | hole(5); 7 | color("red") 8 | cube(size=[10, 10, 10], center=true); 9 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/test.scad: -------------------------------------------------------------------------------- 1 | square([20, 10]); 2 | cube(10); 3 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/testharness.js: -------------------------------------------------------------------------------- 1 | var parser = require('../src/OpenscadOpenjscadParser') 2 | var fs = require('fs') 3 | 4 | var openSCADText = fs.readFileSync(__dirname + '/test.scad', 'UTF8') 5 | var openJSCADResult = parser.parse(openSCADText) 6 | 7 | console.log(openJSCADResult) 8 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert') 2 | const check = require('./helpers').check 3 | const filedir = 'transformations/' 4 | 5 | exports['test scale'] = function () { 6 | check(filedir, 'scaleEx1') 7 | check(filedir, 'scaleEx2') 8 | } 9 | 10 | exports['test rotate'] = function () { 11 | check(filedir, 'rotateEx1') 12 | check(filedir, 'rotateEx2') 13 | } 14 | 15 | exports['test translate'] = function () { 16 | check(filedir, 'translateEx1') 17 | } 18 | 19 | exports['test mirror'] = function () { 20 | check(filedir, 'mirrorEx1') 21 | } 22 | 23 | exports['test multmatrix'] = function () { 24 | check(filedir, 'multmatrixEx1') 25 | check(filedir, 'multmatrixEx2') 26 | } 27 | 28 | exports['test color'] = function () { 29 | check(filedir, 'colorEx1') 30 | check(filedir, 'colorEx1') 31 | } 32 | 33 | /*exports['test minkowski'] = function () { 34 | // todo 35 | //assert.ok(false) 36 | } 37 | 38 | exports['test hull'] = function () { 39 | // todo 40 | //assert.ok(false) 41 | }*/ 42 | 43 | if (module === require.main) require('test').run(exports) 44 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/colorEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [5,5,5],radius: [5,5,5], resolution: 16}).setColor(0.5019607843137255,0,0); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/colorEx1.scad: -------------------------------------------------------------------------------- 1 | color([ 128/255, 0/255, 0/255 ]) 2 | cube(size=[10,10,10],center=false); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/colorEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.sphere({center: [0,0,0], radius: 5, resolution: 16}).setColor(1,0,0); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/colorEx2.scad: -------------------------------------------------------------------------------- 1 | color("red") sphere(5); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/mirrorEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [5,5,5],radius: [5,5,5], resolution: 16}).translate([5,0,-5]).mirrored(CSG.Plane.fromNormalAndPoint([0,1,0], [0,0,0])); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/mirrorEx1.scad: -------------------------------------------------------------------------------- 1 | mirror([ 0, 1, 0 ]) 2 | translate([5, 0, -5]) 3 | cube(size=[10, 10, 10], center=false); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/multmatrixEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,0], end: [0,0,3],radiusStart: 5, radiusEnd: 5, resolution: 16}).transform(new CSG.Matrix4x4( [1,0,0,0,0,1,0,0,0,0,1,0,10,10,10,1] )); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/multmatrixEx1.scad: -------------------------------------------------------------------------------- 1 | multmatrix(m = [ [1, 0, 0, 10], 2 | [0, 1, 0, 10], 3 | [0, 0, 1, 10], 4 | [0, 0, 0, 1] 5 | ]) cylinder(3,5,5); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/multmatrixEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cylinder({start: [0,0,0], end: [0,0,10],radiusStart: 10, radiusEnd: 10, resolution: 30}).union([CSG.cube({center: [5,5,5],radius: [5,5,5], resolution: 16})]).transform(new CSG.Matrix4x4( [0.7071067811865476,0.7071067811865475,0,0,-0.7071067811865475,0.7071067811865476,0,0,0,0,1,0,10,20,0,1] )); 5 | }; -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/multmatrixEx2.scad: -------------------------------------------------------------------------------- 1 | angle=45; 2 | multmatrix(m = [ [cos(angle), -sin(angle), 0, 10], 3 | [sin(angle), cos(angle), 0, 20], 4 | [0, 0, 1, 0], 5 | [0, 0, 0, 1] 6 | ]) union() { 7 | cylinder(r=10.0,h=10,center=false); 8 | cube(size=[10,10,10],center=false); 9 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/rotateEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [5,5,5], resolution: 16}).rotateX(0).rotateY(45).rotateZ(0); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/rotateEx1.scad: -------------------------------------------------------------------------------- 1 | rotate(a=[0,45,0]) { 2 | cube(size=[10, 10, 10], center=true); 3 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/rotateEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [5,5,5],radius: [5,5,5], resolution: 16}).transform(CSG.Matrix4x4.rotation([0,0,0], [1,1,0], 45)); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/rotateEx2.scad: -------------------------------------------------------------------------------- 1 | rotate(a=45, v=[1,1,0]) { 2 | cube(size=[10, 10, 10], center=false); 3 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/scaleEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [5,5,5], resolution: 16}).scale([2,2,2]); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/scaleEx1.scad: -------------------------------------------------------------------------------- 1 | scale(v = [2,2,2]) { 2 | cube(size=[10, 10, 10], center=true); 3 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/scaleEx2.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [0,0,0],radius: [5,5,5], resolution: 16}).scale([2,2,2]); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/scaleEx2.scad: -------------------------------------------------------------------------------- 1 | scale(v = [2,2,2]) 2 | cube(size=[10, 10, 10], center=true); -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/translateEx1.jscad: -------------------------------------------------------------------------------- 1 | function main(){ 2 | 3 | 4 | return CSG.cube({center: [5,5,5],radius: [5,5,5], resolution: 16}).translate([5,0,-5]); 5 | }; 6 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/transformations/translateEx1.scad: -------------------------------------------------------------------------------- 1 | translate(v = [5, 0, -5]) { 2 | cube(size=[10, 10, 10], center=false); 3 | } -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/undef_tests.scad: -------------------------------------------------------------------------------- 1 | echo("*", 3*undef); 2 | echo("!", !undef); 3 | echo("/", 3/undef); 4 | echo("+", 3+undef); 5 | echo("-", 3-undef); 6 | echo("&&", undef&&3); 7 | echo("&&", 3&&undef); 8 | echo("||", undef||3); 9 | echo("||", 3||undef); 10 | echo("%", 3%undef); 11 | echo("<", 3", 3>undef); 15 | echo(">=", 3>=undef); 16 | echo("==", 3==undef); 17 | echo("!=", 3==undef); 18 | 19 | echo( "abs: ",abs(undef)); 20 | echo( "sign: ",sign(undef)); 21 | echo( "cos: ",cos(undef)); 22 | echo( "sin: ",sin(undef)); 23 | echo( "tan: ",tan(undef)); 24 | echo( "acos: ",acos(undef)); 25 | echo( "asin: ",asin(undef)); 26 | echo( "atan: ",atan(undef)); 27 | echo( "atan2: ",atan2(1,undef)); 28 | echo( "atan2: ",atan2(undef,2)); 29 | 30 | echo( "min: ",min(undef,2)); 31 | echo( "max: ",max(undef,2)); 32 | echo( "max: ",max(undef,undef)); 33 | echo( "pow: ",pow(undef,2)); 34 | echo( "sqrt: ",sqrt(undef)); 35 | echo( "ln: ",ln(undef)); 36 | 37 | echo( "ceil: ",ceil(undef)); 38 | echo( "floor: ",floor(undef)); 39 | echo( "log: ",log(undef)); 40 | echo( "log: ",log(undef,2)); 41 | echo( "log: ",log(10,undef)); 42 | echo( "exp: ",exp(undef)); 43 | 44 | echo("sign", sign(undef)); 45 | echo("round", round(undef)); 46 | 47 | 48 | echo("str", str(undef)); 49 | echo("len", len(undef)); 50 | 51 | 52 | sphere(); 53 | -------------------------------------------------------------------------------- /packages/scad-deserializer/tests/vector_matrix_math.scad: -------------------------------------------------------------------------------- 1 | 2 | matrix_3x3 = [[10,20,30],[10,20,30],[10,20,30]]; 3 | vector = [2,0,0]; 4 | matrix_3x2 = [[1,0,0],[1,0,0]]; 5 | 6 | group0 = ["3x3matrix","vector","number","3x2matrix"]; 7 | group1 = [ matrix_3x3, vector, 2, matrix_3x2]; 8 | 9 | function multiply(a,b) = a * b; 10 | function divide(a,b) = a / b; 11 | function add(a,b) = a + b; 12 | function minus(a,b) = a - b; 13 | function modulus(a,b) = a % b; 14 | 15 | function lt(a,b) = a < b; 16 | function lte(a,b) = a <= b; 17 | function eq(a,b) = a == b; 18 | function neq(a,b) = a != b; 19 | function gt(a,b) = a > b; 20 | function gte(a,b) = a >= b; 21 | 22 | group1_len = len(group1); 23 | for (i = [0:group1_len-1]){ 24 | for (j = [0:group1_len-1]){ 25 | echo(str(group0[i]," * ",group0[j], " = "),multiply(group1[i],group1[j])); 26 | echo(str(group0[i]," / ",group0[j], " = "),divide(group1[i],group1[j])); 27 | echo(str(group0[i]," + ",group0[j], " = "),add(group1[i],group1[j])); 28 | echo(str(group0[i]," - ",group0[j], " = "),minus(group1[i],group1[j])); 29 | echo(str(group0[i]," % ",group0[j], " = "),modulus(group1[i],group1[j])); 30 | echo(str(group0[i]," < ",group0[j], " = "),lt(group1[i],group1[j])); 31 | echo(str(group0[i]," <= ",group0[j], " = "),lte(group1[i],group1[j])); 32 | echo(str(group0[i]," == ",group0[j], " = "),eq(group1[i],group1[j])); 33 | echo(str(group0[i]," != ",group0[j], " = "),neq(group1[i],group1[j])); 34 | echo(str(group0[i]," > ",group0[j], " = "),gt(group1[i],group1[j])); 35 | echo(str(group0[i]," >= ",group0[j], " = "),gte(group1[i],group1[j])); 36 | } 37 | } 38 | echo("----"); 39 | echo("!"); 40 | echo("! matrix ", !matrix_3x3); 41 | echo("! vector ", !vector); 42 | echo("----"); 43 | 44 | echo("?:"); 45 | echo("?: matrix ", matrix_3x3?true:false); 46 | echo("?: vextor ", vector?true:false); 47 | echo("----"); 48 | 49 | cube(); -------------------------------------------------------------------------------- /packages/stl-deserializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/stl-deserializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/stl-deserializer 2 | 3 | > stl deserializer for the jscad project 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fstl-deserializer.svg)](https://badge.fury.io/js/%40jscad%2Fstl-deserializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/stl-deserializer) 7 | 8 | ## Overview 9 | 10 | This deserializer converts raw stl data to jscad code (that can be evaluated to CSG/CAG). 11 | 12 | ## Table of Contents 13 | 14 | - [Installation](#installation) 15 | - [Usage](#usage) 16 | - [Contribute](#contribute) 17 | - [License](#license) 18 | 19 | 20 | ## Installation 21 | 22 | ``` 23 | npm install @jscad/stl-deserializer 24 | ``` 25 | 26 | ## Usage 27 | 28 | 29 | ```javascript 30 | const stlDeSerializer = require('@jscad/stl-deserializer') 31 | 32 | const rawData = fs.readFileSync('PATH/TO/file.stl') 33 | const csgData = stlDeSerializer.deserialize(rawData, undefined, {output: 'csg'}) 34 | 35 | ``` 36 | 37 | 38 | ## Contribute 39 | 40 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 41 | 42 | PRs accepted. 43 | 44 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 45 | 46 | 47 | ## License 48 | 49 | [The MIT License (MIT)](./LICENSE) 50 | (unless specified otherwise) 51 | -------------------------------------------------------------------------------- /packages/stl-deserializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/stl-deserializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Stl deserializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test/test.js' --verbose --timeout 10000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "deserializer", 32 | "stl" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "0.7.0", 37 | "@jscad/io-utils": "^1.0.0-alpha.d0fb3056" 38 | }, 39 | "devDependencies": { 40 | "ava": "^0.25.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /packages/stl-deserializer/vt2jscad.js: -------------------------------------------------------------------------------- 1 | // positions, triangles, normals and colors 2 | function vt2jscad (positions, triangles, normals, colors) { 3 | let src = '' 4 | src += 'polyhedron({ points: [\n ' 5 | for (let i = 0, j = 0; i < positions.length; i++) { 6 | if (j++) src += ',\n ' 7 | src += '[' + positions[i] + ']' // .join(", "); 8 | } 9 | src += '],\n polygons: [\n ' 10 | for (let i = 0, j = 0; i < triangles.length; i++) { 11 | if (j++) src += ',\n ' 12 | src += '[' + triangles[i] + ']' // .join(', '); 13 | } 14 | if (colors && triangles.length === colors.length) { 15 | src += '],\n\tcolors: [\n ' 16 | for (let i = 0, j = 0; i < colors.length; i++) { 17 | if (j++) src += ',\n ' 18 | src += '[' + colors[i] + ']' // .join(', '); 19 | } 20 | } 21 | src += '] })\n' 22 | return src 23 | } 24 | 25 | module.exports = { 26 | vt2jscad 27 | } 28 | -------------------------------------------------------------------------------- /packages/stl-serializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/stl-serializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | # [0.2.0](https://github.com/jscad/io/compare/@jscad/stl-serializer@0.1.3...@jscad/stl-serializer@0.2.0) (2018-11-25) 8 | 9 | 10 | 11 | ## [0.1.3](https://github.com/jscad/io/compare/@jscad/stl-serializer@0.1.2...@jscad/stl-serializer@0.1.3) (2018-09-02) 12 | 13 | 14 | ### Bug Fixes 15 | 16 | * **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)) 17 | 18 | 19 | 20 | 21 | 22 | ## [0.1.2](https://github.com/jscad/io/compare/@jscad/stl-serializer@0.1.1...@jscad/stl-serializer@0.1.2) (2018-03-15) 23 | 24 | 25 | 26 | 27 | **Note:** Version bump only for package @jscad/stl-serializer 28 | 29 | 30 | ## [0.1.1](https://github.com/jscad/io/compare/@jscad/stl-serializer@0.1.0...@jscad/stl-serializer@0.1.1) (2017-12-14) 31 | 32 | 33 | 34 | 35 | **Note:** Version bump only for package @jscad/stl-serializer 36 | 37 | 38 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/stl-serializer@0.0.6...@jscad/stl-serializer@0.1.0) (2017-11-29) 39 | 40 | 41 | ### Features 42 | 43 | * add support for a status callback for de/serialization progress ([#49](https://github.com/jscad/io/issues/49)) ([f457cdb](https://github.com/jscad/io/commit/f457cdb)) 44 | 45 | 46 | 47 | 48 | 49 | ## [0.0.6](https://github.com/jscad/io/compare/@jscad/stl-serializer@0.0.5...@jscad/stl-serializer@0.0.6) (2017-11-04) 50 | 51 | 52 | 53 | 54 | **Note:** Version bump only for package @jscad/stl-serializer 55 | 56 | 57 | ## [0.0.5](https://github.com/jscad/io/compare/@jscad/stl-serializer@0.0.4...@jscad/stl-serializer@0.0.5) (2017-10-30) 58 | 59 | 60 | 61 | 62 | **Note:** Version bump only for package @jscad/stl-serializer 63 | 64 | 65 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/stl-serializer@0.0.3...@jscad/stl-serializer@0.0.4) (2017-10-10) 66 | 67 | 68 | 69 | 70 | **Note:** Version bump only for package @jscad/stl-serializer 71 | 72 | 73 | ## 0.0.3 (2017-10-10) 74 | 75 | 76 | 77 | 78 | **Note:** Version bump only for package @jscad/stl-serializer 79 | -------------------------------------------------------------------------------- /packages/stl-serializer/CSGToStla.js: -------------------------------------------------------------------------------- 1 | const { ensureManifoldness } = require('@jscad/io-utils') 2 | const {isCSG} = require('@jscad/csg') 3 | 4 | // objects must be an array of CSG objects (with polygons) 5 | const serializeText = (objects, options) => { 6 | options.statusCallback && options.statusCallback({progress: 0}) 7 | 8 | let result = `solid JSCAD 9 | ${convertToStl(objects, options)} 10 | endsolid JSCAD 11 | ` 12 | options.statusCallback && options.statusCallback({progress: 100}) 13 | return [result] 14 | } 15 | 16 | const convertToStl = (objects, options) => { 17 | let result = [] 18 | objects.forEach(function (object, i) { 19 | result.push(convertToFacets(object, options)) 20 | options.statusCallback && options.statusCallback({progress: 100 * i / objects.length}) 21 | }) 22 | return result.join('\n') 23 | } 24 | 25 | const convertToFacets = (object, options) => { 26 | let result = [] 27 | object.polygons.forEach(function (p, i) { 28 | result.push(convertToFacet(p)) 29 | }) 30 | return result.join('\n') 31 | } 32 | 33 | const vector3DtoStlString = (v) => { 34 | return `${v._x} ${v._y} ${v._z}` 35 | } 36 | 37 | const vertextoStlString = (vertex) => { 38 | return `vertex ${vector3DtoStlString(vertex.pos)}` 39 | } 40 | 41 | const convertToFacet = (polygon) => { 42 | let result = [] 43 | if (polygon.vertices.length >= 3) { 44 | // STL requires triangular polygons. If our polygon has more vertices, create multiple triangles: 45 | let firstVertexStl = vertextoStlString(polygon.vertices[0]) 46 | for (let i = 0; i < polygon.vertices.length - 2; i++) { 47 | let facet = `facet normal ${vector3DtoStlString(polygon.plane.normal)} 48 | outer loop 49 | ${firstVertexStl} 50 | ${vertextoStlString(polygon.vertices[i + 1])} 51 | ${vertextoStlString(polygon.vertices[i + 2])} 52 | endloop 53 | endfacet` 54 | result.push(facet) 55 | } 56 | } 57 | return result.join('\n') 58 | } 59 | 60 | module.exports = { 61 | serializeText 62 | } 63 | -------------------------------------------------------------------------------- /packages/stl-serializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/stl-serializer 2 | 3 | > stl serializer for the jscad project (from CSG) 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fstl-serializer.svg)](https://badge.fury.io/js/%40jscad%2Fstl-serializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/stl-serializer) 7 | 8 | ## Overview 9 | 10 | This serializer outputs a 'blobable' array of data (from a CSG object) 11 | ie an array that can either be passed directly to a Blob (`new Blob(blobable)`) 12 | or converted to a Node.js buffer. 13 | 14 | 15 | ## Table of Contents 16 | 17 | - [Installation](#installation) 18 | - [Usage](#usage) 19 | - [Contribute](#contribute) 20 | - [License](#license) 21 | 22 | 23 | ## Installation 24 | 25 | ``` 26 | npm install @jscad/stl-serializer 27 | ``` 28 | 29 | ## Usage 30 | 31 | 32 | ```javascript 33 | const stlSerializer = require('@jscad/stl-serializer') 34 | 35 | const rawData = stlSerializer.serialize(CSGObject) 36 | 37 | //in browser (with browserify etc) 38 | const blob = new Blob(rawData) 39 | 40 | ``` 41 | 42 | 43 | ## Contribute 44 | 45 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 46 | 47 | PRs accepted. 48 | 49 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 50 | 51 | 52 | ## License 53 | 54 | [The MIT License (MIT)](./LICENSE) 55 | (unless specified otherwise) 56 | -------------------------------------------------------------------------------- /packages/stl-serializer/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | JSCAD Object to STL Format Serialization 3 | 4 | ## License 5 | 6 | Copyright (c) 2018 JSCAD Organization https://github.com/jscad 7 | 8 | All code released under MIT license 9 | 10 | Notes: 11 | 1) CAG conversion to: 12 | none 13 | 2) CSG conversion to: 14 | STL mesh 15 | 3) Path2D conversion to: 16 | none 17 | */ 18 | 19 | const {serializeBinary} = require('./CSGToStlb') 20 | const {serializeText} = require('./CSGToStla') 21 | 22 | const {ensureManifoldness} = require('@jscad/io-utils') 23 | const {toArray} = require('@jscad/io-utils/arrays') 24 | const {isCSG} = require('@jscad/csg') 25 | 26 | const mimeType = 'application/sla' 27 | 28 | const serialize = (...params) => { 29 | let options = {} 30 | let objects 31 | if (params.length === 0) { 32 | throw new Error('no arguments supplied to serialize function !') 33 | } else if (params.length === 1) { 34 | // assumed to be object(s) 35 | objects = Array.isArray(params[0]) ? params[0] : params 36 | } else if (params.length > 1) { 37 | options = params[0] 38 | objects = params[1] 39 | } 40 | // make sure we always deal with arrays of objects as inputs 41 | objects = toArray(objects) 42 | 43 | const defaults = { 44 | binary: true, 45 | statusCallback: null 46 | } 47 | options = Object.assign({}, defaults, options) 48 | 49 | // only use valid CSG objects 50 | let csgs = [] 51 | objects.forEach(function (object, i) { 52 | if (isCSG(object) & object.polygons.length > 0) { 53 | csgs.push(ensureManifoldness(object)) 54 | } 55 | }) 56 | return options.binary ? serializeBinary(csgs, options) : serializeText(csgs, options) 57 | } 58 | 59 | module.exports = { 60 | mimeType, 61 | serialize 62 | } 63 | -------------------------------------------------------------------------------- /packages/stl-serializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/stl-serializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Stl serializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 10000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "serializer", 32 | "stl" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "0.7.0", 37 | "@jscad/io-utils": "^1.0.0-alpha.d0fb3056" 38 | }, 39 | "devDependencies": { 40 | "ava": "^0.25.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /packages/svg-deserializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/svg-deserializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/svg-deserializer 2 | 3 | > svg deserializer for the jscad project 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fsvg-deserializer.svg)](https://badge.fury.io/js/%40jscad%2Fsvg-deserializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/svg-deserializer) 7 | 8 | ## Overview 9 | 10 | This deserializer converts raw svg data to jscad code (that can be evaluated to CSG/CAG). 11 | 12 | ## Table of Contents 13 | 14 | - [Installation](#installation) 15 | - [Usage](#usage) 16 | - [Contribute](#contribute) 17 | - [Acknowledgements](#acknowledgements) 18 | - [Sponsors](#sponsors) 19 | - [License](#license) 20 | 21 | 22 | ## Installation 23 | 24 | ``` 25 | npm install @jscad/svg-deserializer 26 | ``` 27 | 28 | ## Usage 29 | 30 | 31 | ```javascript 32 | const svgDeSerializer = require('@jscad/svg-deserializer').deserialize 33 | 34 | const rawData = fs.readFileSync('PATH/TO/file.svg') 35 | const cagData = svgDeSerializer.deserialize(rawData, undefined, {output: 'csg'}) 36 | 37 | ``` 38 | 39 | 40 | ## Contribute 41 | 42 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 43 | 44 | PRs accepted. 45 | 46 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 47 | 48 | ## Acknowledgements 49 | 50 | OpenJSCAD and its sub components are built upon great open source work, contribution & modules. 51 | 52 | ## Sponsors 53 | 54 | * The upgrades (direct CSG output from this deserializer) and refactoring have been very kindly sponsored by [Copenhagen Fabrication / Stykka](https://www.stykka.com/) 55 | 56 | ## License 57 | 58 | [The MIT License (MIT)](./LICENSE) 59 | (unless specified otherwise) 60 | -------------------------------------------------------------------------------- /packages/svg-deserializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/svg-deserializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Svg deserializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js'", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "deserializer", 32 | "svg" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "0.7.0", 37 | "sax": "1.2.4" 38 | }, 39 | "devDependencies": { 40 | "ava": "^0.25.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /packages/svg-serializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/svg-serializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | # [0.2.0](https://github.com/jscad/io/compare/@jscad/svg-serializer@0.1.3...@jscad/svg-serializer@0.2.0) (2018-11-25) 8 | 9 | 10 | 11 | 12 | ## [0.1.3](https://github.com/jscad/io/compare/@jscad/svg-serializer@0.1.2...@jscad/svg-serializer@0.1.3) (2018-09-02) 13 | 14 | 15 | ### Bug Fixes 16 | 17 | * **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)) 18 | 19 | 20 | 21 | 22 | 23 | ## [0.1.2](https://github.com/jscad/io/compare/@jscad/svg-serializer@0.1.1...@jscad/svg-serializer@0.1.2) (2018-04-01) 24 | 25 | 26 | ### Bug Fixes 27 | 28 | * **svg-serializer:** return array instead of single item ([#60](https://github.com/jscad/io/issues/60)) ([28570a9](https://github.com/jscad/io/commit/28570a9)) 29 | 30 | 31 | 32 | 33 | 34 | ## [0.1.1](https://github.com/jscad/io/compare/@jscad/svg-serializer@0.1.0...@jscad/svg-serializer@0.1.1) (2017-12-14) 35 | 36 | 37 | 38 | 39 | **Note:** Version bump only for package @jscad/svg-serializer 40 | 41 | 42 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/svg-serializer@0.0.5...@jscad/svg-serializer@0.1.0) (2017-11-29) 43 | 44 | 45 | ### Features 46 | 47 | * add support for a status callback for de/serialization progress ([#49](https://github.com/jscad/io/issues/49)) ([f457cdb](https://github.com/jscad/io/commit/f457cdb)) 48 | 49 | 50 | 51 | 52 | 53 | ## [0.0.5](https://github.com/jscad/io/compare/@jscad/svg-serializer@0.0.4...@jscad/svg-serializer@0.0.5) (2017-11-20) 54 | 55 | 56 | 57 | 58 | **Note:** Version bump only for package @jscad/svg-serializer 59 | 60 | 61 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/svg-serializer@0.0.3...@jscad/svg-serializer@0.0.4) (2017-11-04) 62 | 63 | 64 | 65 | 66 | **Note:** Version bump only for package @jscad/svg-serializer 67 | 68 | 69 | ## [0.0.3](https://github.com/jscad/io/compare/@jscad/svg-serializer@0.0.2...@jscad/svg-serializer@0.0.3) (2017-10-10) 70 | 71 | 72 | 73 | 74 | **Note:** Version bump only for package @jscad/svg-serializer 75 | 76 | 77 | ## 0.0.2 (2017-10-10) 78 | 79 | 80 | 81 | 82 | **Note:** Version bump only for package @jscad/svg-serializer 83 | -------------------------------------------------------------------------------- /packages/svg-serializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/svg-serializer 2 | 3 | > svg serializer for the jscad project (from CSG) 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fsvg-serializer.svg)](https://badge.fury.io/js/%40jscad%2Fsvg-serializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/svg-serializer) 7 | 8 | ## Overview 9 | 10 | This serializer outputs a 'blobable' array of data (from a CAG object) 11 | ie an array that can either be passed directly to a Blob (`new Blob(blobable)`) 12 | or converted to a Node.js buffer. 13 | 14 | 15 | ## Table of Contents 16 | 17 | - [Installation](#installation) 18 | - [Usage](#usage) 19 | - [Contribute](#contribute) 20 | - [License](#license) 21 | 22 | 23 | ## Installation 24 | 25 | ``` 26 | npm install @jscad/svg-serializer 27 | ``` 28 | 29 | ## Usage 30 | 31 | 32 | ```javascript 33 | const svgSerializer = require('@jscad/svg-serializer') 34 | 35 | const rawData = svgSerializer.serialize(CAGObject) 36 | 37 | //in browser (with browserify etc) 38 | const blob = new Blob(rawData) 39 | 40 | ``` 41 | 42 | 43 | ## Contribute 44 | 45 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 46 | 47 | PRs accepted. 48 | 49 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 50 | 51 | 52 | ## License 53 | 54 | [The MIT License (MIT)](./LICENSE) 55 | (unless specified otherwise) 56 | -------------------------------------------------------------------------------- /packages/svg-serializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/svg-serializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "Svg serializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 10000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "serializer", 32 | "svg" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "0.7.0", 37 | "@jscad/io-utils": "^1.0.0-alpha.d0fb3056", 38 | "onml": "^0.4.1" 39 | }, 40 | "devDependencies": { 41 | "ava": "^0.25.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /packages/x3d-serializer/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/x3d-serializer/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. 5 | 6 | 7 | # [0.2.0](https://github.com/jscad/io/compare/@jscad/x3d-serializer@0.1.2...@jscad/x3d-serializer@0.2.0) (2018-11-25) 8 | 9 | 10 | 11 | 12 | ## [0.1.2](https://github.com/jscad/io/compare/@jscad/x3d-serializer@0.1.1...@jscad/x3d-serializer@0.1.2) (2018-09-02) 13 | 14 | 15 | ### Bug Fixes 16 | 17 | * **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)) 18 | 19 | 20 | 21 | 22 | 23 | ## [0.1.1](https://github.com/jscad/io/compare/@jscad/x3d-serializer@0.1.0...@jscad/x3d-serializer@0.1.1) (2017-12-14) 24 | 25 | 26 | 27 | 28 | **Note:** Version bump only for package @jscad/x3d-serializer 29 | 30 | 31 | # [0.1.0](https://github.com/jscad/io/compare/@jscad/x3d-serializer@0.0.6...@jscad/x3d-serializer@0.1.0) (2017-11-29) 32 | 33 | 34 | ### Features 35 | 36 | * add support for a status callback for de/serialization progress ([#49](https://github.com/jscad/io/issues/49)) ([f457cdb](https://github.com/jscad/io/commit/f457cdb)) 37 | 38 | 39 | 40 | 41 | 42 | ## [0.0.6](https://github.com/jscad/io/compare/@jscad/x3d-serializer@0.0.5...@jscad/x3d-serializer@0.0.6) (2017-11-04) 43 | 44 | 45 | 46 | 47 | **Note:** Version bump only for package @jscad/x3d-serializer 48 | 49 | 50 | ## [0.0.5](https://github.com/jscad/io/compare/@jscad/x3d-serializer@0.0.4...@jscad/x3d-serializer@0.0.5) (2017-10-30) 51 | 52 | 53 | 54 | 55 | **Note:** Version bump only for package @jscad/x3d-serializer 56 | 57 | 58 | ## [0.0.4](https://github.com/jscad/io/compare/@jscad/x3d-serializer@0.0.3...@jscad/x3d-serializer@0.0.4) (2017-10-10) 59 | 60 | 61 | 62 | 63 | **Note:** Version bump only for package @jscad/x3d-serializer 64 | 65 | 66 | ## 0.0.3 (2017-10-10) 67 | 68 | 69 | 70 | 71 | **Note:** Version bump only for package @jscad/x3d-serializer 72 | -------------------------------------------------------------------------------- /packages/x3d-serializer/README.md: -------------------------------------------------------------------------------- 1 | ## @jscad/x3d-serializer 2 | 3 | > x3d serializer for the jscad project (from CSG) 4 | 5 | [![npm version](https://badge.fury.io/js/%40jscad%2Fx3d-serializer.svg)](https://badge.fury.io/js/%40jscad%2Fx3d-serializer) 6 | [![Build Status](https://travis-ci.org/jscad/io.svg)](https://travis-ci.org/jscad/x3d-serializer) 7 | 8 | ## Overview 9 | 10 | This serializer outputs a 'blobable' array of data (from a CSG object) 11 | ie an array that can either be passed directly to a Blob (`new Blob(blobable)`) 12 | or converted to a Node.js buffer. 13 | 14 | ## Table of Contents 15 | 16 | - [Installation](#installation) 17 | - [Usage](#usage) 18 | - [Contribute](#contribute) 19 | - [License](#license) 20 | 21 | 22 | ## Installation 23 | 24 | ``` 25 | npm install @jscad/x3d-serializer 26 | ``` 27 | 28 | ## Usage 29 | 30 | 31 | ```javascript 32 | const x3dSerializer = require('@jscad/x3d-serializer') 33 | 34 | const rawData = x3dSerializer.serialize(CSGObject) 35 | 36 | //in browser (with browserify etc) 37 | const blob = new Blob(rawData) 38 | 39 | ``` 40 | 41 | 42 | ## Contribute 43 | 44 | For questions about the API, please contact the [User Group](https://plus.google.com/communities/114958480887231067224) 45 | 46 | PRs accepted. 47 | 48 | Small Note: If editing this README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 49 | 50 | 51 | ## License 52 | 53 | [The MIT License (MIT)](./LICENSE) 54 | (unless specified otherwise) 55 | -------------------------------------------------------------------------------- /packages/x3d-serializer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@jscad/x3d-serializer", 3 | "version": "1.0.0-alpha.d0fb3056", 4 | "description": "X3d serializer for jscad project", 5 | "repository": "https://github.com/jscad/io", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "ava './test.js' --verbose --timeout 10000", 9 | "release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 10 | "release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ", 11 | "release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags " 12 | }, 13 | "contributors": [ 14 | { 15 | "name": "Rene K. Mueller", 16 | "url": "http://renekmueller.com" 17 | }, 18 | { 19 | "name": "z3dev", 20 | "url": "http://www.z3d.jp" 21 | }, 22 | { 23 | "name": "Mark 'kaosat-dev' Moissette", 24 | "url": "http://kaosat.net" 25 | } 26 | ], 27 | "keywords": [ 28 | "openjscad", 29 | "jscad", 30 | "csg", 31 | "serializer", 32 | "x3d" 33 | ], 34 | "license": "MIT", 35 | "dependencies": { 36 | "@jscad/csg": "0.7.0", 37 | "@jscad/io-utils": "^1.0.0-alpha.d0fb3056", 38 | "onml": "^0.4.1" 39 | }, 40 | "devDependencies": { 41 | "ava": "^0.25.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /packages/x3d-serializer/test.js: -------------------------------------------------------------------------------- 1 | const test = require('ava') 2 | const {CSG} = require('@jscad/csg') 3 | const serializer = require('./index.js') 4 | 5 | test('serialize CSG objects to X3D', function (t) { 6 | const csg1 = new CSG() 7 | 8 | const observed1 = serializer.serialize({}, csg1) 9 | t.deepEqual(observed1, [expected1]) 10 | 11 | const csg2 = new CSG.cube() 12 | 13 | const observed2 = serializer.serialize({}, csg2) 14 | t.deepEqual(observed2, [expected2]) 15 | 16 | const csg3 = new CSG.cube({center: [5, 5, 5]}).setColor([0.5, 1, 0.5]) 17 | 18 | const observed3 = serializer.serialize({}, [csg2, csg3]) 19 | t.deepEqual(observed3, [expected3]) 20 | }) 21 | 22 | const expected1 = ` 23 | 24 | 25 | 26 | 27 | 28 | 29 | ` 30 | 31 | const expected2 = ` 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | ` 46 | 47 | const expected3 = ` 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | ` 68 | -------------------------------------------------------------------------------- /test/helpers/nearlyEqual.js: -------------------------------------------------------------------------------- 1 | // Compare two numeric values for near equality. 2 | // the given test is fails if the numeric values are outside the given epsilon 3 | function nearlyEqual (t, a, b, epsilon, failMessage) { 4 | if (a === b) { // shortcut, also handles infinities and NaNs 5 | return true 6 | } 7 | 8 | var absA = Math.abs(a) 9 | var absB = Math.abs(b) 10 | var diff = Math.abs(a - b) 11 | if (a === 0 || b === 0 || diff < Number.EPSILON) { 12 | // a or b is zero or both are extremely close to it 13 | // relative error is less meaningful here 14 | if (diff > (epsilon * Number.EPSILON)) { 15 | failMessage = failMessage === undefined ? 'near zero Numbers outside of epsilon' : failMessage 16 | t.fail(failMessage+"("+a+","+b+")") 17 | } 18 | } 19 | // use relative error 20 | if ((diff / Math.min((absA + absB), Number.MAX_VALUE)) > epsilon) { 21 | failMessage = failMessage === undefined ? 'Numbers outside of epsilon' : failMessage 22 | t.fail(failMessage+"("+a+","+b+")") 23 | } 24 | } 25 | 26 | module.exports = { 27 | nearlyEqual: nearlyEqual 28 | } 29 | --------------------------------------------------------------------------------