Skip to content

Commit

Permalink
feat-file-input (#72)
Browse files Browse the repository at this point in the history
file imput supprot for parameters, that reads file the same ways as
require for different types of geometry.

example script:

```js
import * as jscad from '@jscad/modeling'
export const main =({// @jscad-params
  mesh,// {type:"file"}
  check=false,
})=>{  
  return mesh ? mesh : jscad.primitives.sphere()
}
```

no file chosen, the sample script returns a sphere

![image](https://github.com/hrgdavor/jscadui/assets/2480762/54181e8c-64dc-47d7-bf0c-0b0ec0523389)

stl file, the sample script returns the geometry form the stl

![image](https://github.com/hrgdavor/jscadui/assets/2480762/9a8a952e-0b1a-4ac1-8e6c-ae26f4ed0883)
  • Loading branch information
hrgdavor authored Dec 19, 2023
1 parent c70e968 commit f532e4b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
28 changes: 13 additions & 15 deletions apps/jscad-web/src_bundle/bundle.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ const exportData = ({format, options={}})=>{
return withTransferable({ data }, data.filter(v=>typeof v !== 'string'))
}

const importData = (url, readFile, base, root, moduleBase)=>{
try {
const jscad_io = require('./bundle.jscad_io.js', null, readFileWeb)
let idx = url.lastIndexOf('/')
let filename = url.substring(idx+1)
idx = filename.lastIndexOf('.')
let ext = filename.substring(idx+1)
let deserializer = jscad_io.deserializers[ext]
let file = readFile(url,{output:ext === 'stl' ? 'bin':'text'})

if(deserializer) return deserializer({output:'geometry', filename}, file)
throw new Error('unsupportd format in '+url)
} catch (error) {
console.error(error)
throw error
const importData = {
isBinaryExt: ext=>ext === 'stl' ? 'bin':'text',
deserialize: ({url, filename, ext}, fileContent)=>{
try {
const jscad_io = require('./bundle.jscad_io.js', null, readFileWeb)
let deserializer = jscad_io.deserializers[ext]

if(deserializer) return deserializer({output:'geometry', filename}, fileContent)
throw new Error('unsupportd format in '+url)
} catch (error) {
console.error(error)
throw error
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions apps/jscad-web/static/test.file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as jscad from '@jscad/modeling'

export const main =({// @jscad-params
mesh,// {type:"file"}
check=false,
})=>{
return mesh ? mesh : jscad.primitives.sphere()

}
9 changes: 7 additions & 2 deletions packages/fs-provider/fs-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ export * from './src/FileEntry.js'
* @returns {Array<string>}
*/
export const splitPath = path => (typeof path === 'string' ? path.split('/').filter(p => p && p !== '.') : path)

export function extractPathInfo(url){
let idx = url.lastIndexOf('/')
let filename = url.substring(idx+1)
idx = filename.lastIndexOf('.')
let ext = filename.substring(idx+1)
return {url, filename, ext}
}
export const getFile = async (path, sw) => {
let arr = splitPath(path)
let match = await findFileInRoots(sw.roots, arr)
Expand Down Expand Up @@ -233,7 +239,6 @@ export async function fileDropped(sw, files) {
let time = Date.now()
const preLoad = ['/' + sw.fileToRun, '/package.json']
const loaded = await addPreLoadAll(sw, preLoad, true)
console.log(Date.now() - time, 'preload', loaded)

sw.projectName = sw.defProjectName
if (sw.fileToRun !== 'index.js') sw.projectName = sw.fileToRun.replace(/\.js$/, '')
Expand Down
5 changes: 4 additions & 1 deletion packages/require/src/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- typescript import must use .js (it is a bit strange, but probably has good reasons)
*/

import { extractPathInfo } from '../../fs-provider/fs-provider'
import { MODULE_BASE, getExtension, resolveUrl } from './resolveUrl'

export { resolveUrl } from './resolveUrl'
Expand Down Expand Up @@ -49,7 +50,9 @@ export const require = (urlOrSource, transform, readFile, base, root, importData
const resolvedStr = resolved.url.toString()
const isJs = resolvedStr.endsWith('.ts') || resolvedStr.endsWith('.js')
if(!isJs && importData){
return importData(resolvedStr, readFile, base, root, moduleBase)
const info = extractPathInfo(resolvedStr)
let content = readFile(resolvedStr,{output: importData.isBinaryExt(info.ext)})
return importData.deserialize(info, content)
}

isRelativeFile = resolved.isRelativeFile
Expand Down
15 changes: 15 additions & 0 deletions packages/worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { clearFileCache, clearTempCache, readFileWeb, require, requireCache, res
import { exportStlText } from './src/exportStlText.js'
import { combineParameterDefinitions, getParameterDefinitionsFromSource } from './src/getParameterDefinitionsFromSource.js'
import { extractDefaults } from './src/extractDefaults.js'
import { extractPathInfo, readAsArrayBuffer, readAsText } from '../fs-provider/fs-provider.js'

let main
self.JSCAD_WORKER_ENV = {}
Expand Down Expand Up @@ -41,8 +42,22 @@ export const init = params => {
userInstances = params.userInstances
}

async function readFileFile(file, {bin=false}={}){
if(bin) return await readAsArrayBuffer(file)
else return readAsText(file)
}

solids = []
export async function runMain({ params } = {}) {
params = {...params}
for(let p in params){
if(params[p] instanceof File && importData){
const info = extractPathInfo(params[p].name)
let content = await readFileFile(params[p],{bin: importData.isBinaryExt(info.ext)})
params[p] = importData.deserialize(info, content)
}
}
console.log('runMain', params)
let entities = []
const transferable = []

Expand Down

0 comments on commit f532e4b

Please sign in to comment.