-- BatchProcessNew.lua by Egalware s.r.l. 2026/02/09 -- Applicazione lavorazioni, simulazione, generazione e stima -- Intestazioni require( 'EgtBase') _ENV = EgtProtectGlobal() EgtEnableDebug( false) -- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -- TODO DA CANCELLARE!!!! quando verrà passato automaticamente da programma --local WIN = {} --WIN.BASEDIR = 'C:\\EgtData\\Window\\CAMAuto' --WIN.FILE = 'C:\\Temp\\TestSerramenti\\4.bwe' --WIN.MACHINE = 'Saomad-Just3500' --WIN.FLAG = 2 -- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -- Imposto direttorio libreria specializzata per serramenti EgtAddToPackagePath( WIN.BASEDIR .. '\\LuaLibs\\?.lua') -- Imposto direttorio strategie. N.B. Le strategie dovranno essere caricate con il nome del direttorio padre EgtAddToPackagePath( WIN.BASEDIR .. '\\Strategies\\?.lua') -- carico il progetto EgtOpenFile( WIN.FILE) local nMachGroupId = EgtGetFirstMachGroup() if not nMachGroupId then EgtOutLog( 'Errore! Nessun gruppo di lavorazione presente!') return end EgtSetCurrMachGroup( nMachGroupId) -- controlli preliminari local sMachDir = EgtGetCurrMachineDir() local sMachine = WIN.MACHINE EgtResetCurrMachGroup() if not sMachDir then WIN.ERR = 11 WIN.MSG = 'Errore nel caricamento della macchina corrente' WriteErrToLogFile( WIN.ERR, WIN.MSG) return end if not EgtExistsFile( sMachDir .. '\\Window\\WinData.lua') then WIN.ERR = 11 WIN.MSG = 'La macchina corrente non è configurata per lavorare serramenti' WriteErrToLogFile( WIN.ERR, WIN.MSG) return end if not EgtSetCurrMachine( sMachine) then WIN.ERR = 11 WIN.MSG = 'Error selecting machine : ' .. sMachine WriteErrToLogFile( WIN.ERR, WIN.MSG) return end local sTxtLogFile = EgtChangePathExtension( WIN.FILE, '.txt') -- Elimino direttori altre macchine e imposto direttorio macchina corrente per ricerca librerie EgtRemoveBaseMachineDirFromPackagePath() EgtAddToPackagePath( sMachDir .. '\\Window\\?.lua') -- Segnalazione avvio EgtOutLog( '*** Window Process Start ***', 1) -- Carico le librerie _G.package.loaded.WinData = nil _G.package.loaded.WinExec = nil _G.package.loaded.WinLib = nil _G.package.loaded.FeatureData = nil _G.package.loaded.MachiningLib = nil local WinExec = require( 'WinExec') local WinLib = require( 'WinLib') local JSON = require( 'JSON') local PartData = require( 'PartData') -- Carico i dati globali local WinData = require( 'WinData') -- Variabili globali PARTS = {} -- tabella contenente tutte le informazioni di ogni pezzo ------------------------------------------------------------------------------------------------------------- local function GetDataConfig() -- recupero utensili dal magazzino WinExec.GetToolsFromDB() -- TODO da gestire eventuali errori bloccanti return true end ------------------------------------------------------------------------------------------------------------- -- scrittura JSON risultati -- TODO da migliorare/completare local function WriteResultToJson( RESULT) local sData = JSON:encode_pretty( RESULT) -- Salvataggio e visualizzazione tabella local sOutFile = EgtChangePathExtension( WIN.FILE, '.json') local DestFh = io.open( sOutFile, 'w+') if not DestFh then EgtOutBox( 'Error opening ' .. sOutFile, 'TestJson', 'ERROR') return false end DestFh:write( sData) DestFh:close() return true end ------------------------------------------------------------------------------------------------------------- local function WriteErrToLogFile( nErr, sMsg, nRot, idCut, idTask) local hFile = io.open( sTxtLogFile, 'a') hFile:write( 'ERR=' .. tostring( nErr) .. '\n') hFile:write( sMsg .. '\n') hFile:write( 'ROT=' .. tostring( nRot or 0) .. '\n') hFile:write( 'CUTID=' .. tostring( idCut or 0) .. '\n') hFile:write( 'TASKID=' .. tostring( idTask or 0) .. '\n') hFile:close() end local function WriteTimeToLogFile( dTime) local hFile = io.open( sTxtLogFile, 'a') hFile:write( 'TIME=' .. EgtNumToString( dTime) .. '\n') hFile:close() end ------------------------------------------------------------------------------------------------------------- -- *** Recupero i pezzi da processare *** ------------------------------------------------------------------------------------------------------------- local function MyProcessInputData() local nId = EgtGetFirstPart() while nId do -- TODO caricare il file di costanti -- si processano solo BOTTOMRAIL (WIN_PART_TYPES.BOTTOMRAIL = 2) o STANDARD (WIN_PART_TYPES.STD = 3) if EgtGetInfo( nId, 'PART_TYPE', 'd') == 2 or EgtGetInfo( nId, 'PART_TYPE', 'd') == 3 then table.insert( PARTS, { id = nId, sName = ( EgtGetName( nId) or ( 'Id=' .. tonumber( nId)))}) end nId = EgtGetNext( nId) end PartData.GetDimensionPart( PARTS) EgtDeselectAll() return true end ------------------------------------------------------------------------------------------------------------- -- *** Inserimento dei pezzi nel grezzo *** ------------------------------------------------------------------------------------------------------------- local function MyProcessPieces() -- recupero macchinata (è sempre la prima e ce n'è solo una) local nMachGroup = EgtGetFirstMachGroup() EgtSetCurrMachGroup( nMachGroup) for i = 1, #PARTS do PARTS[i].idRaw = EgtGetRawPartFromPart( PARTS[i].id) end -- aggiungo sovramateriale ai grezzi PartData.AddOverMaterialToRaw( PARTS) -- recupero offset per posizionamento for i = 1, #PARTS do PARTS[i].DispOffsets = {} PARTS[i].DispOffsets.Phase1 = {} PARTS[i].DispOffsets.Phase2 = {} local bInsertedAllOffs = PartData.GetDispOffsetFromNotes( PARTS, i) -- se non sono stati inseriti o c'è stato un errore esco subito if not bInsertedAllOffs then EgtOutBox( 'Errore offset non trovati', 'BatchProcessWin', 'ERROR', 'OK') return false end end -- si dispongono i pezzi sulla tavola local bDispOk, sErr = WinData.AdjustDisposition( PARTS) if not bDispOk then if not sErr then sErr = 'Errore non gestito in WinData.AdjustDisposition' end EgtOutBox( sErr, 'BatchProcessWin', 'ERROR', 'OK') return false end -- Impostazione dell'attrezzaggio di default local bOk = EgtImportSetup() if not bOk then EgtImportSetup( 'Default') end return true end ------------------------------------------------------------------------------------------------------------- -- *** Esecuzione *** ------------------------------------------------------------------------------------------------------------- -- WIN.FLAG : -- 0 = generazione e stima, esegue solo un aggiornamento (eventuale) delle lavorazioni -- 1 = da processare completamente, applicazione lavorazioni, simulazione, generazione e stima -- 2 = da processare in preverifica, applicazione lavorazioni e stima -- In generale va completamente riprocessato local bToProcess = true local bToRecalc = false local bCheckNoSim = false -- se il progetto deve essere completamente riprocessato if WIN.FLAG == 1 then bToProcess = true -- se deve essere fatta la preverifica, senza simulazione elseif WIN.FLAG == 2 then bToProcess = true bCheckNoSim = true -- altrimenti solo aggiornato (se necessario) else bToProcess = false end local sLog = 'BatchProcess : ' .. WIN.FILE .. ', ' .. ( WIN.MACHINE or EgtGetCurrMachineName()) .. ', ' .. 'FLAG='..tostring( WIN.FLAG) EgtOutLog( sLog) -- se bisogna processare if bToProcess then -- Se da elaborare EgtOutLog( ' +++ Processing Parts >>>') -- nascondo geometrie varie local vAuxId = { EgtGetFirstNameInGroup( GDB_ID.ROOT, 'Profile'), EgtGetFirstNameInGroup( GDB_ID.ROOT, 'Area*'), EgtGetFirstNameInGroup( GDB_ID.ROOT, 'Aux')} EgtSetStatus( vAuxId, GDB_ST.OFF) if not MyProcessInputData() then return end if not MyProcessPieces() then return end if not GetDataConfig() then return end -- Abilito Vmill EgtSetInfo( EgtGetCurrMachGroup(), 'Vm', '1') WinExec.ProcessFeatures( PARTS) -- Imposto Nome file CN local _, sName, _ = EgtSplitPath( WIN.FILE) EgtSetInfo( EgtGetCurrMachGroup(), 'NcName', sName .. '.cnc') -- Salvo il progetto EgtSaveFile( WIN.FILE) -- altrimenti è solo da ricalcolare (eventualmente) else -- se cambiata configurazione macchina da ultima elaborazione, devo aggiornare if EgtCompareFilesLastWriteTime( WIN.FILE, sMachDir .. '\\Tools\\Tools.data') == -1 or EgtCompareFilesLastWriteTime( WIN.FILE, sMachDir .. '\\' .. sMachine ..'.mlde') == -1 then bToRecalc = true end EgtOutLog( ' +++ Loading Project already processed >>>') -- Passo in modalità lavora EgtSetCurrMachGroup( EgtGetLastMachGroup()) -- Se necessario eseguo aggiornamento con setup corrente e ricalcolo delle lavorazioni if bToRecalc then EgtOutLog( ' +++ Recalculating all dispositions and machinings >>>') EgtImportSetup() EgtApplyAllMachinings() -- Salvo il progetto EgtSaveFile( WIN.FILE) end end -- se è da riprocessare o da ricalcolare, si lancia simulazione e calcolo stima tempi if bToProcess or bToRecalc then -- simulazione e generazione eseguiti se non è la pre-verifica per stima tempi if not bCheckNoSim then -- *** Eseguo simulazione con verifica collisione in cieco *** local bSimOk, nErr, sErr = EgtSimulate() if not bSimOk then if nErr == MCH_SHE.INIT then WIN.ERR = 19 WIN.MSG = 'Error starting simulation' elseif nErr == MCH_SHE.COLLISION then WIN.ERR = 22 WIN.MSG = 'Head-part collision' elseif nErr == MCH_SHE.OUTSTROKE then WIN.ERR = 23 WIN.MSG = 'Axis outstroke ' .. sErr elseif nErr == MCH_SHE.SPECIAL then WIN.ERR = 24 WIN.MSG = 'Special error ' .. sErr else WIN.ERR = 25 WIN.MSG = 'General failure (contact supplier)' end WIN.ROT = 0 WIN.CUTID = 0 WIN.TASKID = 0 local vItem = EgtSplitString( sErr, ';') or {} for i = 1, #vItem do vItem[i] = EgtTrim( vItem[i] or '') if string.find( vItem[i], 'CUTID', 1, true) then WIN.CUTID = EgtGetVal( vItem[i], 'CUTID', 'i') or 0 elseif string.find( vItem[i], 'TASKID', 1, true) then WIN.TASKID = EgtGetVal( vItem[i], 'TASKID', 'i') or 0 end end WriteErrToLogFile( WIN.ERR, WIN.MSG, WIN.ROT, WIN.CUTID, WIN.TASKID) -- TODO gestire collisione su feature specifica!!!!!!!!!!!!!!!!!!!!!!!!!!!! WinExec.AddApplyResultToGlobalList( WIN.ERR, WIN.CUTID, WIN.MSG) WIN.RESULT = WinLib.TableCopyDeep( RESULT) WriteResultToJson( RESULT) return end end end -- *** Genero programma CN *** ( se richiesto) local bIsGenerationEnabled = ( EgtVerifyKeyOption( 110) == false) if bIsGenerationEnabled then EgtOutLog( ' +++ Generating NC part program >>>') -- TODO gestire generazione end -- *** Eseguo stima tempi *** EgtOutLog( ' +++ Estimating T&L >>>') if not EgtEstimate( '', 'EgtCAM5 - ' .. WIN.FILE) then WIN.ERR = 21 WIN.MSG = 'Error estimating production time : ' .. WIN.FILE WriteErrToLogFile( WIN.ERR, WIN.MSG) WinExec.AddApplyResultToGlobalList( WIN.ERR, 0, WIN.MSG) WIN.RESULT = WinLib.TableCopyDeep( RESULT) WriteResultToJson( RESULT) return end local Ttot = EgtGetInfo( EgtGetCurrMachGroup(), 'Ttot', 'd') local sTime = 'Total Time = ' .. EgtNumToString( Ttot, 1) EgtOutLog( sTime) -- Scrittura tempo totale stimato di lavorazione WriteTimeToLogFile( Ttot) RESULT[#RESULT+1] = { dTime = Ttot, sType = 'Time'} -- Riporto risultati in tabella globale WIN WIN.RESULT = WinLib.TableCopyDeep( RESULT) -- Scrittura json risultati WriteResultToJson( RESULT) EgtOutLog( ' +++ BatchProcess completed')