/** * Parser de Código G-Code ISO para CNC (Torno e Fresadora) */ export function parseGCode(rawText, defaultTool = "08") { const lines = rawText.split('\n'); const commands = []; let curX = 0, curY = 0, curZ = 15; let spindleOn = false, spindleDir = 'CW', rpm = 0, feedRate = 1000; let isAbsolute = true, currentTool = defaultTool; for (let idx = 0; idx < lines.length; idx++) { const rawLine = lines[idx]; const cleanLine = rawLine .replace(/;.*$/, '') .replace(/\(.*?\)/g, '') .trim() .toUpperCase(); if (!cleanLine || cleanLine === '%') { commands.push({ lineIndex: idx, raw: rawLine, type: cleanLine === '%' ? 'MISC' : 'EMPTY', desc: cleanLine === '%' ? 'Delimitador de Programa ISO (%)' : 'Comentário ou linha livre', from: { x: curX, y: curY, z: curZ }, to: { x: curX, y: curY, z: curZ }, spindle: spindleOn, spindleDir, spindleRPM: rpm, tool: currentTool, isToolChange: false, feed: feedRate, isCutting: false, distance: 0, arcError: false }); continue; } const tokens = cleanLine.match(/([A-Z])([-+]?[0-9]*\.?[0-9]+)?/g) || []; let motionType = null; let targetX = curX, targetY = curY, targetZ = curZ; let arcI = 0, arcJ = 0, arcR = null, isToolChange = false, desc = ''; for (const token of tokens) { const letter = token[0]; const val = parseFloat(token.substring(1)); if (letter === 'G') { const g = Math.round(val); if (g === 0) motionType = 'G00'; else if (g === 1) motionType = 'G01'; else if (g === 2) motionType = 'G02'; else if (g === 3) motionType = 'G03'; else if (g === 90) isAbsolute = true; else if (g === 91) isAbsolute = false; else if (g === 21) desc += ' [Unid: mm]'; else if (g === 17) desc += ' [Plano XY]'; else if (g === 54) desc += ' [Origem G54]'; } else if (letter === 'M') { const m = Math.round(val); if (m === 3) { spindleOn = true; spindleDir = 'CW'; desc += ' [M03: Fuso Horário]'; } else if (m === 4) { spindleOn = true; spindleDir = 'CCW'; desc += ' [M04: Fuso Anti-Horário]'; } else if (m === 5) { spindleOn = false; rpm = 0; desc += ' [M05: Fuso Parado]'; } else if (m === 6) { isToolChange = true; desc += ` [M06: Troca para T${currentTool}]`; } else if (m === 30) { spindleOn = false; desc += ' [M30: Fim de Programa]'; } } else if (letter === 'X' && !isNaN(val)) targetX = isAbsolute ? val : curX + val; else if (letter === 'Y' && !isNaN(val)) targetY = isAbsolute ? val : curY + val; else if (letter === 'Z' && !isNaN(val)) targetZ = isAbsolute ? val : curZ + val; else if (letter === 'I' && !isNaN(val)) arcI = val; else if (letter === 'J' && !isNaN(val)) arcJ = val; else if (letter === 'R' && !isNaN(val)) arcR = val; else if (letter === 'F' && !isNaN(val) && val >= 0) feedRate = val; else if (letter === 'S' && !isNaN(val)) rpm = Math.round(val); else if (letter === 'T' && !isNaN(val)) currentTool = String(Math.round(val)).padStart(2, '0'); } if (!motionType && tokens.some(t => t.startsWith('X') || t.startsWith('Y') || t.startsWith('Z'))) { motionType = 'G01'; } // Suporte para arco com R caso I e J não tenham sido fornecidos if ((motionType === 'G02' || motionType === 'G03') && arcR !== null && arcI === 0 && arcJ === 0) { const dx = targetX - curX; const dy = targetY - curY; const distChord = Math.hypot(dx, dy); if (distChord > 0 && Math.abs(arcR) >= distChord / 2) { const h = Math.sqrt(Math.max(0, arcR * arcR - (distChord / 2) * (distChord / 2))); const mx = (curX + targetX) / 2; const my = (curY + targetY) / 2; const sign = motionType === 'G02' ? -1 : 1; const cx = mx + sign * h * (-dy / distChord); const cy = my + sign * h * (dx / distChord); arcI = cx - curX; arcJ = cy - curY; } } const startPos = { x: curX, y: curY, z: curZ }; const endPos = { x: targetX, y: targetY, z: targetZ }; const isCutting = (startPos.z < 0 || endPos.z < 0) && (motionType === 'G01' || motionType === 'G02' || motionType === 'G03'); let dist = 0, arcError = false; if (motionType === 'G02' || motionType === 'G03') { const centerX = curX + arcI, centerY = curY + arcJ; const rStart = Math.hypot(arcI, arcJ); const rEnd = Math.hypot(targetX - centerX, targetY - centerY); if (Math.abs(rStart - rEnd) > 1.2 && rStart > 0) arcError = true; if (rStart > 0) { let a1 = Math.atan2(curY - centerY, curX - centerX); let a2 = Math.atan2(targetY - centerY, targetX - centerX); if (motionType === 'G02' && a2 >= a1) a2 -= Math.PI * 2; if (motionType === 'G03' && a2 <= a1) a2 += Math.PI * 2; dist = Math.abs(a2 - a1) * rStart; } else { dist = Math.hypot(endPos.x - startPos.x, endPos.y - startPos.y); } } else { dist = Math.hypot(endPos.x - startPos.x, endPos.y - startPos.y, endPos.z - startPos.z); } if (isToolChange && !motionType) { motionType = 'M06'; dist = 25; } commands.push({ lineIndex: idx, raw: rawLine, type: motionType || 'MISC', desc: desc || motionType || 'Comando CNC', from: startPos, to: endPos, arcI, arcJ, spindle: spindleOn, spindleDir, spindleRPM: rpm, tool: currentTool, isToolChange, feed: feedRate, isCutting, distance: dist, arcError }); curX = targetX; curY = targetY; curZ = targetZ; } return commands; }