Skip to content

Commit 272ce63

Browse files
committed
feat(examples): corrected import examples to use ES imports and IO deserialize
1 parent 95a721c commit 272ce63

File tree

8 files changed

+93
-31377
lines changed

8 files changed

+93
-31377
lines changed

packages/examples/import/AMFImport/Rook.amf

Lines changed: 0 additions & 31324 deletions
This file was deleted.

packages/examples/import/AMFImport/index.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/examples/import/AMFImport/package.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/examples/import/STLImport/index.js

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,48 @@
77
* @authors Simon Clark
88
* @licence MIT License
99
*/
10+
import fs from 'fs'
11+
import path from 'path'
12+
import { fileURLToPath } from 'url'
1013

11-
const { translate, scale, rotateZ } = require('@jscad/modeling').transforms
12-
const { union } = require('@jscad/modeling').booleans
14+
import { translate, scale, rotateZ } from '@jscad/modeling'
15+
import { union } from '@jscad/modeling'
1316

14-
// Load the STL files using require
15-
const sculpture = require('./3d_sculpture-VernonBussler.stl')
16-
const frog = require('./frog-OwenCollins.stl')
17+
import { deserialize } from '@jscad/io'
1718

18-
const main = () => union(
19-
translate([0, 0, 13], rotateZ(-Math.PI / 3, scale([0.25, 0.25, 0.25], frog))),
20-
translate([-5, 6, 0], sculpture)
21-
)
19+
/*
20+
* Load the STL mesh from the given file.
21+
*
22+
* NOTE: This function is required in V3 as imports of non-standard file types are not possible
23+
*/
24+
const loadStl = (filepath) => {
25+
const mimeType = 'model/stl'
26+
27+
let fullpath = filepath
28+
if (!path.isAbsolute(fullpath)) {
29+
// Get the directory name of the current module
30+
const filename = fileURLToPath(import.meta.url)
31+
const dirname = path.dirname(filename)
32+
33+
fullpath = path.join(dirname, filepath)
34+
}
35+
36+
const results = fs.readFileSync(fullpath)
37+
const content = results.buffer
38+
? results.buffer.slice(results.byteOffset, results.byteOffset + results.length)
39+
: results
40+
41+
return deserialize({output: 'geometry'}, mimeType, content)
42+
}
43+
44+
// Load the STL files
45+
const sculpture = loadStl('./3d_sculpture-VernonBussler.stl')
46+
const frog = loadStl('./frog-OwenCollins.stl')
47+
48+
export const main = () => {
49+
return union(
50+
translate([0, 0, 13], rotateZ(-Math.PI / 3, scale([0.25, 0.25, 0.25], frog))),
51+
translate([-5, 6, 0], sculpture)
52+
)
53+
}
2254

23-
module.exports = { main }

packages/examples/import/STLImport/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
],
1010
"author": "Simon Clark",
1111
"license": "MIT",
12-
"dependencies": {}
12+
"dependencies": {},
13+
"type": "module"
1314
}

packages/examples/import/SVGImport/index.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,47 @@
77
* @authors Simon Clark
88
* @licence MIT License
99
*/
10+
import fs from 'fs'
11+
import path from 'path'
12+
import { fileURLToPath } from 'url'
1013

11-
const { translate } = require('@jscad/modeling').transforms
12-
const { extrudeLinear } = require('@jscad/modeling').extrusions
13-
const { polygon } = require('@jscad/modeling').primitives
14+
import { translate } from '@jscad/modeling'
15+
import { extrudeLinear } from '@jscad/modeling'
16+
import { polygon } from '@jscad/modeling'
1417

15-
// Load the SVG files using require
16-
const panda = require('./babypanda2.svg')
18+
import { deserialize } from '@jscad/io'
1719

18-
const main = () => {
19-
// SVG shapes are imported as an array of paths. We want to convert those to polygons to extrude.
20-
const poly = panda.map((shape) => polygon({ points: shape.points }))
21-
return translate([-40, 50, 0], extrudeLinear({ height: 2 }, poly))
20+
/*
21+
* Load the SVG outlines from the given file.
22+
*
23+
* NOTE: This function is required in V3 as imports of non-standard file types are not possible
24+
*/
25+
const loadSvg = (filepath) => {
26+
const mimeType = 'image/svg+xml'
27+
28+
let fullpath = filepath
29+
if (!path.isAbsolute(fullpath)) {
30+
// Get the directory name of the current module
31+
const filename = fileURLToPath(import.meta.url)
32+
const dirname = path.dirname(filename)
33+
34+
fullpath = path.join(dirname, filepath)
35+
}
36+
37+
const results = fs.readFileSync(fullpath)
38+
const content = results.buffer
39+
? results.buffer.slice(results.byteOffset, results.byteOffset + results.length)
40+
: results
41+
42+
return deserialize({output: 'geometry'}, mimeType, content)
2243
}
2344

24-
module.exports = { main }
45+
// Load the SVG files
46+
const panda = loadSvg('./babypanda2.svg')
47+
48+
export const main = () => {
49+
// SVG shapes are imported as an array of paths. So, convert the paths to polygons.
50+
const poly = panda.map((path) => polygon({ points: path.points }))
51+
52+
return translate([-40, 50, 0], extrudeLinear({ height: 2 }, poly))
53+
}

packages/examples/import/SVGImport/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "svg-import",
3-
"version": "0.0.1",
4-
"description": "Demonstrating SVG import. Drag the SVGImport folder onto the JSCAD instance.",
3+
"version": "3.0.1",
4+
"description": "Demonstrating SVG import",
55
"main": "index.js",
6+
"type": "module",
67
"keywords": [
78
"jscad",
8-
"cad"
9+
"cad",
10+
"svg"
911
],
1012
"author": "Simon Clark",
1113
"license": "MIT",

packages/examples/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Example Files for JSCAD",
55
"homepage": "https://openjscad.xyz/",
66
"repository": "https://github.com/jscad/OpenJSCAD.org",
7+
"type": "module",
78
"scripts": {},
89
"private": true,
910
"contributors": [
@@ -31,5 +32,10 @@
3132
"type": "opencollective",
3233
"url": "https://opencollective.com/openjscad",
3334
"logo": "https://opencollective.com/openjscad/logo.txt"
35+
},
36+
"devDependencies": {
37+
"@jscad/cli": "workspace:3.0.3-alpha.0",
38+
"@jscad/modeling": "workspace:3.0.3-alpha.0",
39+
"@jscad/io": "workspace:3.0.3-alpha.0"
3440
}
3541
}

0 commit comments

Comments
 (0)