Skip to content

Commit

Permalink
Use hosted version of occt-import-js.
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacsv committed Apr 27, 2024
1 parent 130e527 commit 5f05ad5
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 625 deletions.
15 changes: 0 additions & 15 deletions libs/occt-import-js-worker.js

This file was deleted.

21 changes: 0 additions & 21 deletions libs/occt-import-js.js

This file was deleted.

504 changes: 0 additions & 504 deletions libs/occt-import-js.license.md

This file was deleted.

Binary file removed libs/occt-import-js.wasm
Binary file not shown.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@
"rollup": "^4.0.2",
"run-python3": "^0.0.5",
"svgo": "^3.0.2",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"web-ifc": "0.0.44"
},
"dependencies": {
"@simonwep/pickr": "1.9.0",
"fflate": "0.8.2",
"occt-import-js": "0.0.21",
"three": "0.163.0",
"web-ifc": "0.0.44"
"three": "0.163.0"
},
"eslintConfig": {
"env": {
Expand Down
3 changes: 2 additions & 1 deletion source/engine/export/exporter3dm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LoadRhinoLibrary } from '../import/importerutils.js';
import { LoadExternalLibraryFromUrl } from '../io/externallibs.js';

Check failure on line 2 in source/engine/export/exporter3dm.js

View workflow job for this annotation

GitHub Actions / build (18.x, macos-latest)

'LoadExternalLibraryFromUrl' is defined but never used
import { FileFormat } from '../io/fileutils.js';
import { MaterialType } from '../model/material.js';
Expand All @@ -20,7 +21,7 @@ export class Exporter3dm extends ExporterBase
ExportContent (exporterModel, format, files, onFinish)
{
if (this.rhino === null) {
LoadExternalLibraryFromUrl ('https://cdn.jsdelivr.net/npm/[email protected]/rhino3dm.min.js').then (() => {
LoadRhinoLibrary ().then (() => {
rhino3dm ().then ((rhino) => {
this.rhino = rhino;
this.ExportRhinoContent (exporterModel, files, onFinish);
Expand Down
4 changes: 2 additions & 2 deletions source/engine/import/importer3dm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IsModelEmpty } from '../model/modelutils.js';
import { Property, PropertyGroup, PropertyType } from '../model/property.js';
import { ConvertThreeGeometryToMesh } from '../threejs/threeutils.js';
import { ImporterBase } from './importerbase.js';
import { UpdateMaterialTransparency } from './importerutils.js';
import { LoadRhinoLibrary, UpdateMaterialTransparency } from './importerutils.js';
import { TextureMap } from '../model/material.js';
import { Mesh } from '../model/mesh.js';
import { Line } from '../model/line.js';
Expand Down Expand Up @@ -49,7 +49,7 @@ export class Importer3dm extends ImporterBase
ImportContent (fileContent, onFinish)
{
if (this.rhino === null) {
LoadExternalLibraryFromUrl ('https://cdn.jsdelivr.net/npm/[email protected]/rhino3dm.min.js').then (() => {
LoadRhinoLibrary ().then (() => {
rhino3dm ().then ((rhino) => {
this.rhino = rhino;
this.ImportRhinoContent (fileContent);
Expand Down
74 changes: 38 additions & 36 deletions source/engine/import/importerfcstd.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Direction } from '../geometry/geometry.js';
import { ImporterBase } from './importerbase.js';
import { GetFileExtension } from '../io/fileutils.js';
import { GetExternalLibPath } from '../io/externallibs.js';
import { ConvertThreeGeometryToMesh } from '../threejs/threeutils.js';
import { ArrayBufferToUtf8String } from '../io/bufferutils.js';
import { Node } from '../model/node.js';
import { ColorToMaterialConverter } from './importerutils.js';
import { ColorToMaterialConverter, CreateOcctWorker } from './importerutils.js';
import { RGBAColor } from '../model/color.js';
import { Property, PropertyGroup, PropertyType } from '../model/property.js';
import { Loc } from '../core/localization.js';
Expand Down Expand Up @@ -344,40 +343,43 @@ export class ImporterFcstd extends ImporterBase

ConvertObjects (objects, onFinish)
{
let workerPath = GetExternalLibPath ('occt-import-js-worker.js');
this.worker = new Worker (workerPath);

let convertedObjectCount = 0;
let colorToMaterial = new ColorToMaterialConverter (this.model);
let onFileConverted = (resultContent) => {
if (resultContent !== null) {
let currentObject = objects[convertedObjectCount];
this.OnFileConverted (currentObject, resultContent, colorToMaterial);
}
convertedObjectCount += 1;
if (convertedObjectCount === objects.length) {
onFinish ();
} else {
let currentObject = objects[convertedObjectCount];
this.worker.postMessage ({
format : 'brep',
buffer : currentObject.fileContent
});
}
};

this.worker.addEventListener ('message', (ev) => {
onFileConverted (ev.data);
});

this.worker.addEventListener ('error', (ev) => {
onFileConverted (null);
});

let currentObject = objects[convertedObjectCount];
this.worker.postMessage ({
format : 'brep',
buffer : currentObject.fileContent
CreateOcctWorker ().then ((worker) => {
this.worker = worker;
this.worker.addEventListener ('message', (ev) => {
onFileConverted (ev.data);
});

this.worker.addEventListener ('error', (ev) => {
onFileConverted (null);
});

let convertedObjectCount = 0;
let colorToMaterial = new ColorToMaterialConverter (this.model);
let onFileConverted = (resultContent) => {
if (resultContent !== null) {
let currentObject = objects[convertedObjectCount];
this.OnFileConverted (currentObject, resultContent, colorToMaterial);
}
convertedObjectCount += 1;
if (convertedObjectCount === objects.length) {
onFinish ();
} else {
let currentObject = objects[convertedObjectCount];
this.worker.postMessage ({
format : 'brep',
buffer : currentObject.fileContent
});
}
};

let currentObject = objects[convertedObjectCount];
this.worker.postMessage ({
format : 'brep',
buffer : currentObject.fileContent
});
}).catch (() => {
this.SetError (Loc ('Failed to load occt-import-js.'));
onFinish ();
});
}

Expand Down
75 changes: 39 additions & 36 deletions source/engine/import/importerocct.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Direction } from '../geometry/geometry.js';
import { GetExternalLibPath } from '../io/externallibs.js';
import { Node } from '../model/node.js';
import { RGBColorFromFloatComponents } from '../model/color.js';
import { ConvertThreeGeometryToMesh } from '../threejs/threeutils.js';
import { ImporterBase } from './importerbase.js';
import { ColorToMaterialConverter } from './importerutils.js';
import { ColorToMaterialConverter, CreateOcctWorker } from './importerutils.js';
import { Unit } from '../model/unit.js';
import { Loc } from '../core/localization.js';

Expand Down Expand Up @@ -41,43 +40,47 @@ export class ImporterOcct extends ImporterBase

ImportContent (fileContent, onFinish)
{
let workerPath = GetExternalLibPath ('occt-import-js-worker.js');
this.worker = new Worker (workerPath);
this.worker.addEventListener ('message', (ev) => {
this.ImportResultJson (ev.data, onFinish);
});
this.worker.addEventListener ('error', (ev) => {
this.SetError (Loc ('Failed to load occt-import-js.'));
onFinish ();
});
CreateOcctWorker ().then ((worker) => {
this.worker = worker;
this.worker.addEventListener ('message', (ev) => {
this.ImportResultJson (ev.data, onFinish);
});
this.worker.addEventListener ('error', (ev) => {
this.SetError (Loc ('Failed to load occt-import-js.'));
onFinish ();
});

let format = null;
if (this.extension === 'stp' || this.extension === 'step') {
format = 'step';
} else if (this.extension === 'igs' || this.extension === 'iges') {
format = 'iges';
} else if (this.extension === 'brp' || this.extension === 'brep') {
format = 'brep';
} else {
onFinish ();
return;
}
let format = null;
if (this.extension === 'stp' || this.extension === 'step') {
format = 'step';
} else if (this.extension === 'igs' || this.extension === 'iges') {
format = 'iges';
} else if (this.extension === 'brp' || this.extension === 'brep') {
format = 'brep';
} else {
onFinish ();
return;
}

if (format === 'step' || format === 'iges') {
this.model.SetUnit (Unit.Millimeter);
}
if (format === 'step' || format === 'iges') {
this.model.SetUnit (Unit.Millimeter);
}

let params = {
linearUnit: 'millimeter',
linearDeflectionType: 'bounding_box_ratio',
linearDeflection: 0.001,
angularDeflection: 0.5
};
let fileBuffer = new Uint8Array (fileContent);
this.worker.postMessage ({
format : format,
buffer : fileBuffer,
params : params
let params = {
linearUnit: 'millimeter',
linearDeflectionType: 'bounding_box_ratio',
linearDeflection: 0.001,
angularDeflection: 0.5
};
let fileBuffer = new Uint8Array (fileContent);
this.worker.postMessage ({
format : format,
buffer : fileBuffer,
params : params
});
}).catch (() => {
this.SetError (Loc ('Failed to load occt-import-js.'));
onFinish ();
});
}

Expand Down
35 changes: 35 additions & 0 deletions source/engine/import/importerutils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IsLower } from '../geometry/geometry.js';
import { PhongMaterial } from '../model/material.js';
import { RGBColor, IntegerToHexString } from '../model/color.js';
import { LoadExternalLibraryFromUrl } from '../io/externallibs.js';

export function NameFromLine (line, startIndex, commentChar)
{
Expand Down Expand Up @@ -100,3 +101,37 @@ export class ColorToMaterialConverter
}
}
}

let occtWorkerUrl = null;

export function CreateOcctWorker (worker)
{
return new Promise ((resolve, reject) => {
if (occtWorkerUrl !== null) {
resolve (new Worker (occtWorkerUrl));
return;
}

let baseUrl = 'https://cdn.jsdelivr.net/npm/[email protected]/dist/';
fetch (baseUrl + 'occt-import-js-worker.js')
.then ((response) => {
if (!response.ok) {
return reject ();
}
return response.text ();
})
.then ((workerScript) => {
workerScript = workerScript.replace ('occt-import-js.js', baseUrl + 'occt-import-js.js');
workerScript = workerScript.replace ('return path', 'return \'' + baseUrl + 'occt-import-js.wasm\'');
let blob = new Blob ([workerScript], { type : 'text/javascript' });
occtWorkerUrl = URL.createObjectURL (blob);
return resolve (new Worker (occtWorkerUrl));
})
.catch (reject);
});
}

export function LoadRhinoLibrary ()
{
return LoadExternalLibraryFromUrl ('https://cdn.jsdelivr.net/npm/[email protected]/rhino3dm.min.js');
}
3 changes: 2 additions & 1 deletion source/engine/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { ImporterPly } from './import/importerply.js';
import { ImporterStl } from './import/importerstl.js';
import { ImporterThreeSvg } from './import/importersvg.js';
import { ImporterThreeBase, ImporterThreeFbx, ImporterThreeDae, ImporterThreeWrl, ImporterThree3mf, ImporterThreeAmf } from './import/importerthree.js';
import { ColorToMaterialConverter, NameFromLine, ParametersFromLine, ReadLines, IsPowerOfTwo, NextPowerOfTwo, UpdateMaterialTransparency } from './import/importerutils.js';
import { ColorToMaterialConverter, NameFromLine, ParametersFromLine, ReadLines, IsPowerOfTwo, NextPowerOfTwo, UpdateMaterialTransparency, CreateOcctWorker } from './import/importerutils.js';
import { BinaryReader } from './io/binaryreader.js';
import { BinaryWriter } from './io/binarywriter.js';
import { ArrayBufferToUtf8String, ArrayBufferToAsciiString, AsciiStringToArrayBuffer, Utf8StringToArrayBuffer, Base64DataURIToArrayBuffer, GetFileExtensionFromMimeType, CreateObjectUrl, CreateObjectUrlWithMimeType, RevokeObjectUrl } from './io/bufferutils.js';
Expand Down Expand Up @@ -193,6 +193,7 @@ export {
IsPowerOfTwo,
NextPowerOfTwo,
UpdateMaterialTransparency,
CreateOcctWorker,
BinaryReader,
BinaryWriter,
ArrayBufferToUtf8String,
Expand Down
5 changes: 0 additions & 5 deletions tools/update_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
fileMap = [
[os.path.join ('web-ifc', 'web-ifc-api.js'), os.path.join ('web-ifc-api-browser.js'), webIfcConverter],
[os.path.join ('web-ifc', 'web-ifc.wasm'), os.path.join ('web-ifc.wasm'), None],

[os.path.join ('occt-import-js', 'dist', 'occt-import-js.js'), os.path.join ('occt-import-js.js'), None],
[os.path.join ('occt-import-js', 'dist', 'occt-import-js-worker.js'), os.path.join ('occt-import-js-worker.js'), None],
[os.path.join ('occt-import-js', 'dist', 'occt-import-js.wasm'), os.path.join ('occt-import-js.wasm'), None],
[os.path.join ('occt-import-js', 'LICENSE.md'), os.path.join ('occt-import-js.license.md'), None]
]

def PrintInfo (message):
Expand Down

0 comments on commit 5f05ad5

Please sign in to comment.