-- BeamNestProcess.lua by Egaltech s.r.l. 2021/06/14 -- Gestione nesting automatico pareti -- Intestazioni require( 'EgtBase') _ENV = EgtProtectGlobal() EgtEnableDebug( false) -- Per test --NEST = {} --NEST.FILE = 'c:\\TechnoEssetre7\\EgtData\\Prods\\0010\\Bar_10_1.btl' --NEST.MACHINE = 'Essetre-90480019_MW' --NEST.FLAG = 3 local sLog = ' +++ BeamNestProcess : ' .. NEST.FILE .. ', ' .. NEST.MACHINE .. ', ' .. LEN["1"] EgtOutLog( sLog) -- Cancello file di log specifico local sLogFile = EgtChangePathExtension( NEST.FILE, '.txt') EgtEraseFile( sLogFile) -- Funzioni per scrittura su file di log specifico local function WriteErrToLogFile( nErr, sMsg, nRot, nCutId, nTaskId) local hFile = io.open( sLogFile, 'a') hFile:write( 'ERR=' .. tostring( nErr) .. '\n') hFile:write( sMsg .. '\n') hFile:write( 'ROT=' .. tostring( nRot or 0) .. '\n') hFile:write( 'CUTID=' .. tostring( nCutId or 0) .. '\n') hFile:write( 'TASKID=' .. tostring( nTaskId or 0) .. '\n') hFile:close() end local function WriteTimeToLogFile( dTime) local hFile = io.open( sLogFile, 'a') hFile:write( 'TIME=' .. EgtNumToString( dTime) .. '\n') hFile:close() end -- Funzione per gestire visualizzazione dopo errore local function PostErrView( nErr, sMsg) if nErr ~= 0 and ( NEST.FLAG == 1 or NEST.FLAG == 2 or NEST.FLAG == 5) then EgtSetView( SCE_VD.ISO_SW, false) EgtZoom( SCE_ZM.ALL) EgtOutBox( sMsg, 'BatchProcess (err=' .. tostring( nErr) .. ')', 'ERRORS') end end -- Funzione per gestire visualizzazione dopo warning local function PostWarnView( nWarn, sMsg) if nWarn ~= 0 and ( NEST.FLAG == 1 or NEST.FLAG == 2 or NEST.FLAG == 5) then EgtSetView( SCE_VD.ISO_SW, false) EgtZoom( SCE_ZM.ALL) EgtOutBox( sMsg, 'BatchProcess (wrn=' .. tostring( nWarn) .. ')', 'WARNINGS') end end -- Funzione per aggiornare dati ausiliari local function UpdateAuxData( sAuxFile) local bModif = false -- Se definito LOAD90, aggiorno local sLoad90 = EgtGetStringFromIni( 'AuxData', 'LOAD90', '', sAuxFile) if sLoad90 ~= '' then local BtlInfoId = EgtGetFirstNameInGroup( GDB_ID.ROOT, 'BtlInfo') or GDB_ID.NULL EgtSetInfo( BtlInfoId, 'LOAD90', sLoad90) bModif = true end return bModif end local function PartsToFill( Parts) local nToFill = 0 for i = 1, #Parts do if Parts[i].Cnt > 0 then nToFill = nToFill + Parts[i].Cnt end end return nToFill end local function ExecMaximumFilling( Raw, Parts) -- Inizializzo maximum filler EgtMaxFillerStart() -- Inserisco i pezzi for i = 1, #Parts do EgtMaxFillerAddPart( i, Parts[i].Len, Parts[i].DispLen or Parts[i].Len, Parts[i].Cnt or 1) end -- Eseguo l'ottimizzazione EgtStartCounter() EgtMaxFillerCompute( Raw.LenToFill, Raw.StartGap, Raw.MidGap, Raw.EndGap, Raw.SortType) local dTime = EgtStopCounter() -- Recupero i risultati local nFilledParts, nDiffParts, dTotFillRatio = EgtMaxFillerGetResults() local OneRes = {} for i = 0, nDiffParts - 1 do local nPartId, nCount = EgtMaxFillerGetOneResult( i) table.insert( OneRes, { Id=nPartId, Count=nCount}) end return { FilledParts=nFilledParts, DiffParts=nDiffParts, FillRatio=dTotFillRatio, Time=dTime, Data=OneRes} end -- Funzione per trovare nome MachGroup local function NewMachGroupName() local nMachGroupId = EgtGetFirstMachGroup() if not nMachGroupId then return 1 end local nMaxMachGroup = 0 while nMachGroupId do sMachGroupName = EgtGetMachGroupName(nMachGroupId) local nMachGroupName = tonumber(sMachGroupName) if nMachGroupName > nMaxMachGroup then nMaxMachGroup = nMachGroupName end nMachGroupId = EgtGetNextMachGroup(nMachGroupId) end return nMaxMachGroup + 1 end -- Imposto direttorio libreria specializzata per Travi local sBaseDir = EgtGetSourceDir() EgtAddToPackagePath( sBaseDir .. 'LuaLibs\\?.lua') -- Imposto la macchina corrente e verifico sia abilitata per la lavorazione delle Travi EgtSetCurrMachine( NEST.MACHINE) local sMachDir = EgtGetCurrMachineDir() if not EgtExistsFile( sMachDir .. '\\Beam\\BeamData.lua') then NEST.ERR = 12 NEST.MSG = 'Error not configured for beam machine : ' .. sMachine WriteErrToLogFile( NEST.ERR, NEST.MSG) PostErrView( NEST.ERR, NEST.MSG) return end -- Elimino direttori altre macchine e imposto direttorio macchina corrente per ricerca librerie EgtRemoveBaseMachineDirFromPackagePath() EgtAddToPackagePath( sMachDir .. '\\Beam\\?.lua') ---- Carico le librerie --_G.package.loaded.WallExec = nil --local WE = require( 'BeamExec') --_G.package.loaded.WallLib = nil --local WL = require( 'BeamLib') --_G.package.loaded.WProcessLapJoint = nil --local LapJoint = require( 'WProcessLapJoint') -- Inizializzo contatori errori e avvisi local nErrCnt = 0 local nWarnCnt = 0 -- Grezzi local Raws = { LenToFill = LEN["1"], StartGap = NEST.STARTOFFSET, MidGap = NEST.OFFSET, EndGap = 0, SortType = -1, Count = QTY["1"]} local nTotRaws = Raws.Count -- Pezzi local Parts = {} -- ciclo su pezzi per aggiungerli al nesting for nPartId, nCount in pairs( PART) do -- recupero lunghezza pezzo local Len = EgtGetInfo( nPartId, "L", 'd') local DispLen = EgtIf( Len < 1000, 2000, 0) table.insert( Parts, {Id = nPartId, Len = Len, DispLen = DispLen, Cnt = nCount}) end -- Ciclo fino ad esaurimento pezzi o barre local nRawTot = 0 local dTime = 0 while Raws.Count > 0 and PartsToFill( Parts) > 0 do -- Eseguo ottimizzazione per una barra e visualizzo il risultato local Res = ExecMaximumFilling( Raws, Parts) -- verifico se ci sono pezzi if Res.DiffParts > 0 then -- creo gruppo di lavorazione local MachGroupName = NewMachGroupName() nMachGroup = EgtAddMachGroup( MachGroupName) EgtSetInfo( nMachGroup, "BARLEN", Raws.LenToFill) EgtSetInfo( nMachGroup, "MATERIAL", NEST.MATERIAL) EgtSetInfo( nMachGroup, "AUTONEST", 1) -- scrivo dati per variabili P di comunicazione con la macchina in gruppo di lavorazione EgtSetInfo( nMachGroup, "PRODID", NEST.PRODID) EgtSetInfo( nMachGroup, "PATTID", nMachGroup) -- Disegno i pezzi local CurrX = Raws.StartGap local nInfoIndex = 1 for i = 1, Res.DiffParts do local PartIndex = Res.Data[i].Id local PartId = Parts[PartIndex].Id local dLen = Parts[PartIndex].Len for j = 1, Res.Data[i].Count do -- creo pezzo copia local nPartDuploId = EgtDuploNew( PartId) EgtSetInfo( nMachGroup, "PART" .. nInfoIndex, nPartDuploId .. "," .. CurrX) CurrX = CurrX + dLen + Raws.MidGap nInfoIndex = nInfoIndex + 1 end end nRawTot = nRawTot + 1 end -- Aggiorno per prossima iterazione Raws.Count = Raws.Count - 1 for i = 1, Res.DiffParts do local PartId = Res.Data[i].Id Parts[PartId].Cnt = Parts[PartId].Cnt - Res.Data[i].Count end end -- creo grezzi per ogni gruppo di lavorazione local nRawCnt = 0 _G.BEAM = {} BEAM.FILE = NEST.FILE BEAM.MACHINE = NEST.MACHINE BEAM.FLAG = 6 -- CREATE_PANEL nMachGroup = EgtGetFirstMachGroup() while nMachGroup do local nNextMachGroup = EgtGetNextMachGroup( nMachGroup) EgtSetCurrMachGroup( nMachGroup) if EgtGetInfo( nMachGroup, "AUTONEST",'i') == 1 then EgtRemoveInfo( nMachGroup, "AUTONEST") EgtSetInfo( nMachGroup, "UPDATEUI", 1) local bOk, sErr = pcall( dofile, EgtGetSourceDir() .. "BatchProcessNew.lua") if not bOk then EgtOutLog( 'Error in BatchProcessNew.lua call (' .. ( sErr or '') ..')') end nRawCnt = nRawCnt + 1 -- aggiorno interfaccia EgtProcessEvents( 200 + ( nRawCnt / nRawTot * 100), 0) end nMachGroup = nNextMachGroup end EgtResetCurrMachGroup() NEST.ERR = 0 EgtOutLog( ' +++ BeamNestProcess completed')