@bitbybit-dev/create-app 1.3.2-rc.1 → 1.4.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -124
- package/dist/index.js +253 -21
- package/dist/index.js.map +1 -1
- package/dist/scaffold.d.ts +30 -0
- package/dist/scaffold.d.ts.map +1 -0
- package/dist/scaffold.js +107 -0
- package/dist/scaffold.js.map +1 -0
- package/package.json +4 -3
- package/src/index.test.ts +309 -8
- package/src/index.ts +285 -24
- package/src/scaffold.test.ts +177 -0
- package/src/scaffold.ts +99 -0
- package/templates/_agent/AGENTS.md +39 -0
- package/templates/_agent/CLAUDE.md +1 -0
- package/templates/_agent/_dot-cursor/mcp.json +5 -0
- package/templates/_agent/_dot-gitignore +7 -0
- package/templates/_agent/_dot-mcp.json +5 -0
- package/templates/_agent/_dot-vscode/mcp.json +5 -0
- package/templates/apps/drone-assembly/AGENTS.section.md +20 -0
- package/templates/apps/drone-assembly/README.md +35 -0
- package/templates/apps/drone-assembly/eslint.config.js +47 -0
- package/templates/apps/drone-assembly/index.html +17 -0
- package/templates/apps/drone-assembly/package.json +31 -0
- package/templates/apps/drone-assembly/public/favicon.ico +0 -0
- package/templates/apps/drone-assembly/public/logo.png +0 -0
- package/templates/apps/drone-assembly/scripts/smoke.ts +172 -0
- package/templates/apps/drone-assembly/src/assembly.ts +99 -0
- package/templates/apps/drone-assembly/src/exports.ts +10 -0
- package/templates/apps/drone-assembly/src/kernel.ts +14 -0
- package/templates/apps/drone-assembly/src/main.ts +298 -0
- package/templates/apps/drone-assembly/src/materials.ts +58 -0
- package/templates/apps/drone-assembly/src/model.ts +280 -0
- package/templates/apps/drone-assembly/src/parts.ts +343 -0
- package/templates/apps/drone-assembly/src/style.css +219 -0
- package/templates/apps/drone-assembly/src/ui.ts +180 -0
- package/templates/apps/drone-assembly/src/vite-env.d.ts +1 -0
- package/templates/apps/drone-assembly/tsconfig.json +29 -0
- package/templates/apps/laser-cut-box/AGENTS.section.md +17 -0
- package/templates/apps/laser-cut-box/README.md +34 -0
- package/templates/apps/laser-cut-box/eslint.config.js +47 -0
- package/templates/apps/laser-cut-box/index.html +17 -0
- package/templates/apps/laser-cut-box/package.json +30 -0
- package/templates/apps/laser-cut-box/public/favicon.ico +0 -0
- package/templates/apps/laser-cut-box/public/logo.png +0 -0
- package/templates/apps/laser-cut-box/scripts/smoke.ts +106 -0
- package/templates/apps/laser-cut-box/src/exports.ts +28 -0
- package/templates/apps/laser-cut-box/src/main.ts +176 -0
- package/templates/apps/laser-cut-box/src/model.ts +92 -0
- package/templates/apps/laser-cut-box/src/panels.ts +229 -0
- package/templates/apps/laser-cut-box/src/style.css +223 -0
- package/templates/apps/laser-cut-box/src/ui.ts +136 -0
- package/templates/apps/laser-cut-box/src/vite-env.d.ts +1 -0
- package/templates/apps/laser-cut-box/tsconfig.json +29 -0
- package/templates/apps/product-configurator/AGENTS.section.md +19 -0
- package/templates/apps/product-configurator/README.md +33 -0
- package/templates/apps/product-configurator/eslint.config.js +47 -0
- package/templates/apps/product-configurator/index.html +17 -0
- package/templates/apps/product-configurator/package.json +30 -0
- package/templates/apps/product-configurator/public/favicon.ico +0 -0
- package/templates/apps/product-configurator/public/logo.png +0 -0
- package/templates/apps/product-configurator/scripts/smoke.ts +69 -0
- package/templates/apps/product-configurator/src/catalog.ts +56 -0
- package/templates/apps/product-configurator/src/exports.ts +19 -0
- package/templates/apps/product-configurator/src/main.ts +134 -0
- package/templates/apps/product-configurator/src/model.ts +153 -0
- package/templates/apps/product-configurator/src/style.css +177 -0
- package/templates/apps/product-configurator/src/ui.ts +150 -0
- package/templates/apps/product-configurator/src/vite-env.d.ts +1 -0
- package/templates/apps/product-configurator/tsconfig.json +29 -0
- package/templates/apps/sheet-metal-unfold/AGENTS.section.md +26 -0
- package/templates/apps/sheet-metal-unfold/README.md +41 -0
- package/templates/apps/sheet-metal-unfold/backend/_dot-env.example +5 -0
- package/templates/apps/sheet-metal-unfold/backend/eslint.config.js +47 -0
- package/templates/apps/sheet-metal-unfold/backend/package.json +28 -0
- package/templates/apps/sheet-metal-unfold/backend/scripts/smoke.ts +52 -0
- package/templates/apps/sheet-metal-unfold/backend/src/app.ts +65 -0
- package/templates/apps/sheet-metal-unfold/backend/src/cloud.ts +42 -0
- package/templates/apps/sheet-metal-unfold/backend/src/index.ts +13 -0
- package/templates/apps/sheet-metal-unfold/backend/tsconfig.build.json +9 -0
- package/templates/apps/sheet-metal-unfold/backend/tsconfig.json +27 -0
- package/templates/apps/sheet-metal-unfold/frontend/eslint.config.js +47 -0
- package/templates/apps/sheet-metal-unfold/frontend/index.html +17 -0
- package/templates/apps/sheet-metal-unfold/frontend/package.json +30 -0
- package/templates/apps/sheet-metal-unfold/frontend/public/favicon.ico +0 -0
- package/templates/apps/sheet-metal-unfold/frontend/public/logo.png +0 -0
- package/templates/apps/sheet-metal-unfold/frontend/scripts/smoke.ts +84 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/cloud-access.ts +37 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/exports.ts +10 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/flat.ts +71 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/main.ts +193 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/model.ts +166 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/style.css +291 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/ui.ts +150 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/vite-env.d.ts +1 -0
- package/templates/apps/sheet-metal-unfold/frontend/tsconfig.json +29 -0
- package/templates/apps/sheet-metal-unfold/frontend/vite.config.ts +9 -0
- package/templates/apps/sheet-metal-unfold/package.json +18 -0
- package/templates/apps/step-to-gltf-cli/AGENTS.section.md +20 -0
- package/templates/apps/step-to-gltf-cli/README.md +34 -0
- package/templates/apps/step-to-gltf-cli/eslint.config.js +47 -0
- package/templates/apps/step-to-gltf-cli/package.json +26 -0
- package/templates/apps/step-to-gltf-cli/samples/bracket.step +2545 -0
- package/templates/apps/step-to-gltf-cli/scripts/smoke.ts +49 -0
- package/templates/apps/step-to-gltf-cli/src/cli.ts +106 -0
- package/templates/apps/step-to-gltf-cli/src/cloud.ts +34 -0
- package/templates/apps/step-to-gltf-cli/src/convert.ts +65 -0
- package/templates/apps/step-to-gltf-cli/src/kernel.ts +14 -0
- package/templates/apps/step-to-gltf-cli/tsconfig.json +27 -0
- package/templates/cloud/AGENTS.section.md +22 -0
- package/templates/cloud/backends/dotnet-rest/BitbybitClient.cs +22 -50
- package/templates/cloud/backends/dotnet-rest/Program.cs +1 -11
- package/templates/cloud/backends/dotnet-rest/dotnet-rest.csproj +4 -1
- package/templates/cloud/backends/hono-rest/eslint.config.js +47 -0
- package/templates/cloud/backends/hono-rest/package.json +11 -4
- package/templates/cloud/backends/hono-rest/src/bitbybit-client.ts +34 -45
- package/templates/cloud/backends/hono-rest/src/index.ts +1 -11
- package/templates/cloud/backends/hono-rest/tsconfig.json +19 -8
- package/templates/cloud/backends/hono-sdk/eslint.config.js +47 -0
- package/templates/cloud/backends/hono-sdk/package.json +12 -5
- package/templates/cloud/backends/hono-sdk/src/bitbybit-client.ts +6 -37
- package/templates/cloud/backends/hono-sdk/src/index.ts +1 -12
- package/templates/cloud/backends/hono-sdk/tsconfig.json +19 -8
- package/templates/cloud/backends/nodejs-rest/eslint.config.js +47 -0
- package/templates/cloud/backends/nodejs-rest/package.json +12 -5
- package/templates/cloud/backends/nodejs-rest/src/bitbybit-client.ts +6 -35
- package/templates/cloud/backends/nodejs-rest/src/index.ts +6 -16
- package/templates/cloud/backends/nodejs-rest/tsconfig.build.json +9 -0
- package/templates/cloud/backends/nodejs-rest/tsconfig.json +17 -7
- package/templates/cloud/backends/nodejs-sdk/eslint.config.js +47 -0
- package/templates/cloud/backends/nodejs-sdk/package.json +14 -7
- package/templates/cloud/backends/nodejs-sdk/src/bitbybit-client.ts +8 -33
- package/templates/cloud/backends/nodejs-sdk/src/index.ts +5 -16
- package/templates/cloud/backends/nodejs-sdk/tsconfig.build.json +9 -0
- package/templates/cloud/backends/nodejs-sdk/tsconfig.json +19 -9
- package/templates/cloud/frontend/eslint.config.js +50 -0
- package/templates/cloud/frontend/index.html +3 -1
- package/templates/cloud/frontend/package.json +13 -4
- package/templates/cloud/frontend/public/favicon.ico +0 -0
- package/templates/cloud/frontend/public/logo.png +0 -0
- package/templates/cloud/frontend/src/App.tsx +11 -9
- package/templates/cloud/frontend/src/api.ts +31 -0
- package/templates/cloud/frontend/src/components/ApiKeyWarning.tsx +28 -25
- package/templates/cloud/frontend/src/components/Header.tsx +4 -2
- package/templates/cloud/frontend/src/components/Tabs.tsx +4 -3
- package/templates/cloud/frontend/src/components/Viewer.tsx +57 -77
- package/templates/cloud/frontend/src/main.tsx +6 -1
- package/templates/cloud/frontend/src/panels/ModelsPanel.tsx +65 -73
- package/templates/cloud/frontend/src/panels/PipelinesPanel.tsx +82 -83
- package/templates/cloud/frontend/src/vite-env.d.ts +1 -0
- package/templates/cloud/frontend/tsconfig.json +19 -7
- package/templates/vite/babylonjs/typescript/AGENTS.section.md +13 -0
- package/templates/vite/babylonjs/typescript/eslint.config.js +47 -0
- package/templates/vite/babylonjs/typescript/index.html +4 -2
- package/templates/vite/babylonjs/typescript/package.json +14 -4
- package/templates/vite/babylonjs/typescript/public/favicon.ico +0 -0
- package/templates/vite/babylonjs/typescript/public/logo.png +0 -0
- package/templates/vite/babylonjs/typescript/scripts/smoke.ts +41 -0
- package/templates/vite/babylonjs/typescript/src/main.ts +8 -17
- package/templates/vite/babylonjs/typescript/src/model.ts +24 -0
- package/templates/vite/babylonjs/typescript/tsconfig.json +16 -12
- package/templates/vite/playcanvas/typescript/AGENTS.section.md +13 -0
- package/templates/vite/playcanvas/typescript/eslint.config.js +47 -0
- package/templates/vite/playcanvas/typescript/index.html +4 -2
- package/templates/vite/playcanvas/typescript/package.json +14 -4
- package/templates/vite/playcanvas/typescript/public/favicon.ico +0 -0
- package/templates/vite/playcanvas/typescript/public/logo.png +0 -0
- package/templates/vite/playcanvas/typescript/scripts/smoke.ts +41 -0
- package/templates/vite/playcanvas/typescript/src/main.ts +11 -22
- package/templates/vite/playcanvas/typescript/src/model.ts +24 -0
- package/templates/vite/playcanvas/typescript/tsconfig.json +16 -12
- package/templates/vite/threejs/typescript/AGENTS.section.md +13 -0
- package/templates/vite/threejs/typescript/eslint.config.js +47 -0
- package/templates/vite/threejs/typescript/index.html +4 -2
- package/templates/vite/threejs/typescript/package.json +16 -6
- package/templates/vite/threejs/typescript/public/favicon.ico +0 -0
- package/templates/vite/threejs/typescript/public/logo.png +0 -0
- package/templates/vite/threejs/typescript/scripts/smoke.ts +41 -0
- package/templates/vite/threejs/typescript/src/main.ts +11 -22
- package/templates/vite/threejs/typescript/src/model.ts +24 -0
- package/templates/vite/threejs/typescript/tsconfig.json +16 -12
- package/templates/vite/babylonjs/typescript/public/vite.svg +0 -1
- package/templates/vite/playcanvas/typescript/public/vite.svg +0 -1
- package/templates/vite/threejs/typescript/public/vite.svg +0 -1
- /package/templates/cloud/backends/dotnet-rest/{_gitignore → _dot-gitignore} +0 -0
- /package/templates/cloud/backends/hono-rest/{_gitignore → _dot-gitignore} +0 -0
- /package/templates/cloud/backends/hono-sdk/{_gitignore → _dot-gitignore} +0 -0
- /package/templates/cloud/backends/nodejs-rest/{.env.example → _dot-env.example} +0 -0
- /package/templates/cloud/backends/nodejs-rest/{_gitignore → _dot-gitignore} +0 -0
- /package/templates/cloud/backends/nodejs-sdk/{.env.example → _dot-env.example} +0 -0
- /package/templates/cloud/backends/nodejs-sdk/{_gitignore → _dot-gitignore} +0 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const CAD_CLOUD_URL = "https://bitbybit.dev/cad-cloud";
|
|
2
|
+
export const STUDIO_KEYS_URL = "https://studio.bitbybit.dev/keys/billing";
|
|
3
|
+
export const KEY_LINE = "BITBYBIT_API_KEY=your-key-here";
|
|
4
|
+
export const NO_KEY_CODE = "NO_API_KEY";
|
|
5
|
+
|
|
6
|
+
export type Access =
|
|
7
|
+
| { state: "ready" }
|
|
8
|
+
| { state: "locked"; reason: string }
|
|
9
|
+
| { state: "unreachable"; message: string }
|
|
10
|
+
| { state: "error"; message: string };
|
|
11
|
+
|
|
12
|
+
const text = (value: unknown, fallback: string): string => (typeof value === "string" ? value : fallback);
|
|
13
|
+
|
|
14
|
+
export function accessFromResponse(status: number, body: unknown): Access {
|
|
15
|
+
const record = body && typeof body === "object" ? (body as Record<string, unknown>) : {};
|
|
16
|
+
if (status === 503 && record["code"] === NO_KEY_CODE) return { state: "locked", reason: text(record["error"], "no API key configured") };
|
|
17
|
+
if (status === 502 || status === 504 || status === 0) return { state: "unreachable", message: "the backend is not running; start it with: cd backend && npm run dev" };
|
|
18
|
+
if (status >= 200 && status < 300) return { state: "ready" };
|
|
19
|
+
return { state: "error", message: text(record["error"], `HTTP ${status}`) };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function renderAccess(root: HTMLElement, access: Access): void {
|
|
23
|
+
root.classList.toggle("hidden", access.state === "ready");
|
|
24
|
+
if (access.state === "ready") { root.innerHTML = ""; return; }
|
|
25
|
+
if (access.state === "locked") {
|
|
26
|
+
root.innerHTML = `
|
|
27
|
+
<strong>Unfolding needs CAD Cloud</strong>
|
|
28
|
+
Everything else on this page runs in your browser on the open-source packages. The sheet-metal unfold is a Pro algorithm that exists only on
|
|
29
|
+
<a href="${CAD_CLOUD_URL}" target="_blank" rel="noopener noreferrer">CAD Cloud</a>; any plan includes it.
|
|
30
|
+
Create a key in <a href="${STUDIO_KEYS_URL}" target="_blank" rel="noopener noreferrer">Bitbybit Studio</a>, put it in <code>backend/.env</code> as
|
|
31
|
+
<code>${KEY_LINE}</code>
|
|
32
|
+
and restart the backend. The key stays on the server; the browser never sees it.
|
|
33
|
+
`;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
root.innerHTML = `<strong>${access.state === "unreachable" ? "Backend not reachable" : "Unfold failed"}</strong>${access.message}`;
|
|
37
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function downloadBlob(blob: Blob, fileName: string): void {
|
|
2
|
+
const url = URL.createObjectURL(blob);
|
|
3
|
+
const link = document.createElement("a");
|
|
4
|
+
link.href = url;
|
|
5
|
+
link.download = fileName;
|
|
6
|
+
document.body.appendChild(link);
|
|
7
|
+
link.click();
|
|
8
|
+
link.remove();
|
|
9
|
+
setTimeout(() => { URL.revokeObjectURL(url); }, 1000);
|
|
10
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export type Vec3 = [number, number, number];
|
|
2
|
+
|
|
3
|
+
export interface UnfoldBend {
|
|
4
|
+
angleDeg: number;
|
|
5
|
+
innerRadius: number;
|
|
6
|
+
allowance: number;
|
|
7
|
+
bendDeduction: number;
|
|
8
|
+
bendLineLength: number;
|
|
9
|
+
lineStart: Vec3;
|
|
10
|
+
lineEnd: Vec3;
|
|
11
|
+
direction: "up" | "down";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface UnfoldSolidReport {
|
|
15
|
+
solidIndex: number;
|
|
16
|
+
ok: boolean;
|
|
17
|
+
error?: string;
|
|
18
|
+
warning?: string;
|
|
19
|
+
thickness?: number;
|
|
20
|
+
kFactor?: number;
|
|
21
|
+
bends?: UnfoldBend[];
|
|
22
|
+
flatBBox?: [number, number, number, number];
|
|
23
|
+
flatArea?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface UnfoldResult {
|
|
27
|
+
taskId: string;
|
|
28
|
+
report: UnfoldSolidReport[];
|
|
29
|
+
flatStep: string;
|
|
30
|
+
downloads: { format: string; downloadUrl: string; filename: string }[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface FlatSummary {
|
|
34
|
+
solids: number;
|
|
35
|
+
unfolded: number;
|
|
36
|
+
bends: number;
|
|
37
|
+
thickness: number | null;
|
|
38
|
+
flatArea: number;
|
|
39
|
+
flatWidth: number | null;
|
|
40
|
+
flatHeight: number | null;
|
|
41
|
+
developedLength: number | null;
|
|
42
|
+
problems: string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function bendLines(report: UnfoldSolidReport[]): [Vec3, Vec3][] {
|
|
46
|
+
return report.flatMap((solid) => (solid.ok && solid.bends ? solid.bends.map((bend): [Vec3, Vec3] => [bend.lineStart, bend.lineEnd]) : []));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function toScene([x, y, z]: Vec3): Vec3 {
|
|
50
|
+
return [x, z, -y];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function summarize(report: UnfoldSolidReport[]): FlatSummary {
|
|
54
|
+
const unfolded = report.filter((solid) => solid.ok);
|
|
55
|
+
const bends = unfolded.flatMap((solid) => solid.bends ?? []);
|
|
56
|
+
const first = unfolded[0];
|
|
57
|
+
const box = first?.flatBBox;
|
|
58
|
+
const width = box ? box[2] - box[0] : null;
|
|
59
|
+
const height = box ? box[3] - box[1] : null;
|
|
60
|
+
return {
|
|
61
|
+
solids: report.length,
|
|
62
|
+
unfolded: unfolded.length,
|
|
63
|
+
bends: bends.length,
|
|
64
|
+
thickness: first?.thickness ?? null,
|
|
65
|
+
flatArea: unfolded.reduce((sum, solid) => sum + (solid.flatArea ?? 0), 0),
|
|
66
|
+
flatWidth: width,
|
|
67
|
+
flatHeight: height,
|
|
68
|
+
developedLength: width !== null && height !== null ? Math.max(width, height) : null,
|
|
69
|
+
problems: report.filter((solid) => !solid.ok).map((solid) => `solid ${solid.solidIndex}: ${solid.error ?? "could not be unfolded"}`),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import "./style.css";
|
|
2
|
+
import type * as THREE from "three";
|
|
3
|
+
import { ACESFilmicToneMapping, Color, MeshPhysicalMaterial, PMREMGenerator, Vector3 } from "three";
|
|
4
|
+
import { RoomEnvironment } from "three/addons/environments/RoomEnvironment.js";
|
|
5
|
+
import { BitByBitBase, Inputs, initBitByBit, initThreeJS, type InitBitByBitOptions } from "@bitbybit-dev/threejs";
|
|
6
|
+
import { accessFromResponse, renderAccess } from "./cloud-access";
|
|
7
|
+
import { downloadBlob } from "./exports";
|
|
8
|
+
import { bendLines, summarize, toScene, type UnfoldResult } from "./flat";
|
|
9
|
+
import { buildModel, defaultParams, type PartModel, type PartParams } from "./model";
|
|
10
|
+
import { mountPoint, mountUi, type ExportFormat, type View } from "./ui";
|
|
11
|
+
|
|
12
|
+
type Drawn = THREE.Group;
|
|
13
|
+
type Shape = PartModel["shape"];
|
|
14
|
+
|
|
15
|
+
const LAYOUT_LIFT = 1;
|
|
16
|
+
|
|
17
|
+
void start();
|
|
18
|
+
|
|
19
|
+
function isMesh(object: THREE.Object3D): object is THREE.Mesh {
|
|
20
|
+
return "isMesh" in object && object.isMesh === true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function cameraOptions(sceneSize: number): Inputs.ThreeJSCamera.OrbitCameraDto {
|
|
24
|
+
const camera = new Inputs.ThreeJSCamera.OrbitCameraDto();
|
|
25
|
+
camera.distance = sceneSize * Math.SQRT2;
|
|
26
|
+
camera.distanceMin = sceneSize * 0.02;
|
|
27
|
+
camera.distanceMax = sceneSize * 10;
|
|
28
|
+
return camera;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function start(): Promise<void> {
|
|
32
|
+
const sceneOptions = new Inputs.ThreeJSScene.InitThreeJSDto();
|
|
33
|
+
sceneOptions.canvasId = "three-canvas";
|
|
34
|
+
sceneOptions.sceneSize = 400;
|
|
35
|
+
sceneOptions.enableGround = true;
|
|
36
|
+
sceneOptions.groundColor = "#2a2d31";
|
|
37
|
+
sceneOptions.enableShadows = true;
|
|
38
|
+
sceneOptions.orbitCameraOptions = cameraOptions(sceneOptions.sceneSize);
|
|
39
|
+
const { scene, renderer, orbitCamera, startAnimationLoop } = initThreeJS(sceneOptions);
|
|
40
|
+
startAnimationLoop();
|
|
41
|
+
|
|
42
|
+
renderer.toneMapping = ACESFilmicToneMapping;
|
|
43
|
+
renderer.toneMappingExposure = 1.05;
|
|
44
|
+
scene.environment = new PMREMGenerator(renderer).fromScene(new RoomEnvironment(), 0.04).texture;
|
|
45
|
+
scene.environmentIntensity = 0.6;
|
|
46
|
+
const steel = new MeshPhysicalMaterial({ color: new Color("#b8bcc2"), metalness: 1, roughness: 0.38 });
|
|
47
|
+
|
|
48
|
+
const bitbybit = new BitByBitBase();
|
|
49
|
+
const options: InitBitByBitOptions = {
|
|
50
|
+
enableOCCT: true,
|
|
51
|
+
enableJSCAD: false,
|
|
52
|
+
enableManifold: false,
|
|
53
|
+
};
|
|
54
|
+
await initBitByBit(scene, bitbybit, options);
|
|
55
|
+
|
|
56
|
+
let params: PartParams = { ...defaultParams };
|
|
57
|
+
let view: View = "part";
|
|
58
|
+
let part: { model: PartModel; drawn: Drawn[] } | undefined;
|
|
59
|
+
let flat: { result: UnfoldResult; shape: Shape; drawn: Drawn[] } | undefined;
|
|
60
|
+
let generation = 0;
|
|
61
|
+
let timer: number | undefined;
|
|
62
|
+
|
|
63
|
+
const ui = mountUi(mountPoint("panel"), params, {
|
|
64
|
+
onChange: (next) => {
|
|
65
|
+
params = next;
|
|
66
|
+
window.clearTimeout(timer);
|
|
67
|
+
timer = window.setTimeout(() => void rebuild(), 150);
|
|
68
|
+
},
|
|
69
|
+
onView: (next) => { view = next; void redraw(); },
|
|
70
|
+
onUnfold: () => void unfold(),
|
|
71
|
+
onExport: (format) => void exportModel(format),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
async function rebuild(): Promise<void> {
|
|
75
|
+
const mine = ++generation;
|
|
76
|
+
ui.setStatus("Building the part...");
|
|
77
|
+
try {
|
|
78
|
+
const model = await buildModel(bitbybit.occt, params);
|
|
79
|
+
if (mine !== generation) return;
|
|
80
|
+
if (part) part.drawn.forEach(dispose);
|
|
81
|
+
part = { model, drawn: [] };
|
|
82
|
+
if (flat) { flat.drawn.forEach(dispose); flat = undefined; ui.setFlat(null); }
|
|
83
|
+
view = "part";
|
|
84
|
+
await redraw();
|
|
85
|
+
ui.setEstimate(model.estimate, model.volume);
|
|
86
|
+
ui.setStatus(`Part built in the browser: ${model.estimate.bends} bends, ${(model.volume / 1000).toFixed(1)} cm³ of sheet.`);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
ui.setStatus(`Could not build this part: ${error instanceof Error ? error.message : String(error)}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function redraw(): Promise<void> {
|
|
93
|
+
if (part) { part.drawn.forEach(dispose); part.drawn = []; }
|
|
94
|
+
if (flat) { flat.drawn.forEach(dispose); flat.drawn = []; }
|
|
95
|
+
const drawOptions = new Inputs.Draw.DrawOcctShapeOptions();
|
|
96
|
+
drawOptions.faceMaterial = steel;
|
|
97
|
+
drawOptions.edgeColour = "#26292d";
|
|
98
|
+
drawOptions.edgeWidth = 1;
|
|
99
|
+
drawOptions.drawEdges = true;
|
|
100
|
+
drawOptions.drawTwoSided = false;
|
|
101
|
+
drawOptions.precision = 0.02;
|
|
102
|
+
if (view === "part" && part) {
|
|
103
|
+
part.drawn.push(await bitbybit.draw.drawAnyAsync({ entity: part.model.shape, options: drawOptions }));
|
|
104
|
+
frame(Math.max(part.model.params.baseWidth, part.model.params.flangeHeight, part.model.params.depth), part.model.params.flangeHeight / 2);
|
|
105
|
+
} else if (view === "flat" && flat) {
|
|
106
|
+
flat.drawn.push(await bitbybit.draw.drawAnyAsync({ entity: flat.shape, options: drawOptions }));
|
|
107
|
+
const lineOptions = new Inputs.Draw.DrawBasicGeometryOptions();
|
|
108
|
+
lineOptions.colours = "#f0cebb";
|
|
109
|
+
lineOptions.size = 2;
|
|
110
|
+
for (const [start, end] of bendLines(flat.result.report)) {
|
|
111
|
+
const lifted = (point: ReturnType<typeof toScene>): ReturnType<typeof toScene> => [point[0], point[1] + LAYOUT_LIFT + 0.1, point[2]];
|
|
112
|
+
flat.drawn.push(await bitbybit.draw.drawAnyAsync({ entity: { points: [lifted(toScene(start)), lifted(toScene(end))] }, options: lineOptions }));
|
|
113
|
+
}
|
|
114
|
+
const summary = summarize(flat.result.report);
|
|
115
|
+
frame(Math.max(summary.flatWidth ?? 100, summary.flatHeight ?? 100), 0);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function frame(extent: number, targetHeight: number): void {
|
|
120
|
+
if (!orbitCamera) return;
|
|
121
|
+
const distance = extent * 2.2;
|
|
122
|
+
const target = new Vector3(0, targetHeight, 0);
|
|
123
|
+
orbitCamera.orbitCamera.resetAndLookAtPoint(new Vector3(distance * 0.6, targetHeight + distance * 0.55, distance * 0.6), target);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function unfold(): Promise<void> {
|
|
127
|
+
if (!part) return;
|
|
128
|
+
ui.setBusy(true);
|
|
129
|
+
ui.setStatus("Exporting the part as STEP and sending it to CAD Cloud through the backend...");
|
|
130
|
+
try {
|
|
131
|
+
const stepText = await bitbybit.occt.io.saveShapeSTEPAndReturn({ shape: part.model.shape, fileName: "channel.step", adjustYtoZ: true, tryDownload: false });
|
|
132
|
+
const response = await fetch("/api/unfold", {
|
|
133
|
+
method: "POST",
|
|
134
|
+
headers: { "Content-Type": "application/json" },
|
|
135
|
+
body: JSON.stringify({ stepText, fileName: "channel.step", kFactor: part.model.params.kFactor }),
|
|
136
|
+
}).catch(() => null);
|
|
137
|
+
const status = response?.status ?? 0;
|
|
138
|
+
const body: unknown = response ? await response.json().catch(() => null) : null;
|
|
139
|
+
const access = accessFromResponse(status, body);
|
|
140
|
+
renderAccess(ui.accessRoot, access);
|
|
141
|
+
if (access.state !== "ready") {
|
|
142
|
+
ui.setStatus(access.state === "locked" ? "The part still works without a key; only the unfold needs one." : access.message);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const result = body as UnfoldResult;
|
|
146
|
+
const loaded = await bitbybit.occt.io.loadSTEPorIGESFromText({ text: result.flatStep, fileType: Inputs.OCCT.fileTypeEnum.step, adjustZtoY: true });
|
|
147
|
+
const shape = await bitbybit.occt.transforms.translate({ shape: loaded, translation: [0, LAYOUT_LIFT, 0] });
|
|
148
|
+
if (flat) flat.drawn.forEach(dispose);
|
|
149
|
+
flat = { result, shape, drawn: [] };
|
|
150
|
+
const summary = summarize(result.report);
|
|
151
|
+
ui.setFlat(summary);
|
|
152
|
+
view = summary.unfolded > 0 ? "flat" : "part";
|
|
153
|
+
for (const button of document.querySelectorAll<HTMLButtonElement>(".views button")) button.classList.toggle("active", button.dataset["view"] === view);
|
|
154
|
+
await redraw();
|
|
155
|
+
ui.setStatus(summary.unfolded > 0
|
|
156
|
+
? `Unfolded on CAD Cloud as task ${result.taskId}: ${summary.bends} bends, ${(summary.developedLength ?? 0).toFixed(2)} mm developed against the ${part.model.estimate.total.toFixed(2)} mm estimate.`
|
|
157
|
+
: `CAD Cloud could not unfold this part: ${summary.problems.join("; ")}`);
|
|
158
|
+
} catch (error) {
|
|
159
|
+
ui.setStatus(`Unfold failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
160
|
+
} finally {
|
|
161
|
+
ui.setBusy(false);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function exportModel(format: ExportFormat): Promise<void> {
|
|
166
|
+
const p = params;
|
|
167
|
+
const name = `channel-${p.baseWidth}x${p.flangeHeight}x${p.depth}`;
|
|
168
|
+
if (format === "part-step" && part) {
|
|
169
|
+
await bitbybit.occt.io.saveShapeSTEP({ shape: part.model.shape, fileName: `${name}.step`, adjustYtoZ: true, tryDownload: true });
|
|
170
|
+
} else if (format === "flat-step" && flat) {
|
|
171
|
+
downloadBlob(new Blob([flat.result.flatStep], { type: "application/step" }), `${name}-flat.step`);
|
|
172
|
+
} else if (format === "flat-dxf" && flat) {
|
|
173
|
+
const paths = await bitbybit.occt.io.shapeToDxfPaths({ shape: flat.shape, angularDeflection: 0.1, curvatureDeflection: 0.1, minimumOfPoints: 2, uTolerance: 1e-9, minimumLength: 1e-7 });
|
|
174
|
+
const cut = await bitbybit.occt.io.dxfPathsWithLayer({ paths, layer: "cut", color: "#000000" });
|
|
175
|
+
const bends = bendLines(flat.result.report).map(([start, end]) => ({ segments: [{ points: [[start[0], start[1]], [end[0], end[1]]] as [number, number][], closed: false }] }));
|
|
176
|
+
const bendLayer = await bitbybit.occt.io.dxfPathsWithLayer({ paths: bends, layer: "bend", color: "#ff0000" });
|
|
177
|
+
await bitbybit.occt.io.dxfCreate({ pathsParts: [cut, bendLayer], colorFormat: Inputs.OCCT.dxfColorFormatEnum.aci, acadVersion: Inputs.OCCT.dxfAcadVersionEnum.AC1009, fileName: `${name}-flat.dxf`, tryDownload: true });
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function dispose(group: Drawn): void {
|
|
182
|
+
scene.remove(group);
|
|
183
|
+
group.traverse((object) => {
|
|
184
|
+
if (!isMesh(object)) return;
|
|
185
|
+
object.geometry.dispose();
|
|
186
|
+
for (const material of Array.isArray(object.material) ? object.material : [object.material]) {
|
|
187
|
+
if (material !== steel) material.dispose();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
await rebuild();
|
|
193
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type { BitByBitBase } from "@bitbybit-dev/threejs";
|
|
2
|
+
|
|
3
|
+
export type Occt = BitByBitBase["occt"];
|
|
4
|
+
|
|
5
|
+
export type Point3 = [number, number, number];
|
|
6
|
+
|
|
7
|
+
export interface PartParams {
|
|
8
|
+
baseWidth: number;
|
|
9
|
+
flangeHeight: number;
|
|
10
|
+
lipHeight: number;
|
|
11
|
+
depth: number;
|
|
12
|
+
thickness: number;
|
|
13
|
+
bendRadius: number;
|
|
14
|
+
kFactor: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface FlatEstimate {
|
|
18
|
+
bends: number;
|
|
19
|
+
outerLengths: number;
|
|
20
|
+
removedAtBends: number;
|
|
21
|
+
allowancePerBend: number;
|
|
22
|
+
total: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PartModel {
|
|
26
|
+
params: PartParams;
|
|
27
|
+
shape: Awaited<ReturnType<Occt["operations"]["pipe"]>>;
|
|
28
|
+
volume: number;
|
|
29
|
+
profileArea: number;
|
|
30
|
+
estimate: FlatEstimate;
|
|
31
|
+
mounting: Mounting;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface Mounting {
|
|
35
|
+
baseHoles: [number, number][];
|
|
36
|
+
|
|
37
|
+
flangeSlots: { x: number; y: number; length: number }[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const MOUNTING = { holeRadius: 2.2, slotLength: 10, margin: 3 } as const;
|
|
41
|
+
|
|
42
|
+
export const UNITS = "mm";
|
|
43
|
+
|
|
44
|
+
export const LIMITS = {
|
|
45
|
+
baseWidth: { min: 20, max: 300, step: 1 },
|
|
46
|
+
flangeHeight: { min: 10, max: 200, step: 1 },
|
|
47
|
+
lipHeight: { min: 0, max: 60, step: 1 },
|
|
48
|
+
depth: { min: 10, max: 300, step: 1 },
|
|
49
|
+
thickness: { min: 0.5, max: 6, step: 0.1 },
|
|
50
|
+
bendRadius: { min: 0.5, max: 12, step: 0.1 },
|
|
51
|
+
kFactor: { min: 0.3, max: 0.5, step: 0.01 },
|
|
52
|
+
} as const;
|
|
53
|
+
|
|
54
|
+
export const defaultParams: PartParams = {
|
|
55
|
+
baseWidth: 80,
|
|
56
|
+
flangeHeight: 40,
|
|
57
|
+
lipHeight: 12,
|
|
58
|
+
depth: 60,
|
|
59
|
+
thickness: 2,
|
|
60
|
+
bendRadius: 2,
|
|
61
|
+
kFactor: 0.44,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export function clampParams(params: PartParams): PartParams {
|
|
65
|
+
const clamp = (value: number, limit: { min: number; max: number }): number => Math.min(Math.max(value, limit.min), limit.max);
|
|
66
|
+
const thickness = clamp(params.thickness, LIMITS.thickness);
|
|
67
|
+
const bendRadius = clamp(params.bendRadius, LIMITS.bendRadius);
|
|
68
|
+
const bend = bendRadius + thickness;
|
|
69
|
+
const baseWidth = Math.max(clamp(params.baseWidth, LIMITS.baseWidth), 2 * bend + 4 * thickness);
|
|
70
|
+
const lipRequested = clamp(params.lipHeight, LIMITS.lipHeight);
|
|
71
|
+
const lipHeight = lipRequested === 0 ? 0 : Math.max(lipRequested, bend + 2 * thickness);
|
|
72
|
+
const flangeHeight = Math.max(clamp(params.flangeHeight, LIMITS.flangeHeight), (lipHeight > 0 ? 2 : 1) * bend + 2 * thickness);
|
|
73
|
+
return {
|
|
74
|
+
baseWidth, flangeHeight, lipHeight, thickness, bendRadius,
|
|
75
|
+
depth: clamp(params.depth, LIMITS.depth),
|
|
76
|
+
kFactor: clamp(params.kFactor, LIMITS.kFactor),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function centreLine(p: PartParams): Point3[] {
|
|
81
|
+
const { baseWidth: w, flangeHeight: h, lipHeight: l, thickness: t } = p;
|
|
82
|
+
const half = w / 2;
|
|
83
|
+
const mid = t / 2;
|
|
84
|
+
if (l === 0) {
|
|
85
|
+
return [[-half + mid, h, 0], [-half + mid, mid, 0], [half - mid, mid, 0], [half - mid, h, 0]];
|
|
86
|
+
}
|
|
87
|
+
return [[-half + l, h - mid, 0], [-half + mid, h - mid, 0], [-half + mid, mid, 0], [half - mid, mid, 0], [half - mid, h - mid, 0], [half - l, h - mid, 0]];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function midRadius(p: PartParams): number {
|
|
91
|
+
return p.bendRadius + p.thickness / 2;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function centreLineLength(p: PartParams): number {
|
|
95
|
+
const line = centreLine(p);
|
|
96
|
+
const bends = line.length - 2;
|
|
97
|
+
const R = midRadius(p);
|
|
98
|
+
let straight = 0;
|
|
99
|
+
for (const [i, b] of line.entries()) {
|
|
100
|
+
const a = line[i - 1];
|
|
101
|
+
if (a) straight += Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
102
|
+
}
|
|
103
|
+
return straight - 2 * R * bends + bends * (Math.PI / 2) * R;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function profileArea(p: PartParams): number {
|
|
107
|
+
return p.thickness * centreLineLength(p);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function flatLengthEstimate(p: PartParams): FlatEstimate {
|
|
111
|
+
const bends = p.lipHeight > 0 ? 4 : 2;
|
|
112
|
+
const outerLengths = p.baseWidth + 2 * p.flangeHeight + 2 * p.lipHeight;
|
|
113
|
+
const removedAtBends = bends * 2 * (p.bendRadius + p.thickness);
|
|
114
|
+
const allowancePerBend = (Math.PI / 2) * (p.bendRadius + p.kFactor * p.thickness);
|
|
115
|
+
return { bends, outerLengths, removedAtBends, allowancePerBend, total: outerLengths - removedAtBends + bends * allowancePerBend };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function mounting(p: PartParams): Mounting {
|
|
119
|
+
const { holeRadius: r, slotLength, margin } = MOUNTING;
|
|
120
|
+
const bend = p.bendRadius + p.thickness;
|
|
121
|
+
const half = p.baseWidth / 2;
|
|
122
|
+
const baseHoles: [number, number][] = half / 2 + r + margin <= half - bend && p.depth / 4 + r + margin <= p.depth / 2
|
|
123
|
+
? [[-half / 2, -p.depth / 4], [half / 2, p.depth / 4]]
|
|
124
|
+
: [];
|
|
125
|
+
const flatTop = p.lipHeight > 0 ? p.flangeHeight - bend : p.flangeHeight;
|
|
126
|
+
const flangeSlots = flatTop - bend >= 2 * r + 2 * margin && p.depth >= slotLength + 2 * margin
|
|
127
|
+
? [{ x: -half, y: (bend + flatTop) / 2, length: slotLength }, { x: half - p.thickness, y: (bend + flatTop) / 2, length: slotLength }]
|
|
128
|
+
: [];
|
|
129
|
+
return { baseHoles, flangeSlots };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const SLOT_CORNER = MOUNTING.holeRadius * 0.99;
|
|
133
|
+
|
|
134
|
+
export function removedVolume(p: PartParams): number {
|
|
135
|
+
const m = mounting(p);
|
|
136
|
+
const { holeRadius: r } = MOUNTING;
|
|
137
|
+
const hole = Math.PI * r * r;
|
|
138
|
+
const slot = 2 * r * MOUNTING.slotLength - (4 - Math.PI) * SLOT_CORNER * SLOT_CORNER;
|
|
139
|
+
return (m.baseHoles.length * hole + m.flangeSlots.length * slot) * p.thickness;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function buildModel(occt: Occt, params: PartParams): Promise<PartModel> {
|
|
143
|
+
const p = clampParams(params);
|
|
144
|
+
const sharp = await occt.shapes.wire.createPolylineWire({ points: centreLine(p) });
|
|
145
|
+
const path = await occt.fillets.fillet2d({ shape: sharp, radius: midRadius(p) });
|
|
146
|
+
const start = await occt.shapes.wire.startPointOnWire({ shape: path });
|
|
147
|
+
const tangent = await occt.shapes.wire.tangentOnWireAtParam({ shape: path, param: 0 });
|
|
148
|
+
const length = Math.hypot(tangent[0], tangent[1], tangent[2]);
|
|
149
|
+
const direction: Point3 = [tangent[0] / length, tangent[1] / length, tangent[2] / length];
|
|
150
|
+
const section = await occt.shapes.wire.createRectangleWire({ width: p.thickness, length: p.depth, center: start, direction });
|
|
151
|
+
const swept = await occt.operations.pipe({ shape: path, shapes: [section] });
|
|
152
|
+
const features = mounting(p);
|
|
153
|
+
const cutters = [];
|
|
154
|
+
for (const [x, z] of features.baseHoles) {
|
|
155
|
+
cutters.push(await occt.shapes.solid.createCylinder({ radius: MOUNTING.holeRadius, height: p.thickness + 2, center: [x, -1, z], direction: [0, 1, 0] }));
|
|
156
|
+
}
|
|
157
|
+
for (const slot of features.flangeSlots) {
|
|
158
|
+
const outline = await occt.shapes.wire.createRectangleWire({ width: 2 * MOUNTING.holeRadius, length: slot.length, center: [slot.x - 1, slot.y, 0], direction: [1, 0, 0] });
|
|
159
|
+
const rounded = await occt.fillets.fillet2d({ shape: outline, radius: SLOT_CORNER });
|
|
160
|
+
const face = await occt.shapes.face.createFaceFromWire({ shape: rounded, planar: true });
|
|
161
|
+
cutters.push(await occt.operations.extrude({ shape: face, direction: [p.thickness + 2, 0, 0] }));
|
|
162
|
+
}
|
|
163
|
+
const shape = cutters.length > 0 ? await occt.booleans.difference({ shape: swept, shapes: cutters, keepEdges: false }) : swept;
|
|
164
|
+
const volume = await occt.shapes.solid.getSolidVolume({ shape });
|
|
165
|
+
return { params: p, shape, volume, profileArea: profileArea(p), estimate: flatLengthEstimate(p), mounting: features };
|
|
166
|
+
}
|