Skip to content

Commit

Permalink
Simplify worker code.
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacsv committed Apr 27, 2024
1 parent e235a86 commit 5e96d10
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 29 deletions.
16 changes: 7 additions & 9 deletions dist/occt-import-js-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ importScripts ('occt-import-js.js');

onmessage = async function (ev)
{
let occt = await occtimportjs ();
let result = null;
if (ev.data.format === 'step') {
result = occt.ReadStepFile (ev.data.buffer, ev.data.params);
} else if (ev.data.format === 'iges') {
result = occt.ReadIgesFile (ev.data.buffer, ev.data.params);
} else if (ev.data.format === 'brep') {
result = occt.ReadBrepFile (ev.data.buffer, ev.data.params);
}
let modulOverrides = {
locateFile: function (path) {
return path;
}
};
let occt = await occtimportjs (modulOverrides);
let result = occt.ReadFile (ev.data.format, ev.data.buffer, ev.data.params);
postMessage (result);
};
16 changes: 7 additions & 9 deletions dist/occt-import-js.js

Large diffs are not rendered by default.

Binary file modified dist/occt-import-js.wasm
Binary file not shown.
35 changes: 26 additions & 9 deletions occt-import-js/src/js-interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ static void EnumerateNodeMeshes (const NodePtr& node, const std::function<void (
}
}

static emscripten::val ReadFile (ImporterPtr importer, const emscripten::val& content, const ImportParams& params)
static emscripten::val ImportFile (ImporterPtr importer, const emscripten::val& buffer, const ImportParams& params)
{
emscripten::val resultObj (emscripten::val::object ());

const std::vector<uint8_t>& contentArr = emscripten::vecFromJSArray<std::uint8_t> (content);
Importer::Result importResult = importer->LoadFile (contentArr, params);
const std::vector<uint8_t>& bufferArr = emscripten::vecFromJSArray<std::uint8_t> (buffer);
Importer::Result importResult = importer->LoadFile (bufferArr, params);
resultObj.set ("success", importResult == Importer::Result::Success);
if (importResult != Importer::Result::Success) {
return resultObj;
Expand Down Expand Up @@ -215,29 +215,46 @@ static ImportParams GetImportParams (const emscripten::val& paramsVal)
return params;
}

emscripten::val ReadStepFile (const emscripten::val& content, const emscripten::val& params)
emscripten::val ReadStepFile (const emscripten::val& buffer, const emscripten::val& params)
{
ImporterPtr importer = std::make_shared<ImporterStep> ();
ImportParams importParams = GetImportParams (params);
return ReadFile (importer, content, importParams);
return ImportFile (importer, buffer, importParams);
}

emscripten::val ReadIgesFile (const emscripten::val& content, const emscripten::val& params)
emscripten::val ReadIgesFile (const emscripten::val& buffer, const emscripten::val& params)
{
ImporterPtr importer = std::make_shared<ImporterIges> ();
ImportParams importParams = GetImportParams (params);
return ReadFile (importer, content, importParams);
return ImportFile (importer, buffer, importParams);
}

emscripten::val ReadBrepFile (const emscripten::val& content, const emscripten::val& params)
emscripten::val ReadBrepFile (const emscripten::val& buffer, const emscripten::val& params)
{
ImporterPtr importer = std::make_shared<ImporterBrep> ();
ImportParams importParams = GetImportParams (params);
return ReadFile (importer, content, importParams);
return ImportFile (importer, buffer, importParams);
}

emscripten::val ReadFile (const std::string& format, const emscripten::val& buffer, const emscripten::val& params)
{
if (format == "step") {
return ReadStepFile (buffer, params);
} else if (format == "iges") {
return ReadIgesFile (buffer, params);
} else if (format == "brep") {
return ReadBrepFile (buffer, params);
} else {
emscripten::val resultObj (emscripten::val::object ());
resultObj.set ("success", false);
return resultObj;
}
}

EMSCRIPTEN_BINDINGS (occtimportjs)
{
emscripten::function<emscripten::val, const std::string&, const emscripten::val&, const emscripten::val&> ("ReadFile", &ReadFile);

emscripten::function<emscripten::val, const emscripten::val&, const emscripten::val&> ("ReadStepFile", &ReadStepFile);
emscripten::function<emscripten::val, const emscripten::val&, const emscripten::val&> ("ReadIgesFile", &ReadIgesFile);
emscripten::function<emscripten::val, const emscripten::val&, const emscripten::val&> ("ReadBrepFile", &ReadBrepFile);
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ before (async function () {
occt = await occtimportjs;
});

function LoadFile (format, fileUrl)
{
let fileContent = fs.readFileSync (fileUrl);
return occt.ReadFile (format, fileContent, null);
}

function LoadStepFile (fileUrl)
{
let fileContent = fs.readFileSync (fileUrl);
Expand Down Expand Up @@ -336,3 +342,18 @@ it ('Convert to other units', function () {
});

});

describe ('General Import', function () {

it ('Format string test', function () {
let stepResult = LoadFile ('step', './test/testfiles/simple-basic-cube/cube.stp');
assert (stepResult.success);
let igesResult = LoadFile ('iges', './test/testfiles/cube-10x10mm/Cube 10x10.igs');
assert (igesResult.success);
let brepResult = LoadFile ('brep', './test/testfiles/cax-if-brep/as1_pe_203.brep');
assert (brepResult.success);
let otherResult = LoadFile ('other', './test/testfiles/simple-basic-cube/cube.stp');
assert (!otherResult.success);
});

});
4 changes: 2 additions & 2 deletions tools/setup_emscripten_win.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ pushd %~dp0\..

call git clone https://github.com/emscripten-core/emsdk.git
call cd emsdk
call emsdk install 3.1.35
call emsdk activate 3.1.35
call emsdk install 3.1.58
call emsdk activate 3.1.58
call emsdk install mingw-7.1.0-64bit
call emsdk activate mingw-7.1.0-64bit
call cd ..
Expand Down

0 comments on commit 5e96d10

Please sign in to comment.