Files
3dprinting/LuaLibs/CalcSlices.lua
T
Emmanuele Sassi 261aec363d - Correzioni e migliorie su pezzo di esempio
- Gestione avvio script solo quando eseguiti precedenti
- Correzioni varie
2022-04-13 15:35:05 +02:00

172 lines
6.0 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.1
-- 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()
local dZmax = b3Box:getMax():getZ()
--dZmin = 1030
--dZmax = 1031
-- 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
-- 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
local dDeltaZ = EgtIf( nLayCnt == 1, 0.2, 0)
--dDeltaZ = 0
nNewId, nPntCnt, nCrvCnt, nSrfCnt = EgtPlaneSurfTmInters( Point3d( 0, 0, dPosZ + dDeltaZ), Z_AX(), nStmId, nLayId, GDB_RT.GLOB)
-- 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)
dDeltaZ = dDeltaZ + EgtIf( vtN and vtN:getZ() < 0, 0.01, - 0.01)
EgtEmptyGroup( nLayId)
nNewId, nPntCnt, nCrvCnt, nSrfCnt = EgtPlaneSurfTmInters( Point3d( 0, 0, dPosZ + dDeltaZ), Z_AX(), nStmId, nLayId, GDB_RT.GLOB)
end
-- salvo i risultati nel layer
if nNewId then
-- elimino eventuali punti e superfici
for i = 1, nPntCnt do
EgtErase( nNewId + i - 1)
end
nNewId = nNewId + nPntCnt
nPntCnt = 0
for i = 1, nSrfCnt do
EgtErase( nNewId + nPntCnt + nCrvCnt + i - 1)
end
nSrfCnt = 0
-- correggo eventuali spostamenti in Z
if abs( dDeltaZ) > GEO.EPS_SMALL then
for nId = nNewId, nNewId + nPntCnt + nCrvCnt + nSrfCnt - 1 do
EgtMove( nId, Vector3d( 0, 0, -dDeltaZ))
end
end
-- verifico presenza contorni aperti
local vOpenId = {}
for Id = nNewId + nPntCnt, nNewId + nPntCnt + nCrvCnt - 1 do
if not EgtCurveIsClosed( Id) then
table.insert( vOpenId, Id)
end
end
-- provo a concatenare i contorni aperti
if #vOpenId > 1 then
local nFirstId, nCnt = EgtCurveCompoByReorder( nLayId, vOpenId, Point3d(0,0,0), true, GDB_RT.LOC, dTol)
if nFirstId and nCnt == 1 then
nCrvCnt = nCrvCnt - #vOpenId + 1
vOpenId = {}
end
end
-- imposto dati ausiliari
EgtSetName( nLayId, EgtIf( #vOpenId == 0, '', '**') .. 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