Skip to content

Commit

Permalink
feat(tutorial6): new tutorial on handling parameters for designs
Browse files Browse the repository at this point in the history
* feat(plugins): enhanced plugins to support parameter values

* fix(tutorials): updated to match changes to plugins, support for parameter values

* feat(tutorial6): new tutorial on handling parameters for designs

* docs(tutorial6): changes to documentation
  • Loading branch information
z3dev authored Jan 26, 2021
1 parent 0f0a0d6 commit 75b8d50
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

VUE Components for rendering and compiling JSCAD designs

## ALPHA ALPHA ALPHA
## ALPHA ALPHA ALPHA
## ALPHA ALPHA ALPHA

Expand Down Expand Up @@ -32,8 +31,9 @@ npm run build
- Tutorial 3 - Drag and Drop any JSCAD design (file or directory), then compile and view
- Tutorial 4 - Fetch a JSCAD design (file) from a remote site (URL), then compile and view
- Tutorial 5 - Edit a JSCAD design on line, then compile and view
- Tutorial 6 - Change a parameter for a JSCAD design, then compile and view

## License

[The MIT License (MIT)](https://github.com/jscad/OpenJSCAD.org/blob/master/LICENSE)
[The MIT License (MIT)](./LICENSE)
(unless specified otherwise)
19 changes: 10 additions & 9 deletions src/compilerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const compilerPlugin = (store) => {
// called when the store is initialized
store.subscribe((mutation, state) => {
if (mutation.type === 'compile') {
const fileList = mutation.payload

const defaults = { fileList: [], parameterValues: {} }
const { fileList, parameterValues } = Object.assign({}, defaults, mutation.payload)

// how to compile depends on the payload
// console.log('payload',fileList)
// console.log('fileList',fileList)
// console.log('parameterValues',parameterValues)

if (fileList.item && ('function' === typeof fileList.item)) {
store.commit('setStatus',`processing (${fileList.length} files)...`)
Expand All @@ -18,22 +19,22 @@ const compilerPlugin = (store) => {
files.push(fileList.item ? fileList.item(i) : fileList[i])
}
// compile
compileFromFiles(store, files)
compileFromFiles(store, files, parameterValues)
} else if (Array.isArray(fileList) && fileList.length > 0 && fileList[0].filesystem) {
// compile the list of FileSystem entries (from drag and drop of files)
store.commit('setStatus',`processing (file system)...`)
compileFromFiles(store, fileList)
compileFromFiles(store, fileList, parameterValues)
} else if (fileList.name && fileList.ext && fileList.source && fileList.fullPath) {
store.commit('setStatus',`processing (fake file entry)...`)
compileFromSource(store, fileList)
compileFromSource(store, fileList, parameterValues)
} else {
store.commit('setStatus',`error (Invalid source; should be FileList, Array[FileSystem], or Object)`)
}
}
})
}

const compileFromFiles = (store, files) => {
const compileFromFiles = (store, files, parameterValues) => {
//console.log('compileFromFile',files)

const handleParamsOrSolids = (crap, paramsOrSolids) => {
Expand All @@ -58,7 +59,7 @@ const compileFromFiles = (store, files) => {
})
}

const compileFromSource = (store, fileEntry) => {
const compileFromSource = (store, fileEntry, parameterValues) => {
store.commit('setStatus','compiling...')

const handleParamsOrSolids = (crap, paramsOrSolids) => {
Expand All @@ -69,7 +70,7 @@ const compileFromSource = (store, fileEntry) => {
}

try {
const data = { filesAndFolders: [ fileEntry ], serialize: false }
const data = { filesAndFolders: [ fileEntry ], parameterValues, serialize: false }
const objects = evaluation.rebuildGeometry(data, handleParamsOrSolids)
} catch (error) {
store.commit('setStatus',`error (${error})`)
Expand Down
2 changes: 1 addition & 1 deletion src/fetchPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const fetchPlugin = (store) => {
}
const onsucess = (fileEntry) => {
store.commit('setStatus',`fetched...`)
store.commit('compile',fileEntry)
store.commit('compile',{ fileList: fileEntry })
}

// validate the URL
Expand Down
5 changes: 2 additions & 3 deletions tutorial1.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@
setStatus (state, status) {
state.status = status
},
compile (state, fileList) {
// console.log('compile',fileList.length)
compile (state, design) {
// NOTE: compile is perfomed by the PLUGIN
}
}
Expand All @@ -109,7 +108,7 @@
// validations
const files = target.files
if (files.length === 1) {
store.commit("compile", files) // COMPILE
store.commit("compile", { fileList: files }) // COMPILE
return
}
store.commit("setStatus", 'please try again')
Expand Down
5 changes: 2 additions & 3 deletions tutorial2.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@
setStatus (state, status) {
state.status = status
},
compile (state, fileList) {
// console.log('compile',fileList.length)
compile (state, design) {
// NOTE: compile is perfomed by the PLUGIN
}
}
Expand All @@ -107,7 +106,7 @@
// validations
const files = target.files
if (files.length === 1) {
store.commit("compile", files) // COMPILE
store.commit("compile", { fileList: files }) // COMPILE
return
}
store.commit("setStatus", 'please try again')
Expand Down
5 changes: 2 additions & 3 deletions tutorial3.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ <h3>DRAG AND DROP ANY JSCAD DESIGN TO THIS PAGE</h3>
setStatus (state, status) {
state.status = status
},
compile (state, fileList) {
// console.log('compile',fileList.length)
compile (state, design) {
// NOTE: compile is perfomed by the PLUGIN
}
}
Expand Down Expand Up @@ -122,7 +121,7 @@ <h3>DRAG AND DROP ANY JSCAD DESIGN TO THIS PAGE</h3>
entry = entry.webkitGetAsEntry ? entry.webkitGetAsEntry() : entry
this.entries.push(entry)
}
store.commit("compile", this.entries) // COMPILE
store.commit("compile", { fileList: this.entries }) // COMPILE
return
}
store.commit("setStatus", 'please try again')
Expand Down
2 changes: 1 addition & 1 deletion tutorial4.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ <h3>OR DRAG AND DROP ANY URL TO THIS PAGE</h3>
setStatus (state, status) {
state.status = status
},
compile (state, fileList) {
compile (state, design) {
// NOTE: compile is perfomed by the PLUGIN
},
fetch (state, url) {
Expand Down
8 changes: 4 additions & 4 deletions tutorial5.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h3>ENTER SOME TEXT FOR A JSCAD DESIGN</h3>
setStatus (state, status) {
state.status = status
},
compile (state, fileList) {
compile (state, design) {
// NOTE: compile is perfomed by the PLUGIN
}
}
Expand Down Expand Up @@ -114,14 +114,14 @@ <h3>ENTER SOME TEXT FOR A JSCAD DESIGN</h3>
},
methods: {
updateDesign(event) {
// create a fake file entry for the compilation // FILE ENTRY
const fake = {
// create a fake file entry for the compilation
const fileEntry = {
name: 'fake',
ext: 'js',
source: this.source,
fullPath: '/fake.js'
}
store.commit("compile", fake) // COMPILE
store.commit("compile", { fileList: fileEntry }) // COMPILE
}
}
})
Expand Down
141 changes: 141 additions & 0 deletions tutorial6.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>JSCAD Design - Tutorial 6</title>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vuex"></script>
<style>
.viewer{
width: 8cm;
height: 8cm;
margin: 0;
outline: 1px solid black;
background-color: transparent;
}
</style>
</head>
<body>
<script src="./dist/jscad-vue-components.js" ID="LIBRARY"></script>

<div id="app">
<div>
<jscad-viewer></jscad-viewer>
</div>
<div>
<p>Geometries: {{ count }}</p>
<p>Status: <span style="color:blue">{{ status }}</span></p>
</div>
<div>
<h3>ENTER SOME TEXT FOR A JSCAD DESIGN</h3>
<textarea autofocus cols=100 rows=12 maxlength=1000 wrap="hard" name="textarea" v-model="source" ID="TEXT INPUT">
</textarea>
<input type="submit" value="Compile" v-on:click="updateDesign">
<h3>CHANGE THE PARAMETER</h3>
<input type="number" v-model="radius" ID="PARAMETER">
</div>
</div>

<script>
/**
* This is Part 6 of working with JSCAD designs.
* This is another simple application, which allows the input of a parameter to a JSCAD design. See PARAMETER above.
*
* Just change the parameter, and push the 'Compile' button.
* VUE keeps track of the changes to the parameter, updating the 'radius' data attribute.
* When the 'Compile' button is pushed, an event is sent to updateDesign().
*
* Behind the scene, the 'source' is repackaged into a 'fake' FileEntry. See FILE ENTRY below.
* In addition, the 'radius' is converted on NUMBER and placed into a list of parameter values. See PARAMETER VALUES below.
* The contents and the parameter values are passed to the store for compilation. See COMPILE below.
* Once the compile is complete, the JSCAD solids are updated, and WA LA!
*
* Things to notice about the design:
* - the main() function now takes an argument, an anonymous object with a 'radius' attribute
*
* This works but there are some issues.
* - This design does not define the parameters, therefore no checks are performed. Try changing the 'radius' to a negative value.
* - The viewer STOPS while the compliation completes.
*/

// global registration of viewer component
Vue.component(jscadVueComponents.viewerComponent.name, jscadVueComponents.viewerComponent.properties)

// global store with minimum state for viewer component
const store = new Vuex.Store({
state: {
count: 0,
solids: [],
status: ''
},
plugins: [jscadVueComponents.compilerPlugin],
getters: {
getSolids: (state) => {
return state.solids
}
},
mutations: {
// @param {State} state
// @param {Array} solids
setSolids (state, solids) {
solids.forEach((solid, i) => {
Vue.set(state.solids, i, solid)
})
// trigger callback to viewer component
state.count = solids.length
},
setStatus (state, status) {
state.status = status
},
compile (state, fileList, parameters) {
// NOTE: compile is perfomed by the PLUGIN
}
}
})

// initiate the Vue based application, which includes the jscad-viewer
let app = new Vue({
el: '#app',
store,
data: {
source: `
const { circle } = require('@jscad/modeling').primitives
const main = ({ radius }) => {
const partA = circle({radius})
return partA
}
module.exports = { main }
`,
// PARAMETER
radius: 10,
entries: []
},
computed: {
count () {
return store.state.count
},
status () {
return store.state.status
}
},
methods: {
updateDesign(event) {
// create a fake file entry for the compilation
const fileEntry = { // FILE ENTRY
name: 'fake',
ext: 'js',
source: this.source,
fullPath: '/fake.js'
}
const parameterValues = { radius: Number.parseFloat(this.radius) } // PARAMETER VALUES
store.commit("compile", { fileList: fileEntry, parameterValues } ) // COMPILE
}
}
})

</script>

</body>
</html>

0 comments on commit 75b8d50

Please sign in to comment.