import { useState } from 'react'
import { Button } from "/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "/components/ui/card"
import { Input } from "/components/ui/input"
import { Label } from "/components/ui/label"
type CutPiece = {
width: number
height: number
quantity: number
}
const firstFitDecreasing = (boardWidth: number, boardHeight: number, cutPieces: CutPiece[]): { x: number, y: number, pieceIndex: number }[] => {
const sortedPieces: { piece: CutPiece, originalIndex: number }[] = cutPieces
.flatMap((piece, originalIndex) => Array(piece.quantity).fill({ piece, originalIndex }))
.sort((a, b) => b.piece.width * b.piece.height - a.piece.width * a.piece.height)
const placedPieces: { x: number, y: number, pieceIndex: number }[] = []
const board = Array.from({ length: boardHeight }, () => Array(boardWidth).fill(false))
for (const { piece, originalIndex } of sortedPieces) {
let placed = false
for (let y = 0; y <= boardHeight - piece.height && !placed; y++) {
for (let x = 0; x <= boardWidth - piece.width && !placed; x++) {
let canPlace = true
for (let dy = 0; dy < piece.height && canPlace; dy++) {
for (let dx = 0; dx < piece.width && canPlace; dx++) {
if (board[y + dy][x + dx]) {
canPlace = false
}
}
}
if (canPlace) {
for (let dy = 0; dy < piece.height; dy++) {
for (let dx = 0; dx < piece.width; dx++) {
board[y + dy][x + dx] = true
}
}
placedPieces.push({ x, y, pieceIndex: originalIndex })
placed = true
}
}
}
}
return placedPieces
}
export default function BoardCuttingOptimizer() {
const [boardWidth, setBoardWidth] = useState
(undefined)
const [boardHeight, setBoardHeight] = useState(undefined)
const [boardQuantity, setBoardQuantity] = useState(undefined)
const [cutPieces, setCutPieces] = useState([])
const [cutPieceWidth, setCutPieceWidth] = useState(undefined)
const [cutPieceHeight, setCutPieceHeight] = useState(undefined)
const [cutPieceQuantity, setCutPieceQuantity] = useState(undefined)
const [placedPieces, setPlacedPieces] = useState<{ x: number, y: number, pieceIndex: number }[][]>([])
const addCutPiece = () => {
if (cutPieceWidth !== undefined && cutPieceHeight !== undefined && cutPieceQuantity !== undefined) {
setCutPieces([...cutPieces, { width: cutPieceWidth, height: cutPieceHeight, quantity: cutPieceQuantity }])
}
}
const removeCutPiece = (index: number) => {
const newCutPieces = cutPieces.filter((_, i) => i !== index)
setCutPieces(newCutPieces)
}
const calculateCuttingPlan = () => {
if (boardWidth !== undefined && boardHeight !== undefined && boardQuantity !== undefined) {
const plans: { x: number, y: number, pieceIndex: number }[][] = []
let remainingPieces = [...cutPieces]
for (let i = 0; i < boardQuantity; i++) {
const plan = firstFitDecreasing(boardWidth, boardHeight, remainingPieces)
plans.push(plan)
// Update remaining pieces based on the placed pieces
const placedIndices = new Set(plan.map(p => p.pieceIndex))
const newRemainingPieces: CutPiece[] = []
remainingPieces.forEach((piece, index) => {
if (!placedIndices.has(index)) {
newRemainingPieces.push(piece)
}
})
remainingPieces = newRemainingPieces
}
setPlacedPieces(plans)
}
}
const printResult = () => {
const printContent = document.getElementById('print-content')
if (printContent) {
const printWindow = window.open('', '', 'width=800,height=600')
if (printWindow) {
printWindow.document.open()
printWindow.document.write(`
Plan de Cortado Optimizado
Plan de Cortado Optimizado
${printContent.innerHTML}
`)
printWindow.document.close()
printWindow.print()
printWindow.close()
}
}
}
return (
Optimizador de Cortado de Tableros
Calcular Plan de Cortado
Imprimir Resultado
Plan de Cortado Optimizado
{placedPieces.map((plan, boardIndex) => (
Tablero {boardIndex + 1}
{plan.map((placedPiece, index) => {
const piece = cutPieces[placedPiece.pieceIndex]
const fontSize = Math.min(piece?.width || 0, piece?.height || 0) / 3
return (
{`${piece?.width}x${piece?.height}`}
)
})}
))}
)
}