@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,176 @@
|
|
|
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 { downloadBlob, layoutSvg } from "./exports";
|
|
7
|
+
import { buildModel, defaultParams, type BoxModel, type BoxParams } from "./model";
|
|
8
|
+
import { mountPoint, mountUi, type ExportFormat, type View } from "./ui";
|
|
9
|
+
|
|
10
|
+
const PANEL_COLOURS: Record<string, string> = {
|
|
11
|
+
bottom: "#d4b07c", lid: "#e0bf8c", front: "#c9a36e", back: "#c9a36e", left: "#bd9762", right: "#bd9762",
|
|
12
|
+
};
|
|
13
|
+
const CUT_EDGE = "#3b2a17";
|
|
14
|
+
|
|
15
|
+
void start();
|
|
16
|
+
|
|
17
|
+
function isMesh(object: THREE.Object3D): object is THREE.Mesh {
|
|
18
|
+
return "isMesh" in object && object.isMesh === true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function cameraOptions(sceneSize: number): Inputs.ThreeJSCamera.OrbitCameraDto {
|
|
22
|
+
const camera = new Inputs.ThreeJSCamera.OrbitCameraDto();
|
|
23
|
+
camera.distance = sceneSize * Math.SQRT2;
|
|
24
|
+
camera.distanceMin = sceneSize * 0.02;
|
|
25
|
+
camera.distanceMax = sceneSize * 10;
|
|
26
|
+
return camera;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function start(): Promise<void> {
|
|
30
|
+
const sceneOptions = new Inputs.ThreeJSScene.InitThreeJSDto();
|
|
31
|
+
sceneOptions.canvasId = "three-canvas";
|
|
32
|
+
sceneOptions.sceneSize = 600;
|
|
33
|
+
sceneOptions.enableGround = true;
|
|
34
|
+
sceneOptions.groundColor = "#2a2d31";
|
|
35
|
+
sceneOptions.enableShadows = true;
|
|
36
|
+
sceneOptions.orbitCameraOptions = cameraOptions(sceneOptions.sceneSize);
|
|
37
|
+
const { scene, renderer, orbitCamera, startAnimationLoop } = initThreeJS(sceneOptions);
|
|
38
|
+
startAnimationLoop();
|
|
39
|
+
|
|
40
|
+
renderer.toneMapping = ACESFilmicToneMapping;
|
|
41
|
+
renderer.toneMappingExposure = 1.05;
|
|
42
|
+
scene.environment = new PMREMGenerator(renderer).fromScene(new RoomEnvironment(), 0.04).texture;
|
|
43
|
+
scene.environmentIntensity = 0.4;
|
|
44
|
+
const plywood = new Map<string, MeshPhysicalMaterial>();
|
|
45
|
+
|
|
46
|
+
const bitbybit = new BitByBitBase();
|
|
47
|
+
const options: InitBitByBitOptions = {
|
|
48
|
+
enableOCCT: true,
|
|
49
|
+
enableJSCAD: false,
|
|
50
|
+
enableManifold: false,
|
|
51
|
+
};
|
|
52
|
+
await initBitByBit(scene, bitbybit, options);
|
|
53
|
+
|
|
54
|
+
let params: BoxParams = { ...defaultParams };
|
|
55
|
+
let view: View = "assembled";
|
|
56
|
+
let current: { model: BoxModel; drawn: THREE.Group[] } | undefined;
|
|
57
|
+
let generation = 0;
|
|
58
|
+
let timer: number | undefined;
|
|
59
|
+
let framedFor = "";
|
|
60
|
+
|
|
61
|
+
const ui = mountUi(mountPoint("panel"), params, {
|
|
62
|
+
onChange: (next) => {
|
|
63
|
+
params = next;
|
|
64
|
+
window.clearTimeout(timer);
|
|
65
|
+
timer = window.setTimeout(() => void rebuild(), 150);
|
|
66
|
+
},
|
|
67
|
+
onView: (next) => {
|
|
68
|
+
view = next;
|
|
69
|
+
framedFor = "";
|
|
70
|
+
void redraw();
|
|
71
|
+
},
|
|
72
|
+
onExport: (format) => void exportModel(format),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
async function rebuild(): Promise<void> {
|
|
76
|
+
const mine = ++generation;
|
|
77
|
+
ui.setStatus("Building...");
|
|
78
|
+
try {
|
|
79
|
+
const model = await buildModel(bitbybit.occt, params);
|
|
80
|
+
if (mine !== generation) return;
|
|
81
|
+
if (current) current.drawn.forEach(dispose);
|
|
82
|
+
current = { model, drawn: [] };
|
|
83
|
+
await redraw();
|
|
84
|
+
ui.setFacts(model.layout, model.params);
|
|
85
|
+
ui.setStatus(`${model.parts.length} parts, ${model.parts[0]?.fingers ?? 0} fingers on the longest edge.`);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
ui.setStatus(`Could not build this box: ${error instanceof Error ? error.message : String(error)}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function redraw(): Promise<void> {
|
|
92
|
+
if (!current) return;
|
|
93
|
+
current.drawn.forEach(dispose);
|
|
94
|
+
current.drawn = [];
|
|
95
|
+
for (const part of current.model.parts) {
|
|
96
|
+
const drawOptions = new Inputs.Draw.DrawOcctShapeOptions();
|
|
97
|
+
drawOptions.faceMaterial = plywoodFor(part.id);
|
|
98
|
+
drawOptions.edgeColour = CUT_EDGE;
|
|
99
|
+
drawOptions.edgeWidth = 1;
|
|
100
|
+
drawOptions.drawEdges = true;
|
|
101
|
+
drawOptions.drawTwoSided = false;
|
|
102
|
+
const entity = view === "assembled" ? part.assembled : part.flatSolid;
|
|
103
|
+
current.drawn.push(await bitbybit.draw.drawAnyAsync({ entity, options: drawOptions }));
|
|
104
|
+
}
|
|
105
|
+
if (view === "flat") {
|
|
106
|
+
const sheetOptions = new Inputs.Draw.DrawOcctShapeOptions();
|
|
107
|
+
sheetOptions.edgeColour = "#f0cebb";
|
|
108
|
+
sheetOptions.edgeWidth = 1;
|
|
109
|
+
sheetOptions.drawEdges = true;
|
|
110
|
+
sheetOptions.drawFaces = false;
|
|
111
|
+
current.drawn.push(await bitbybit.draw.drawAnyAsync({ entity: current.model.sheetOutline, options: sheetOptions }));
|
|
112
|
+
}
|
|
113
|
+
frame();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function plywoodFor(id: string): MeshPhysicalMaterial {
|
|
117
|
+
let material = plywood.get(id);
|
|
118
|
+
if (!material) {
|
|
119
|
+
material = new MeshPhysicalMaterial({ color: new Color(PANEL_COLOURS[id] ?? "#d4b07c"), roughness: 0.62, metalness: 0, clearcoat: 0.25, clearcoatRoughness: 0.35 });
|
|
120
|
+
plywood.set(id, material);
|
|
121
|
+
}
|
|
122
|
+
return material;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function frame(): void {
|
|
126
|
+
if (!orbitCamera || !current) return;
|
|
127
|
+
const p = current.model.params;
|
|
128
|
+
const key = `${view}:${view === "assembled" ? Math.max(p.length, p.width, p.height) : Math.max(p.sheetWidth, p.sheetHeight)}`;
|
|
129
|
+
if (key === framedFor) return;
|
|
130
|
+
framedFor = key;
|
|
131
|
+
if (view === "assembled") {
|
|
132
|
+
const distance = Math.max(p.length, p.width, p.height) * 2.2;
|
|
133
|
+
const target = new Vector3(0, p.height / 2, 0);
|
|
134
|
+
orbitCamera.orbitCamera.resetAndLookAtPoint(new Vector3(distance * 0.6, target.y + distance * 0.5, distance * 0.6), target);
|
|
135
|
+
} else {
|
|
136
|
+
const distance = Math.max(p.sheetWidth, p.sheetHeight) * 1.3;
|
|
137
|
+
orbitCamera.orbitCamera.resetAndLookAtPoint(new Vector3(0, distance, 0.001), new Vector3(0, 0, 0));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function exportModel(format: ExportFormat): Promise<void> {
|
|
142
|
+
if (!current) return;
|
|
143
|
+
const { model } = current;
|
|
144
|
+
const p = model.params;
|
|
145
|
+
const name = `box-${p.length}x${p.width}x${p.height}`;
|
|
146
|
+
ui.setStatus(`Exporting ${format.toUpperCase()}...`);
|
|
147
|
+
if (format === "svg") {
|
|
148
|
+
downloadBlob(new Blob([layoutSvg(model.layout, p.sheetWidth, p.sheetHeight)], { type: "image/svg+xml" }), `${name}-layout.svg`);
|
|
149
|
+
} else if (format === "dxf") {
|
|
150
|
+
const pathsParts = [];
|
|
151
|
+
for (const part of model.parts) {
|
|
152
|
+
const paths = await bitbybit.occt.io.shapeToDxfPaths({ shape: part.flat, angularDeflection: 0.1, curvatureDeflection: 0.1, minimumOfPoints: 2, uTolerance: 1e-9, minimumLength: 1e-7 });
|
|
153
|
+
pathsParts.push(await bitbybit.occt.io.dxfPathsWithLayer({ paths, layer: "cut", color: "#000000" }));
|
|
154
|
+
}
|
|
155
|
+
await bitbybit.occt.io.dxfCreate({ pathsParts, colorFormat: Inputs.OCCT.dxfColorFormatEnum.aci, acadVersion: Inputs.OCCT.dxfAcadVersionEnum.AC1009, fileName: `${name}-layout.dxf`, tryDownload: true });
|
|
156
|
+
} else {
|
|
157
|
+
const compound = await bitbybit.occt.shapes.compound.makeCompound({ shapes: model.parts.map((part) => part.assembled) });
|
|
158
|
+
await bitbybit.occt.io.saveShapeSTEP({ shape: compound, fileName: `${name}.step`, adjustYtoZ: true, tryDownload: true });
|
|
159
|
+
}
|
|
160
|
+
ui.setStatus(`${format.toUpperCase()} downloaded.`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function dispose(group: THREE.Group): void {
|
|
164
|
+
scene.remove(group);
|
|
165
|
+
const shared = new Set<THREE.Material>(plywood.values());
|
|
166
|
+
group.traverse((object) => {
|
|
167
|
+
if (!isMesh(object)) return;
|
|
168
|
+
object.geometry.dispose();
|
|
169
|
+
for (const material of Array.isArray(object.material) ? object.material : [object.material]) {
|
|
170
|
+
if (!shared.has(material)) material.dispose();
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
await rebuild();
|
|
176
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { BitByBitBase } from "@bitbybit-dev/threejs";
|
|
2
|
+
import { clampParams, layoutParts, partsFor, slotCorner, type BoxParams, type Layout, type PlacedPart, type Slot } from "./panels";
|
|
3
|
+
|
|
4
|
+
export type Occt = BitByBitBase["occt"];
|
|
5
|
+
|
|
6
|
+
type Shape = Awaited<ReturnType<Occt["operations"]["extrude"]>>;
|
|
7
|
+
|
|
8
|
+
export interface BuiltPart extends PlacedPart {
|
|
9
|
+
face: Shape;
|
|
10
|
+
assembled: Shape;
|
|
11
|
+
flat: Shape;
|
|
12
|
+
flatSolid: Shape;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface BoxModel {
|
|
16
|
+
params: BoxParams;
|
|
17
|
+
parts: BuiltPart[];
|
|
18
|
+
layout: Layout;
|
|
19
|
+
sheetOutline: Shape;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const LAYOUT_LIFT = 1;
|
|
23
|
+
|
|
24
|
+
export { clampParams, defaultParams, LIMITS, UNITS, type BoxParams } from "./panels";
|
|
25
|
+
|
|
26
|
+
export async function buildModel(occt: Occt, params: BoxParams): Promise<BoxModel> {
|
|
27
|
+
const p = clampParams(params);
|
|
28
|
+
const parts = partsFor(p);
|
|
29
|
+
const layout = layoutParts(parts, p);
|
|
30
|
+
const built: BuiltPart[] = [];
|
|
31
|
+
for (const part of layout.parts) {
|
|
32
|
+
const wire = await occt.shapes.wire.createPolygonWire({ points: part.outline.map(([u, v]) => [u, 0, v]) });
|
|
33
|
+
const face = part.slot ? await faceWithHole(occt, wire, await slotWire(occt, part.slot)) : await occt.shapes.face.createFaceFromWire({ shape: wire, planar: true });
|
|
34
|
+
const flat = await occt.transforms.translate({ shape: face, translation: [part.x - p.sheetWidth / 2, 0, part.y - p.sheetHeight / 2] });
|
|
35
|
+
const flatSlab = await occt.operations.extrude({ shape: flat, direction: [0, p.thickness, 0] });
|
|
36
|
+
const flatSolid = await occt.transforms.translate({ shape: flatSlab, translation: [0, LAYOUT_LIFT, 0] });
|
|
37
|
+
const slab = await occt.operations.extrude({ shape: face, direction: [0, p.thickness, 0] });
|
|
38
|
+
const assembled = await place(occt, slab, part.id, p);
|
|
39
|
+
built.push({ ...part, face, flat, flatSolid, assembled });
|
|
40
|
+
}
|
|
41
|
+
const sheetOutline = await occt.shapes.wire.createRectangleWire({ width: p.sheetWidth, length: p.sheetHeight, center: [0, LAYOUT_LIFT, 0], direction: [0, 1, 0] });
|
|
42
|
+
return { params: p, parts: built, layout, sheetOutline };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
type Wire = Awaited<ReturnType<Occt["shapes"]["wire"]["createRectangleWire"]>>;
|
|
46
|
+
|
|
47
|
+
function winding(points: number[][]): number {
|
|
48
|
+
let sum = 0;
|
|
49
|
+
for (let i = 0; i < points.length; i++) {
|
|
50
|
+
const [ax = 0, , az = 0] = points[i] ?? [];
|
|
51
|
+
const [bx = 0, , bz = 0] = points[(i + 1) % points.length] ?? [];
|
|
52
|
+
sum += ax * bz - bx * az;
|
|
53
|
+
}
|
|
54
|
+
return sum;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function faceWithHole(occt: Occt, outline: Wire, hole: Wire): Promise<Shape> {
|
|
58
|
+
const both = await occt.shapes.compound.makeCompound({ shapes: [outline, hole] });
|
|
59
|
+
const runs = await occt.shapes.wire.wiresToPoints({ shape: both, angularDeflection: 0.1, curvatureDeflection: 0.1, minimumOfPoints: 2, uTolerance: 1e-9, minimumLength: 1e-7 });
|
|
60
|
+
const [outlineRun = [], holeRun = []] = runs;
|
|
61
|
+
const turned = Math.sign(winding(outlineRun)) === Math.sign(winding(holeRun)) ? await occt.shapes.wire.reversedWire({ shape: hole }) : hole;
|
|
62
|
+
return occt.shapes.face.createFaceFromWires({ shapes: [outline, turned], planar: true });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function slotWire(occt: Occt, slot: Slot): Promise<Wire> {
|
|
66
|
+
const rectangle = await occt.shapes.wire.createRectangleWire({ width: slot.length, length: 2 * slot.radius, center: [0, 0, 0], direction: [0, 1, 0] });
|
|
67
|
+
return occt.fillets.fillet2d({ shape: rectangle, radius: slotCorner(slot) });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function place(occt: Occt, slab: Shape, id: string, p: BoxParams): Promise<Shape> {
|
|
71
|
+
const t = p.thickness;
|
|
72
|
+
const upright = (shape: Shape): Promise<Shape> => occt.transforms.rotate({ shape, axis: [1, 0, 0], angle: -90 });
|
|
73
|
+
const move = (shape: Shape, translation: [number, number, number]): Promise<Shape> => occt.transforms.translate({ shape, translation });
|
|
74
|
+
switch (id) {
|
|
75
|
+
case "bottom": return slab;
|
|
76
|
+
case "lid": return move(slab, [0, p.height - t, 0]);
|
|
77
|
+
case "front": return move(await upright(slab), [0, p.height / 2, -p.width / 2 + t]);
|
|
78
|
+
case "back": {
|
|
79
|
+
const turned = await occt.transforms.rotate({ shape: await upright(slab), axis: [0, 1, 0], angle: 180 });
|
|
80
|
+
return move(turned, [0, p.height / 2, p.width / 2 - t]);
|
|
81
|
+
}
|
|
82
|
+
case "left": {
|
|
83
|
+
const turned = await occt.transforms.rotate({ shape: await upright(slab), axis: [0, 1, 0], angle: 90 });
|
|
84
|
+
return move(turned, [-p.length / 2 + t, p.height / 2, 0]);
|
|
85
|
+
}
|
|
86
|
+
case "right": {
|
|
87
|
+
const turned = await occt.transforms.rotate({ shape: await upright(slab), axis: [0, 1, 0], angle: -90 });
|
|
88
|
+
return move(turned, [p.length / 2 - t, p.height / 2, 0]);
|
|
89
|
+
}
|
|
90
|
+
default: throw new Error(`no placement for panel ${id}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
export type Joint = "fingers" | "slots" | "plain";
|
|
2
|
+
|
|
3
|
+
export type Point2 = [number, number];
|
|
4
|
+
|
|
5
|
+
export interface BoxParams {
|
|
6
|
+
length: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
thickness: number;
|
|
10
|
+
kerf: number;
|
|
11
|
+
fingerWidth: number;
|
|
12
|
+
lid: boolean;
|
|
13
|
+
sheetWidth: number;
|
|
14
|
+
sheetHeight: number;
|
|
15
|
+
gap: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface EdgeSpec {
|
|
19
|
+
joint: Joint;
|
|
20
|
+
inset: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface PanelSpec {
|
|
24
|
+
id: string;
|
|
25
|
+
u: number;
|
|
26
|
+
v: number;
|
|
27
|
+
edges: [EdgeSpec, EdgeSpec, EdgeSpec, EdgeSpec];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface Slot {
|
|
31
|
+
length: number;
|
|
32
|
+
radius: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface Part {
|
|
36
|
+
id: string;
|
|
37
|
+
outline: Point2[];
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
fingers: number;
|
|
41
|
+
|
|
42
|
+
slot?: Slot;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface PlacedPart extends Part {
|
|
46
|
+
x: number;
|
|
47
|
+
y: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface Layout {
|
|
51
|
+
parts: PlacedPart[];
|
|
52
|
+
usedWidth: number;
|
|
53
|
+
usedHeight: number;
|
|
54
|
+
fits: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const UNITS = "mm";
|
|
58
|
+
|
|
59
|
+
export const LIMITS = {
|
|
60
|
+
length: { min: 40, max: 600, step: 1 },
|
|
61
|
+
width: { min: 40, max: 600, step: 1 },
|
|
62
|
+
height: { min: 20, max: 400, step: 1 },
|
|
63
|
+
thickness: { min: 1.5, max: 12, step: 0.5 },
|
|
64
|
+
kerf: { min: 0, max: 0.5, step: 0.05 },
|
|
65
|
+
fingerWidth: { min: 5, max: 60, step: 1 },
|
|
66
|
+
sheetWidth: { min: 100, max: 1500, step: 10 },
|
|
67
|
+
sheetHeight: { min: 100, max: 1000, step: 10 },
|
|
68
|
+
gap: { min: 1, max: 20, step: 0.5 },
|
|
69
|
+
} as const;
|
|
70
|
+
|
|
71
|
+
export const defaultParams: BoxParams = {
|
|
72
|
+
length: 120,
|
|
73
|
+
width: 80,
|
|
74
|
+
height: 60,
|
|
75
|
+
thickness: 3,
|
|
76
|
+
kerf: 0.15,
|
|
77
|
+
fingerWidth: 12,
|
|
78
|
+
lid: true,
|
|
79
|
+
sheetWidth: 400,
|
|
80
|
+
sheetHeight: 300,
|
|
81
|
+
gap: 4,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export function clampParams(params: BoxParams): BoxParams {
|
|
85
|
+
const clamp = (value: number, limit: { min: number; max: number }): number => Math.min(Math.max(value, limit.min), limit.max);
|
|
86
|
+
const thickness = clamp(params.thickness, LIMITS.thickness);
|
|
87
|
+
const length = Math.max(clamp(params.length, LIMITS.length), thickness * 6);
|
|
88
|
+
const width = Math.max(clamp(params.width, LIMITS.width), thickness * 6);
|
|
89
|
+
const height = Math.max(clamp(params.height, LIMITS.height), thickness * 4);
|
|
90
|
+
return {
|
|
91
|
+
...params,
|
|
92
|
+
length, width, height, thickness,
|
|
93
|
+
kerf: clamp(params.kerf, LIMITS.kerf),
|
|
94
|
+
fingerWidth: Math.max(clamp(params.fingerWidth, LIMITS.fingerWidth), thickness),
|
|
95
|
+
sheetWidth: clamp(params.sheetWidth, LIMITS.sheetWidth),
|
|
96
|
+
sheetHeight: clamp(params.sheetHeight, LIMITS.sheetHeight),
|
|
97
|
+
gap: clamp(params.gap, LIMITS.gap),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function panelSpecs(p: BoxParams): PanelSpec[] {
|
|
102
|
+
const t = p.thickness;
|
|
103
|
+
const edge = (joint: Joint, inset = 0): EdgeSpec => ({ joint, inset });
|
|
104
|
+
const top = (inset: number): EdgeSpec => (p.lid ? edge("slots", inset) : edge("plain"));
|
|
105
|
+
const specs: PanelSpec[] = [
|
|
106
|
+
{ id: "bottom", u: p.length - 2 * t, v: p.width - 2 * t, edges: [edge("fingers"), edge("fingers"), edge("fingers"), edge("fingers")] },
|
|
107
|
+
{ id: "front", u: p.length - 2 * t, v: p.height, edges: [edge("slots"), edge("fingers"), top(0), edge("fingers")] },
|
|
108
|
+
{ id: "back", u: p.length - 2 * t, v: p.height, edges: [edge("slots"), edge("fingers"), top(0), edge("fingers")] },
|
|
109
|
+
{ id: "left", u: p.width, v: p.height, edges: [edge("slots", t), edge("slots"), top(t), edge("slots")] },
|
|
110
|
+
{ id: "right", u: p.width, v: p.height, edges: [edge("slots", t), edge("slots"), top(t), edge("slots")] },
|
|
111
|
+
];
|
|
112
|
+
if (p.lid) specs.push({ id: "lid", u: p.length - 2 * t, v: p.width - 2 * t, edges: [edge("fingers"), edge("fingers"), edge("fingers"), edge("fingers")] });
|
|
113
|
+
return specs;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function segmentsAlong(edgeLength: number, fingerWidth: number): number {
|
|
117
|
+
const n = Math.max(3, Math.floor(edgeLength / fingerWidth));
|
|
118
|
+
return n % 2 === 1 ? n : n - 1;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function outlineFor(spec: PanelSpec, p: BoxParams): Point2[] {
|
|
122
|
+
const t = p.thickness;
|
|
123
|
+
const k = p.kerf / 2;
|
|
124
|
+
const [c0, c1, c2, c3]: [Point2, Point2, Point2, Point2] = [[-spec.u / 2, -spec.v / 2], [spec.u / 2, -spec.v / 2], [spec.u / 2, spec.v / 2], [-spec.u / 2, spec.v / 2]];
|
|
125
|
+
const sides: { a: Point2; b: Point2; n: Point2; edge: EdgeSpec }[] = [
|
|
126
|
+
{ a: c0, b: c1, n: [0, -1], edge: spec.edges[0] },
|
|
127
|
+
{ a: c1, b: c2, n: [1, 0], edge: spec.edges[1] },
|
|
128
|
+
{ a: c2, b: c3, n: [0, 1], edge: spec.edges[2] },
|
|
129
|
+
{ a: c3, b: c0, n: [-1, 0], edge: spec.edges[3] },
|
|
130
|
+
];
|
|
131
|
+
const points: Point2[] = [];
|
|
132
|
+
for (const { a, b, n, edge } of sides) {
|
|
133
|
+
const d: Point2 = [Math.sign(b[0] - a[0]), Math.sign(b[1] - a[1])];
|
|
134
|
+
const len = Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1]);
|
|
135
|
+
const at = (u: number, level: number): Point2 => [a[0] + d[0] * u + n[0] * (k + level), a[1] + d[1] * u + n[1] * (k + level)];
|
|
136
|
+
const { joint, inset } = edge;
|
|
137
|
+
points.push(at(-k, 0));
|
|
138
|
+
if (joint === "plain") continue;
|
|
139
|
+
const span = len - 2 * inset;
|
|
140
|
+
const count = segmentsAlong(span, p.fingerWidth);
|
|
141
|
+
const s = span / count;
|
|
142
|
+
const level = (i: number): number => (i % 2 === 0 ? (joint === "fingers" ? t : -t) : 0);
|
|
143
|
+
let current = 0;
|
|
144
|
+
for (let i = 0; i <= count; i++) {
|
|
145
|
+
const next = i === count ? 0 : level(i);
|
|
146
|
+
if (next === current) continue;
|
|
147
|
+
const boundary = inset + i * s + (next === 0 ? (current > 0 ? k : -k) : (next > 0 ? -k : k));
|
|
148
|
+
points.push(at(boundary, current), at(boundary, next));
|
|
149
|
+
current = next;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return tidy(points);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const same = (a: Point2, b: Point2): boolean => Math.abs(a[0] - b[0]) < 1e-9 && Math.abs(a[1] - b[1]) < 1e-9;
|
|
156
|
+
|
|
157
|
+
function tidy(points: Point2[]): Point2[] {
|
|
158
|
+
const out = [...points];
|
|
159
|
+
const around = (index: number): Point2 | undefined => out[(index + out.length) % out.length];
|
|
160
|
+
let changed = true;
|
|
161
|
+
while (changed && out.length > 3) {
|
|
162
|
+
changed = false;
|
|
163
|
+
for (let i = 0; i < out.length; i++) {
|
|
164
|
+
const previous = around(i - 1);
|
|
165
|
+
const here = around(i);
|
|
166
|
+
const next = around(i + 1);
|
|
167
|
+
if (!previous || !here || !next) break;
|
|
168
|
+
if (same(here, next)) { out.splice(i, 1); changed = true; break; }
|
|
169
|
+
if (same(previous, next)) { out.splice(i, 2); changed = true; break; }
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export const HANDLE = { length: 30, radius: 4, clearance: 12 } as const;
|
|
176
|
+
|
|
177
|
+
export const slotCorner = (slot: Slot): number => slot.radius * 0.99;
|
|
178
|
+
|
|
179
|
+
export function handleFor(spec: PanelSpec, p: BoxParams): Slot | undefined {
|
|
180
|
+
if (spec.id !== "lid") return undefined;
|
|
181
|
+
const room = 2 * (p.thickness + HANDLE.clearance);
|
|
182
|
+
const length = Math.min(HANDLE.length, spec.u - room);
|
|
183
|
+
if (length < 3 * HANDLE.radius || spec.v - room < 2 * HANDLE.radius) return undefined;
|
|
184
|
+
return { length, radius: HANDLE.radius };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function slotArea(slot: Slot): number {
|
|
188
|
+
const r = slotCorner(slot);
|
|
189
|
+
return slot.length * 2 * slot.radius - (4 - Math.PI) * r * r;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function partsFor(p: BoxParams): Part[] {
|
|
193
|
+
return panelSpecs(p).map((spec) => {
|
|
194
|
+
const outline = outlineFor(spec, p);
|
|
195
|
+
const xs = outline.map((pt) => pt[0]);
|
|
196
|
+
const ys = outline.map((pt) => pt[1]);
|
|
197
|
+
const width = Math.max(...xs) - Math.min(...xs);
|
|
198
|
+
const height = Math.max(...ys) - Math.min(...ys);
|
|
199
|
+
const slot = handleFor(spec, p);
|
|
200
|
+
const part: Part = { id: spec.id, outline, width, height, fingers: segmentsAlong(Math.max(spec.u, spec.v), p.fingerWidth) };
|
|
201
|
+
return slot ? { ...part, slot } : part;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function layoutParts(parts: Part[], p: BoxParams): Layout {
|
|
206
|
+
const order = [...parts].sort((a, b) => b.height - a.height || b.width - a.width);
|
|
207
|
+
const placed: PlacedPart[] = [];
|
|
208
|
+
let cursorX = 0;
|
|
209
|
+
let cursorY = 0;
|
|
210
|
+
let rowHeight = 0;
|
|
211
|
+
let usedWidth = 0;
|
|
212
|
+
for (const part of order) {
|
|
213
|
+
if (cursorX > 0 && cursorX + part.width > p.sheetWidth) {
|
|
214
|
+
cursorX = 0;
|
|
215
|
+
cursorY += rowHeight + p.gap;
|
|
216
|
+
rowHeight = 0;
|
|
217
|
+
}
|
|
218
|
+
placed.push({ ...part, x: cursorX + part.width / 2, y: cursorY + part.height / 2 });
|
|
219
|
+
cursorX += part.width + p.gap;
|
|
220
|
+
rowHeight = Math.max(rowHeight, part.height);
|
|
221
|
+
usedWidth = Math.max(usedWidth, cursorX - p.gap);
|
|
222
|
+
}
|
|
223
|
+
const usedHeight = cursorY + rowHeight;
|
|
224
|
+
return { parts: placed, usedWidth, usedHeight, fits: usedWidth <= p.sheetWidth && usedHeight <= p.sheetHeight };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function partsOverlap(a: PlacedPart, b: PlacedPart): boolean {
|
|
228
|
+
return Math.abs(a.x - b.x) < (a.width + b.width) / 2 && Math.abs(a.y - b.y) < (a.height + b.height) / 2;
|
|
229
|
+
}
|