This commit is contained in:
Халимов Рустам
2026-01-10 17:09:53 +03:00
parent f7911e7147
commit a9b07a07d8
3 changed files with 297 additions and 267 deletions

View File

@@ -4,9 +4,11 @@ import { AppConfig, LayoutSplits, GeneratedPart, Partition } from '../types';
export const calculateParts = (config: AppConfig, splits: LayoutSplits): GeneratedPart[] => {
const parts: GeneratedPart[] = [];
const safeX = splits.x || [];
const safeY = splits.y || [];
const safeParts = splits.partitions || {};
// ЗАЩИТА ОТ ОШИБОК ДАННЫХ
const safeX = Array.isArray(splits?.x) ? splits.x : [];
const safeY = Array.isArray(splits?.y) ? splits.y : [];
const safePartitions = splits?.partitions || {};
const xPoints = [0, ...[...safeX].sort((a, b) => a - b), 1];
const yPoints = [0, ...[...safeY].sort((a, b) => a - b), 1];
@@ -15,13 +17,16 @@ export const calculateParts = (config: AppConfig, splits: LayoutSplits): Generat
for (let i = 0; i < xPoints.length - 1; i++) {
for (let j = 0; j < yPoints.length - 1; j++) {
const rawX = xPoints[i] * config.drawer.width;
const rawY = yPoints[j] * config.drawer.depth;
const rawW = (xPoints[i + 1] - xPoints[i]) * config.drawer.width;
const rawD = (yPoints[j + 1] - yPoints[j]) * config.drawer.depth;
const internalPartitions = safeParts[`${i}-${j}`] || [];
// Получаем перегородки
const internalPartitions = safePartitions[`${i}-${j}`] || [];
// Допуски
const realWidth = rawW - config.printerTolerance;
const realDepth = rawD - config.printerTolerance;
const realX = rawX + (config.printerTolerance / 2);
@@ -46,7 +51,8 @@ export const calculateParts = (config: AppConfig, splits: LayoutSplits): Generat
return parts;
};
// Геометрия
// ... Вспомогательные функции (createBinGeometry, exportSTL) ...
// (Они остаются без изменений из прошлого ответа, там всё верно)
const createRoundedRectShape = (width: number, height: number, radius: number): THREE.Shape => {
const shape = new THREE.Shape();
const x = -width / 2;
@@ -74,23 +80,15 @@ const createRoundedRectShape = (width: number, height: number, radius: number):
};
export const createBinGeometry = (
width: number,
depth: number,
height: number,
thickness: number,
radius: number = 0,
partitions: Partition[] = []
width: number, depth: number, height: number, thickness: number, radius: number = 0, partitions: Partition[] = []
): THREE.BufferGeometry => {
const geometries: THREE.BufferGeometry[] = [];
// 1. ДНО
const floorShape = createRoundedRectShape(width, depth, radius);
const floorGeo = new THREE.ExtrudeGeometry(floorShape, { depth: thickness, bevelEnabled: false, curveSegments: 12 });
floorGeo.rotateX(-Math.PI / 2);
geometries.push(floorGeo);
// 2. ВНЕШНИЕ СТЕНКИ
const outerShape = createRoundedRectShape(width, depth, radius);
const innerRadius = Math.max(0, radius - thickness);
const innerWidth = width - (2 * thickness);
@@ -107,51 +105,32 @@ export const createBinGeometry = (
wallGeo.translate(0, thickness, 0);
geometries.push(wallGeo);
// 3. ВНУТРЕННИЕ ПЕРЕГОРОДКИ
partitions.forEach(p => {
let pWidth = 0;
let pDepth = 0;
let pX = 0;
let pY = 0;
let pWidth = 0, pDepth = 0, pX = 0, pY = 0;
if (p.axis === 'x') {
pWidth = thickness;
pDepth = innerDepth;
pX = (-innerWidth / 2) + (innerWidth * p.offset);
pY = 0;
pWidth = thickness; pDepth = innerDepth;
pX = (-innerWidth / 2) + (innerWidth * p.offset); pY = 0;
} else {
pWidth = innerWidth;
pDepth = thickness;
pX = 0;
pY = (-innerDepth / 2) + (innerDepth * p.offset);
pWidth = innerWidth; pDepth = thickness;
pX = 0; pY = (-innerDepth / 2) + (innerDepth * p.offset);
}
const pRadius = p.rounded ? Math.min(radius, thickness / 1.5) : 0;
const partShape = createRoundedRectShape(pWidth, pDepth, pRadius);
const partGeo = new THREE.ExtrudeGeometry(partShape, {
depth: p.height,
bevelEnabled: false,
curveSegments: 8
});
const partGeo = new THREE.ExtrudeGeometry(partShape, { depth: p.height, bevelEnabled: false, curveSegments: 8 });
partGeo.rotateX(-Math.PI / 2);
partGeo.translate(pX, thickness, pY);
geometries.push(partGeo);
});
const merged = mergeBufferGeometries(geometries);
if (merged) merged.computeVertexNormals();
return merged || new THREE.BoxGeometry(1, 1, 1);
};
export const generateSTL = (mesh: THREE.Object3D): Uint8Array | string => {
const exporter = new STLExporter();
const result = exporter.parse(mesh, { binary: true });
if (result instanceof DataView) {
return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
}
if (result instanceof DataView) return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
return result as string;
};