RepliCAD

Форк
0
/
importers.ts 
79 строк · 2.1 Кб
1
import { cast } from "./shapes";
2
import { getOC } from "./oclib";
3
import { localGC } from "./register";
4

5
const uniqueId = () =>
6
  Date.now().toString(36) + Math.random().toString(36).substring(2);
7

8
/**
9
 * Creates a new shapes from a STEP file (as a Blob or a File).
10
 *
11
 * @category Import
12
 */
13
export async function importSTEP(STLBlob: Blob) {
14
  const oc = getOC();
15
  const [r, gc] = localGC();
16

17
  const fileName = uniqueId();
18
  const bufferView = new Uint8Array(await STLBlob.arrayBuffer());
19
  oc.FS.writeFile(`/${fileName}`, bufferView);
20

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());
26

27
    const shape = cast(stepShape);
28
    gc();
29
    return shape;
30
  } else {
31
    oc.FS.unlink("/" + fileName);
32
    gc();
33
    throw new Error("Failed to load STEP file");
34
  }
35
}
36

37
/** Creates a new shapes from a STL file (as a Blob or a File).
38
 *
39
 * This process can be relatively long depending on how much tesselation has
40
 * been done to your STL.
41
 *
42
 * This function tries to clean a bit the triangulation of faces, but can fail
43
 * in bad ways.
44
 *
45
 * @category Import
46
 */
47
export async function importSTL(STLBlob: Blob) {
48
  const oc = getOC();
49
  const [r, gc] = localGC();
50

51
  const fileName = uniqueId();
52
  const bufferView = new Uint8Array(await STLBlob.arrayBuffer());
53
  oc.FS.writeFile(`/${fileName}`, bufferView);
54

55
  const reader = r(new oc.StlAPI_Reader());
56
  const readShape = r(new oc.TopoDS_Shell());
57

58
  if (reader.Read(readShape, fileName)) {
59
    oc.FS.unlink("/" + fileName);
60

61
    const shapeUpgrader = r(
62
      new oc.ShapeUpgrade_UnifySameDomain_2(readShape, true, true, false)
63
    );
64
    shapeUpgrader.Build();
65
    const upgradedShape = r(shapeUpgrader.Shape());
66

67
    const solidSTL = r(new oc.BRepBuilderAPI_MakeSolid_1());
68
    solidSTL.Add(oc.TopoDS.Shell_1(upgradedShape));
69
    const asSolid = r(solidSTL.Solid());
70

71
    const shape = cast(asSolid);
72
    gc();
73
    return shape;
74
  } else {
75
    oc.FS.unlink("/" + fileName);
76
    gc();
77
    throw new Error("Failed to load STL file");
78
  }
79
}
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.