-- CalcSlices.lua by Egaltech s.r.l. 2022/09/18 -- Calcolo percorsi di lavoro per Stampa 3d -- Tabella per definizione modulo local CalcSlices = {} -- Intestazioni require( 'EgtBase') EgtOutLog( ' CalcSlices started', 1) -- Dati local AMD = require( 'AddManData') --------------------------------------------------------------------- local s_nPartId --------------------------------------------------------------------- local function ComputeZSlices( dZmin, dDeltaZ, dZmax) local dSliceStep = EgtGetInfo( s_nPartId, KEY_SLICE_STEP, 'd') or 10000 local ptSlices = { dZmin + dDeltaZ} local dPosZ = dZmin + dSliceStep while dPosZ < dZmax do table.insert( ptSlices, dPosZ) dPosZ = dPosZ + dSliceStep end return ptSlices end -------------------------------------------------------------------- local function ComputeMaxH( nStmId, frSlicing, HMax) local b3BoxGlob = EgtGetBBoxGlob( nStmId, GDB_BB.STANDARD) if b3BoxGlob:getMax():getZ() < b3BoxGlob:getMin():getZ() + HMax + GEO.EPS_SMALL then return GEO.INFINITO end if AreSameVectorApprox( frSlicing:getVersZ(), Z_AX()) then return HMax else -- se slicing inclinato local nGrpTmp = EgtGroup( EgtGetParent( nStmId)) local nNewId, nPnt, nCrv, nSrf = EgtPlaneSurfTmInters( Point3d( 0, 0, HMax), Z_AX(), nStmId, nGrpTmp) if not nNewId then return GEO.INFINITO end local b3Box = EgtGetBBoxGlob( nGrpTmp, GDB_BB.STANDARD) EgtErase( nGrpTmp) local ptMin = b3Box:getMin() ptMin:toLoc( frSlicing) return ptMin:getZ() end return GEO.INFINITO end -------------------------------------------------------------------- local function AdjustAuxSolids( nSolidsLay) -- gestisce intersezioni fra solidi ausiliari unendoli local nCurr = EgtGetFirstInGroup( nSolidsLay or GDB_ID.NULL) if not nCurr then return end local nGrpTmp = EgtGroup( nSolidsLay) while nCurr do if EgtGetType( nCurr) == GDB_TY.SRF_MESH then -- verifico se interseca uno dei solidi successivi local bInters = true -- per entrare nel loop while bInters do bInters = false local nNext = EgtGetNext( nCurr) while nNext do if EgtGetType( nNext) == GDB_TY.SRF_MESH then -- verifico se le due superfici si intersecano local nFirst = EgtSurfTmSurfTmInters( nCurr, nNext, nGrpTmp) -- se intersezione unisco i due solidi e ripeto il check if nFirst then EgtSurfTmAdd( nCurr, nNext) EgtErase( nNext) bInters = true end end nNext = EgtGetNext( nNext) end end end nCurr = EgtGetNext( nCurr) end EgtErase( nGrpTmp) end -------------------------------------------------------------------- local function ReadParam( nId, sKey, sType, table) local info = EgtGetInfo( nId, sKey, sType) if info == nil then info = EgtGetInfo( s_nPartId, sKey, sType) end table[sKey] = info end -------------------------------------------------------------------- local function GetRibParams( nId) local RibParam = {} ReadParam( nId, KEY_RIBS_SHELLS_NBR, 'i', RibParam) ReadParam( nId, KEY_RIBS_INVERT_DIR, 'b', RibParam) ReadParam( nId, KEY_RIBS_LEAD_IN_INVERT, 'b', RibParam) ReadParam( nId, KEY_RIBS_LEAD_IN_LEN, 'd', RibParam) ReadParam( nId, KEY_RIBS_LEAD_OUT_INVERT, 'b', RibParam) ReadParam( nId, KEY_RIBS_LEAD_OUT_LEN, 'd', RibParam) ReadParam( nId, KEY_RIBS_LEAD_OUT_COASTING, 'd', RibParam) ReadParam( nId, KEY_RIBS_LEAD_OUT_WIPE, 'd', RibParam) ReadParam( nId, KEY_RIBS_LEAD_OUT_WIPE_DIR, 'd', RibParam) -- overlap può essere diverso tra le varie costolature solo se non sono collegate local bLink = EgtGetInfo( s_nPartId, KEY_RIBS_LINK, 'b') or false if bLink then RibParam[ KEY_RIBS_OVERLAP] = EgtGetInfo( s_nPartId, KEY_RIBS_OVERLAP, 'i') -- parametro generico else ReadParam( nId, KEY_RIBS_OVERLAP, 'i', RibParam) -- parametro specifico della costolatura end return RibParam end -------------------------------------------------------------------- local function GetAuxSolidsParams( nId) local AuxSolidsParam = {} ReadParam( nId, KEY_AUX_SOLIDS_OVERLAP, 'i', AuxSolidsParam) ReadParam( nId, KEY_AUX_SOLIDS_LINK_PARAM, 'd', AuxSolidsParam) ReadParam( nId, KEY_AUX_SOLIDS_SP_OFFSET, 'd', AuxSolidsParam) ReadParam( nId, KEY_AUX_SOLIDS_COASTING_LEN, 'd', AuxSolidsParam) ReadParam( nId, KEY_AUX_SOLIDS_WIPE_LEN, 'd', AuxSolidsParam) ReadParam( nId, KEY_AUX_SOLIDS_WIPE_DIR, 'd', AuxSolidsParam) local sInfill = EgtGetInfo( nId, KEY_AUX_SOLIDS_INFILL, 's') if sInfill then -- se parametro specifico per il solido if sInfill == 'ZigZag' then AuxSolidsParam[KEY_AUX_SOLIDS_INFILL] = INFILL_TYPE.ZIGZAG elseif sInfill == 'Offset' then AuxSolidsParam[KEY_AUX_SOLIDS_INFILL] = INFILL_TYPE.OFFSET else AuxSolidsParam[KEY_AUX_SOLIDS_INFILL] = INFILL_TYPE.NONE end else -- altrimenti recupero il parametro generale dal pezzo AuxSolidsParam[KEY_AUX_SOLIDS_INFILL] = EgtGetInfo( s_nPartId, KEY_AUX_SOLIDS_INFILL, 'i') end local sPrintOrder = EgtGetInfo( nId, KEY_AUX_SOLIDS_PRINT_ORDER, 's') if sPrintOrder then -- se parametro specifico per il solido if sPrintOrder == 'OutToIn' then AuxSolidsParam[KEY_AUX_SOLIDS_PRINT_ORDER] = PRINT_ORDER.EXT_INT_INF else AuxSolidsParam[KEY_AUX_SOLIDS_PRINT_ORDER] = PRINT_ORDER.INF_INT_EXT end else -- altrimenti recupero il parametro generale dal pezzo AuxSolidsParam[KEY_AUX_SOLIDS_PRINT_ORDER] = EgtGetInfo( s_nPartId, KEY_AUX_SOLIDS_PRINT_ORDER, 'i') end local sLinkType = EgtGetInfo( nId, KEY_AUX_SOLIDS_LINK_TYPE, 's') if sLinkType then -- se parametro specifico per il solido if sLinkType == 'None' then AuxSolidsParam[KEY_AUX_SOLIDS_LINK_TYPE] = LINK_TYPE.NONE elseif sLinkType == 'BiArc' then AuxSolidsParam[KEY_AUX_SOLIDS_LINK_TYPE] = LINK_TYPE.BIARC else AuxSolidsParam[KEY_AUX_SOLIDS_LINK_TYPE] = LINK_TYPE.LINEAR end else -- altrimenti recupero il parametro generale dal pezzo AuxSolidsParam[KEY_AUX_SOLIDS_LINK_TYPE] = EgtGetInfo( s_nPartId, KEY_AUX_SOLIDS_LINK_TYPE, 'i') end return AuxSolidsParam end -------------------------------------------------------------------- local function SlicingExtraObjects( vtSlicing, nLay, nType, sNameGrp, sName) -- recupero gli oggeti di cui fare slicing local vIds = EgtGetAllInGroup( nLay) if not vIds or #vIds == 0 then return end -- rimuovo gli indici che non corrispondono a trimesh local k = 1 while k <= #vIds do local nType = EgtGetType( vIds[k]) if nType ~= GDB_TY.SRF_MESH then table.remove( vIds, k) else k = k + 1 end end if #vIds == 0 then return end -- recupero i parametri di ogni oggetto local tabParams = {} local vToBeDone = {} for i = 1, #vIds do local vParams = {} local bToBeDone = true if nType == TYPE.RIB then vParams = GetRibParams( vIds[i]) if vParams[ KEY_RIBS_SHELLS_NBR] == 0 then bToBeDone = false end elseif nType == TYPE.EXTRA_SHELL then vParams[KEY_SHELL_NBR_DIFF] = EgtGetInfo( vIds[i], KEY_SHELL_NBR_DIFF, 'i') or EgtGetInfo( s_nPartId, KEY_SHELL_NBR_DIFF, 'i') if vParams[KEY_SHELL_NBR_DIFF] == 0 then bToBeDone = false end elseif nType == TYPE.AUX_SOLID then vParams = GetAuxSolidsParams( vIds[i]) if vParams[KEY_AUX_SOLIDS_INFILL] == INFILL_TYPE.NONE then bToBeDone = false end end table.insert( tabParams, vParams) table.insert( vToBeDone, bToBeDone) end local nLayId = EgtGetFirstNameInGroup( s_nPartId, SLICE_LAYER .. '*') while nLayId do -- recupero quota per slicing local dZ = EgtGetInfo( nLayId, KEY_SLICE_REAL_Z, 'd') local dDeltaZ = EgtGetInfo( nLayId, KEY_SLICE_DELTAZ, 'd') -- creo gruppo per le costolature local nGrp = EgtGroup( nLayId) EgtSetName( nGrp, sNameGrp) EgtSetStatus( nGrp, GDB_ST.OFF) for i = 1, #vIds do if vToBeDone[i] then -- slicing oggetto local nNewId, nPntCnt, nCrvCnt, nSrfCnt = EgtPlaneSurfTmInters( ORIG() + ( dZ + dDeltaZ) * vtSlicing, vtSlicing, vIds[i], nGrp, GDB_RT.GLOB, TOLER) -- verifico se necessario ricalcolo local dCorr = 0 local bOpen = false if nType ~= TYPE.RIB and nNewId then for nId = nNewId + nPntCnt, nNewId + nPntCnt + nCrvCnt - 1 do if not EgtCurveIsClosed( nId) then bOpen = true break end end end if nSrfCnt > 0 or bOpen then -- elimino vecchio risultato for j = nNewId, nNewId + nPntCnt + nCrvCnt + nSrfCnt - 1 do EgtErase( j) end dCorr = 0.01 nNewId, nPntCnt, nCrvCnt, nSrfCnt = EgtPlaneSurfTmInters( ORIG() + ( dZ + dDeltaZ + dCorr) * vtSlicing, vtSlicing, vIds[i], nGrp, GDB_RT.GLOB, TOLER) end if nNewId then -- rimuovo punti for nId = nNewId, nNewId + nPntCnt -1 do EgtErase( nId) end -- rinomino le curve, correggo di DeltaZ e assegno parametri for nId = nNewId + nPntCnt, nNewId + nPntCnt + nCrvCnt - 1 do EgtSetName( nId, sName .. tostring( i)) EgtMove( nId, - ( dDeltaZ + dCorr) * vtSlicing) for sKey, sVal in pairs( tabParams[i]) do EgtSetInfo( nId, sKey, sVal) end end -- rimuovo superfici for nId = nNewId + nPntCnt + nCrvCnt, nNewId + nPntCnt + nCrvCnt + nSrfCnt - 1 do EgtErase( nId) end end end end -- verifico che il gruppo non sia vuoto if not EgtGetFirstInGroup( nGrp) then EgtErase( nGrp) end nLayId = EgtGetNextName( nLayId, SLICE_LAYER .. '*') end return true end --------------------------------------------------------------------- local function ValueInArray( vArr, nValue) for _, val in ipairs( vArr) do if val == nValue then return true end end return false end --------------------------------------------------------------------- function CalcSlices.Exec( nPartId, nStmId, HMax) -- costanti local TOLER = 0.2 local SMALL_TOLER = 0.02 -- da portare a 0.5 local BIG_TOLER = 2.0 local MIN_LEN = 20.0 local MIN_AREA = 25.0 s_nPartId = nPartId -- recupero la direzione dello slicing local vtSlicing = Z_AX() local bSlicing45 = EgtGetInfo( s_nPartId, KEY_SLICING_45, 'b') if bSlicing45 then vtSlicing = VectorFromSpherical( 1, 45, 0) end EgtSetInfo( s_nPartId, KEY_SLICING_DIR, vtSlicing) local frSlicing = Frame3d( ORIG(), vtSlicing) -- recupero il box della superficie local b3Box = EgtGetBBoxRef( nStmId, GDB_BB.STANDARD, frSlicing) if not b3Box or b3Box:isEmpty() then EgtOutText( 'La superficie selezionata è vuota') EgtPause( 1000) return end -- Parametri di slicing local dZmin = b3Box:getMin():getZ() local dZmax = b3Box:getMax():getZ() local dMaxH = ComputeMaxH( nStmId, frSlicing, HMax) dZmax = min( dZmax, dMaxH) -- dZmin = 20 -- dZmax = 230 -- Eseguo slicing local nLayCnt = 1 local nRecalc = 0 local dDeltaZStart = 0.2 --dDeltaZStart = 0 local vZSlices = ComputeZSlices( dZmin, dDeltaZStart, dZmax) local nLayId = EgtParPlanesSurfTmInters( ORIG(), vtSlicing, vZSlices, nStmId, nPartId, GDB_RT.GLOB, TOLER) local vErr = {} local vPrevCen = {} -- scorro i risultati dello slicing while nLayId do local dDeltaZ = EgtIf( nLayCnt == 1, dDeltaZStart, 0) local dPosZ = vZSlices[nLayCnt]- dDeltaZ EgtSetInfo( nLayId, KEY_SLICE_Z, dPosZ - dZmin) EgtSetInfo( nLayId, KEY_SLICE_REAL_Z, dPosZ) EgtSetInfo( nLayId, KEY_SLICE_NBR, nLayCnt) -- verifico se necessario ricalcolo local nId = EgtGetLastInGroup( nLayId) local bRecalc = not nId local vtRecalc while nId and not bRecalc do local nType = EgtGetType( nId) if nType == GDB_TY.SRF_MESH then bRecalc = true vtRecalc = EgtSurfTmFacetNormVersor( nId, 0) elseif nType == GDB_TY.GEO_POINT then bRecalc = true end nId = EgtGetPrev( nId) end -- eseguo eventuale ricalcolo ::RECALC:: if bRecalc then nRecalc = nRecalc + 1 EgtEmptyGroup( nLayId) dDeltaZ = dDeltaZ + EgtIf( vtRecalc and vtRecalc:getZ() > 0, -0.01, 0.01) -- eseguo il ricalcolo solo a quella quota EgtPlaneSurfTmInters( ORIG() + ( dPosZ + dDeltaZ) * vtSlicing, vtSlicing, nStmId, nLayId, GDB_RT.GLOB, TOLER) EgtOutLog( 'Warning : Recalc at Lay'.. EgtNumToString( nLayCnt)) end EgtSetInfo( nLayId, KEY_SLICE_DELTAZ, dDeltaZ) -- salvo i risultati nel layer local vClosedId = {} local vOpenId = {} nId = EgtGetFirstInGroup( nLayId) while nId do if EgtGetType( nId) == GDB_TY.GEO_POINT or EgtGetType( nId) == GDB_TY.SRF_MESH then -- se punto o superficie lo elimino EgtErase( nId) else -- correggo eventuali spostamenti in Z if abs( dDeltaZ) > GEO.EPS_SMALL then EgtMove( nId, - dDeltaZ * vtSlicing) end -- verifico presenza contorni aperti if EgtCurveIsClosed( nId) then table.insert( vClosedId, nId) else table.insert( vOpenId, nId) end end nId = EgtGetNext( nId) end -- se presenti percorsi aperti, provo a concatenarli if #vOpenId > 0 then local nRordId, nRordCnt = EgtCurveCompoByReorder( nLayId, vOpenId, ORIG(), true, GDB_RT.LOC, BIG_TOLER) if nRordId then vOpenId = {} for nId = nRordId, nRordId + nRordCnt - 1 do if EgtCurveIsClosed( nId) then table.insert( vClosedId, nId) else table.insert( vOpenId, nId) end end end end -- chiudo percorsi aperti praticamente chiusi ed elimino quelli troppo corti for i = #vOpenId, 1, -1 do local ptStart = EgtSP( vOpenId[i]) local ptEnd = EgtEP( vOpenId[i]) if EgtCurveLength( vOpenId[i]) < MIN_LEN then EgtErase( vOpenId[i]) table.remove( vOpenId, i) elseif AreSamePointEpsilon( ptStart, ptEnd, BIG_TOLER) then local ptMid = ( ptStart + ptEnd) / 2 EgtModifyCurveStartPoint( vOpenId[i], ptMid) EgtModifyCurveEndPoint( vOpenId[i], ptMid) table.insert( vClosedId, vOpenId[i]) table.remove( vOpenId, i) end end local bAllClosed = ( #vOpenId == 0) -- elimino percorsi chiusi di area troppo piccola for i = #vClosedId, 1, -1 do local _, _, dArea = EgtCurveArea( vClosedId[i]) if dArea and dArea < MIN_AREA then EgtErase( vClosedId[i]) table.remove( vClosedId, i) end end -- se necessario e possibile, torno a ricalcolo if not bAllClosed and nRecalc < 2 then bRecalc = true goto RECALC end -- imposto dati ausiliari EgtSetName( nLayId, EgtIf( bAllClosed, '', '__') .. SLICE_LAYER .. EgtNumToString( nLayCnt)) EgtSetInfo( nLayId, 'CrvCnt', #vClosedId + #vOpenId) EgtSetColor( vClosedId or {}, 'TEAL') EgtSetColor( vOpenId or {}, 'RED') -- creo flat region a partire dalle curve chiuse ottenute con lo slicing local nGrp = EgtGroup( nLayId, frSlicing) for i = 1, #vClosedId do EgtRelocateGlob( vClosedId[i], nGrp) EgtApproxCurve( vClosedId[i], GDB_CA.ARCS, SMALL_TOLER) EgtRelocateGlob( vClosedId[i], nLayId) end EgtErase( nGrp) local nSurfFR, nSrfNbr = EgtSurfFlatRegion( nLayId, vClosedId) if nSurfFR then -- elimino le curve originali for i = 1, #vClosedId do EgtErase( vClosedId[i]) end -- esplodo in superfici elementari local vSrfId = {} for j = 1, nSrfNbr do local nCurrSfrId = nSurfFR + j - 1 local nSfrId, nSfrCnt = EgtExplodeSurf( nCurrSfrId) for nId = nSfrId, nSfrId + nSfrCnt - 1 do table.insert( vSrfId, nId) end end -- calcolo i centri delle superfici local vCen = {} for j = 1, #vSrfId do local nId = vSrfId[j] local ptCen = EgtCP( nId, GDB_ID.ROOT) table.insert( vCen, ptCen) end -- ordino le superfici rispetto ai centri del layer precedente local vOrd = {} if #vPrevCen > 1 then for j = 1, #vPrevCen do local nMin = 0 local dMinDist = GEO.INFINITO for k = 1, #vCen do if not ValueInArray( vOrd, k) then local dDist = dist( vPrevCen[j], vCen[k]) if dDist < dMinDist then dMinDist = dDist nMin = k end end end if nMin ~= 0 then vOrd[j] = nMin end end -- se necessario, completo il vettore di ordine if #vOrd < #vSrfId then for j = 1, #vSrfId do if not ValueInArray( vOrd, j) then table.insert( vOrd, j) end end end else EgtSpInit() for j = 1, #vSrfId do EgtSpAddPoint( vCen[j]:getX(), vCen[j]:getY()) end vOrd = EgtSpCalculate( SHP_TY.OPEN) EgtSpTerminate() if not vOrd then for j = 1, #vSrfId do vOrd[j] = j end end end -- salvo i nuovi centri for j = 1, #vSrfId do vPrevCen[j] = vCen[vOrd[j]] end -- creo i gruppi di percorsi for j = 1, #vSrfId do local nId = vSrfId[vOrd[j]] -- per ogni chunk creo un gruppo di percorsi local nGrpCrv = EgtGroup( nLayId) EgtSetName( nGrpCrv, CONTOUR_GRP .. EgtNumToString( j, 1)) EgtRelocateGlob( nId, nGrpCrv) EgtSetName( nId, LAYER_SRF) EgtSetStatus( nId, GDB_ST.OFF) -- estraggo i contorni della superficie local nCsId, nCsCnt = EgtExtractSurfFrChunkLoops( nId, 0, nGrpCrv) for nId2 = nCsId, nCsId + nCsCnt - 1 do EgtSetName( nId2, OUTER_CRV) EgtSetStatus( nId2, GDB_ST.OFF) EgtSetColor( nId2, 'BLACK') end end else local bErr = true -- se nessuna curva if #vClosedId == 0 then bErr = false -- se una sola curva, ne verifico larghezza media elseif #vClosedId == 1 then local dLen = EgtCurveLength( vClosedId[1]) local _, _, dArea = EgtCurveArea( vClosedId[1]) if not dArea or dArea / dLen < SMALL_TOLER then bErr = false end end -- cambio nome al layer EgtSetName( nLayId, '__' .. SLICE_LAYER .. EgtNumToString( nLayCnt)) -- se vero errore lo segnalo if bErr then table.insert( vErr, nLayCnt) end end -- passo al layer successivo if EgtProcessEvents( nLayCnt / ( #vZSlices + 3) * 100, 0) == 1 then EgtDraw() return end nLayCnt = nLayCnt + 1 nLayId = EgtGetNext( nLayId) end -- costolature local nRibsLay = EgtGetFirstNameInGroup( s_nPartId, LAY_RIBS) if nRibsLay then SlicingExtraObjects( vtSlicing, nRibsLay, TYPE.RIB, RIBS_GRP, RIBS_CRV) end if EgtProcessEvents( ( #vZSlices + 1) / ( #vZSlices + 3) * 100, 0) == 1 then return end -- solidi per regioni con diverso numero di passate local nShellNbrLay = EgtGetFirstNameInGroup( s_nPartId, LAY_SHELL_NBR) if nShellNbrLay then SlicingExtraObjects( vtSlicing, nShellNbrLay, TYPE.EXTRA_SHELL, SHELL_NBR_GRP, SHELL_NBR_CRV) end if EgtProcessEvents( ( #vZSlices + 2) / ( #vZSlices + 3) * 100, 0) == 1 then return end -- solidi ausiliari local nAuxSolidsLay = EgtGetFirstNameInGroup( s_nPartId, LAY_AUX_SOLIDS) if nAuxSolidsLay then AdjustAuxSolids( nAuxSolidsLay) SlicingExtraObjects( vtSlicing, nAuxSolidsLay, TYPE.AUX_SOLID, AUX_SOLIDS_GRP, AUX_SOLIDS_CRV) end if EgtProcessEvents( ( #vZSlices + 3) / ( #vZSlices + 3) * 100, 0) == 1 then return end -- eventuale segnalazione errori if #vErr > 0 then EgtOutBox( 'Slicing Error on layers :\n' .. table.concat( vErr, ','), 'SlicingCalc') end end --------------------------------------------------------------------- return CalcSlices