Skip to content

Commit

Permalink
implement nest
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Jan 5, 2025
1 parent fb820fd commit e4a18fc
Show file tree
Hide file tree
Showing 5 changed files with 370 additions and 46 deletions.
6 changes: 1 addition & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 21 additions & 33 deletions server/api/project/[slug]/nest.post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineEventHandler, readBody } from "h3";
import { parseAndCombine } from "~~/server/core/dxf/parser";
import { nest } from "~~/server/core/nest";

import { connectDB } from "~~/server/db/mongo";

Expand All @@ -8,40 +8,28 @@ export default defineEventHandler(async (event) => {
const body = await readBody(event);
const { files, params } = body;

const nestResultId = await nest(files, params, slug);

const db = await connectDB();
db.collection("nest_request").insertOne({
slug: slug,
body: body,
requestedAt: new Date(),
});

const project = await db.collection("projects").findOne({ slug });
const arrayOfDxfWithCount = files.map((file) => {
const dxfString = project.dxf.find((d) => d.slug === file.slug).data;
return {
polygones: parseAndCombine(dxfString, 0.1).closed,
count: file.count,
};
});

const jaguarRequest = buildNestJson(arrayOfDxfWithCount);

return {
status: 200,
body: {
jaguarRequest,
},
};
});
const nestResult = await db
.collection("nest_request")
.findOne({ _id: nestResultId });

function buildNestJson(arrayOfDxfWithCount) {
const polgyones = arrayOfDxfWithCount.map((dxf) => {
return dxf.polygones;
});
if (nestResult.isFail === true) {
throw creaError({
status: 400,
message: "Error during nesting, cannot placed all item into bin",
});
}

const firstDxf = polgyones[0];
const firstPolygone = firstDxf[0];
let message = "All items placed";
if (!nestResult.isAllItemsPlaced) {
message = "Not all items placed";
}

console.log(firstPolygone);
return 0;
}
return {
message: message,
usage: nestResult.usage,
};
});
6 changes: 3 additions & 3 deletions server/core/dxf/entityToPoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export function entityToPoints(entity, tolerance) {
const specificKeys = Object.keys(entity.specific);
if (specificKeys.length === 0) return [];

const etype = specificKeys[0].toLowerCase(); // Assuming one key per entity
const type = specificKeys[0].toLowerCase(); // Assuming one key per entity

switch (etype) {
switch (type) {
case "line":
return lineToPoints(entity.specific.Line);

Expand All @@ -36,7 +36,7 @@ export function entityToPoints(entity, tolerance) {
return splineToPoints(entity.specific.Spline, tolerance);

default:
console.warn(`Unsupported entity type: ${etype}`);
console.warn(`Unsupported entity type: ${type}`);
return [];
}
}
Expand Down
124 changes: 124 additions & 0 deletions server/core/dxf/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* Deeply clones an object using structuredClone or a fallback.
* @param {object} obj - The object to clone.
* @returns {object} - The deep cloned object.
*/
function deepClone(obj) {
if (typeof structuredClone === "function") {
return structuredClone(obj);
}
// Fallback if structuredClone is not available
return JSON.parse(JSON.stringify(obj));
}

/**
* Applies an in-place transform to a copy of the given entity and returns the transformed copy.
* The transform is:
* 1) Rotate around (0,0) by 'angle' radians.
* 2) Translate by (dx, dy).
*
* @param {object} entity - The DXF entity object (with entity.specific).
* @param {{ x: number, y: number, angle: number }} transform - The transform parameters.
* @returns {object} - The transformed copy of the entity.
*/
export function transformEntity(entity, transform) {
if (!entity || !entity.specific) return null;

// Clone the entity to ensure the original object is not modified
const entityCopy = deepClone(entity);

const { x: dx, y: dy, angle } = transform;

const cosA = Math.cos(angle);
const sinA = Math.sin(angle);

function applyTransform(pt) {
const rx = pt.x * cosA - pt.y * sinA;
const ry = pt.x * sinA + pt.y * cosA;
pt.x = rx + dx;
pt.y = ry + dy;
}

const specificKeys = Object.keys(entityCopy.specific);
if (specificKeys.length === 0) return entityCopy;
const type = specificKeys[0].toLowerCase();

switch (type) {
case "line": {
const line = entityCopy.specific.Line;
if (line && line.p1 && line.p2) {
applyTransform(line.p1);
applyTransform(line.p2);
}
break;
}

case "lwpolyline":
case "polyline": {
const poly =
entityCopy.specific.LwPolyline || entityCopy.specific.Polyline;
if (poly && Array.isArray(poly.vertices)) {
for (const v of poly.vertices) {
applyTransform(v);
}
}
break;
}

case "circle": {
const circle = entityCopy.specific.Circle;
if (circle && circle.center) {
applyTransform(circle.center);
}
break;
}

case "arc": {
const arc = entityCopy.specific.Arc;
if (arc && arc.center) {
applyTransform(arc.center);
const deltaDeg = toDegrees(angle);
arc.start_angle += deltaDeg;
arc.end_angle += deltaDeg;
}
break;
}

case "ellipse": {
const ellipse = entityCopy.specific.Ellipse;
if (ellipse && ellipse.center) {
applyTransform(ellipse.center);
const deltaDeg = toDegrees(angle);
ellipse.rotation = (ellipse.rotation || 0) + deltaDeg;

if (typeof ellipse.start_angle === "number") {
ellipse.start_angle += deltaDeg;
}
if (typeof ellipse.end_angle === "number") {
ellipse.end_angle += deltaDeg;
}
}
break;
}

case "spline": {
const spline = entityCopy.specific.Spline;
if (spline && Array.isArray(spline.control_points)) {
for (const cp of spline.control_points) {
applyTransform(cp);
}
}
break;
}

default:
console.warn(`Unsupported entity type: ${type}`);
break;
}

return entityCopy;
}

function toDegrees(rad) {
return (rad * 180) / Math.PI;
}
Loading

0 comments on commit e4a18fc

Please sign in to comment.