Files
3dprinting/LuaLibs/CalcSlices.lua
T
DarioS aa7e4f87c4 3dPrinting :
- correzioni varie.
2022-04-19 17:21:45 +02:00

187 lines
6.3 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, HMax)
-- costanti
local dToler = 1.0
local dMinLen = 5.0
local dMinArea = 100.0
-- recupero 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()
dZmax = min( dZmax, dZmin + HMax)
--dZmin = 460
--dZmax = 465
-- se slicing già presente lo cancello
for i = 1, 2 do
local sFind = EgtIf( i == 1, '', '__') .. SLICE_LAYER .. "*"
local nOldSliceId = EgtGetFirstNameInGroup( nPartId, sFind)
while nOldSliceId do
EgtErase( nOldSliceId)
nOldSliceId = EgtGetFirstNameInGroup( nPartId, sFind)
end
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
local nNewId, nPntCnt, nCrvCnt, nStmCnt = EgtPlaneSurfTmInters( Point3d( 0, 0, dPosZ + dDeltaZ), Z_AX(), nStmId, nLayId, GDB_RT.GLOB, dToler)
-- verifico se ricalcolare con piccolo spostamento
local bRecalc = ( not nNewId or nStmCnt ~= 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, nStmCnt = EgtPlaneSurfTmInters( Point3d( 0, 0, dPosZ + dDeltaZ), Z_AX(), nStmId, nLayId, GDB_RT.GLOB, dToler)
end
-- salvo i risultati nel layer
if nNewId then
-- elimino eventuali punti e superfici
for nId = nNewId, nNewId + nPntCnt - 1 do
EgtErase( nId)
end
nNewId = nNewId + nPntCnt
nPntCnt = nil
for nId = nNewId + nCrvCnt, nNewId + nCrvCnt + nStmCnt - 1 do
EgtErase( nId)
end
nStmCnt = nil
-- correggo eventuali spostamenti in Z
if abs( dDeltaZ) > GEO.EPS_SMALL then
for nId = nNewId, nNewId + nCrvCnt - 1 do
EgtMove( nId, Vector3d( 0, 0, -dDeltaZ))
end
end
-- verifico presenza contorni aperti
local vOpenId = {}
for nId = nNewId, nNewId + nCrvCnt - 1 do
if not EgtCurveIsClosed( nId) then
table.insert( vOpenId, nId)
end
end
-- chiudo percorsi aperti praticamente chiusi
for i = #vOpenId, 1, -1 do
local ptStart = EgtSP( vOpenId[i])
local ptEnd = EgtEP( vOpenId[i])
if AreSamePointEpsilon( ptStart, ptEnd, dToler) and EgtCurveLength( vOpenId[i]) > dMinLen then
local ptMid = ( ptStart + ptEnd) / 2
EgtModifyCurveStartPoint( vOpenId[i], ptMid)
EgtModifyCurveEndPoint( vOpenId[i], ptMid)
table.remove( vOpenId, i)
end
end
local bAllClosed = ( #vOpenId == 0)
-- elimino percorsi chiusi di area troppo piccola
for nId = nNewId, nNewId + nCrvCnt - 1 do
local _, _, dArea = EgtCurveArea( nId)
if dArea and dArea < dMinArea then
EgtErase( nId)
end
end
-- imposto dati ausiliari
EgtSetName( nLayId, EgtIf( bAllClosed, '', '__') .. SLICE_LAYER .. EgtNumToString( nLayCnt))
EgtSetInfo( nLayId, 'CrvCnt', nCrvCnt)
EgtSetColor( EgtTableFill( nNewId, nCrvCnt) or {}, 'BLACK')
-- creo flat region a partire dalle curve ottenute con lo slicing
local vCrvs = {}
for nId = nNewId, nNewId + nCrvCnt - 1 do
table.insert( vCrvs, nId)
EgtApproxCurve( nId, GDB_CA.ARCS, dToler / 10)
end
local nSurfFR = EgtSurfFlatRegion( nLayId, vCrvs)
for i = 1, #vCrvs do
EgtErase( vCrvs[i])
end
if nSurfFR then
-- analizzo i singoli chunks
local nSfrId, nSfrCnt = EgtExplodeSurf( nSurfFR)
for nId = nSfrId, nSfrId + nSfrCnt - 1 do
-- per ogni chunk creo un gruppo di percorsi
local nGrpCrv = EgtGroup( nLayId)
EgtSetName( nGrpCrv, CONTOUR_GRP .. EgtNumToString( nId, 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, 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