1
import { cast } from "./shapes";
2
import { getOC } from "./oclib";
3
import { localGC } from "./register";
6
Date.now().toString(36) + Math.random().toString(36).substring(2);
9
* Creates a new shapes from a STEP file (as a Blob or a File).
13
export async function importSTEP(STLBlob: Blob) {
15
const [r, gc] = localGC();
17
const fileName = uniqueId();
18
const bufferView = new Uint8Array(await STLBlob.arrayBuffer());
19
oc.FS.writeFile(`/${fileName}`, bufferView);
21
const reader = r(new oc.STEPControl_Reader_1());
22
if (reader.ReadFile(fileName)) {
23
oc.FS.unlink("/" + fileName);
24
reader.TransferRoots(r(new oc.Message_ProgressRange_1()));
25
const stepShape = r(reader.OneShape());
27
const shape = cast(stepShape);
31
oc.FS.unlink("/" + fileName);
33
throw new Error("Failed to load STEP file");
37
/** Creates a new shapes from a STL file (as a Blob or a File).
39
* This process can be relatively long depending on how much tesselation has
40
* been done to your STL.
42
* This function tries to clean a bit the triangulation of faces, but can fail
47
export async function importSTL(STLBlob: Blob) {
49
const [r, gc] = localGC();
51
const fileName = uniqueId();
52
const bufferView = new Uint8Array(await STLBlob.arrayBuffer());
53
oc.FS.writeFile(`/${fileName}`, bufferView);
55
const reader = r(new oc.StlAPI_Reader());
56
const readShape = r(new oc.TopoDS_Shell());
58
if (reader.Read(readShape, fileName)) {
59
oc.FS.unlink("/" + fileName);
61
const shapeUpgrader = r(
62
new oc.ShapeUpgrade_UnifySameDomain_2(readShape, true, true, false)
64
shapeUpgrader.Build();
65
const upgradedShape = r(shapeUpgrader.Shape());
67
const solidSTL = r(new oc.BRepBuilderAPI_MakeSolid_1());
68
solidSTL.Add(oc.TopoDS.Shell_1(upgradedShape));
69
const asSolid = r(solidSTL.Solid());
71
const shape = cast(asSolid);
75
oc.FS.unlink("/" + fileName);
77
throw new Error("Failed to load STL file");