-- CalcSlices.lua by Egaltech s.r.l. 2022/07/06 -- 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 SlicingRibs( vtSlicing, dZmin) local nRibsShells = EgtGetInfo( s_nPartId, KEY_RIBS_SHELLS_NBR, 'i') or 0 if nRibsShells == 0 then return end -- recupero le superfici che definiscono le nervature local nRibsLay = EgtGetFirstNameInGroup( s_nPartId, LAY_RIBS) local vRibsIds = EgtGetAllInGroup( nRibsLay or GDB_ID.NULL) if not vRibsIds or #vRibsIds == 0 then return end -- sistemo la direzione normale dei piani for i = 1, #vRibsIds do if EgtGetType( vRibsIds[i]) == GDB_TY.SRF_MESH then local vtN = EgtSurfTmFacetNormVersor( vRibsIds[i], 0) local dVal = EgtIf( abs( vtN:getX()) > abs( vtN:getY()) - GEO.EPS_SMALL, vtN:getX(), vtN:getY()) if dVal < - GEO.EPS_SMALL then EgtInvertSurf( vRibsIds[i]) end end end local nLayId = EgtGetFirstNameInGroup( s_nPartId, SLICE_LAYER .. '*') while nLayId do -- recupero quota per slicing local dZ = EgtGetInfo( nLayId, KEY_SLICE_Z, 'd') local dDeltaZ = EgtGetInfo( nLayId, KEY_SLICE_DELTAZ, 'd') -- creo gruppo per le costolature local nGrp = EgtGroup( nLayId) EgtSetName( nGrp, RIBS_GRP) EgtSetStatus( nGrp, GDB_ST.OFF) for i = 1, #vRibsIds do if EgtGetType( vRibsIds[i]) == GDB_TY.SRF_MESH then -- slicing costolatura local nNewId, nPntCnt, nCrvCnt, nSrfCnt = EgtPlaneSurfTmInters( ORIG() + ( dZmin + dZ + dDeltaZ) * vtSlicing, vtSlicing, vRibsIds[i], nGrp, GDB_RT.GLOB, TOLER) if nNewId then -- rimuovo punti for nId = nNewId, nNewId + nPntCnt -1 do EgtErase( nId) end -- rinomino le curve e correggo di DeltaZ for nId = nNewId + nPntCnt, nNewId + nPntCnt + nCrvCnt - 1 do EgtSetName( nId, RIBS_CRV .. tostring( i)) EgtMove( nId, - dDeltaZ * vtSlicing) 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 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 = 227 --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_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) for i = 1, #vClosedId do EgtErase( vClosedId[i]) end if nSurfFR then -- 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) EgtSetColor( nId2, 'BLACK') end end else -- errore EgtSetName( nLayId, '__' .. SLICE_LAYER .. EgtNumToString( nLayCnt)) table.insert( vErr, nLayCnt) end -- passo al layer successivo nLayCnt = nLayCnt + 1 nLayId = EgtGetNext( nLayId) end -- costolature SlicingRibs( vtSlicing, dZmin) -- eventuale segnalazione errori if #vErr > 0 then EgtOutBox( 'Slicing Error on layers :\n' .. table.concat( vErr, ','), 'SlicingCalc') end end --------------------------------------------------------------------- return CalcSlices