-- Process.lua by Egalware s.r.l. 2024/06/13 -- Gestione calcolo disposizione e lavorazioni per serramenti -- Si opera sulla macchina corrente -- 2024/06/13 PRIMA VERSIONE -- Intestazioni require( 'EgtBase') _ENV = EgtProtectGlobal() EgtEnableDebug( true) -- TODO da cancellare quando verrà passato automaticamente da programma local WIN = {} WIN.BASEDIR = 'C:\\EgtData\\EgwWindowLua\\CAMAuto' -- 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') -- Verifico che la macchina corrente sia abilitata per la lavorazione delle Travi local sMachDir = EgtGetCurrMachineDir() if not sMachDir then EgtOutBox( 'Errore nel caricamento della macchina corrente', 'Lavora Serramenti', 'ERROR') return end if not EgtExistsFile( sMachDir .. '\\Window\\WinData.lua') then EgtOutBox( 'La macchina corrente non è configurata per lavorare serramenti', 'Lavora Serramenti', 'ERROR') return end -- 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.WinExec = nil -- TODO controllare se c'è un modo migliore per resettare librerie delle strategie caricate precedentemente -- Per ottimizzare potremmo anche ciclare solo fino al numero di strategie raggiunto per il momento. -- Infatti difficile ci siano 9999 strategie. -- reset strategie caricate come librerie for i = 1, 99 do local IdSTRTemp = EgtReplaceString( tostring( i/10000, 4), '0.', '') local sLibraryToReload = "STR" .. IdSTRTemp .. "\\STR" .. IdSTRTemp local sLibraryConfigToReload = sLibraryToReload .. "Config" if _G.package.loaded[sLibraryToReload] then _G.package.loaded[sLibraryToReload] = nil end if _G.package.loaded[sLibraryConfigToReload] then _G.package.loaded[sLibraryConfigToReload] = nil end end local WinExec = require( 'WinExec') -- Variabili globali PARTS = {} -- Colore del grezzo local ColA = Color3d( 255, 165, 0, 30) --------------------------------------------------------------------- -- Crea il grezzo che verrà messo in macchina --------------------------------------------------------------------- local function AlignPieces( PARTS) for i = 1, #PARTS do -- TODO allinea X a globale, ma bisognerebbe tenere in considerazione l'orientamento pezzi che vuole la macchina local dRotX, dRotY, dRotZ = GetFixedAxesRotABCFromFrame( PARTS[i].idFrame) local ptOrigFrame = getOrigin( PARTS[i].idFrame) if dRotX ~= 0 then EgtRotate( PARTS[i].id, ptOrigFrame, X_AX(), dRotX) end if dRotY ~= 0 then EgtRotate( PARTS[i].id, ptOrigFrame, Y_AX(), dRotY) end if dRotZ ~= 0 then EgtRotate( PARTS[i].id, ptOrigFrame, Z_AX(), dRotZ) end end end ------------------------------------------------------------------------------------------------------------- -- *** Recupero i pezzi da processare *** ------------------------------------------------------------------------------------------------------------- local function MyProcessInputData() -- Recupero le travi selezionate local nId = EgtGetFirstSelectedObj() while nId do local nPartId = EgtGetParent( EgtGetParent( nId or GDB_ID.NULL) or GDB_ID.NULL) if nPartId then local bFound = false for i = 1, #PARTS do if PARTS[i].id == nPartId then bFound = true break end end if not bFound then table.insert( PARTS, { nInd = #PARTS + 1, id = nPartId, sName = ( EgtGetName( nPartId) or ( 'Id=' .. tonumber( nPartId)))}) end end nId = EgtGetNextSelectedObj() end if #PARTS == 0 then EgtOutBox( 'Non sono state selezionati pezzi', 'Lavora Pezzi', 'ERROR') return false else -- allineo i pezzi per come se li aspetta la macchina AlignPieces() -- recupero tutte le dimensioni necessarie local sOut = '' for i = 1, #PARTS do PARTS[i].idSolid = EgtGetFirstNameInGroup( PARTS[i].id, 'Solid') PARTS[i].idFrame = EgtGetFirstNameInGroup( EgtGetFirstNameInGroup( PARTS[i].id, 'Geo'), 'Frame') PARTS[i].b3Solid = EgtGetBBoxGlob( PARTS[i].idSolid or GDB_ID.NULL, GDB_BB.STANDARD) PARTS[i].dLength = PARTS[1].b3Solid:getDimX() PARTS[i].dWidth = PARTS[1].b3Solid:getDimY() PARTS[i].dHeight = PARTS[1].b3Solid:getDimZ() sOut = sOut .. PARTS[i].sName .. ', ' end sOut = sOut:sub( 1, -3) EgtOutLog( 'Pezzi selezionati : ' .. sOut, 1) end EgtDeselectAll() return true end ------------------------------------------------------------------------------------------------------------- local function GetDataConfig() -- recupero utensili dal magazzino WinExec.GetToolsFromDB() -- TODO da gestire eventuali errori bloccanti return true end ------------------------------------------------------------------------------------------------------------- local function GetDispOffsetFromNotes( nPieceIndex) local bAllOffsetsAreOk = false local DispOffset = {} DispOffset.Phase1 = {} DispOffset.Phase2 = {} DispOffset.Phase1.dOffsetX = 0 -- dovrà essere calcolato in base alle lavorazioni DispOffset.Phase1.dOffsetY = EgtGetInfo( PARTS[nPieceIndex].id, 'OFFY_1', 'd') DispOffset.Phase1.dOffsetZ = EgtGetInfo( PARTS[nPieceIndex].id, 'OFFZ_1', 'd') DispOffset.Phase2.dOffsetX = 0 -- dovrà essere calcolato in base alle lavorazioni DispOffset.Phase2.dOffsetY = EgtGetInfo( PARTS[nPieceIndex].id, 'OFFY_2', 'd') DispOffset.Phase2.dOffsetZ = EgtGetInfo( PARTS[nPieceIndex].id, 'OFFZ_2', 'd') -- controllo se tutti gli offset sono settati if DispOffset.Phase1.dOffsetY and DispOffset.Phase1.dOffsetZ and DispOffset.Phase2.dOffsetY and DispOffset.Phase2.dOffsetZ then bAllOffsetsAreOk = true end return bAllOffsetsAreOk end ------------------------------------------------------------------------------------------------------------- local function GetDispOffsetFromInput( nPieceIndex) -- assegno alle stringhe i valori letti, in modo che vengano proposti quelli nel dialogo local sOffYPh1 = EgtIf( PARTS[nPieceIndex].DispOffset.Phase1.dOffsetY, tostring( PARTS[nPieceIndex].DispOffset.Phase1.dOffsetY), tostring( PARTS[nPieceIndex].dWidth / 2)) local sOffZPh1 = EgtIf( PARTS[nPieceIndex].DispOffset.Phase1.dOffsetZ, tostring( PARTS[nPieceIndex].DispOffset.Phase1.dOffsetZ), '0') local sOffYPh2 = EgtIf( PARTS[nPieceIndex].DispOffset.Phase2.dOffsetY, tostring( PARTS[nPieceIndex].DispOffset.Phase2.dOffsetY), tostring( PARTS[nPieceIndex].dWidth / 2)) local sOffZPh2 = EgtIf( PARTS[nPieceIndex].DispOffset.Phase2.dOffsetZ, tostring( PARTS[nPieceIndex].DispOffset.Phase2.dOffsetZ), '0') local vInp = EgtDialogBox( 'Dati di disposizione pezzo: ' .. PARTS[nPieceIndex].sName, {'Sporgenza laterale FASE1', sOffYPh1}, {'Posizione Z FASE1', sOffZPh1}, {'Sporgenza laterale FASE2', sOffYPh2}, {'Posizione Z FASE2', sOffZPh2}) if not vInp or #vInp == 0 then return false end -- salvo input nei valori che utilizzerò dopo PARTS[nPieceIndex].DispOffset.Phase1.dOffsetY = tonumber( vInp[1]) PARTS[nPieceIndex].DispOffset.Phase1.dOffsetZ = tonumber( vInp[2]) PARTS[nPieceIndex].DispOffset.Phase2.dOffsetY = tonumber( vInp[3]) PARTS[nPieceIndex].DispOffset.Phase2.dOffsetZ = tonumber( vInp[4]) return true end --------------------------------------------------------------------- -- Crea il grezzo che verrà messo in macchina --------------------------------------------------------------------- local function AddOverMaterialToRaw( PARTS) for i = 1, #PARTS do PARTS[i].RawOffset = {} -- recupero le info PARTS[i].RawOffset.dOverMatIn = EgtGetInfo( PARTS[i].id, 'OVERMAT_IN', 'd') or 5 PARTS[i].RawOffset.dOverMatOut = EgtGetInfo( PARTS[i].id, 'OVERMAT_OUT', 'd') or 5 PARTS[i].RawOffset.dOverMatLeft = EgtGetInfo( PARTS[i].id, 'OVERMAT_LEFT', 'd') or 5 PARTS[i].RawOffset.dOverMatRight = EgtGetInfo( PARTS[i].id, 'OVERMAT_RIGHT', 'd') or 5 -- aggiungo sovramateriale local dLength = PARTS[i].dLength + PARTS[i].RawOffset.dOverMatIn + PARTS[i].RawOffset.dOverMatOut local dWidth = PARTS[i].dWidth + PARTS[i].RawOffset.dOverMatLeft + PARTS[i].RawOffset.dOverMatRight local dHeight = PARTS[i].dHeight -- altezza non ha sovramteriale EgtModifyRawPartSize( PARTS[i].idRaw, dLength, dWidth, dHeight) end end --------------------------------------------------------------------- -- Crea il grezzo che verrà messo in macchina --------------------------------------------------------------------- local function CreateRaws( PARTS) for i = 1, #PARTS do PARTS[i].idRaw = EgtAddRawPartWithPart( PARTS[i].id, PARTS[i].idSolid, 0, ColA) end end ------------------------------------------------------------------------------------------------------------- -- *** Inserimento delle travi nel grezzo *** ------------------------------------------------------------------------------------------------------------- local function MyProcessPieces() -- verifico che i pezzi selezionati siano compatibili con la macchina WinData.VerifyPieces( PARTS) -- si crea il grezzo CreateRaws( PARTS) -- aggiunta sovramateriale AddOverMaterialToRaw( PARTS) -- recupero offset per posizionamento for i = 1, #PARTS do local bInsertedAllOffs = GetDispOffsetFromNotes( i) -- se non sono settati nelle note, li chiedo if not bInsertedAllOffs then bInsertedAllOffs = GetDispOffsetFromInput( i) end -- se non sono stati inseriti o c'è stato un errore esco subito if not bInsertedAllOffs then return false end end -- si dispongono i pezzi sulla tavola WinData.DisposePiecesOnTable( PARTS) return true end ------------------------------------------------------------------------------------------------------------- -- *** Inserimento delle lavorazioni *** ------------------------------------------------------------------------------------------------------------- local function MyProcessFeatures() --local bOk = WinExec.ProcessFeatures( PARTS) return true end ------------------------------------------------------------------------------------------------------------- -- *** Esecuzione *** ------------------------------------------------------------------------------------------------------------- if not MyProcessInputData() then return end if not MyProcessPieces() then return end if not GetDataConfig() then return end -- Abilito Vmill EgtSetInfo( EgtGetCurrMachGroup(), 'Vm', '1') if not MyProcessFeatures() then return end