Files
3dprinting/LuaLibs/CalcSlices.lua
T
2022-04-12 10:27:33 +02:00

142 lines
5.1 KiB
Lua

-- CalcSlices.lua by Egaltech s.r.l. 2022/03/30
-- 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 function GetSliceStep()
local nParamsGrp = EgtGetFirstNameInGroup( GDB_ID.ROOT, PARAMS_GRP)
local dStep = EgtGetInfo( nParamsGrp, KEY_SLICE_STEP, 'd')
return dStep
end
---------------------------------------------------------------------
function CalcSlices.Exec( nPartId, nStmId)
local dTol = 0.05
-- recuper il box della superficie
local b3Box = EgtGetBBoxRef( nStmId, GDB_BB.STANDARD, EgtGetGridFrame())
if not b3Box or b3Box:isEmpty() then
EgtOutText( 'La superficie selezionata è vuota')
EgtPause( 1000)
return
end
-- Parametri di slicing
local dZmin = b3Box:getMin():getZ() + 0.1 --0.1 * dStep
local dZmax = b3Box:getMax():getZ() - 0.1 -- 0.1 * dStep
-- se slicing già presente lo cancello
local nOldSliceId = EgtGetFirstNameInGroup( nPartId, SLICE_LAYER .."*") or GDB_ID.NULL
while nOldSliceId ~= GDB_ID.NULL do
EgtErase( nOldSliceId)
nOldSliceId = EgtGetFirstNameInGroup( nPartId, SLICE_LAYER .."*") or GDB_ID.NULL
end
EgtStartCounter()
-- Eseguo slicing
local dPosZ = dZmin
local nLayCnt = 1
while dPosZ < dZmax do
-- nuovo layer
local nLayId = EgtGroup( nPartId)
EgtSetInfo( nLayId, KEY_SLICE_Z, dPosZ)
EgtSetInfo( nLayId, KEY_SLICE_NBR, nLayCnt)
-- calcolo intersezione
nNewId, nPntCnt, nCrvCnt, nSrfCnt = EgtPlaneSurfTmInters( Point3d( 0, 0, dPosZ), Z_AX(), nStmId, nLayId, GDB_RT.GRID)
-- verifico se ricalcolare con piccolo spostamento
local bRecalc = not nNewId or ( nSrfCnt ~= 0)
if not bRecalc then
for Id = nNewId + nPntCnt, nNewId + nPntCnt + nCrvCnt - 1 do
if not EgtCurveIsClosed( Id) then
bRecalc = true
break
end
end
end
-- se richiesto, eseguo ricalcolo
if bRecalc then
local vtN = EgtSurfTmFacetNormVersor( nNewId or 0 + nPntCnt or 0 + nCrvCnt or 0, 0)
local dDeltaZ = EgtIf( vtN and vtN:getZ() < 0, 0.005, - 0.005)
EgtEmptyGroup( nLayId)
nNewId, nPntCnt, nCrvCnt, nSrfCnt = EgtPlaneSurfTmInters( Point3d( 0, 0, dPosZ + dDeltaZ), Z_AX(), nStmId, nLayId, GDB_RT.GRID)
end
-- salvo i risultati nel layer
if nNewId then
-- verifico presenza contorni aperti
local bAllClosed = true
for Id = nNewId + nPntCnt, nNewId + nPntCnt + nCrvCnt - 1 do
if not EgtCurveIsClosed( Id) then
bAllClosed = false
break
end
end
-- salvo
EgtSetName( nLayId, EgtIf( bAllClosed, '', '**') .. SLICE_LAYER .. EgtNumToString( nLayCnt))
EgtSetInfo( nLayId, 'PntCnt', nPntCnt)
EgtSetInfo( nLayId, 'CrvCnt', nCrvCnt)
EgtSetInfo( nLayId, 'SrfCnt', nSrfCnt)
EgtSetColor( EgtTableFill( nNewId, nPntCnt) or {}, 'BLUE')
EgtSetColor( EgtTableFill( nNewId + nPntCnt, nCrvCnt) or {}, 'BLACK')
EgtSetColor( EgtTableFill( nNewId + nPntCnt + nCrvCnt, nSrfCnt) or {}, 'AQUA')
-- creo flat region a partire dalle curve ottenute con lo slicing
local vCrvs = {}
for nInd = 0, nCrvCnt - 1 do
table.insert( vCrvs, nNewId + nPntCnt + nInd)
EgtApproxCurve( nNewId + nPntCnt + nInd, GDB_CA.ARCS, dTol)
end
local nSurfFR = EgtSurfFlatRegion( nLayId, vCrvs)
for nInd = 1, #vCrvs do
EgtErase( vCrvs[nInd])
end
if nSurfFR then
-- analizzo i singoli chunks
local nSfrId, nSrfCnt = EgtExplodeSurf( nSurfFR)
for nInd = 0, nSrfCnt - 1 do
-- per ogni chunk creo un gruppo di percorsi
local nGrpCrv = EgtGroup( nLayId)
EgtSetName( nGrpCrv, CONTOUR_GRP .. EgtNumToString( nInd + 1, 1))
EgtRelocateGlob( nSfrId + nInd, nGrpCrv)
EgtSetName( nSfrId + nInd, LAYER_SRF)
EgtSetStatus( nSfrId + nInd, GDB_ST.OFF)
-- estraggo i contorni della superficie
local nCrvId, nCrvCnt = EgtExtractSurfFrChunkLoops( nSfrId + nInd, 0, nGrpCrv)
for nInd2 = 0, nCrvCnt - 1 do
EgtSetName( nCrvId + nInd2, OUTER_CRV)
EgtSetColor( nCrvId + nInd2, 'BLACK')
end
end
else
-- errore
EgtSetName( nLayId, EgtIf( bAllClosed, '', '**') .. SLICE_LAYER .. EgtNumToString( nLayCnt))
end
-- errore
else
EgtSetName( nLayId, EgtIf( bAllClosed, '', '**') .. SLICE_LAYER .. EgtNumToString( nLayCnt))
end
local dStep = GetSliceStep( dPosZ, dStep)
dPosZ = dPosZ + dStep
nLayCnt = nLayCnt + 1
end
end
---------------------------------------------------------------------
return CalcSlices