Files
3dprinting/LuaLibs/CalcSlices.lua
T
DarioS 5b66831752 3dPrinting :
- modifiche e correzioni varie a CalcSlices
- modifiche a CalcSolids.
2022-04-26 08:21:46 +02:00

198 lines
7.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, 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
-- 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 = 740
---dZmax = 741
-- 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
local nRecalc = 0
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, TOLER)
-- se necessario, eseguo ricalcolo
local bRecalc = ( not nNewId or nStmCnt ~= 0)
if bRecalc then
nRecalc = nRecalc + 1
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, TOLER)
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 vClosedId = {}
local vOpenId = {}
for nId = nNewId, nNewId + nCrvCnt - 1 do
if EgtCurveIsClosed( nId) then
table.insert( vClosedId, nId)
else
table.insert( vOpenId, nId)
end
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
-- 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
for i = 1, #vClosedId do
EgtApproxCurve( vClosedId[i], GDB_CA.ARCS, SMALL_TOLER)
end
local nSurfFR = EgtSurfFlatRegion( nLayId, vClosedId)
for i = 1, #vClosedId do
EgtErase( vClosedId[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