Skip to content

Commit

Permalink
multifile examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgdavor committed Dec 30, 2023
1 parent ca5cdab commit ac4c6b7
Show file tree
Hide file tree
Showing 14 changed files with 31,457 additions and 7 deletions.
31,324 changes: 31,324 additions & 0 deletions apps/jscad-web/examples/AMFImport/Rook.amf

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions apps/jscad-web/examples/AMFImport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* AMF Import Demonstration
* @category Imports
* @skillLevel 1
* @description Importing AMF files. Drag the whole AMFImport folder into JSCAD
* @tags amf, import
* @authors Simon Clark
* @licence MIT License
*/

// Load the AMF files using require
const rook = require('./Rook.amf')

const main = () => rook

module.exports = { main }
13 changes: 13 additions & 0 deletions apps/jscad-web/examples/AMFImport/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "amf-import",
"version": "0.0.1",
"description": "Demonstrating AMF import. Drag the AMFImport folder onto the JSCAD instance.",
"main": "index.js",
"keywords": [
"jscad",
"cad"
],
"author": "Simon Clark",
"license": "MIT",
"dependencies": {}
}
Binary file not shown.
Binary file not shown.
23 changes: 23 additions & 0 deletions apps/jscad-web/examples/STLImport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* STL Import Demonstration
* @category Imports
* @skillLevel 1
* @description Importing STL files. Drag the whole STLImport folder into JSCAD
* @tags stl, import
* @authors Simon Clark
* @licence MIT License
*/

const { translate, scale, rotateZ } = require('@jscad/modeling').transforms
const { union } = require('@jscad/modeling').booleans

// Load the STL files using require
const sculpture = require('./3d_sculpture-VernonBussler.stl')
const frog = require('./frog-OwenCollins.stl')

const main = () => union(
translate([0, 0, 13], rotateZ(-Math.PI / 3, scale([0.25, 0.25, 0.25], frog))),
translate([-5, 6, 0], sculpture)
)

module.exports = { main }
13 changes: 13 additions & 0 deletions apps/jscad-web/examples/STLImport/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "stl-import",
"version": "0.0.1",
"description": "Demonstrating STL file import. Drag the STLImport folder onto the JSCAD instance.",
"main": "index.js",
"keywords": [
"jscad",
"cad"
],
"author": "Simon Clark",
"license": "MIT",
"dependencies": {}
}
17 changes: 17 additions & 0 deletions apps/jscad-web/examples/SVGImport/babypanda2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions apps/jscad-web/examples/SVGImport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* SVG Import Demonstration
* @category Imports
* @skillLevel 1
* @description Importing SVG files. Drag the whole SVGImport folder into JSCAD
* @tags svg, import
* @authors Simon Clark
* @licence MIT License
*/

const { translate } = require('@jscad/modeling').transforms
const { extrudeLinear } = require('@jscad/modeling').extrusions
const { polygon } = require('@jscad/modeling').primitives

// Load the SVG files using require
const panda = require('./babypanda2.svg')

const main = () => {
// SVG shapes are imported as an array of paths. We want to convert those to polygons to extrude.
const poly = panda.map((shape) => polygon({ points: shape.points }))
return translate([-40, 50, 0], extrudeLinear({ height: 2 }, poly))
}

module.exports = { main }
13 changes: 13 additions & 0 deletions apps/jscad-web/examples/SVGImport/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "svg-import",
"version": "0.0.1",
"description": "Demonstrating SVG import. Drag the SVGImport folder onto the JSCAD instance.",
"main": "index.js",
"keywords": [
"jscad",
"cad"
],
"author": "Simon Clark",
"license": "MIT",
"dependencies": {}
}
11 changes: 7 additions & 4 deletions apps/jscad-web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { ViewState } from './src/viewState.js'
import * as welcome from './src/welcome.js'

export const byId = id => document.getElementById(id)
const toUrl = path => new URL(path, document.baseURI).toString()
const appBase = document.baseURI
let currentBase = appBase
const toUrl = path => new URL(path, appBase).toString()

const viewState = new ViewState()

Expand Down Expand Up @@ -193,16 +195,17 @@ const paramChangeCallback = async params => {
if(lastParams && lastParams != params) paramChangeCallback(lastParams)
}

const runScript = async ({ script, url = './index.js', base, root }) => {
const runScript = async ({ script, url = './index.js', base = currentBase, root }) => {
currentBase = base
loadDefault = false // don't load default model if something else was loaded
const result = await sendCmdAndSpin('runScript', { script, url, base, root })
genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback })
handlers.entities(result)
}

const loadExample = source => {
const loadExample = (source, base=appBase) => {
editor.setSource(source)
runScript({ script: source })
runScript({ script: source, base })
}

// Initialize three engine
Expand Down
3 changes: 3 additions & 0 deletions apps/jscad-web/src/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export const examples = [
{ name: 'Slicer', source: './examples/slicer.example.js' },
{ name: 'Balloons', source: './examples/balloons.example.js' },
{ name: 'Parameter Types', source: './examples/parameters.example.js' },
{ name: 'Import AMF', source: './examples/AMFImport/index.js' },
{ name: 'Import STL', source: './examples/STLImport/index.js' },
{ name: 'Import SVG', source: './examples/SVGImport/index.js' },
]
6 changes: 3 additions & 3 deletions apps/jscad-web/src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { examples } from './examples.js'

const menu = document.getElementById('menu')

export const init = (loadExample) => {
export const init = loadExample => {
const button = document.getElementById('menu-button')
const content = document.getElementById('menu-content')

Expand All @@ -12,7 +12,7 @@ export const init = (loadExample) => {
})

// Close menu when anything else is clicked
window.addEventListener('click', (e) => {
window.addEventListener('click', e => {
if (!button.contains(e.target) && !content.contains(e.target)) {
dismiss()
}
Expand All @@ -28,7 +28,7 @@ export const init = (loadExample) => {
a.innerText = name
a.addEventListener('click', async () => {
console.log(`load example ${name}`)
loadExample(await (await fetch(source)).text())
loadExample(await (await fetch(source)).text(), new URL(source, document.baseURI).toString())
})
const li = document.createElement('li')
li.appendChild(a)
Expand Down
1 change: 1 addition & 0 deletions packages/worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const importReg = /import(?:(?:(?:[ \n\t]+([^ *\n\t\{\},]+)[ \n\t]*(?:,|[ \n\t]+
const exportReg = /export.*from/

const runScript = async ({ script, url, base=globalBase, root=base }) => {
console.log('run script with base:', base)
if(!script) script = readFileWeb(resolveUrl(url, base, root).url)

const shouldTransform = url.endsWith('.ts') || script.includes('import') && (importReg.test(script) || exportReg.test(script))
Expand Down

0 comments on commit ac4c6b7

Please sign in to comment.