Files
egwmultienginescript/BeamWallPipeLib.lua
Emmanuele Sassi 491da764ff - gestione errori su dati d'ingresso
- verifica sulla stima fatta per sezione
- aggiunta funziona CreateProduction ed abbozzo CalcProduction
- anticipato subscribe a risposta verifica via Redis
- aggiunta lettura dati del profilo
2026-02-02 18:33:24 +01:00

496 lines
17 KiB
Lua

--
-- EEEEEEEEEE GGGGGG TTTTTTTTTTTTTT
-- EEEEEEEEEE GGGGGGGGGG TTTTTTTTTTTTTT
-- EEEE GGGG GGGG TTTT
-- EEEE GGGG TTTT
-- EEEEEEE GGGG GGGGGGG TTTT
-- EEEEEEE GGGG GGGGGGG TTTT
-- EEEE GGGG GGGG TTTT
-- EEEE GGGG GGGG TTTT
-- EEEEEEEEEE GGGGGGGGGG TTTT
-- EEEEEEEEEE GGGGGG TTTT
--
-- BeamWallPipeLib by Egalware s.r.l. 2023/05/09
-- Questo script fa da libreria delle funzioni di calcolo e verifica delle travi e pareti
-- Tabella per definizione modulo
local BeamWallPipeLib = {}
require( 'EgtBase')
--_ENV = EgtProtectGlobal()
--EgtEnableDebug( true)
local sIniFilePath = EgtGetIniFile()
local sBaseDir = EgtGetStringFromIni( 'Beam', 'BaseDir', '', sIniFilePath)
EgtAddToPackagePath( sBaseDir .. '\\?.lua')
-- modalita' di chiamata
QUESTION_MODES = {
NULL = 0,
PREVIEW = 1,
BOM = 2,
HARDWARE = 3,
CONFIG = 4,
ORDER = 5,
VERIFY = 6,
}
-- Tipo di progetto
PROJECT_TYPES = {
NULL = 0,
PROJ = 1,
PROD = 2,
}
-- Tipo di progetto (Beam o Wall)
BWTYPES = {
NULL = 0,
BEAM = 1,
WALL = 2,
}
CALCSTATES = {
NOTCALCULATED = -1,
OK = 0,
INFO = 1,
WARNING = 2,
ERROR_ = 3,
COLLISION = 4,
}
CMDTYPES = {
GENERATE = 0,
MODIFY = 1,
SIMULATE = 2,
CHECK = 3,
CHECKGEN = 4,
POINT_CLOUD = 5,
RAWPART = 6,
EDIT = 7,
CHECKNOSIM = 8,
TOPOLOGY = 9,
OPENTOOLDB = 11, -- Non utilizzato
OPENMACHINIGDB = 12, -- Non utilizzato
}
local CUTID = 'CUTID'
local BTL_PRT_PDN = 'PDN'
local OUTLINE = 'Outline'
local BTL_FTR_GRP = 'GRP'
local BTL_FTR_MAINID = 'MAINID'
local BTL_FTR_AUXID = 'AUXID'
local PROCESSINGS = 'Processings'
local TASKID = 'TASKID'
_G.package.loaded.JSON = nil
local JSON = require( 'JSON')
-- Canali
local QuestionChannel = "EgwVerifyQuestion"
local AnswerChannel = "EgwVerifyAnswer"
local nAnswerChannel_Count = 0
local nAnswerChannel_LastSecond = 0
local nRedisConnectionId = 0
---------------------------------------------------------------------
-- funzione che verifica la connessione a Redis
local function VerifyRedisConnection( sConnectionString)
local bRedisConnect = false
if LUX and LUX.REDISID and LUX.REDISID > 0 then
-- connessione già creata dall'esterno
nRedisConnectionId = LUX.REDISID
elseif nRedisConnectionId == 0 then
-- se nessuna connessione la creo
bRedisConnect, nRedisConnectionId = EgtRedisAsyncConnect( sConnectionString)
if not bRedisConnect then
EgtOutLog( 'Errore! Impossibile connettersi con il server Redis dal Lua!')
end
end
return bRedisConnect
end
---------------------------------------------------------------------
-- funzione che aggiorna CutId e TaskId
local function UpdateCutIdAndTaskId( nPartId)
-- Assegno Id a CutId per calcoli
EgtSetInfo( nPartId, CUTID, nPartId, true)
-- Assegno TASKID agli outline per calcoli
local nOutLayerId = EgtGetFirstNameInGroup( nPartId, OUTLINE)
local nOutlineId = EgtGetFirstInGroup( nOutLayerId)
while nOutlineId do
-- verifico che sia una feature
local nGRP = EgtGetInfo( nOutlineId, BTL_FTR_GRP, 'i')
if nGRP then
-- verifico se ha un principale
local nMainOffsId = EgtGetInfo( nOutlineId, BTL_FTR_MAINID, 'i')
local nTaskId = nOutlineId
if nMainOffsId then
nTaskId = nOutlineId + nMainOffsId
end
-- assegno TASKID alla feature per calcoli
EgtSetInfo( nOutlineId, TASKID, nTaskId)
-- recupero eventuali AuxId
local sAuxIds = EgtGetInfo( nOutlineId, BTL_FTR_AUXID)
if sAuxIds then
local AuxIdArray = EgtSplitString( sAuxIds, ",")
if AuxIdArray.Count > 0 then
for AuxIdIndex = 1, #AuxIdArray do
local AuxId = AuxIdArray[AuxIdIndex]
AuxId = EgtTrim( AuxId)
AuxId = AuxId:gsub("^%+", "")
local nAuxId = tonumber( AuxId)
if nAuxId then
EgtSetInfo( nOutlineId + nAuxId, TASKID, nTaskId)
end
end
end
end
end
nOutlineId = EgtGetNext( nOutlineId)
end
-- Assegno TASKID alle feature per calcoli
local nProcessingId = EgtGetFirstNameInGroup( nPartId, PROCESSINGS)
local nFeatureId = EgtGetFirstInGroup( nProcessingId)
while nFeatureId do
-- verifico che sia una feature
local nGRP = EgtGetInfo( nFeatureId, BTL_FTR_GRP, 'i')
if nGRP then
-- verifico se ha un principale
local nMainOffsId = EgtGetInfo( nFeatureId, BTL_FTR_MAINID, 'i')
local nTaskId = nFeatureId
if nMainOffsId then
nTaskId = nFeatureId + nMainOffsId
end
-- assegno TASKID alla feature per calcoli
EgtSetInfo( nFeatureId, TASKID, nTaskId)
-- recupero eventuali AuxId
local sAuxIds = EgtGetInfo( nFeatureId, BTL_FTR_AUXID)
if sAuxIds then
local AuxIdArray = EgtSplitString( sAuxIds, ",")
for AuxIdIndex = 1, #AuxIdArray do
local AuxId = AuxIdArray[AuxIdIndex]
AuxId = EgtTrim( AuxId)
AuxId = AuxId:gsub("^%+", "")
local nAuxId = tonumber( AuxId)
if nAuxId then
EgtSetInfo( nFeatureId + nAuxId, TASKID, nTaskId)
end
end
end
end
nFeatureId = EgtGetNext( nFeatureId)
end
return true
end
---------------------------------------------------------------------
-- funzione che crea un Bwe da un pezzo Btl
local function CreateBWEFromPart( nPartId, sProjDirPath)
-- Aggiorno identificativi per segnalazione errori
UpdateCutIdAndTaskId( nPartId)
-- Disabilito segnalazione modificato
local bOldEnMod = EgtGetEnableModified()
if bOldEnMod then
EgtDisableModified()
end
-- Recupero visibilita' pezzo e lo imposto a visibile
local nOldPartMode = 0
EgtGetMode( nPartId, nOldPartMode)
EgtSetMode( nPartId, GDB_MD.STD)
-- Salvo pezzo nel suo progetto
local bOk = false
local nPDN = EgtGetInfo( nPartId, BTL_PRT_PDN, 'i')
--EgtSaveFile( 'c:\\Temp\\TestPDN.nge')
local sPartFilePath = sProjDirPath .. '\\' .. tostring(nPDN) .. '.bwe'
bOk = EgtSaveObjToFile({ nPartId, EgtGetFirstNameInGroup( GDB_ID.ROOT, BTLINFO), EgtGetFirstNameInGroup( GDB_ID.ROOT, PROJECTINFO)}, sPartFilePath, GDB_NT.CMPTXT)
-- Ripristino visibilita' pezzo
EgtSetMode( nPartId, nOldPartMode)
-- Ripristino stato segnalazione modifica
if bOldEnMod then
EgtEnableModified()
end
return EgtIf( bOk, sPartFilePath, '')
end
---------------------------------------------------------------------
-- funzione che crea un Bwe da un MachGroup
local function CreateBWEFromMachGroup( nMachGroupId, sProjDirPath)
-- Aggiorno identificativi per segnalazione errori
EgtSetCurrMachGroup( nMachGroupId)
local nRawId = EgtGetFirstRawPart()
while nRawId do
if EgtVerifyRawPartPhase( nRawId, 1) then
local nPartId = EgtGetFirstPartInRawPart( nRawId)
while nPartId do
UpdateCutIdAndTaskId(nPartId)
nPartId = EgtGetNextPartInRawPart( nPartId)
end
end
nRawId = EgtGetNextRawPart( nRawId)
end
-- Salvo gruppo di lavorazione nel suo progetto
local sMachGroupName = EgtGetMachGroupName( nMachGroupId)
local sMachGroupFilePath = sProjDirPath .. '\\' .. sMachGroupName .. '.bwe'
if EgtSaveMachGroupToFile( nMachGroupId, { EgtGetFirstNameInGroup( GDB_ID.ROOT, BTLINFO), EgtGetFirstNameInGroup( GDB_ID.ROOT, PROJECTINFO)}, sMachGroupFilePath, GDB_NT.CMPTEXT) then
return sMachGroupFilePath
end
return ''
end
---------------------------------------------------------------------
-- funzione che verifica un pezzo Btl
function BeamWallPipeLib.VerifyBtlPart( nPartId, BarPath, ProjType, sMachineName, GlobState)
-- verifico connessione a Redis
local bRedisConnect = VerifyRedisConnection( 'redis.ufficio:26379, serviceName=devel, DefaultDatabase=6, keepAlive=180, connectTimeout=15000, syncTimeout=15000, asyncTimeout=15000, abortConnect=false, ssl=false, allowAdmin=true')
BarPath = CreateBWEFromPart( nPartId, BarPath)
local CurrAnswerChannel = AnswerChannel .. os.time()
local Args = { BarId = nPartId,
--ProgramPage = PROJECT_TYPES.PROJ,
BarPath = BarPath,
--ProjType = BWTYPES.BEAM,
MachineName = sMachineName,
--GlobState = CALCSTATES.NOTCALCULATED,
CmdType = CMDTYPES.CHECKNOSIM,
OutputChannel = CurrAnswerChannel,
Mode = QUESTION_MODES.VERIFY,
UID = 1}
local Question = { nThreadIndex = 0,
nId = 1,
ExecEnvironment = 2,
Args = Args}
local JsonQuestion = JSON:encode( Question)
local bOk = EgtRedisAsyncPublish( nRedisConnectionId, QuestionChannel, JsonQuestion)
local AnswerArgs
if bOk then
EgtOutLog('Messaggio Redis pubblicato:' .. QuestionChannel .. ' ' .. JsonQuestion)
local bOk, Answer = EgtRedisAsyncSubscribeOneMessage( nRedisConnectionId, CurrAnswerChannel, 60000)
-- se la connessione è stata creata in questa funzione la chiudo
if bRedisConnect then
EgtRedisAsyncDisconnect( nRedisConnectionId)
nRedisConnectionId = 0
end
if bOk then
EgtOutLog('Risposta Redis ricevuta:' .. Answer)
local JsonAnswer = JSON:decode( Answer)
AnswerArgs = JsonAnswer['Args']
AnswerArgs.OutputChannel = nil
return 1, AnswerArgs
else
local sErrorMsg = 'Errore! Risposta dal canale di verifica non arrivata!'
EgtOutLog(sErrorMsg)
return 0, { Error = sErrorMsg}
end
else
local sErrorMsg = 'Errore! Pubblicazione sul canale di verifica fallita!'
EgtOutLog(sErrorMsg)
return 0, { Error = sErrorMsg}
end
end
local function GetAnswerChannelCode()
local nCurrTime = os.time()
if nCurrTime == nAnswerChannel_LastSecond then
nAnswerChannel_Count = nAnswerChannel_Count + 1
return nCurrTime .. nAnswerChannel_Count
else
nAnswerChannel_LastSecond = nCurrTime
nAnswerChannel_Count = 0
return nCurrTime
end
end
function BeamWallPipeLib.AsyncVerifyPartCalc( nPartId, BarPath, ProjType, sMachineName, GlobState, bIsMachGroup)
-- verifico connessione a Redis
local bRedisConnect = VerifyRedisConnection( 'redis.ufficio:26379, serviceName=devel, DefaultDatabase=6, keepAlive=180, connectTimeout=15000, syncTimeout=15000, asyncTimeout=15000, abortConnect=false, ssl=false, allowAdmin=true')
EgtResetCurrMachGroup()
if bIsMachGroup then
BarPath = CreateBWEFromMachGroup( nPartId, BarPath)
else
BarPath = CreateBWEFromPart( nPartId, BarPath)
end
local CurrAnswerChannel = AnswerChannel .. GetAnswerChannelCode()
local Args = { BarId = nPartId,
--ProgramPage = PROJECT_TYPES.PROJ,
BarPath = BarPath,
--ProjType = BWTYPES.BEAM,
MachineName = sMachineName,
--GlobState = CALCSTATES.NOTCALCULATED,
CmdType = CMDTYPES.CHECKNOSIM,
OutputChannel = CurrAnswerChannel,
Mode = QUESTION_MODES.VERIFY,
UID = 1}
local Question = { nThreadIndex = 0,
nId = 1,
ExecEnvironment = 2,
Args = Args}
local JsonQuestion = JSON:encode( Question)
EgtRedisAsyncSubscribe( nRedisConnectionId, CurrAnswerChannel)
local bOk = EgtRedisAsyncPublish( nRedisConnectionId, QuestionChannel, JsonQuestion)
if not bOk then
EgtOutLog( 'Error! Verify Redis publish failed on PartId ' .. nPartId .. ' and channel ' .. CurrAnswerChannel .. '!')
EgtRedisAsyncUnsubscribe( nRedisConnectionId, CurrAnswerChannel)
EgtOutLog( 'EgtRedisAsyncUnsubscribe ' .. nRedisConnectionId .. ' ' .. CurrAnswerChannel)
end
return bOk, CurrAnswerChannel
end
function BeamWallPipeLib.AsyncVerifyPartResult( CurrAnswerChannel)
-- verifico connessione a Redis
local bRedisConnect = VerifyRedisConnection( 'redis.ufficio:26379, serviceName=devel, DefaultDatabase=6, keepAlive=180, connectTimeout=15000, syncTimeout=15000, asyncTimeout=15000, abortConnect=false, ssl=false, allowAdmin=true')
local bOk, nCount, Answer = EgtRedisAsyncGetMessage( nRedisConnectionId, CurrAnswerChannel)
if bRedisConnect then
EgtRedisAsyncDisconnect( nRedisConnectionId)
nRedisConnectionId = 0
end
if bOk then
if nCount > 0 then
EgtOutLog('Risposta Redis ricevuta:' .. Answer)
EgtRedisAsyncUnsubscribe( nRedisConnectionId, CurrAnswerChannel)
EgtOutLog( 'EgtRedisAsyncUnsubscribe ' .. nRedisConnectionId .. ' ' .. CurrAnswerChannel)
local JsonAnswer = JSON:decode( Answer)
if JsonAnswer then
AnswerArgs = JsonAnswer['Args']
AnswerArgs.OutputChannel = nil
return true, true, AnswerArgs
else
return true, true
end
else
return true, false
end
else
return false
end
end
---------------------------------------------------------------------
-- funzione che analizza i risultati della verifica
local function AnalyzeResult( RESULT)
local bMachiningOk = true
local dTime = 0
for nElementIndex = 1, #RESULT do
local CurrElement = RESULT[nElementIndex]
if CurrElement.sType == 'Part' then
if tonumber( CurrElement.nErr) > 0 then
bMachiningOk = false
end
elseif CurrElement.sType == 'Feature' then
if not CurrElement.ChosenStrategy or not CurrElement.ChosenStrategy.sStatus or ( CurrElement.ChosenStrategy.sStatus ~= 'Completed' and CurrElement.ChosenStrategy.sStatus ~= 'Not-Completed') then
bMachiningOk = false
end
elseif CurrElement.sType == 'Time' then
dTime = CurrElement.dTime
end
end
return bMachiningOk, dTime
end
---------------------------------------------------------------------
-- funzione che verifica una trave
function BeamWallPipeLib.VerifyBeam( QuestionArgs)
if QuestionArgs and QuestionArgs.BarPath and QuestionArgs.MachineName and QuestionArgs.CmdType then
local BarId = tonumber( QuestionArgs["BarId"])
local BarPath = QuestionArgs["BarPath"]
local MachineName = QuestionArgs["MachineName"]
local CmdType = tonumber( QuestionArgs["CmdType"])
--local ProgramPage = tonumber( QuestionArgs["ProgramPage"])
_G.BEAM = { BASEDIR = sBaseDir,
FILE = BarPath,
MACHINE = MachineName,
FLAG = CmdType,
BW = true }
local bOk, sErr = pcall( dofile, BEAM.BASEDIR .. "\\BatchProcessNew.lua")
if not bOk then
local sErrorMsg = 'Errore! Chiamata alla funzione BatchProcessNew.lua fallita! (' .. ( sErr or '') .. ')'
EgtOutLog( sErrorMsg)
return 0, { Error = sErrorMsg}
end
local bMachiningOk ,dTime = AnalyzeResult( RESULT)
--local nResult = EgtIf( bMachiningOk, 1, 0)
local AnswerArgs = {}
AnswerArgs.ERR = BEAM.ERR
AnswerArgs.BarId = BarId
AnswerArgs.BarPath = BarPath
AnswerArgs.Time = dTime
AnswerArgs.MachiningOk = EgtIf( bMachiningOk, 1, 0)
_G.BEAM = {}
return 1, AnswerArgs
else
local sErrorMsg = 'Errore! Domanda senza argomenti che sono obbligatori!'
EgtOutLog( sErrorMsg)
return 0, { Error = sErrorMsg}
end
end
---------------------------------------------------------------------
-- funzione che esegue il nesting sui pezzi passati
function BeamWallPipeLib.NestBeam( PartIdList, RawList, MachineNameList, StartOffset, Offset, LogFile)
--if QuestionArgs and QuestionArgs.BarPath and QuestionArgs.MachineName and QuestionArgs.CmdType then
--local BarId = tonumber( QuestionArgs["BarId"])
--local BarPath = QuestionArgs["BarPath"]
--local MachineName = QuestionArgs["MachineName"]
--local CmdType = tonumber( QuestionArgs["CmdType"])
--local ProgramPage = tonumber( QuestionArgs["ProgramPage"])
_G.NEST = { BASEDIR = sBaseDir,
FILE = LogFile,
MACHINELIST = MachineNameList,
STARTOFFSET = 20,
OFFSET = 7 }
_G.PARTLIST = PartIdList
_G.RAWLIST = RawList
local sFileName = EgtGetStringFromIni( 'Nest', 'NestExec', '', sIniFilePath)
--local bOk, sErr = pcall( dofile, sBaseDir .. '\\' .. sFileName .. 'New.lua')
local bOk, sErr = pcall( dofile, sBaseDir .. '\\' .. 'NestProcessNew.lua')
if not bOk then
local sErrorMsg = 'Errore! Chiamata alla funzione NestProcessNew.lua fallita! (' .. ( sErr or '') .. ')'
EgtOutLog( sErrorMsg)
return false, sErrorMsg
end
--EgtSaveFile()
--local bMachiningOk ,dTime = AnalyzeResult( RESULT)
----local nResult = EgtIf( bMachiningOk, 1, 0)
--local AnswerArgs = {}
--AnswerArgs.ERR = BEAM.ERR
--AnswerArgs.BarId = BarId
--AnswerArgs.BarPath = BarPath
--AnswerArgs.Time = dTime
--AnswerArgs.MachiningOk = EgtIf( bMachiningOk, 1, 0)
--_G.BEAM = {}
return true
--else
-- local sErrorMsg = 'Errore! Domanda senza argomenti che sono obbligatori!'
-- EgtOutLog( sErrorMsg)
-- return 0, { Error = sErrorMsg}
--end
end
---------------------------------------------------------------------
return BeamWallPipeLib