Files
NKCLUAScript/Main.lua
T

1203 lines
50 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
--
-- by EgalTech s.r.l.
-- Nesting manager by EgalTech s.r.l. 2020/08/13
-- necessary to find socket.core and mime.core
package.cpath = package.cpath .. ";/EgtProg/LuaLibs/?.dll"
package.cpath = package.cpath .. ";/EgtDevTest/LUAREST/32/?.lua"
require( 'EgtBase')
_ENV = EgtProtectGlobal()
EgtEnableDebug( true)
-- Imposto direttorio libreria specializzata per Nesting
local sBaseDir = EgtGetSourceDir()
EgtOutLog("BaseDir=" .. sBaseDir)
EgtAddToPackagePath( sBaseDir .. '?.lua')
local Config = require( 'Config')
local UtilityLib = require( 'UtilityLib')
local RESTLib = require( 'RESTLib')
local NestingLib = require( 'NestingLib')
local socket = require( 'socket')
------------------------------------------- PARAMETERS -------------------------------------------
local DebugCode = false
local ProblematicItem = 138284
------------------------------------------- ************** -------------------------------------------
local function ManageError(ErrorList, readBatch, Send)
if #ErrorList > 0 then
-- creo batch di risposta
local answ = {}
answ.BatchId = readBatch.BatchId
answ.EnvNum = readBatch.EnvNum
answ.ProcType = readBatch.ProcType
answ.MachineType = readBatch.MachineType
answ.OrderType = readBatch.OrderType
answ.ProcessNotes = ""
answ.EstimatedWorktime = 0
answ.DrawingPath = ""
answ.PartList = {}
answ.CNCPath = ""
answ.SurfaceWork = 0
answ.SurfaceTotal = 0
answ.BunkList = {}
answ.CartList = {}
answ.BinList = {}
answ.ErrorList = ErrorList
answ.ProcessStatus = 2
answ.ProcessingRuntime = EgtStopCounter() / 1000
-- se doppia macchina
if not Send and Config.bDoubleMach then
return true, answ
end
-- invio dati
local bOk = RESTLib.set(Config.sPathBatch, answ)
if bOk then print("OK") end
return true
end
return false
end
--
-- funzione che esegue nesting finale
function Nesting(readBatch)
-- imposto macchina in uso
NestingLib.ManageMachine(false)
-- elimino tutti i file presenti nella cartella nesting
EgtEmptyDirectory(Config.sBasePath .. "/Nesting" )
EgtOutText("Nesting start")
-- lista errori
local ErrorList = {}
-- conto tutto
if not readBatch.OrderList then return end
local OrderCount = #readBatch.OrderList
local KitCount = 0
local PartCount = 0
local NoDXFCount = 0
local NoMaterialCount = 0
local NoMaterialCount2 = 0
for Order = 1, #readBatch.OrderList do
KitCount = KitCount + #readBatch.OrderList[Order].KitList
for Kit = 1, #readBatch.OrderList[Order].KitList do
PartCount = PartCount + #readBatch.OrderList[Order].KitList[Kit].PartList
end
end
-- recupero lista materiali
local materials = RESTLib.get(Config.sPathMat)
for MaterialIndex = 1, #materials do
materials[MaterialIndex].PriorityList = {}
end
-- leggo materiali per tutti i pezzi, metto i verniciati con priorita' 1 sui materiali ed elimino ordini e kit dei verniciati
local OrderList = {}
local CalcReadBatch = RESTLib.get(Config.sPathBatch)
if not CalcReadBatch or CalcReadBatch.BatchId == 0 then
-- error!!
end
for OrderIndex = 1, #CalcReadBatch.OrderList do
table.insert(OrderList, CalcReadBatch.OrderList[OrderIndex])
end
NestingLib.FilterPaintedParts(OrderList, materials, ErrorList)
-- raggruppo gli ordini simili per ottimizzare l'uso del materiale
-- creo cart
-- assegno priorita' ai pezzi e li divido sui materiali
local Carts = {} -- lista dei cart
-- creo cart a partire da lista ordini e kit
-- NestingLib.CreateCart(readBatch, Carts)
NestingLib.CreateCart(OrderList, Carts)
local StartCartIndex = 0
if readBatch.NumIndexStart then
StartCartIndex = readBatch.NumIndexStart
end
for CartIndex = 1, #Carts do
Carts[CartIndex].CartIndex = StartCartIndex + CartIndex - 1
end
-- se debug
if DebugCode then
-- verifico di aver messo tutti i kit sui cart
local KitList = {}
-- raccolgo tutti i kit dai cart in una lista
for CartIndex = 1, #Carts do
for OrderIndex = 1, #Carts[CartIndex].OrderList do
for KitIndex = 1, #Carts[CartIndex].OrderList[OrderIndex].KitList do
table.insert(KitList, Carts[CartIndex].OrderList[OrderIndex].KitList[KitIndex])
end
end
end
-- aggiungo i kit dei painted
for OrderIndex = 1, #OrderList do
for KitIndex = 1, #OrderList[OrderIndex].KitList do
if OrderList[OrderIndex].KitList[KitIndex].IsPaint then
table.insert(KitList, OrderList[OrderIndex].KitList[KitIndex])
end
end
end
-- passo i kit del readbatch
for OrderIndex = 1, #readBatch.OrderList do
for BKitIndex = 1, #readBatch.OrderList[OrderIndex].KitList do
local bFound = false
for VKitIndex = #KitList, 1, -1 do
if readBatch.OrderList[OrderIndex].KitList[BKitIndex].KitId == KitList[VKitIndex].KitId then
if not bFound then
bFound = true
table.remove(KitList, VKitIndex)
else
table.insert(ErrorList, {ErrType = "E.12", Uid = "KitId = " .. readBatch.OrderList[OrderIndex].KitList[BKitIndex].KitId, Description = "Kit duplicated after putting them on cart!"})
end
end
end
if not bFound then
table.insert(ErrorList, {ErrType = "E.12", Uid = "KitId = " .. readBatch.OrderList[OrderIndex].KitList[BKitIndex].KitId, Description = "Kit lost after putting them on cart!"})
end
end
end
-- verifico se sono rimasti elementi nella tabella kit
if #KitList > 0 then
table.insert(ErrorList, {ErrType = "E.12", Uid = "KitId = " .. KitList[1].KitId, Description = #KitList .. " kit more than in batch request after putting kit on cart!"})
end
end
-- imposto priorità sui pezzi in base ai carrelli e li divido per materiale e priorità
NestingLib.SetPriorityProcessMaterial(Carts, materials, ErrorList)
-- se debug
if DebugCode then
-- verifico di aver messo tutti i part su un materiale
local PartList = {}
-- raccolgo tutti i part dai materiali in una lista
for MaterialIndex = 1, #materials do
for PriorityIndex = 1, #materials[MaterialIndex].PriorityList do
for PartIndex = 1, #materials[MaterialIndex].PriorityList[PriorityIndex].PartList do
table.insert(PartList, materials[MaterialIndex].PriorityList[PriorityIndex].PartList[PartIndex])
end
end
end
-- passo i part del readbatch
for OrderIndex = 1, #readBatch.OrderList do
for KitIndex = 1, #readBatch.OrderList[OrderIndex].KitList do
for BPartIndex = 1, #readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList do
local bFound = false
for VPartIndex = #PartList, 1, -1 do
if readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId == PartList[VPartIndex].PartId then
if not bFound then
bFound = true
table.remove(PartList, VPartIndex)
else
table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId, Description = "Part duplicated after distributing them on materials!"})
end
end
end
if not bFound then
table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId, Description = "Part lost after distributing them on materials!"})
end
end
end
end
-- verifico se sono rimasti elementi nella tabella kit
if #PartList > 0 then
table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. PartList[1].PartId, Description = #PartList .. " part more than in batch request after distributing them on materials!"})
end
end
-- creo bin necessari
local Bins = {}
NestingLib.CreateBin(materials, Bins)
-- se debug
if DebugCode then
-- verifico di aver messo tutti i painted part su un bin
end
PartCount1 = 0
local MatIde = 21
-- local MatIndex = MatIde + 1
-- local MatCount = MatIndex
local MatIndex = 1
local MatCount = #materials
--for Material = 1, #materials do
for Material = MatIndex, MatCount do
--for Material = 2, 2 do
for Priority = 1, #materials[Material].PriorityList do
for Part = 1, #materials[Material].PriorityList[Priority].PartList do
PartCount1 = PartCount1 + 1
if materials[Material].PriorityList[Priority].PartList[Part].PartId == ProblematicItem then
local x = 2
end
end
end
end
-- contatore dei pezzi ricevuti per errori sul numero finale dei pezzi
--local VPartList = {}
--for Priority = 1, #materials[11].PartList do
-- for Part = 1, #materials[11].PartList[Priority].PartList do
-- table.insert(VPartList, materials[11].PartList[Priority].PartList[Part].PartId)
-- end
--end
function comparePriority(a,b)
return a.Priority < b.Priority
end
-- per ogni materiale riordino le priorita'
local SortingArray = {}
for MaterialIndex = 1, #materials do
table.sort(materials[MaterialIndex].PriorityList, comparePriority)
end
-- per ogni materiale creo un progetto
local PARTID = "PartId"
local PRIORITY = "Priority"
local ROUGHER = {".375 ROUGHER","ROUGHER"}
local DTMXPOS = "DATA MATRIX CODE POSITION"
local MatSheetList = {}
--for Material = 1, #materials do
for Material = MatIndex, MatCount do
-- for Material = 15, 15 do
--for Material = 4, 4 do
-- se è il materiale nullo salto al prossimo
if materials[Material].MatId ~= 0 and #materials[Material].PriorityList > 0 then
EgtOutText("Material: " .. materials[Material].MatDesc)
EgtNewFile()
materials[Material].SheetList = {}
local LastRawId = GDB_ID.NULL
-- recupero lista verniciati (priorità 1)
local PartToBeMachined = {}
-- verifico se c'è lista pezzi verniciati
-- if materials[Material].PartList[1].Priority == 1 then
---- PartToBeMachined = materials[Material].PartList[1].PartList
-- PartToBeMachined = {}
-- for Part = 1, #materials[Material].PartList[1].PartList do
-- table.insert(PartToBeMachined, materials[Material].PartList[1].PartList[Part])
-- end
-- -- chiamo il nesting sui verniciati
-- LastRawId = NestingLib.NestPartInRawPart(nil, PartToBeMachined, materials[Material], ErrorList)
-- -- recupero lista di pezzi di priorità più bassa
-- if #materials[Material].PartList > 1 then
---- PartToBeMachined = materials[Material].PartList[2].PartList
-- PartToBeMachined = {}
-- for Part = 1, #materials[Material].PartList[2].PartList do
-- table.insert(PartToBeMachined, materials[Material].PartList[2].PartList[Part])
-- end
--
-- end
-- -- riempio con questi
-- LastRawId = NestingLib.NestPartInRawPart(LastRawId, PartToBeMachined, materials[Material], ErrorList)
-- end
-- for Index = 1, materials[Material].PartList do
-- if materials[Material].PartList[Index].Priority == 1 then
-- PartToBeMachined = materials[Material].PartList[Index].PartList
-- end
-- end
-- for Index = 1, materials[Material].PartList do
-- if materials[Material].PartList[Index].Priority == 2 then
-- PartToBeMachined = materials[Material].PartList[Index].PartList
-- end
-- end
-- -- riempio con quelli di priorità 2
-- LastRawId = NestingLib.NestPartInRawPart(LastRawId, PartToBeMachined, materials[Material])
-- se il primo gruppo ha priorita' 1, ne aggiungo i pezzi a quelli da nestare
if materials[Material].PriorityList[1].Priority == 1 then
for Part = 1, #materials[Material].PriorityList[1].PartList do
table.insert(PartToBeMachined, materials[Material].PriorityList[1].PartList[Part])
if materials[Material].PriorityList[1].PartList[Part].PartId == ProblematicItem then
local x = 2
end
end
end
local Counter = true
local PriorityCount = #materials[Material].PriorityList
-- ciclo sulle priorità dei pezzi
for Index = 1, PriorityCount do
-- verifico che non siano i verniciati
if materials[Material].PriorityList[Index].Priority ~= 1 then
if Counter then
-- annullo LastRaw perche' devo partire con un nuovo grezzo, altrimenti aggiungo pezzi sbagliati (riempimento gia' fatto!)
LastRawId = GDB_ID.NULL
for Part = 1, #materials[Material].PriorityList[Index].PartList do
table.insert(PartToBeMachined, materials[Material].PriorityList[Index].PartList[Part])
if materials[Material].PriorityList[Index].PartList[Part].PartId == ProblematicItem then
local x = 2
end
end
-- PartToBeMachined = materials[Material].PartList[Index].PartList
else
PartToBeMachined = {}
for Part = 1, #materials[Material].PriorityList[Index].PartList do
table.insert(PartToBeMachined, materials[Material].PriorityList[Index].PartList[Part])
if materials[Material].PriorityList[Index].PartList[Part].PartId == ProblematicItem then
local x = 2
end
end
-- PartToBeMachined = materials[Material].PartList[Index].PartList
end
-- se Counter true faccio il nesting, se false completo grezzo
-- verifico anche che non sia il primo giro in cui dovrei prima riempire i pannelli dei pezzi painted
LastRawId = NestingLib.NestPartInRawPart(LastRawId, PartToBeMachined, materials[Material], ErrorList, false, false)
if ManageError(ErrorList, readBatch) then return end
Counter = not Counter
elseif PriorityCount == 1 then
LastRawId = NestingLib.NestPartInRawPart(GDB_ID.NULL, PartToBeMachined, materials[Material], ErrorList, false, false)
if ManageError(ErrorList, readBatch) then return end
end
-- -- se Counter true faccio il nesting, se false completo grezzo
-- -- verifico anche che non sia il primo giro in cui dovrei prima riempire i pannelli dei pezzi painted
-- if Counter and not materials[Material].PartList[Index].Priority == 2 then -- LastRawId = NestPartInRawPart(LastRawId, PartList, materials[Material])
-- end
end
-- se il numero di gruppi delle priorita' e' dispari eseguo un altro nesting sui pezzi rimasti dal riempimento
if materials[Material].PriorityList[1].Priority == 1 then
PriorityCount = PriorityCount - 1
end
if PriorityCount % 2 == 0 then
-- annullo LastRaw perche' devo partire con un nuovo grezzo, altrimenti aggiungo pezzi sbagliati (riempimento gia' fatto!)
LastRawId = GDB_ID.NULL
LastRawId = NestingLib.NestPartInRawPart(LastRawId, PartToBeMachined, materials[Material], ErrorList, false, false)
if ManageError(ErrorList, readBatch) then return end
end
-- calcolo lavorazioni, stime, ecc
NestingLib.PostNestOp(materials[Material], ErrorList, false, readBatch.BatchId, 1)
NestingLib.PostNestOp(materials[Material], ErrorList, false, readBatch.BatchId, 2)
NestingLib.PostNestOp(materials[Material], ErrorList, false, readBatch.BatchId, 3)
if ManageError(ErrorList, readBatch) then return end
-- salvo il progetto di nesting
local sProjectPath = NestingLib.VerifyPath("Nesting", readBatch.BatchId, nil)
EgtSaveFile(sProjectPath .. "/" .. materials[Material].MatId .. ".nge")
end
end
EgtOutText("End sheets nest")
-- se debug
--if DebugCode then
-- verifico se ci sono tutti i pezzi dopo il nesting perche' i pezzi grandi quasi come il foglio potrebbero non essere nestati
if Config.bDoubleMach then
-- verifico di aver messo tutti i part su un grezzo
local PartList = {}
-- raccolgo tutti i part dai materiali in una lista
for MaterialIndex = 1, #materials do
if materials[MaterialIndex].SheetList then
for SheetIndex = 1, #materials[MaterialIndex].SheetList do
for PartIndex = 1, #materials[MaterialIndex].SheetList[SheetIndex].PartList do
table.insert(PartList, tonumber(materials[MaterialIndex].SheetList[SheetIndex].PartList[PartIndex].PartId))
end
end
end
end
-- passo i part del readbatch
for OrderIndex = 1, #readBatch.OrderList do
for KitIndex = 1, #readBatch.OrderList[OrderIndex].KitList do
for BPartIndex = 1, #readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList do
local bFound = false
for VPartIndex = #PartList, 1, -1 do
if readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId == PartList[VPartIndex] then
if not bFound then
bFound = true
table.remove(PartList, VPartIndex)
else
table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId, Description = "Part duplicated after nesting!"})
end
end
end
if not bFound then
table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId, Description = "Part lost after nesting!"})
end
end
end
end
-- verifico se sono rimasti elementi nella tabella kit
if #PartList > 0 then
table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. PartList[1], Description = #PartList .. " part more than in batch request after nesting!"})
end
end
local MPCount = 0
-- ordino fogli da mettere sui bunk
local SheetList = {}
for Material = 1, #materials do
if materials[Material].SheetList and #materials[Material].SheetList > 0 then
for Sheet = 1, #materials[Material].SheetList do
-- segno dimensioni materiale sul foglio
materials[Material].SheetList[Sheet].L_mm = materials[Material].L_mm
materials[Material].SheetList[Sheet].W_mm = materials[Material].W_mm
materials[Material].SheetList[Sheet].T_mm = materials[Material].T_mm
-- elimino priorita' 1
for PriorityIndex = 1, #materials[Material].SheetList[Sheet].PriorityList do
if tonumber(materials[Material].SheetList[Sheet].PriorityList[PriorityIndex]) == 1 then
table.remove(materials[Material].SheetList[Sheet].PriorityList, PriorityIndex)
if #materials[Material].SheetList[Sheet].PriorityList == 0 then
table.insert(materials[Material].SheetList[Sheet].PriorityList, "2")
end
end
end
-- verifico conto pezzi
for Part = 1, #materials[Material].SheetList[Sheet].PartList do
if materials[Material].SheetList[Sheet].PartList[Part].PartId == tostring(ProblematicItem) then
local x = 2
end
MPCount = MPCount + 1
end
-- se lista ordinata non vuota
if #SheetList > 0 then
local bInserted = false
-- scorro le priorita'
for SearchIndex = 1, #SheetList do
local bNotToBeInserted = false
local MaxList = math.min(#SheetList[SearchIndex].PriorityList, #materials[Material].SheetList[Sheet].PriorityList)
for PriorityIndex = 1, MaxList do
if not bInserted and not bNotToBeInserted and
((tonumber(SheetList[SearchIndex].PriorityList[PriorityIndex]) > tonumber(materials[Material].SheetList[Sheet].PriorityList[PriorityIndex])) or
(tonumber(SheetList[SearchIndex].PriorityList[PriorityIndex]) == tonumber(materials[Material].SheetList[Sheet].PriorityList[PriorityIndex]) and
PriorityIndex == MaxList and #SheetList[SearchIndex].PriorityList > #materials[Material].SheetList[Sheet].PriorityList)) then
table.insert(SheetList, SearchIndex, materials[Material].SheetList[Sheet])
bInserted = true
elseif
(tonumber(SheetList[SearchIndex].PriorityList[PriorityIndex]) < tonumber(materials[Material].SheetList[Sheet].PriorityList[PriorityIndex])) then
bNotToBeInserted = true
-- elseif ((SheetList[SearchIndex].PriorityList[PriorityIndex] == materials[Material].SheetList[Sheet].PriorityList[PriorityIndex])
-- and PriorityIndex == MaxList) then
-- if #SheetList[SearchIndex].PriorityList > #materials[Material].SheetList[Sheet].PriorityList then
-- table.insert(SheetList, PriorityIndex, materials[Material].SheetList[Sheet])
-- else
-- table.insert(SheetList, PriorityIndex + 1, materials[Material].SheetList[Sheet])
-- end
end
end
end
if not bInserted then
table.insert(SheetList, materials[Material].SheetList[Sheet])
end
else
table.insert(SheetList, materials[Material].SheetList[Sheet])
end
end
end
end
-- BUNK CREATI PER PRIORITA' E ALTEZZA MASSIMA
-- creo bunk prendendo X fogli con priorita' piu' bassa
local SheetXBunk = 10
local BunkList = {}
local BunkIndex = 1
local CurrBunk = {BunkIndex = BunkIndex, SheetList = {}}
table.insert(BunkList, CurrBunk)
local CurrSheetOnBunk = 0
local CurrBunkPriority = 2
local CurrBunkThickness = 0
for Sheet = 1, #SheetList do
-- if tonumber(SheetList[Sheet].PriorityList[1]) == 1 or tonumber(SheetList[Sheet].PriorityList[#SheetList[Sheet].PriorityList]) <= CurrBunkPriority + 1 then
if tonumber(SheetList[Sheet].PriorityList[#SheetList[Sheet].PriorityList]) <= CurrBunkPriority + 1 and CurrBunkThickness + SheetList[Sheet].T_mm < Config.MaxBunkThickness then
SheetList[Sheet].SheetIndex = CurrSheetOnBunk + 1
table.insert(CurrBunk.SheetList, SheetList[Sheet])
CurrSheetOnBunk = CurrSheetOnBunk + 1
CurrBunkThickness = CurrBunkThickness + SheetList[Sheet].T_mm
else
BunkIndex = BunkIndex + 1
SheetList[Sheet].SheetIndex = 1
CurrBunk = {BunkIndex = BunkIndex, SheetList = {}}
table.insert(CurrBunk.SheetList, SheetList[Sheet])
table.insert(BunkList, CurrBunk)
CurrSheetOnBunk = 1
local nSheetFirstPriority = tonumber(SheetList[Sheet].PriorityList[1])
for i = 2, #(SheetList[Sheet].PriorityList) do
nSheetFirstPriority = min(nSheetFirstPriority, tonumber(SheetList[Sheet].PriorityList[i]))
end
CurrBunkPriority = EgtIf( nSheetFirstPriority == 2, nSheetFirstPriority, nSheetFirstPriority + 1)
CurrBunkThickness = 0
end
end
-- BUNK CREATI CON SPESSORE MASSIMO
-- -- creo bunk prendendo X fogli con priorita' piu' bassa
-- local MaxBunkThickness = 500
-- local BunkList = {}
-- local BunkIndex = 1
-- local CurrBunk = {BunkIndex = BunkIndex, SheetList = {}}
-- table.insert(BunkList, CurrBunk)
-- local CurrBunkThickness = 0
-- local CurrSheetOnBunk = 0
-- for Sheet = 1, #SheetList do
-- if CurrBunkThickness < Config.MaxBunkThickness then
-- SheetList[Sheet].SheetIndex = CurrSheetOnBunk + 1
-- table.insert(CurrBunk.SheetList, SheetList[Sheet])
-- CurrSheetOnBunk = CurrSheetOnBunk + 1
-- CurrBunkThickness = CurrBunkThickness + SheetList[Sheet].T_mm
-- else
-- BunkIndex = BunkIndex + 1
-- SheetList[Sheet].SheetIndex = 1
-- CurrBunk = {BunkIndex = BunkIndex, SheetList = {}}
-- table.insert(CurrBunk.SheetList, SheetList[Sheet])
-- table.insert(BunkList, CurrBunk)
-- CurrSheetOnBunk = 1
-- CurrBunkThickness = SheetList[Sheet].T_mm
-- end
-- end
-- BUNK CREATI CON NUMERO MASSIMO DI FOGLI
-- -- creo bunk prendendo X fogli con priorita' piu' bassa
-- local SheetXBunk = 10
-- local BunkList = {}
-- local BunkIndex = 1
-- local CurrBunk = {BunkIndex = BunkIndex, SheetList = {}}
-- table.insert(BunkList, CurrBunk)
-- local CurrSheetOnBunk = 0
-- for Sheet = 1, #SheetList do
-- if CurrSheetOnBunk < SheetXBunk then
-- SheetList[Sheet].SheetIndex = CurrSheetOnBunk + 1
-- table.insert(CurrBunk.SheetList, SheetList[Sheet])
-- CurrSheetOnBunk = CurrSheetOnBunk + 1
-- else
-- BunkIndex = BunkIndex + 1
-- SheetList[Sheet].SheetIndex = 1
-- CurrBunk = {BunkIndex = BunkIndex, SheetList = {}}
-- table.insert(CurrBunk.SheetList, SheetList[Sheet])
-- table.insert(BunkList, CurrBunk)
-- CurrSheetOnBunk = 1
-- end
-- end
-- Ordino fogli sui singoli BUNK per dimensione
for BunkIndex = 1, #BunkList do
local OrderedSheets = {}
-- divido i fogli in gruppi della stessa lunghezza
for SheetIndex = 1, #BunkList[BunkIndex].SheetList do
if #OrderedSheets > 0 then
local bInserted = false
for OrderedSheetIndex = 1, #OrderedSheets do
if BunkList[BunkIndex].SheetList[SheetIndex].L_mm == OrderedSheets[OrderedSheetIndex].L_mm then
bInserted = true
table.insert(OrderedSheets[OrderedSheetIndex].SheetList, BunkList[BunkIndex].SheetList[SheetIndex])
end
end
if not bInserted then
table.insert(OrderedSheets, {L_mm = BunkList[BunkIndex].SheetList[SheetIndex].L_mm, SheetList = {BunkList[BunkIndex].SheetList[SheetIndex]}})
end
else
table.insert(OrderedSheets, {L_mm = BunkList[BunkIndex].SheetList[SheetIndex].L_mm, SheetList = {BunkList[BunkIndex].SheetList[SheetIndex]}})
end
end
-- ordino i gruppi di pari lunghezza
function compareLenght(a,b)
return a.L_mm < b.L_mm
end
-- per ogni materiale riordino le priorita'
-- for SheetIndex = 1, #OrderedSheets do
table.sort(OrderedSheets, compareLenght)
-- ordino i fogli per materiale all'interno dei gruppi
function compareMaterial(a,b)
return a.MatId < b.MatId
end
for OrderedIndex = 1, #OrderedSheets do
table.sort(OrderedSheets[OrderedIndex].SheetList, compareMaterial)
end
-- end
-- rimetto in lista tutti gli sheet come ordinati
BunkList[BunkIndex].SheetList = {}
local Index = 1
for OrderedSheetIndex = 1, #OrderedSheets do
for SheetIndex = 1, #OrderedSheets[OrderedSheetIndex].SheetList do
OrderedSheets[OrderedSheetIndex].SheetList[SheetIndex].SheetIndex = Index
table.insert(BunkList[BunkIndex].SheetList, OrderedSheets[OrderedSheetIndex].SheetList[SheetIndex])
Index = Index + 1
end
end
end
-- segno info con contenuto etichetta fogli
-- local SHEETLABEL = "SheetLabel"
-- for Material = 1, #materials do
-- if EgtOpenFile(Config.sBasePath .. "/Nesting/" .. materials[Material].MatId .. ".nge") then
-- local nMachGroupId = EgtGetFirstMachGroup()
-- while nMachGroupId do
-- local bOk = EgtSetCurrMachGroup(nMachGroupId)
-- local sMachGroupName = EgtGetMachGroupName(nMachGroupId)
-- for BunkIndex = 1, #BunkList do
-- for SheetIndex = 1, #BunkList[BunkIndex].SheetList do
-- if BunkList[BunkIndex].SheetList[SheetIndex].MatId == materials[Material].MatId and BunkList[BunkIndex].SheetList[SheetIndex].MachGroupName == sMachGroupName then
-- local nRaw = EgtGetFirstRawPart()
-- local nSheetLabel = EgtGetFirstNameInGroup(nRaw, SHEETLABEL)
-- local bOk = EgtSetInfo(nSheetLabel, SHEETLABEL, BunkIndex .. "." .. SheetIndex)
-- end
-- end
-- end
-- nMachGroupId = EgtGetNextMachGroup(nMachGroupId)
-- end
-- EgtSaveFile()
-- end
-- end
-- se debug
if DebugCode then
-- verifico di aver messo tutti gli sheet sui bunk
local SheetList = {}
-- raccolgo tutti gli sheet dai bunk in una lista
for BunkIndex = 1, #BunkList do
for SheetIndex = 1, #BunkList[BunkIndex].SheetList do
table.insert(SheetList, BunkList[BunkIndex].SheetList[SheetIndex])
end
end
-- passo gli sheet dei material
for MaterialIndex = 1, #materials do
if materials[MaterialIndex].SheetList then
for BSheetIndex = 1, #materials[MaterialIndex].SheetList do
local bFound = false
for VSheetIndex = 1, #SheetList do
if materials[MaterialIndex].SheetList[BSheetIndex] == SheetList[VSheetIndex] then
bFound = true
table.remove(SheetList, VSheetIndex)
end
end
if not bFound then
table.insert(ErrorList, {ErrType = "E.12", Uid = "SheetId = " ..materials[MaterialIndex].MaterialId .." - " .. materials[MaterialIndex].SheetList[BSheetIndex].SheetIndex,
Description = "Sheet lost after distribution on bunks!"})
end
end
end
end
-- verifico se sono rimasti elementi nella tabella kit
if #SheetList > 0 then
table.insert(ErrorList, {ErrType = "E.12", Uid = "SheetId = " .. SheetList[1].SheetIndex, Description = #PartList .. " sheet more than in materials after distribution on bunks!"})
end
end
local MPCount2 = 0
local TotThickness = 0
for CurrBunk = 1, #BunkList do
for CurrSheet = 1, #BunkList[CurrBunk].SheetList do
TotThickness = TotThickness + BunkList[CurrBunk].SheetList[CurrSheet].T_mm
MPCount2 = MPCount2 + 1
end
end
-- verifica assenza pezzi tra ricevuti e messi sui fogli
--local SVPartList = {}
--for Sheet = 1, #materials[11].SheetList do
-- for Part = 1, #materials[11].SheetList[Sheet].PartList do
-- table.insert(SVPartList, materials[11].SheetList[Sheet].PartList[Part].PartId)
-- end
--end
--table.sort(VPartList)
--table.sort(SVPartList)
--local ListCount = #VPartList
--local List1 = VPartList
--local List2 = SVPartList
--local Only1 = {}
--local Only2 = {}
--if #SVPartList > #VPartList then
-- ListCount = #SVPartList
-- List1 = SVPartList
-- List2 = VPartList
--end
--local Index1, Index2 = 1, 1
--while Index1 <= #List1 do
-- if Index2 <= #List2 then
-- if List1[Index1] > tonumber(List2[Index2]) then
-- table.insert(Only2, List2[Index2])
-- Index2 = Index2 + 1
-- elseif List1[Index1] < tonumber(List2[Index2]) then
-- table.insert(Only1, List2[Index2])
-- Index1 = Index1 + 1
-- Index2 = Index2 + 1
-- end
-- else
-- table.insert(Only1, List2[Index2])
-- Index1 = Index1 + 1
-- end
--end
-- creo batch di risposta
local answ = {}
answ.BatchId = readBatch.BatchId
answ.EnvNum = readBatch.EnvNum
answ.ProcType = readBatch.ProcType
answ.MachineType = readBatch.MachineType
answ.OrderType = readBatch.OrderType
answ.ProcessNotes = ""
-- sommo tempo totale di lavorazione e superficie materiale utilizzata
local TotTime = 0
local SurfaceWork = 0
local SurfaceTotal = 0
local ChangeSheetTime = 100
for Material = 1, #materials do
if materials[Material] and materials[Material].SheetList and #materials[Material].SheetList > 0 then
for Sheet = 1, #materials[Material].SheetList do
TotTime = TotTime + materials[Material].SheetList[Sheet].EstimatedWorktime + ChangeSheetTime
SurfaceWork = SurfaceWork + materials[Material].SheetList[Sheet].SurfaceWork
SurfaceTotal = SurfaceTotal + materials[Material].SheetList[Sheet].SurfaceTotal
end
end
end
answ.EstimatedWorktime = TotTime
answ.SurfaceWork = SurfaceWork
answ.SurfaceTotal = SurfaceTotal
-- aggiungo i bunk alla risposta
answ.BunkList = BunkList
-- aggiungo carrelli con kit alla risposta eliminando gli ordini
-- local StartCartIndex = 0
-- if readBatch.NumIndexStart then
-- StartCartIndex = readBatch.NumIndexStart
-- end
for CartIndex = 1, #Carts do
Carts[CartIndex].KitList = {}
-- Carts[CartIndex].CartIndex = StartCartIndex + CartIndex - 1
for OrderIndex = 1, #Carts[CartIndex].OrderList do
for KitIndex = 1, #Carts[CartIndex].OrderList[OrderIndex].KitList do
table.insert(Carts[CartIndex].KitList, Carts[CartIndex].OrderList[OrderIndex].KitList[KitIndex])
end
end
end
answ.CartList = Carts
-- aggiungo bins alla risposta
answ.BinList = Bins
-- attacco lista errori e tempo di computazione
answ.ErrorList = ErrorList
if #ErrorList == 0 then
answ.ProcessStatus = 3
else
answ.ProcessStatus = 2
end
answ.ProcessingRuntime = EgtStopCounter() / 1000
-- invio dati
local bOk = RESTLib.set(Config.sPathBatch, answ)
if bOk then print("OK") end
EgtOutText("Nesting end")
end
--
-- funzione che esegue calcolo stima tempi
function Estimate(readBatch)
-- calcolo lavorazioni, stime, ecc
local IsOffLine = false
if readBatch.OrderType == 1 then
IsOffLine = true
end
-- imposto macchina in uso
NestingLib.ManageMachine(IsOffLine)
EgtOutText("Estimate start")
-- lista errori
local ErrorList = {}
-- conto tutto
local OrderCount = #readBatch.OrderList
local KitCount = 0
local PartCount = 0
local NoDXFCount = 0
local NoMaterialCount = 0
local NoMaterialCount2 = 0
for Order = 1, #readBatch.OrderList do
KitCount = KitCount + #readBatch.OrderList[Order].KitList
for Kit = 1, #readBatch.OrderList[Order].KitList do
PartCount = PartCount + #readBatch.OrderList[Order].KitList[Kit].PartList
end
end
-- recupero lista materiali
local materials = RESTLib.get(Config.sPathMat)
-- aggiungo liste pezzi
for Material = 1, #materials do
if not materials[Material].PartList then materials[Material].PartList = {} end
end
-- divido pezzi per materiale
local CurrOrderIndex = 1
local CurrOrderCount = #readBatch.OrderList
if Config.bDoubleMach then
CurrOrderIndex = readBatch.CurrOrderIndex
CurrOrderCount = readBatch.CurrOrderIndex
end
for Order = CurrOrderIndex, CurrOrderCount do
OrderCount = OrderCount + 1
for Kit = 1, #readBatch.OrderList[Order].KitList do
KitCount = KitCount + 1
for Part = 1, #readBatch.OrderList[Order].KitList[Kit].PartList do
PartCount = PartCount + 1
local TempPart = readBatch.OrderList[Order].KitList[Kit].PartList[Part]
-- leggo le proprietà dal dxf e le imposto
local MatExtCode, PdfLink, Paint, OptParameters, ErrorType = UtilityLib.readDXF(TempPart.CadFilePath, TempPart.PartExtCode, true, readBatch.CreatePng)
-- solo se trovo il materiale vado avanti per questo pezzo
if ErrorType == 0 and MatExtCode then
TempPart.MatExtCode = MatExtCode
TempPart.OptParameters = OptParameters
TempPart.OptParameters.PdfLink = PdfLink
local PaintVal = "NO"
if Paint then
PaintVal = "YES"
end
TempPart.OptParameters.PaintFlag = PaintVal
-- suddivido i pezzi per materiale
for Material = 1, #materials do
if TempPart.MatExtCode == materials[Material].MatExtCode then
TempPart.MatId = materials[Material].MatId
-- aggiungo il pezzo all'interno del materiale
table.insert(materials[Material].PartList, TempPart)
end
end
elseif ErrorType == 1 then
table.insert(ErrorList, {ErrType = "E.1", Uid = "PartId = " .. TempPart.PartId, Description = "Can't find DXF file"})
EgtOutLog("Part skipped: " .. TempPart.PartId)
elseif ErrorType == 2 then
table.insert(ErrorList, {ErrType = "E.2", Uid = "PartId = " .. TempPart.PartId, Description = "Can't find material tag in DXF"})
EgtOutLog("Part skipped: " .. TempPart.PartId)
else
table.insert(ErrorList, {ErrType = "E.6", Uid = "PartId = " .. TempPart.PartId, Description = "Error in DXF reading"})
EgtOutLog("Part skipped: " .. TempPart.PartId)
end
end
end
end
PartCount1 = 0
local MatIde = 10
-- local MatIndex = MatIde + 1
-- local MatCount = MatIndex
local MatIndex = 1
local MatCount = #materials
-- per ogni materiale creo un progetto
local PARTID = "PartId"
local PRIORITY = "Priority"
local ROUGHER = {".375 ROUGHER","ROUGHER"}
local DTMXPOS = "DATA MATRIX CODE POSITION"
local MatSheetList = {}
--for Material = 1, #materials do
for Material = MatIndex, MatCount do
-- se è il materiale nullo salto al prossimo
if materials[Material].MatId ~= 0 and #materials[Material].PartList > 0 then
EgtOutText("Material: " .. materials[Material].MatDesc)
EgtNewFile()
materials[Material].SheetList = {}
local PartToBeMachined = {}
for Part = 1, #materials[Material].PartList do
table.insert(PartToBeMachined, materials[Material].PartList[Part])
end
local IsValidation = false
if readBatch.MaxTime == 1 then
IsValidation = true
end
NestingLib.NestPartInRawPart(nil, PartToBeMachined, materials[Material], ErrorList, true, IsValidation)
local bOk, answ = ManageError(ErrorList, readBatch)
-- se doppia macchina
if bOk then
if Config.bDoubleMach then
return answ
else
return
end
end
NestingLib.PostNestOp(materials[Material], ErrorList, true, readBatch.BatchId)
bOk, answ = ManageError(ErrorList, readBatch)
-- se doppia macchina
if bOk then
if Config.bDoubleMach then
return answ
else
return
end
end
-- -- salvo il progetto di nesting
-- EgtSaveFile(Config.sBasePath .. "/Estimate/" .. materials[Material].MatId .. ".nge")
-- salvo il progetto di nesting
local sProjectPath = NestingLib.VerifyPath("Estimate", readBatch.BatchId, nil)
EgtSaveFile(sProjectPath .. "/" .. materials[Material].MatId .. ".nge")
end
end
-- verifico se ci sono tutti i pezzi dopo il nesting perche' i pezzi grandi quasi come il foglio potrebbero non essere nestati
if Config.bDoubleMach then
-- verifico di aver messo tutti i part su un grezzo
local PartList = {}
-- raccolgo tutti i part dai materiali in una lista
for MaterialIndex = 1, #materials do
if materials[MaterialIndex].SheetList then
for SheetIndex = 1, #materials[MaterialIndex].SheetList do
for PartIndex = 1, #materials[MaterialIndex].SheetList[SheetIndex].PartList do
table.insert(PartList, tonumber(materials[MaterialIndex].SheetList[SheetIndex].PartList[PartIndex].PartId))
end
end
end
end
-- passo i part del readbatch
for OrderIndex = CurrOrderIndex, CurrOrderCount do
for KitIndex = 1, #readBatch.OrderList[OrderIndex].KitList do
for BPartIndex = 1, #readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList do
local bFound = false
for VPartIndex = #PartList, 1, -1 do
if readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId == PartList[VPartIndex] then
if not bFound then
bFound = true
table.remove(PartList, VPartIndex)
else
table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId, Description = "Part duplicated after nesting!"})
end
end
end
if not bFound then
table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId, Description = "Part lost after nesting!"})
end
end
end
end
-- verifico se sono rimasti elementi nella tabella kit
if #PartList > 0 then
table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. PartList[1], Description = #PartList .. " part more than in batch request after nesting!"})
end
end
EgtOutText("End sheets estimate")
-- creo batch di risposta
local answ = {}
answ.BatchId = readBatch.BatchId
answ.EnvNum = readBatch.EnvNum
answ.ProcType = readBatch.ProcType
answ.MachineType = readBatch.MachineType
answ.OrderType = readBatch.OrderType
answ.ProcessNotes = ""
-- sommo tempo totale di lavorazione
local TotTime = 0
local ChangeSheetTime = 100
for Material = 1, #materials do
if materials[Material] and materials[Material].SheetList and #materials[Material].SheetList > 0 then
for Sheet = 1, #materials[Material].SheetList do
TotTime = TotTime + materials[Material].SheetList[Sheet].EstimatedWorktime + ChangeSheetTime
end
end
end
answ.EstimatedWorktime = TotTime + (TotTime * 0.1) -- aggiungo 10% di sicurezza
if readBatch.OrderType == 1 then
answ.DrawingPath = NestingLib.VerifyPath("SVG", readBatch.BatchId, nil) .. "/"
answ.CNCPath = NestingLib.VerifyPath("CNC", readBatch.BatchId, nil) .. "/"
end
-- recupero pezzi analizzati
answ.PartList = {}
for Material = MatIndex, MatCount do
if materials[Material].PartList and #materials[Material].PartList > 0 then
for Part = 1, #materials[Material].PartList do
table.insert(answ.PartList, materials[Material].PartList[Part])
end
end
end
-- attacco lista errori e tempo di computazione
answ.ErrorList = ErrorList
if #ErrorList == 0 then
answ.ProcessStatus = 3
else
answ.ProcessStatus = 2
end
answ.ProcessingRuntime = EgtStopCounter() / 1000
-- se doppia macchina
if Config.bDoubleMach then
return answ
end
-- invio dati
local bOk = RESTLib.set(Config.sPathBatch, answ)
if bOk then print("OK") end
EgtOutText("Estimate end")
end
--
-- funzione che esegue calcolo stima tempi
function ExtendedEstimate(readBatch)
-- creo batch di risposta
local answ = {}
answ.BatchId = readBatch.BatchId
answ.EnvNum = readBatch.EnvNum
answ.ProcType = readBatch.ProcType
answ.MachineType = readBatch.MachineType
answ.OrderType = readBatch.OrderType
answ.ProcessNotes = ""
-- lista errori
local ErrorList = {}
-- tempo totale stimato
local TotTime = 0
answ.EstOrderList = {}
answ.PartList = {}
-- chiamo stima per ogni materiale
for Order = 1, #readBatch.OrderList do
readBatch.CurrOrderIndex = Order
local estm = Estimate(readBatch)
-- aggiungo pezzi a lista pezzi del risultato
if #estm.ErrorList == 0 then
for PartIndex = 1, #estm.PartList do
table.insert(answ.PartList, estm.PartList[PartIndex])
end
end
-- accodo errori dell'ordine
for ErrorIndex = 1, #estm.ErrorList do
table.insert(ErrorList, estm.ErrorList[ErrorIndex])
end
if ManageError(ErrorList, readBatch, true) then return end
-- recupero lista materiali
local materials = RESTLib.get(Config.sPathMat)
for MaterialIndex = 1, #materials do
materials[MaterialIndex].PriorityList = {}
end
local OrderList = {}
local Carts = {}
table.insert( OrderList, readBatch.OrderList[Order])
-- calcolo numero carrelli
NestingLib.FilterPaintedParts(OrderList, materials, ErrorList)
NestingLib.CreateCart(OrderList, Carts)
-- imposto priorità sui pezzi in base ai carrelli e li divido per materiale e priorità
NestingLib.SetPriorityProcessMaterial(Carts, materials, ErrorList)
-- creo bin necessari
local Bins = {}
NestingLib.CreateBin(materials, Bins)
-- riporto numero pezzi, carrelli e stima tempo taglio nell'ordine
readBatch.OrderList[Order].NumCart = #Carts
readBatch.OrderList[Order].NumBin = #Bins
readBatch.OrderList[Order].NumPart = #estm.PartList
readBatch.OrderList[Order].TotTime = estm.EstimatedWorktime
TotTime = TotTime + estm.EstimatedWorktime
-- aggiungo ordine a lista ordini del risultato
table.insert(answ.EstOrderList, readBatch.OrderList[Order])
end
-- -- verifico se ci sono tutti i pezzi dopo il nesting perche' i pezzi grandi quasi come il foglio potrebbero non essere nestati
-- if true then
-- -- verifico di aver messo tutti i part su un grezzo
-- local PartList = {}
-- -- raccolgo tutti i part dai materiali in una lista
-- for MaterialIndex = 1, #materials do
-- if materials[MaterialIndex].SheetList then
-- for SheetIndex = 1, #materials[MaterialIndex].SheetList do
-- for PartIndex = 1, #materials[MaterialIndex].SheetList[SheetIndex].PartList do
-- table.insert(PartList, tonumber(materials[MaterialIndex].SheetList[SheetIndex].PartList[PartIndex].PartId))
-- end
-- end
-- end
-- end
-- -- passo i part del readbatch
-- for OrderIndex = 1, #readBatch.OrderList do
-- for KitIndex = 1, #readBatch.OrderList[OrderIndex].KitList do
-- for BPartIndex = 1, #readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList do
-- local bFound = false
-- for VPartIndex = #PartList, 1, -1 do
-- if readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId == PartList[VPartIndex] then
-- if not bFound then
-- bFound = true
-- table.remove(PartList, VPartIndex)
-- else
-- table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId, Description = "Part duplicated after nesting!"})
-- end
-- end
-- end
-- if not bFound then
-- table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. readBatch.OrderList[OrderIndex].KitList[KitIndex].PartList[BPartIndex].PartId, Description = "Part lost after nesting!"})
-- end
-- end
-- end
-- end
-- -- verifico se sono rimasti elementi nella tabella kit
-- if #PartList > 0 then
-- table.insert(ErrorList, {ErrType = "E.12", Uid = "PartId = " .. PartList[1], Description = #PartList .. " part more than in batch request after nesting!"})
-- end
-- end
-- correggo e riporto tempo totale di lavorazione
answ.EstimatedWorktime = TotTime
-- attacco lista errori e tempo di computazione
answ.ErrorList = ErrorList
if #ErrorList == 0 then
answ.ProcessStatus = 3
else
answ.ProcessStatus = 2
end
answ.ProcessingRuntime = EgtStopCounter() / 1000
-- invio dati
local bOk = RESTLib.set(Config.sPathBatch, answ)
if bOk then print("OK") end
EgtOutText("ExtendedEstimate end")
end
--
-- ciclo principale
while true do
EgtNewFile()
if EgtProcessEvents( 1, 5) == 1 then
break
else
-- leggo un batch
local readBatch = RESTLib.get(Config.sPathBatch)
if not readBatch or readBatch.BatchId == 0 then --or #readBatch == 0 then
EgtPause(Config.CycleDelay)
-- socket.sleep(0.5)
-- EgtOutLog("Server connection error!")
else
EgtStartCounter()
-- readBatch.OrderType = 1 -> Offline
if readBatch.ProcType == 1 or readBatch.OrderType == 1 then
Estimate(readBatch)
elseif readBatch.ProcType == 3 then
Config.bDoubleMach = true
ExtendedEstimate(readBatch)
else
Nesting(readBatch)
end
end
end
end