-- MachiningLib.lua by Egalware s.r.l. 2024/04/02 -- Libreria ricerca lavorazioni per Travi -- 2024/04/02 PRIMA VERSIONE CALCOLO LAVORAZIONI CON STRATEGIE -- Tabella per definizione modulo local MachiningLib = {} -- Include require( 'EgtBase') -- Carico i dati globali local BeamData = require( 'BeamData') EgtOutLog( ' MachiningLib started', 1) --------------------------------------------------------------------- -- TODO da considerare solo angolo 2D?? local function GetToolEntryAngle( Proc, vtTool) local Angle = {} local dSinAngle = -10 * GEO.EPS_SMALL local vtNorm if Proc.AffectedFaces.bTop then vtNorm = Z_AX() dSinAngle = max( dSinAngle, vtTool * vtNorm) end if Proc.AffectedFaces.bBottom then vtNorm = -Z_AX() dSinAngle = max( dSinAngle, vtTool * vtNorm) end if Proc.AffectedFaces.bFront then vtNorm = -Y_AX() dSinAngle = max( dSinAngle, vtTool * vtNorm) end if Proc.AffectedFaces.bBack then vtNorm = Y_AX() dSinAngle = max( dSinAngle, vtTool * vtNorm) end if Proc.AffectedFaces.bLeft then vtNorm = -X_AX() dSinAngle = max( dSinAngle, vtTool * vtNorm) end if Proc.AffectedFaces.bRight then vtNorm = X_AX() dSinAngle = max( dSinAngle, vtTool * vtNorm) end local dCosAngle = sqrt( 1 - sqr( dSinAngle)) local dAngle = acos( dCosAngle) local dTanAngle if dAngle ~= 0 and dAngle ~= 90 then dTanAngle = sqrt( 1 - dCosAngle * dCosAngle) / dCosAngle end Angle.dValue = dAngle Angle.dSin = dSinAngle Angle.dCos = dCosAngle Angle.dTan = dTanAngle return Angle end ------------------------------------------------------------------------------------------------------------- function MachiningLib.GetMachiningSteps( dMachiningDepth, dStep) local MachiningSteps = {} MachiningSteps.dStepLength = 0 MachiningSteps.nCount = ceil( ( dMachiningDepth - 10 * GEO.EPS_SMALL) / dStep) if MachiningSteps.nCount > 1 then MachiningSteps.dStepLength = ( dMachiningDepth - dStep) / ( MachiningSteps.nCount - 1) end return MachiningSteps end ------------------------------------------------------------------------------------------------------------- -- 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 TOOLS[i].sType ~= ToolSearchParameters.sType then bIsToolCompatible = false elseif TOOLS[i].dDiameter > ToolSearchParameters.dMaxToolDiameter then bIsToolCompatible = false elseif ToolSearchParameters.sMillShape == 'STANDARD' and ( TOOLS[i].dSideAngle ~= 0 or TOOLS[i].bIsPen) then bIsToolCompatible = false elseif ToolSearchParameters.sMillShape == 'DOVETAIL' and not TOOLS[i].bIsDoveTail then bIsToolCompatible = false elseif ToolSearchParameters.sMillShape == 'TSHAPEMILL' and not TOOLS[i].bIsTMill then bIsToolCompatible = false elseif ToolSearchParameters.sMillShape == 'PEN' and not TOOLS[i].bIsPen then bIsToolCompatible = false -- TODO controllare montaggio e verificare se direzione utensile raggiungibile. Serve funzione in BeamData end -- scelgo il migliore if bIsToolCompatible then -- calcolo riduzione del massimo materiale utilizzabile local ToolEntryAngle = GetToolEntryAngle( Proc, ToolSearchParameters.vtToolDir) -- se ToolHolder più grande dell'utensile, il primo oggetto in collisione è il ToolHolder. Altrimenti il motore. local dDimObjToCheck = EgtIf( TOOLS[i].ToolHolder.dDiameter > TOOLS[i].dDiameter, TOOLS[i].ToolHolder.dDiameter, BeamData.C_SIMM_ENC) local dCurrentMaxMatReduction = BeamData.COLL_SIC or 5 -- TODO implementare le funzioni di Tool Collision Avoidance (vedi wiki e FacesBysaw -> CalcLeadInOutPerpGeom) -- TODO considerare anche il caso in cui lo stelo sia più grande del diametro utensile -- TODO nei confronti tra valori gestire tolleranze -- calcolo riduzione per non toccare con ToolHolder / Motore if ToolEntryAngle.dValue > 0 and ToolEntryAngle.dValue < 90 then dCurrentMaxMatReduction = dCurrentMaxMatReduction / ToolEntryAngle.dSin + ( ( dDimObjToCheck - TOOLS[i].dDiameter) / 2) / ToolEntryAngle.dTan end -- 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 rapporto lunghezza / diametro minore 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 obbligatori if type( ToolSearchParameters.vtToolDirection) ~= 'table' then error( 'FindBlade : missing tool direction') end if type( ToolSearchParameters.bAllowTopHead) ~= 'boolean' then error( 'FindBlade : missing top head info') end if type( ToolSearchParameters.bAllowBottomHead) ~= 'boolean' then error( 'FindBlade : missing bottom head info') end if not ToolSearchParameters.bAllowTopHead and not ToolSearchParameters.bAllowBottomHead then error( 'FindBlade : wrong head info') end -- parametri opzionali ToolSearchParameters.dElevation = ToolSearchParameters.dElevation or 0 ToolSearchParameters.bForceLongcutBlade = ToolSearchParameters.bForceLongcutBlade or false local nBestToolIndex for i = 1, #TOOLS do local bIsToolCompatible = false if TOOLS[i].sFamily == 'SAWBLADE' then if ToolSearchParameters.bAllowTopHead and not ToolSearchParameters.bAllowBottomHead then bIsToolCompatible = TOOLS[i].SetupInfo.bIsTopHead elseif ToolSearchParameters.bAllowBottomHead and not ToolSearchParameters.bAllowTopHead then bIsToolCompatible = TOOLS[i].SetupInfo.bIsBottomHead end 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 per cercare utensile tipo SEGA A CATENA con certe caratteristiche -- TODO da completare function MachiningLib.FindChainSaw( Proc, ToolSearchParameters) local ToolInfo = {} -- parametri obbligatori if type( ToolSearchParameters.vtToolDirection) ~= 'table' then error( 'FindBlade : missing tool direction') end if type( ToolSearchParameters.bAllowTopHead) ~= 'boolean' then error( 'FindBlade : missing top head info') end if type( ToolSearchParameters.bAllowBottomHead) ~= 'boolean' then error( 'FindBlade : missing bottom head info') end if not ToolSearchParameters.bAllowTopHead and not ToolSearchParameters.bAllowBottomHead then error( 'FindBlade : wrong head info') end -- parametri opzionali ToolSearchParameters.dElevation = ToolSearchParameters.dElevation or 0 ToolSearchParameters.bExtendWithCornerRadius = ToolSearchParameters.bExtendWithCornerRadius or false local nBestToolIndex local dBestToolResidualDepth = 0 for i = 1, #TOOLS do local bIsToolCompatible = false if TOOLS[i].sFamily == 'MORTISE' then if ToolSearchParameters.bAllowTopHead and not ToolSearchParameters.bAllowBottomHead then bIsToolCompatible = TOOLS[i].SetupInfo.bIsTopHead elseif ToolSearchParameters.bAllowBottomHead and not ToolSearchParameters.bAllowTopHead then bIsToolCompatible = TOOLS[i].SetupInfo.bIsBottomHead end end -- TODO nei confronti tra valori gestire tolleranze if bIsToolCompatible then if ToolSearchParameters.dElevation > 10 * GEO.EPS_SMALL and ToolSearchParameters.bExtendWithCornerRadius then ToolSearchParameters.dElevation = ToolSearchParameters.dElevation + TOOLS[i].dCornerRadius end -- TODO gestire accorciamento massimo materiale per inclinazione local dCurrentResidualDepth = ToolSearchParameters.dElevation - 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 -- scelgo utensile con rapporto lunghezza / diametro minore if ( TOOLS[i].dLength / pow( TOOLS[i].dDiameter, 1.5)) < ( TOOLS[nBestToolIndex].dLength / pow( TOOLS[nBestToolIndex].dDiameter, 1.5)) then nBestToolIndex = i dBestToolResidualDepth = dCurrentResidualDepth 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 aggiungere una nuova lavorazione -- TODO da fare function MachiningLib.AddNewMachining( Machining) local nErr local sErr -- Controllo parametri obbligatori if not Machining.nType or not Machining.nToolIndex or not Machining.idProc or not Machining.idFaceToMachine then return false end -- Drilling if Machining.nType == MCH_MY.DRILLING then Machining.sTypeName = 'Drill_' -- Milling elseif Machining.nType == MCH_MY.MILLING then -- se utensile lama if TOOLS[Machining.nToolIndex].sFamily == 'SAWBLADE' then Machining.sTypeName = 'Cut_' else Machining.sTypeName = 'Mill_' end -- Pocketing elseif Machining.nType == MCH_MY.POCKETING then Machining.sTypeName = 'Pocket_' -- Mortising elseif Machining.nType == MCH_MY.MORTISING then Machining.sTypeName = 'ChSaw_' end -- se nome non definito, assegno alla lavorazioen un nome standard if not Machining.sOperationName then Machining.sOperationName = Machining.sTypeName .. ( EgtGetName( Machining.idProc) or tostring( Machining.idProc)) .. '_' .. tostring( Machining.idFaceToMachine) end if not Machining.sToolName then Machining.sToolName = TOOLS[Machining.nToolIndex].sName end -- creazione lavorazione local nOperationId = EgtCreateMachining( Machining.sOperationName, Machining.nType, Machining.sToolName) if nOperationId then -- impostazione geometria EgtSetMachiningGeometry( {{ Machining.idProc, Machining.idFaceToMachine}}) -- impostazione parametri lavorazione if Machining.dDepth then EgtSetMachiningParam( MCH_MP.DEPTH, Machining.dDepth) end if Machining.bInvert then EgtSetMachiningParam( MCH_MP.INVERT, Machining.bInvert) end if Machining.nWorkside then EgtSetMachiningParam( MCH_MP.WORKSIDE, Machining.nWorkside) end if Machining.nFaceuse then EgtSetMachiningParam( MCH_MP.FACEUSE, Machining.nFaceuse) end if Machining.nSCC then EgtSetMachiningParam( MCH_MP.SCC, Machining.nSCC) end if Machining.bToolInvert then EgtSetMachiningParam( MCH_MP.TOOLINVERT, Machining.bToolInvert) end if Machining.sBlockedAxis then EgtSetMachiningParam( MCH_MP.BLOCKEDAXIS, Machining.sBlockedAxis) end if Machining.sInitialAngles then EgtSetMachiningParam( MCH_MP.INITANGS, Machining.sInitialAngles) end if Machining.nHeadSide then EgtSetMachiningParam( MCH_MP.HEADSIDE, Machining.nHeadSide) end if Machining.nSubType then EgtSetMachiningParam( MCH_MP.SUBTYPE, Machining.nSubType) end if Machining.dOverlap then EgtSetMachiningParam( MCH_MP.OVERL, Machining.dOverlap) end -- step if Machining.Steps then if Machining.Steps.dStepType then EgtSetMachiningParam( MCH_MP.STEPTYPE, Machining.Steps.dStepType) end if Machining.Steps.dStep then EgtSetMachiningParam( MCH_MP.STEP, Machining.Steps.dStep) end if Machining.Steps.dSideStep then EgtSetMachiningParam( MCH_MP.SIDESTEP, Machining.Steps.dSideStep) end end if Machining.dStartPos then EgtSetMachiningParam( MCH_MP.STARTPOS, Machining.dStartPos) end if Machining.dReturnPos then EgtSetMachiningParam( MCH_MP.RETURNPOS, Machining.dReturnPos) end if Machining.dRadialOffset then EgtSetMachiningParam( MCH_MP.OFFSR, Machining.dRadialOffset) end if Machining.dLongitudinalOffset then EgtSetMachiningParam( MCH_MP.OFFSL, Machining.dLongitudinalOffset) end -- paraemtri attacco if Machining.LeadIn then if Machining.LeadIn.nType then EgtSetMachiningParam( MCH_MP.LEADINTYPE, Machining.LeadIn.nType) end if Machining.LeadIn.dStartAddLength then EgtSetMachiningParam( MCH_MP.STARTADDLEN, Machining.LeadIn.dStartAddLength) end if Machining.LeadIn.dTangentDistance then EgtSetMachiningParam( MCH_MP.LITANG, Machining.LeadIn.dTangentDistance) end if Machining.LeadIn.dPerpDistance then EgtSetMachiningParam( MCH_MP.LIPERP, Machining.LeadIn.dPerpDistance) end if Machining.LeadIn.dElevation then EgtSetMachiningParam( MCH_MP.LIELEV, Machining.LeadIn.dElevation) end if Machining.LeadIn.dCompLength then EgtSetMachiningParam( MCH_MP.LICOMPLEN, Machining.LeadIn.dCompLength) end end -- parametri uscita if Machining.LeadOut then if Machining.LeadOut.nType then EgtSetMachiningParam( MCH_MP.LEADOUTTYPE, Machining.LeadOut.nType) end if Machining.LeadOut.dEndAddLength then EgtSetMachiningParam( MCH_MP.ENDADDLEN, Machining.LeadOut.dEndAddLength) end if Machining.LeadOut.dTangentDistance then EgtSetMachiningParam( MCH_MP.LOTANG, Machining.LeadOut.dTangentDistance) end if Machining.LeadOut.dPerpDistance then EgtSetMachiningParam( MCH_MP.LOPERP, Machining.LeadOut.dPerpDistance) end if Machining.LeadOut.dElevation then EgtSetMachiningParam( MCH_MP.LOELEV, Machining.LeadOut.dElevation) end if Machining.LeadOut.dCompLength then EgtSetMachiningParam( MCH_MP.LOCOMPLEN, Machining.LeadOut.dCompLength) end end if Machining.dStartSlowLen then EgtSetMachiningParam( MCH_MP.STARTSLOWLEN, Machining.dStartSlowLen) end if Machining.dEndSlowLen then EgtSetMachiningParam( MCH_MP.ENDSLOWLEN, Machining.dEndSlowLen) end if Machining.dThrouAddLen then EgtSetMachiningParam( MCH_MP.THROUADDLEN, 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 Machining.nMachiningOrder then -- sSystemNotes = EgtSetValInNotes( sSystemNotes, 'MachiningOrder', Machining.nMachiningOrder) --end EgtSetMachiningParam( MCH_MP.SYSNOTES, sSystemNotes) -- parametri da settare nelle note utente local sUserNotes = EgtGetMachiningParam( MCH_MP.USERNOTES) if Machining.dMaxElev then sUserNotes = EgtSetValInNotes( sUserNotes, 'MaxElev', Machining.dMaxElev) end EgtSetMachiningParam( MCH_MP.USERNOTES, sUserNotes) local bIsApplyOk = MachiningLib.ApplyMachining( true, false) if not bIsApplyOk then nErr, sErr = EgtGetLastMachMgrError() EgtSetOperationMode( nOperationId, false) return false end else return false end return true, sErr end ------------------------------------------------------------------------------------------------------------- function MachiningLib.ApplyMachining( bRecalc, bApplyPost) local bResult = EgtApplyMachining( bRecalc, bApplyPost) return bResult end ------------------------------------------------------------------------------------------------------------- -- funzione che restituisce l'indice MRR (Material Removal Rate) della strategia. Dati in ingresso: Step, SideStep, Feed, ToolIndex, MachiningType function MachiningLib.GetToolMRR( Parameters) local dMRR = 1 -- se ho già tutti i parametri if not Parameters.dStep or not Parameters.dSideStep or not Parameters.dFeed then -- se manca qualche parametro, lo recupero da parametro di default dell'utensile if Parameters.nToolIndex then Parameters.dStep = Parameters.dStep or TOOLS[Parameters.nToolIndex].dStep Parameters.dSideStep = Parameters.dSideStep or TOOLS[Parameters.nToolIndex].dSideStep Parameters.dFeed = Parameters.dFeed or TOOLS[Parameters.nToolIndex].Feeds.dFeed -- se non riesco a calcolare, ritorno un indice molto basso else return GEO.EPS_SMALL end end dMRR = Parameters.dStep * Parameters.dSideStep * Parameters.dFeed -- Unità: dm/min per avere un indice vicino alle unità return dMRR / pow( 10, 6) end ------------------------------------------------------------------------------------------------------------- return MachiningLib