Files
egwwindowlua/CAMAuto/LuaLibs/MachiningLib.lua
T
andrea.villa 28e38b0441 - Lavorazione profili nella fase opportuna
- Migliorie varie
2024-06-21 17:15:37 +02:00

361 lines
16 KiB
Lua

-- MachiningLib.lua by Egalware s.r.l. 2024/06/17
-- Libreria ricerca lavorazioni per serramenti
-- Tabella per definizione modulo
local MachiningLib = {}
-- Include
require( 'EgtBase')
-- Carico i dati globali
local WinData = require( 'WinData')
local ID = require( 'Identity')
EgtOutLog( ' MachiningLib started', 1)
-------------------------------------------------------------------------------------------------------------
-- funzione per cercare utensile tipo FRESA con certe caratteristiche
function MachiningLib.FindMill( Proc, ToolSearchParameters)
local ToolInfo = {}
local nBestToolIndex
local dBestToolResidualDepth = 0
for i = 1, #TOOLS do
-- prima verifico che utensile sia compatibile
local bIsToolCompatible = true
if ToolSearchParameters.sName and ToolSearchParameters.sName ~= TOOLS[i].sName then
bIsToolCompatible = false
elseif ToolSearchParameters.dMaxToolDiameter and TOOLS[i].dDiameter > ToolSearchParameters.dMaxToolDiameter then
bIsToolCompatible = false
elseif ToolSearchParameters.sMillShape and ToolSearchParameters.sMillShape == 'STANDARD' and ( TOOLS[i].dSideAngle ~= 0 or TOOLS[i].bIsPen) then
bIsToolCompatible = false
elseif ToolSearchParameters.sMillShape and ToolSearchParameters.sMillShape == 'DOVETAIL' and not TOOLS[i].bIsDoveTail then
bIsToolCompatible = false
elseif ToolSearchParameters.sMillShape and ToolSearchParameters.sMillShape == 'TSHAPEMILL' and not TOOLS[i].bIsTMill then
bIsToolCompatible = false
elseif ToolSearchParameters.sMillShape and ToolSearchParameters.sMillShape == 'PEN' and not TOOLS[i].bIsPen then
bIsToolCompatible = false
elseif ToolSearchParameters.sType and TOOLS[i].sType ~= ToolSearchParameters.sType then
-- se sto cercando una fresa che non può lavorare di testa, quelle che lavorano di testa sono comunque ammesse
if TOOLS[i].sType == 'MILL_STD' and ToolSearchParameters.sType == 'MILL_NOTIP' then
bIsToolCompatible = true
else
bIsToolCompatible = false
end
end
-- scelgo il migliore
if bIsToolCompatible then
local dCurrentMaxMatReduction = WinData.COLL_SIC or 5
-- dCurrMachReduction = negativo -> limitare, positivo -> mm extra disponibili
local dCurrentResidualDepth = ToolSearchParameters.dElevation + dCurrentMaxMatReduction - TOOLS[i].dMaxDepth
-- se non ancora trovato, oppure se completo e il migliore fino ad ora non è completo: corrente è il migliore
if not nBestToolIndex or ( dBestToolResidualDepth > 0 and dCurrentResidualDepth <= 0) then
nBestToolIndex = i
dBestToolResidualDepth = dCurrentResidualDepth
-- altrimenti scelgo il migliore
else
-- se entrambi completi
if dBestToolResidualDepth <= 0 and dCurrentResidualDepth <= 0 then
-- se il migliore era su aggregato e corrente montanto direttamente, prediligo utensile montato direttamente
if not TOOLS[i].SetupInfo.bToolOnAggregate and TOOLS[i].SetupInfo.bToolOnAggregate then
nBestToolIndex = i
dBestToolResidualDepth = dCurrentResidualDepth
-- se hanno stesso montaggio
elseif TOOLS[i].SetupInfo.bToolOnAggregate == TOOLS[nBestToolIndex].SetupInfo.bToolOnAggregate then
-- scelgo utensile con indice di bontà utensile calcolato come: lunghezza / massimo materiale / diametro
if ( TOOLS[i].dLength / TOOLS[i].dMaxMaterial) / TOOLS[i].dDiameter < ( TOOLS[nBestToolIndex].dLength / TOOLS[nBestToolIndex].dMaxMaterial) / TOOLS[nBestToolIndex].dDiameter then
nBestToolIndex = i
dBestToolResidualDepth = dCurrentResidualDepth
end
end
-- se entrambi incompleti
elseif dBestToolResidualDepth > 0 and dCurrentResidualDepth > 0 then
--scelgo quello che lavora di più
if dCurrentResidualDepth < dBestToolResidualDepth then
nBestToolIndex = i
dBestToolResidualDepth = dCurrentResidualDepth
end
end
end
end
end
ToolInfo.nToolIndex = nBestToolIndex
ToolInfo.dResidualDepth = dBestToolResidualDepth
return ToolInfo
end
-------------------------------------------------------------------------------------------------------------
-- funzione per cercare utensile tipo LAMA con certe caratteristiche
-- TODO da completare
function MachiningLib.FindBlade( Proc, ToolSearchParameters)
local ToolInfo = {}
-- parametri opzionali
ToolSearchParameters.dElevation = ToolSearchParameters.dElevation or 0
ToolSearchParameters.bForceLongcutBlade = ToolSearchParameters.bForceLongcutBlade or false
local nBestToolIndex
for i = 1, #TOOLS do
local bIsToolCompatible = false
-- TODO per il momento si prende la prima lama. Da completare
if TOOLS[i].sFamily == 'SAWBLADE' then
bIsToolCompatible = true
end
if bIsToolCompatible then
nBestToolIndex = i
break
end
end
ToolInfo.nToolIndex = nBestToolIndex
return ToolInfo
end
-------------------------------------------------------------------------------------------------------------
-- funzione per cercare utensile tipo PUNTA A FORARE con certe caratteristiche
-- TODO da fare
function MachiningLib.FindDrill()
end
-------------------------------------------------------------------------------------------------------------
-- funzione che ritorna fase di lavoro
function MachiningLib.GetPhaseMach( Proc)
local nPhase
-- se info già calcolata
if Proc.nPhase then
nPhase = Proc.nPhase
-- altrimenti si calcola il comportamento standard
else
if Proc.sEntityName == 'Left' or Proc.sEntityName == 'Out' then
nPhase = 1
end
if Proc.sEntityName == 'Right' or Proc.sEntityName == 'In' then
nPhase = 2
end
end
return nPhase
end
-------------------------------------------------------------------------------------------------------------
-- salva in lista globale la lavorazione appena calcolata
function MachiningLib.AddNewMachining( ProcToAdd, MachiningToAdd, AuxInfoToAdd)
-- Controllo parametri obbligatori
if not MachiningToAdd.nType or not MachiningToAdd.nToolIndex or not MachiningToAdd.Geometry or not ProcToAdd.id then
return false
end
-- Drilling
if MachiningToAdd.nType == MCH_MY.DRILLING then
MachiningToAdd.sTypeName = 'Drill_'
-- Milling
elseif MachiningToAdd.nType == MCH_MY.MILLING then
-- se utensile lama
if TOOLS[MachiningToAdd.nToolIndex].sFamily == 'SAWBLADE' then
MachiningToAdd.sTypeName = 'Cut_'
else
MachiningToAdd.sTypeName = 'Mill_'
end
-- Pocketing
elseif MachiningToAdd.nType == MCH_MY.POCKETING then
MachiningToAdd.sTypeName = 'Pocket_'
-- Mortising
elseif MachiningToAdd.nType == MCH_MY.MORTISING then
MachiningToAdd.sTypeName = 'ChSaw_'
end
-- se nome non definito, assegno alla lavorazioen un nome standard
if not MachiningToAdd.sOperationName then
MachiningToAdd.sOperationName = MachiningToAdd.sTypeName .. ( EgtGetName( ProcToAdd.id) or tostring( ProcToAdd.id)) -- TODO serve scrivere faccia? -->> .. '_' .. tostring( MachiningToAdd.Geometry[1][2])
end
if not MachiningToAdd.sToolName then
MachiningToAdd.sToolName = TOOLS[MachiningToAdd.nToolIndex].sName
end
local Machining = {}
Machining.Proc = ProcToAdd
Machining.Machining = MachiningToAdd
Machining.AuxInfo = AuxInfoToAdd
table.insert( MACHININGS, Machining)
return true
end
-------------------------------------------------------------------------------------------------------------
-- funzione per aggiungere una nuova lavorazione
function MachiningLib.AddOperation( OperationToInsert)
local nErr
local sErr = ''
local bAreAllMachiningApplyOk = true
-- creazione lavorazione
local nOperationId = EgtCreateMachining( OperationToInsert.Machining.sOperationName, OperationToInsert.Machining.nType, OperationToInsert.Machining.sToolName)
EgtSetCurrMachining( nOperationId)
if nOperationId then
-- impostazione geometria
EgtSetMachiningGeometry( OperationToInsert.Machining.Geometry)
-- impostazione parametri lavorazione
if OperationToInsert.Machining.sDepth then
EgtSetMachiningParam( MCH_MP.DEPTH_STR, OperationToInsert.Machining.sDepth)
end
if OperationToInsert.Machining.bInvert then
EgtSetMachiningParam( MCH_MP.INVERT, OperationToInsert.Machining.bInvert)
end
if OperationToInsert.Machining.nWorkSide then
EgtSetMachiningParam( MCH_MP.WORKSIDE, OperationToInsert.Machining.nWorkSide)
end
if OperationToInsert.Machining.nFaceuse then
EgtSetMachiningParam( MCH_MP.FACEUSE, OperationToInsert.Machining.nFaceuse)
end
if OperationToInsert.Machining.nSCC then
EgtSetMachiningParam( MCH_MP.SCC, OperationToInsert.Machining.nSCC)
end
if OperationToInsert.Machining.bToolInvert then
EgtSetMachiningParam( MCH_MP.TOOLINVERT, OperationToInsert.Machining.bToolInvert)
end
if OperationToInsert.Machining.sBlockedAxis then
EgtSetMachiningParam( MCH_MP.BLOCKEDAXIS, OperationToInsert.Machining.sBlockedAxis)
end
if OperationToInsert.Machining.sInitialAngles then
EgtSetMachiningParam( MCH_MP.INITANGS, OperationToInsert.Machining.sInitialAngles)
end
if OperationToInsert.Machining.nHeadSide then
EgtSetMachiningParam( MCH_MP.HEADSIDE, OperationToInsert.Machining.nHeadSide)
end
if OperationToInsert.Machining.nSubType then
EgtSetMachiningParam( MCH_MP.SUBTYPE, OperationToInsert.Machining.nSubType)
end
if OperationToInsert.Machining.dOverlap then
EgtSetMachiningParam( MCH_MP.OVERL, OperationToInsert.Machining.dOverlap)
end
-- step
if OperationToInsert.Machining.Steps then
if OperationToInsert.Machining.Steps.dStepType then
EgtSetMachiningParam( MCH_MP.STEPTYPE, OperationToInsert.Machining.Steps.dStepType)
end
if OperationToInsert.Machining.Steps.dStep then
EgtSetMachiningParam( MCH_MP.STEP, OperationToInsert.Machining.Steps.dStep)
end
if OperationToInsert.Machining.Steps.dSideStep then
EgtSetMachiningParam( MCH_MP.SIDESTEP, OperationToInsert.Machining.Steps.dSideStep)
end
end
if OperationToInsert.Machining.dStartPos then
EgtSetMachiningParam( MCH_MP.STARTPOS, OperationToInsert.Machining.dStartPos)
end
if OperationToInsert.Machining.dReturnPos then
EgtSetMachiningParam( MCH_MP.RETURNPOS, OperationToInsert.Machining.dReturnPos)
end
if OperationToInsert.Machining.dRadialOffset then
EgtSetMachiningParam( MCH_MP.OFFSR, OperationToInsert.Machining.dRadialOffset)
end
if OperationToInsert.Machining.dLongitudinalOffset then
EgtSetMachiningParam( MCH_MP.OFFSL, OperationToInsert.Machining.dLongitudinalOffset)
end
-- paraemtri attacco
if OperationToInsert.Machining.LeadIn then
if OperationToInsert.Machining.LeadIn.nType then
EgtSetMachiningParam( MCH_MP.LEADINTYPE, OperationToInsert.Machining.LeadIn.nType)
end
if OperationToInsert.Machining.LeadIn.dStartAddLength then
EgtSetMachiningParam( MCH_MP.STARTADDLEN, OperationToInsert.Machining.LeadIn.dStartAddLength)
end
if OperationToInsert.Machining.LeadIn.dTangentDistance then
EgtSetMachiningParam( MCH_MP.LITANG, OperationToInsert.Machining.LeadIn.dTangentDistance)
end
if OperationToInsert.Machining.LeadIn.dPerpDistance then
EgtSetMachiningParam( MCH_MP.LIPERP, OperationToInsert.Machining.LeadIn.dPerpDistance)
end
if OperationToInsert.Machining.LeadIn.dElevation then
EgtSetMachiningParam( MCH_MP.LIELEV, OperationToInsert.Machining.LeadIn.dElevation)
end
if OperationToInsert.Machining.LeadIn.dCompLength then
EgtSetMachiningParam( MCH_MP.LICOMPLEN, OperationToInsert.Machining.LeadIn.dCompLength)
end
end
-- parametri uscita
if OperationToInsert.Machining.LeadOut then
if OperationToInsert.Machining.LeadOut.nType then
EgtSetMachiningParam( MCH_MP.LEADOUTTYPE, OperationToInsert.Machining.LeadOut.nType)
end
if OperationToInsert.Machining.LeadOut.dEndAddLength then
EgtSetMachiningParam( MCH_MP.ENDADDLEN, OperationToInsert.Machining.LeadOut.dEndAddLength)
end
if OperationToInsert.Machining.LeadOut.dTangentDistance then
EgtSetMachiningParam( MCH_MP.LOTANG, OperationToInsert.Machining.LeadOut.dTangentDistance)
end
if OperationToInsert.Machining.LeadOut.dPerpDistance then
EgtSetMachiningParam( MCH_MP.LOPERP, OperationToInsert.Machining.LeadOut.dPerpDistance)
end
if OperationToInsert.Machining.LeadOut.dElevation then
EgtSetMachiningParam( MCH_MP.LOELEV, OperationToInsert.Machining.LeadOut.dElevation)
end
if OperationToInsert.Machining.LeadOut.dCompLength then
EgtSetMachiningParam( MCH_MP.LOCOMPLEN, OperationToInsert.Machining.LeadOut.dCompLength)
end
end
if OperationToInsert.Machining.dStartSlowLen then
EgtSetMachiningParam( MCH_MP.STARTSLOWLEN, OperationToInsert.Machining.dStartSlowLen)
end
if OperationToInsert.Machining.dEndSlowLen then
EgtSetMachiningParam( MCH_MP.ENDSLOWLEN, OperationToInsert.Machining.dEndSlowLen)
end
if OperationToInsert.Machining.dThrouAddLen then
EgtSetMachiningParam( MCH_MP.THROUADDLEN, OperationToInsert.Machining.dThrouAddLen)
end
-- parametri da settare nelle note di sistema
-- TODO da decidere quali sono le note da salvare qui. Probabilmente tutte quelle relative all'ordine delle lavorazioni
local sSystemNotes = EgtGetMachiningParam( MCH_MP.SYSNOTES)
--if OperationToInsert.Machining.nMachiningOrder then
-- sSystemNotes = EgtSetValInNotes( sSystemNotes, 'MachiningOrder', OperationToInsert.Machining.nMachiningOrder)
--end
EgtSetMachiningParam( MCH_MP.SYSNOTES, sSystemNotes)
-- parametri da settare nelle note utente
local sUserNotes = EgtGetMachiningParam( MCH_MP.USERNOTES)
if OperationToInsert.Machining.dMaxElev then
sUserNotes = EgtSetValInNotes( sUserNotes, 'MaxElev', OperationToInsert.Machining.dMaxElev)
end
EgtSetMachiningParam( MCH_MP.USERNOTES, sUserNotes)
local b3MachEncumbrance = nil
local bIsApplyOk = MachiningLib.ApplyMachining( true, false)
if not bIsApplyOk then
bAreAllMachiningApplyOk = false
nErr, sErr = EgtGetLastMachMgrError()
EgtSetOperationMode( nOperationId, false)
else
local nClId = EgtGetFirstNameInGroup( nOperationId, 'CL')
local ptMin = EgtGetInfo( nClId, 'MMIN', 'p')
local ptMax = EgtGetInfo( nClId, 'MMAX', 'p')
-- box percorso lavorazione
b3MachEncumbrance = BBox3d( ptMin, ptMax)
end
return bIsApplyOk, sErr, b3MachEncumbrance
else
return false, 'UNEXPECTED ERROR: Error on creating machining'
end
end
-------------------------------------------------------------------------------------------------------------
function MachiningLib.ApplyMachining( bRecalc, bApplyPost)
local bResult = EgtApplyMachining( bRecalc, bApplyPost)
return bResult
end
-------------------------------------------------------------------------------------------------------------
return MachiningLib