-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use web worker to load opencascade WASM an parse breps
- Loading branch information
1 parent
993e4c3
commit 1b76dcc
Showing
14 changed files
with
217 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { worker } from "../worker" | ||
|
||
export function parseBRep(content: string): Promise<Uint8Array> { | ||
return new Promise<Uint8Array>((resolve, reject) => { | ||
// Define handlers | ||
function handleMessage(message: MessageEvent) { | ||
worker.removeEventListener('message', handleMessage) | ||
worker.removeEventListener('messageerror', handleMessageError) | ||
worker.removeEventListener('error', handleError) | ||
if (message.data instanceof Uint8Array) { | ||
resolve(message.data) | ||
} else { | ||
reject('Return message data type unexpected: ' + message.data) | ||
} | ||
} | ||
function handleMessageError(message: MessageEvent) { | ||
worker.removeEventListener('message', handleMessage) | ||
worker.removeEventListener('messageerror', handleMessageError) | ||
worker.removeEventListener('error', handleError) | ||
reject(message) | ||
} | ||
function handleError(error: ErrorEvent) { | ||
worker.removeEventListener('message', handleMessage) | ||
worker.removeEventListener('messageerror', handleMessageError) | ||
worker.removeEventListener('error', handleError) | ||
reject(error) | ||
} | ||
// Add handlers | ||
worker.addEventListener('message', handleMessage) | ||
worker.addEventListener('messageerror', handleMessageError) | ||
worker.addEventListener('error', handleError) | ||
// Post message | ||
worker.postMessage(content) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const worker = new Worker('/scripts/worker/main.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
"regex" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"name": "productboard-worker", | ||
"version": "0.0.1", | ||
"description": "Background worker", | ||
"license": "UNLICENSED", | ||
"private": "true", | ||
"type": "module", | ||
"author": { | ||
"name": "Georg Hackenberg", | ||
"email": "[email protected]" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Georg Hackenberg", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Christian Zehetner", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Jürgen Humenberger", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Dominik Frühwirth", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ghackenberg/caddrive.git", | ||
"directory": "packages/worker" | ||
}, | ||
"scripts": { | ||
"clean": "rm -rf public", | ||
"build": "webpack --config webpack.prod.js", | ||
"lint": "eslint src/scripts", | ||
"loc": "sloc src/scripts", | ||
"dev": "webpack serve --config webpack.dev.js", | ||
"start": "http-server -p 3005 -s" | ||
}, | ||
"dependencies": { | ||
"productboard-freecad": "^0.0.1" | ||
}, | ||
"devDependencies": { | ||
"ts-loader": "^9.4.1", | ||
"webpack": "^5.38.1", | ||
"webpack-cli": "^4.7.2", | ||
"webpack-dev-server": "^4.8.1", | ||
"webpack-merge": "^5.8.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import initOpenCascade from 'opencascade.js' | ||
|
||
const OCCT = initOpenCascade() | ||
|
||
OCCT.then(() => console.log('OpenCascade initialized!')) | ||
|
||
addEventListener('message', async message => { | ||
// Check message data type | ||
if (typeof message.data != 'string') { | ||
throw 'Call message data type unexpected: ' + typeof message.data | ||
} | ||
// Cast message data | ||
const content = message.data as string | ||
|
||
// Wait for OCCT to load | ||
const occt = await OCCT | ||
|
||
// Parse shape | ||
//console.log('Reading BRep') | ||
const shape = new occt.TopoDS_Shape() | ||
occt.FS.createDataFile('.', 'part.brp', content, true, true, true) | ||
const builder = new occt.BRep_Builder() | ||
const readProgress = new occt.Message_ProgressRange_1() | ||
occt.BRepTools.Read_2(shape, './part.brp', builder, readProgress) | ||
occt.FS.unlink('./part.brp') | ||
|
||
// Visualize shape | ||
//console.log('Meshing BRep') | ||
const storageformat = new occt.TCollection_ExtendedString_1() | ||
const doc = new occt.TDocStd_Document(storageformat) | ||
const shapeTool = occt.XCAFDoc_DocumentTool.ShapeTool(doc.Main()).get() | ||
shapeTool.SetShape(shapeTool.NewShape(), shape) | ||
new occt.BRepMesh_IncrementalMesh_2(shape, 0.1, false, 0.1, false) | ||
|
||
// Export a GLB file (this will also perform the meshing) | ||
//console.log('Writing GLB') | ||
const glbFileName = new occt.TCollection_AsciiString_2('./part.glb') | ||
const cafWriter = new occt.RWGltf_CafWriter(glbFileName, true) | ||
const docHandle = new occt.Handle_TDocStd_Document_2(doc) | ||
const fileInfo = new occt.TColStd_IndexedDataMapOfStringString_1() | ||
const writeProgress = new occt.Message_ProgressRange_1() | ||
cafWriter.Perform_2(docHandle, fileInfo, writeProgress) | ||
|
||
// Read the GLB file from the virtual file system | ||
//console.log('Readling GLB') | ||
const glbFileData = occt.FS.readFile('./part.glb', { encoding: "binary" }) | ||
occt.FS.unlink('./part.glb') | ||
|
||
// Returning GLB data | ||
postMessage(glbFileData) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"target": "ES2017", | ||
"rootDirs": ["src", "../brep/src", "../freecad/src"], | ||
"outDir": "bin", | ||
"lib": ["ES2017", "WebWorker"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { resolve } from 'path' | ||
|
||
import 'webpack' | ||
|
||
export default { | ||
stats: 'minimal', | ||
entry: './src/main.ts', | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.tsx?$/, | ||
use: 'ts-loader', | ||
exclude: /node_modules/, | ||
},{ | ||
test: /\.(wasm)$/i, | ||
type: 'asset/resource', | ||
generator: { | ||
filename: 'modules/[hash][ext][query]' | ||
} | ||
} | ||
], | ||
}, | ||
resolve: { | ||
alias: { | ||
'three': resolve('../../node_modules/three') | ||
}, | ||
extensions: ['.ts', '.js'], | ||
}, | ||
output: { | ||
path: resolve('public'), | ||
filename: 'scripts/worker/[name].js' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { resolve } from 'path' | ||
|
||
import { merge } from 'webpack-merge' | ||
|
||
import common from './webpack.common.js' | ||
|
||
export default merge(common, { | ||
mode: 'development', | ||
devServer: { | ||
static: resolve('public'), | ||
port: 3005 | ||
}, | ||
devtool: 'inline-source-map' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { merge } from 'webpack-merge' | ||
|
||
import common from './webpack.common.js' | ||
|
||
export default merge(common, { | ||
mode: 'production', | ||
devtool: 'source-map' | ||
}) |