Skip to content

Commit 7e8043e

Browse files
improved node setup for bitbybit
1 parent 4ef9807 commit 7e8043e

File tree

5 files changed

+2532
-1161
lines changed

5 files changed

+2532
-1161
lines changed

examples/node/express-app/bitbybit.ts

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,71 @@
11
import { createRequire } from "module";
2-
import { OCCTService } from "@bitbybit-dev/occt/lib/occ-service.js";
3-
import { OccHelper } from "@bitbybit-dev/occt/lib/occ-helper.js";
4-
import { VectorHelperService } from "@bitbybit-dev/occt/lib/api/vector-helper.service.js";
5-
import { ShapesHelperService } from "@bitbybit-dev/occt/lib/api/shapes-helper.service.js";
6-
import initOpenCascade from "@bitbybit-dev/occt/bitbybit-dev-occt/index.js";
2+
3+
// Base utilities
4+
import {
5+
MathBitByBit,
6+
Logic,
7+
Lists,
8+
TextBitByBit,
9+
Vector,
10+
Point,
11+
Transforms,
12+
Color,
13+
GeometryHelper,
14+
Line,
15+
Polyline,
16+
} from "@bitbybit-dev/base";
17+
18+
// Core functionality
19+
import { JSONBitByBit, CSVBitByBit, Verb, ContextBase } from "@bitbybit-dev/core";
20+
21+
// JSCAD for Node.js
22+
import { Jscad } from "@bitbybit-dev/jscad";
23+
24+
// Manifold for Node.js
25+
import { ManifoldService } from "@bitbybit-dev/manifold";
26+
27+
// OCCT for Node.js
28+
import { OCCTService, OccHelper, VectorHelperService, ShapesHelperService } from "@bitbybit-dev/occt";
29+
import initOpenCascade, { BitbybitOcctModule } from "@bitbybit-dev/occt/bitbybit-dev-occt/index.js";
30+
31+
// Manifold WASM module
32+
import Module from "manifold-3d";
33+
34+
// JSON path for advanced JSON queries
35+
import { JSONPath } from "jsonpath-plus";
36+
37+
// NURBS functionality
38+
import vrb from "verb-nurbs-web";
739

840
const require = createRequire(import.meta.url);
941

1042
export class BitByBitBase {
11-
public occt!: OCCTService;
43+
// Base utilities
44+
public math!: MathBitByBit;
45+
public logic!: Logic;
46+
public lists!: Lists;
47+
public text!: TextBitByBit;
48+
public vector!: Vector;
49+
public point!: Point;
50+
public transforms!: Transforms;
51+
public color!: Color;
52+
public line!: Line;
53+
public polyline!: Polyline;
1254

13-
constructor() {
14-
}
55+
// Core functionality
56+
public json!: JSONBitByBit;
57+
public csv!: CSVBitByBit;
58+
public verb!: Verb;
59+
60+
// Geometry kernels
61+
public jscad!: Jscad;
62+
public manifold!: ManifoldService;
63+
public occt!: OCCTService;
1564

1665
async init() {
17-
// For Node.js, we need to specify the path to the WASM file
66+
// Initialize OCCT (OpenCascade) with locateFile for Node.js WASM loading
1867
const wasmPath = require.resolve("@bitbybit-dev/occt/bitbybit-dev-occt/bitbybit-dev-occt.5e93f201.wasm");
19-
20-
const occ = await initOpenCascade({
68+
const occ = await (initOpenCascade as (options?: { locateFile?: (path: string) => string }) => Promise<BitbybitOcctModule>)({
2169
locateFile: (path: string) => {
2270
if (path.endsWith(".wasm")) {
2371
return wasmPath;
@@ -26,6 +74,42 @@ export class BitByBitBase {
2674
}
2775
});
2876

77+
// Initialize JSCAD
78+
const jscadModule = await import("@bitbybit-dev/jscad/jscad-generated.js");
79+
const jscad = jscadModule.default();
80+
this.jscad = new Jscad(jscad);
81+
82+
// Initialize Manifold
83+
const manifoldWasmPath = require.resolve("manifold-3d/manifold.wasm");
84+
const wasm = await Module({
85+
locateFile: () => manifoldWasmPath,
86+
});
87+
wasm.setup();
88+
this.manifold = new ManifoldService(wasm);
89+
90+
// Initialize base utilities
91+
const geometryHelper = new GeometryHelper();
92+
this.math = new MathBitByBit();
93+
this.lists = new Lists();
94+
this.logic = new Logic();
95+
this.color = new Color(this.math);
96+
this.vector = new Vector(this.math, geometryHelper);
97+
this.transforms = new Transforms(this.vector, this.math);
98+
this.point = new Point(geometryHelper, this.transforms, this.vector, this.lists);
99+
this.line = new Line(this.vector, this.point, geometryHelper);
100+
this.polyline = new Polyline(this.vector, this.point, this.line, geometryHelper);
101+
this.text = new TextBitByBit(this.point);
102+
103+
// Initialize core functionality with context
104+
const context = new ContextBase();
105+
context.verb = { geom: vrb.geom, core: vrb.core };
106+
context.jsonpath = JSONPath;
107+
108+
this.verb = new Verb(context, geometryHelper, this.math);
109+
this.json = new JSONBitByBit(context);
110+
this.csv = new CSVBitByBit();
111+
112+
// Initialize OCCT service
29113
const vecHelper = new VectorHelperService();
30114
const shapesHelper = new ShapesHelperService();
31115
const occHelper = new OccHelper(vecHelper, shapesHelper, occ);

examples/node/express-app/index.ts

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,86 @@
11
import express, { Express, Request, Response } from "express";
22
import dotenv from "dotenv";
3-
import { BitByBitBase } from "./bitbybit.js";
3+
import { BitByBitBase } from "./bitbybit";
4+
5+
dotenv.config();
46

57
const bitbybit = new BitByBitBase();
8+
let initialized = false;
69

710
async function run() {
11+
console.log("Initializing BitByBit with OCCT, JSCAD, and Manifold kernels...");
812
await bitbybit.init();
13+
initialized = true;
14+
console.log("BitByBit initialized successfully!");
915
}
1016

1117
run();
1218

13-
dotenv.config();
14-
1519
const app: Express = express();
16-
const port = process.env.PORT;
17-
20+
const port = process.env.PORT || 3000;
1821

22+
// Endpoint demonstrating OCCT capabilities
1923
app.get("/", (req: Request, res: Response) => {
20-
if (bitbybit) {
21-
const cube = bitbybit.occt?.shapes.solid.createCube({ size: 3, center: [0, 0, 0] });
22-
const stepTXT = bitbybit.occt?.io.saveShapeSTEP({
23-
shape: cube,
24-
fileName: "cube.step",
25-
adjustYtoZ: true,
26-
tryDownload: false,
27-
});
28-
res.send(`${stepTXT}`);
29-
} else {
30-
res.send("OCC not initialised");
24+
if (!initialized) {
25+
res.send("BitByBit is still initializing...");
26+
return;
27+
}
28+
29+
const cube = bitbybit.occt.shapes.solid.createCube({ size: 3, center: [0, 0, 0] });
30+
const stepTXT = bitbybit.occt.io.saveShapeSTEP({
31+
shape: cube,
32+
fileName: "cube.step",
33+
adjustYtoZ: true,
34+
tryDownload: false,
35+
});
36+
res.send(`OCCT STEP output:\n${stepTXT}`);
37+
});
38+
39+
// Endpoint demonstrating math utilities
40+
app.get("/math", (req: Request, res: Response) => {
41+
if (!initialized) {
42+
res.send("BitByBit is still initializing...");
43+
return;
3144
}
45+
46+
const result = {
47+
remapValue: bitbybit.math.remap({ number: 0.5, fromLow: 0, fromHigh: 1, toLow: 0, toHigh: 100 }),
48+
randomNumber: bitbybit.math.random(),
49+
};
50+
res.json(result);
51+
});
52+
53+
// Endpoint demonstrating vector operations
54+
app.get("/vector", (req: Request, res: Response) => {
55+
if (!initialized) {
56+
res.send("BitByBit is still initializing...");
57+
return;
58+
}
59+
60+
const vec1: [number, number, number] = [1, 2, 3];
61+
const vec2: [number, number, number] = [4, 5, 6];
62+
const result = {
63+
add: bitbybit.vector.add({ first: vec1, second: vec2 }),
64+
cross: bitbybit.vector.cross({ first: vec1, second: vec2 }),
65+
dot: bitbybit.vector.dot({ first: vec1, second: vec2 }),
66+
length: bitbybit.vector.length({ vector: vec1 }),
67+
};
68+
res.json(result);
69+
});
70+
71+
// Endpoint demonstrating VERB NURBS
72+
app.get("/verb", (req: Request, res: Response) => {
73+
if (!initialized) {
74+
res.send("BitByBit is still initializing...");
75+
return;
76+
}
77+
78+
const curve = bitbybit.verb.curve.createBezierCurve({
79+
points: [[0, 0, 0], [1, 2, 0], [3, 1, 0], [4, 0, 0]],
80+
weights: [1, 1, 1, 1],
81+
});
82+
const point = bitbybit.verb.curve.pointAtParam({ curve, parameter: 0.5 });
83+
res.json({ curveType: "Bezier", pointAtHalf: point });
3284
});
3385

3486
app.listen(port, () => {

0 commit comments

Comments
 (0)