-- FaceData.lua by Egalware s.r.l. 2024/04/18 -- Libreria lettura o calcolo dati e proprietà delle facce di una trimesh -- 2024/04/02 PRIMA VERSIONE CALCOLO LAVORAZIONI CON STRATEGIE -- Tabella per definizione modulo local FaceData = {} -- carico librerie local BeamLib = require( 'BeamLib') local Logs = require( 'Logs') --------------------------------------------------------------------- -- restituisce la matrice delle adiacenze di Proc dove i e j sono le facce e a(ij) è l'angolo tra di esse; 0 se nessuna adiacenza function FaceData.GetAdjacencyMatrix( Proc) local vAdj = {} -- essendo la matrice simmetrica a diagonale nulla, ne calcolo solo la metà superiore for i = 1, Proc.nFct do vAdj[i] = {} for j = i + 1, Proc.nFct do _, _, _, vAdj[i][j] = EgtSurfTmFacetsContact( Proc.id, i - 1, j - 1, GDB_ID.ROOT) if not vAdj[i][j] then vAdj[i][j] = 0 end end end -- riempio di conseguenza il resto della matrice for i = 1, Proc.nFct do vAdj[i][i] = 0 for j = i + 1, Proc.nFct do vAdj[j][i] = vAdj[i][j] end end return vAdj end --------------------------------------------------------------------- -- restituisce un vettore con gli indici (0 based) delle facce triangolari (o quasi) di Proc function FaceData.GetTriangularFaces( Proc) -- se la feature ha una sola faccia, esco subito if Proc.nFct <= 1 then return end local vTriangularFaces = {} for i = 1, Proc.nFct do if BeamLib.Is3EdgesApprox( Proc, i - 1) then table.insert( vTriangularFaces, i - 1) end end return vTriangularFaces end ------------------------------------------------------------------------------------------------------------- local function GetNotAdjacentFaces( Proc, idFace) local NotAdjacentFaces = {} for i = 1, Proc.nFct do if Proc.Faces[i].id ~= idFace then local bIsAdjacent = false for j = 1, #Proc.Faces[idFace + 1].Adjacencies do if Proc.Faces[i].id == Proc.Faces[idFace + 1].Adjacencies[j] then bIsAdjacent = true end end if not bIsAdjacent then table.insert( NotAdjacentFaces, Proc.Faces[i]) end end end return NotAdjacentFaces end --------------------------------------------------------------------- -- restituisce una tabella che correla numero di adiacenze e id delle facce con quello stesso numero di adiacenze function FaceData.GetFacesByAdjacencyNumber( Proc) -- se la feature ha una sola faccia, esco subito if Proc.nFct <= 1 then return {} end local FacesByAdjacencyNumber = {} for i = 1, 10 do FacesByAdjacencyNumber[i] = {} end for i = 1, Proc.nFct do table.insert( FacesByAdjacencyNumber[#Proc.Faces[i].Adjacencies], Proc.Faces[i]) end return FacesByAdjacencyNumber end ------------------------------------------------------------------------------------------------------------- function FaceData.GetFacesInfo( Proc, Part) EgtOutLog( '---Faces START---') local Faces = {} local vAdj if Proc.AdjacencyMatrix then vAdj = Proc.AdjacencyMatrix else vAdj = FaceData.GetAdjacencyMatrix( Proc) end -- reset eventuali visualizzazioni facce a due colori EgtSurfTmResetTwoColors( Proc.id) for i = 1, Proc.nFct do Faces[i] = {} Faces[i].id = i - 1 Faces[i].ptCenter, Faces[i].vtN = EgtSurfTmFacetCenter( Proc.id, i - 1, GDB_ID.ROOT) if Proc.nFct < 6 then -- frame OCS faccia Faces[i].vtFrameHV = Frame3d( Faces[i].ptCenter, Faces[i].vtN) -- elevazione calcolata rispetto al box della parte Faces[i].dElevation = EgtSurfTmFacetElevationInBBox( Proc.id, i - 1, Part.b3Solid, true, GDB_ID.ROOT) -- TODO qui sarebbe meglio l'area vera e non quella del rettangolo minimo local _, dLongEdgeDimension, dShortEdgeDimension = EgtSurfTmFacetMinAreaRectangle( Proc.id, i - 1, GDB_ID.ROOT) Faces[i].dArea = dShortEdgeDimension * dLongEdgeDimension local nFaceType, vEdges = EgtSurfTmGetFacetOutlineInfo( Proc.id, i - 1, GDB_ID.ROOT) Faces[i].bIsOkForMachining = nFaceType < 1 Faces[i].Edges = vEdges -- TODO valutare se fare un output unico alla fine o gestire log in altro modo EgtOutLog( 'Facet ' .. Faces[i].id .. ' of ' .. Proc.nFct - 1) EgtOutLog( ' vtN: ' .. tostring( Faces[i].vtN)) -- adiacenze della faccia -- TODO chiamarle in modo che si capisca che sono solo gli id e non l'intero oggetto faccia Faces[i].Adjacencies = {} for j = 1, Proc.nFct do if vAdj[i][j] and vAdj[i][j] ~= 0 and ( i ~= j) then table.insert( Faces[i].Adjacencies, j - 1) if EgtGetDebugLevel() >= 3 then EgtOutLog( ' Adjacent to facet: ' .. j - 1) end end end end end EgtOutLog( '---Faces END---') return Faces end ------------------------------------------------------------------------------------------------------------- -- TODO valutare refactoring per mettere i calcoli del tunnel in una funzione -- TODO creare le facce tunnel solo se non ci sono già. Verirficare all'inizio se già presente in AddGrpId e nel caso riferire all'id di quella esistente e ricalcolare solo le informazioni della faccia. local function GetTunnelFaces( Proc, Part) local TunnelAddedFaces = {} if not ( Proc.Topology.bIsThrough and Proc.Topology.bAllRightAngles and Proc.nFct < 5) then error( 'GetTunnelFaces : Topology not implemented') end -- direzione del tunnel local vtTunnelDirection = Proc.Faces[1].vtN ^ Proc.Faces[ Proc.Faces[1].Adjacencies[1] + 1].vtN -- centro del tunnel local frTunnel = Frame3d( Proc.Faces[1].ptCenter, vtTunnelDirection) local b3Tunnel = EgtGetBBoxRef( Proc.id, GDB_BB.STANDARD, frTunnel) local ptTunnelCenter = b3Tunnel:getCenter() ptTunnelCenter:toGlob( frTunnel) -- recupero gruppo per geometria addizionale local nAddGrpId = BeamLib.GetAddGroup( Proc.idPart) if not nAddGrpId then -- TODO gestire meglio questo errore. Non conviene creare e verificare all'inizio se il gruppo esiste? EgtOutLog( 'Error : missing AddGroup') return TunnelAddedFaces end -- faccia centrale local nMiddleTmId = EgtSurfTmPlaneInBBox( nAddGrpId, ptTunnelCenter, vtTunnelDirection, Part.b3Solid, GDB_ID.ROOT) -- TODO se non si riesce a costruire la faccia bisogna dare errore o semplicemente non ritornarla?? for i = 1, Proc.nFct do EgtCutSurfTmPlane( nMiddleTmId, Proc.Faces[i].ptCenter, -Proc.Faces[i].vtN, false, GDB_ID.ROOT) end -- facce laterali local nLateralTmId = EgtCopyGlob( Proc.id, nAddGrpId) or GDB_ID.NULL EgtCutSurfTmPlane( nLateralTmId, ptTunnelCenter, -vtTunnelDirection, false, GDB_ID.ROOT) -- unione facce -- TODO cambiare nome alla trimesh?? non contiene più solamente la faccia di mezzo TunnelAddedFaces.MiddleFaceTm = {} TunnelAddedFaces.MiddleFaceTm.id = EgtSurfTmBySewing( nAddGrpId, { nMiddleTmId, nLateralTmId}, true) -- TODO c'è un modo più elegante per raccogliere le informazioni delle facce aggiunte TunnelAddedFaces.MiddleFaceTm.sType = 'Tunnel' TunnelAddedFaces.MiddleFaceTm.nFct = Proc.nFct + 1 TunnelAddedFaces.MiddleFaceTm.Faces = FaceData.GetFacesInfo( TunnelAddedFaces.MiddleFaceTm, Part) return TunnelAddedFaces end ------------------------------------------------------------------------------------------------------------- local function GetBottomFaces( Proc) local BottomFaces = {} if Proc.Topology.sFamily == 'Tunnel' then return nil elseif not ( Proc.Topology.sFamily == 'Rabbet' or Proc.Topology.sFamily == 'VGroove' or Proc.Topology.sFamily == 'Groove' or Proc.Topology.sFamily == 'Pocket') then error( 'GetBottomFace : Topology not implemented') end -- la faccia di fondo ha sempre Fct - 1 adiacenze local FacesByAdjacencyNumber = FaceData.GetFacesByAdjacencyNumber( Proc) if FacesByAdjacencyNumber then BottomFaces = FacesByAdjacencyNumber[ Proc.nFct - 1] -- si rimuovono le facce non adatte ad essere lavorate local nBottomFaces = #BottomFaces local nCurrentFace = 1 while nCurrentFace <= nBottomFaces do if not BottomFaces[nCurrentFace].bIsOkForMachining then table.remove( BottomFaces, nCurrentFace) nBottomFaces = nBottomFaces - 1 end nCurrentFace = nCurrentFace + 1 end -- la BottomFace 1 è sempre quella con minor elevazione table.sort( BottomFaces, function (a, b) return a.dElevation < b.dElevation end) end if #BottomFaces == 0 then return nil end BottomFaces[1].sType = 'Bottom' BottomFaces[1].MainEdges = {} BottomFaces[1].MainEdges.LongEdges = {} BottomFaces[1].MainEdges.SideEdges = {} local ClosedEdgesSortedByGreatestLength = {} for i = 1, #BottomFaces[1].Edges do if not BottomFaces[1].Edges[i].Open then table.insert( ClosedEdgesSortedByGreatestLength, {}) ClosedEdgesSortedByGreatestLength[#ClosedEdgesSortedByGreatestLength].nIndex = i ClosedEdgesSortedByGreatestLength[#ClosedEdgesSortedByGreatestLength].dLength = BottomFaces[1].Edges[i].Len end end table.sort( ClosedEdgesSortedByGreatestLength, function (a, b) return a.dLength > b.dLength end) local nFirstLongEdgeIndex = ClosedEdgesSortedByGreatestLength[1].nIndex for i = 1, #BottomFaces[1].Edges do local nPreviousEdgeIndex = i - 1 if i == 1 then nPreviousEdgeIndex = #BottomFaces[1].Edges end local nNextEdgeIndex = i + 1 if i == #BottomFaces[1].Edges then nNextEdgeIndex = 1 end local CurrentEdge = {} CurrentEdge.idAdjacentFace = BottomFaces[1].Edges[i].Adj CurrentEdge.vtToolDirection = Vector3d( BottomFaces[1].Edges[i].Norm) CurrentEdge.dLength = BottomFaces[1].Edges[i].Len CurrentEdge.dLengthOnX = CurrentEdge.dLength * CurrentEdge.vtToolDirection:getY() CurrentEdge.dElevation = BottomFaces[1].Edges[i].Elev CurrentEdge.bIsOpen = BottomFaces[1].Edges[i].Open CurrentEdge.bIsStartOpen = BottomFaces[1].Edges[nPreviousEdgeIndex].Open CurrentEdge.bIsEndOpen = BottomFaces[1].Edges[nNextEdgeIndex].Open if i == nFirstLongEdgeIndex then BottomFaces[1].MainEdges.LongEdges[1] = CurrentEdge BottomFaces[1].MainEdges.LongEdges[1].sType = 'Long' elseif nNextEdgeIndex == nFirstLongEdgeIndex then BottomFaces[1].MainEdges.SideEdges[1] = CurrentEdge BottomFaces[1].MainEdges.SideEdges[1].sType = 'Side' elseif nPreviousEdgeIndex == nFirstLongEdgeIndex then BottomFaces[1].MainEdges.SideEdges[2] = CurrentEdge BottomFaces[1].MainEdges.SideEdges[2].sType = 'Side' else BottomFaces[1].MainEdges.LongEdges[2] = CurrentEdge BottomFaces[1].MainEdges.LongEdges[2].sType = 'Long' end end return BottomFaces end ------------------------------------------------------------------------------------------------------------- local function GetLongFaces( Proc, MainFaces) local LongFaces = {} if Proc.nFct > 5 then error( 'GetLongFaces : Topology not implemented') end local BottomFace local BottomFaces = MainFaces.BottomFaces or GetBottomFaces( Proc) if BottomFaces and #BottomFaces > 0 then BottomFace = BottomFaces[1] end local idFirstLongFace = GDB_ID.NULL local idSecondLongFace = GDB_ID.NULL if not BottomFace then local FacesSortedByGreatestArea = {} for i = 1, Proc.nFct do FacesSortedByGreatestArea[i] = {} FacesSortedByGreatestArea[i].id = Proc.Faces[i].id FacesSortedByGreatestArea[i].dArea = Proc.Faces[i].dArea end table.sort( FacesSortedByGreatestArea, function (a, b) return a.dArea > b.dArea end) idFirstLongFace = FacesSortedByGreatestArea[1].id local FacesNotAdjacent = GetNotAdjacentFaces( Proc, idFirstLongFace) idSecondLongFace = FacesNotAdjacent[1].id else if not BottomFace.MainEdges.LongEdges[1].bIsOpen then idFirstLongFace = BottomFace.MainEdges.LongEdges[1].idAdjacentFace end if not BottomFace.MainEdges.LongEdges[2].bIsOpen then idSecondLongFace = BottomFace.MainEdges.LongEdges[2].idAdjacentFace end end if idFirstLongFace > -1 and Proc.Faces[idFirstLongFace + 1].bIsOkForMachining then table.insert( LongFaces, Proc.Faces[idFirstLongFace + 1]) end if idSecondLongFace > -1 and Proc.Faces[idSecondLongFace + 1].bIsOkForMachining then table.insert( LongFaces, Proc.Faces[idSecondLongFace + 1]) end for i = 1, #LongFaces do LongFaces[i].sType = 'Long' LongFaces[i].MainEdges = {} LongFaces[i].MainEdges.SideEdges = {} LongFaces[i].MainEdges.OppositeEdges = {} for j = 1, #LongFaces[i].Edges do local nPreviousEdgeIndex = j - 1 if j == 1 then nPreviousEdgeIndex = #LongFaces[1].Edges end local nNextEdgeIndex = j + 1 if j == #LongFaces[i].Edges then nNextEdgeIndex = 1 end local CurrentEdge = {} CurrentEdge.idAdjacentFace = LongFaces[i].Edges[j].Adj CurrentEdge.vtToolDirection = Vector3d( LongFaces[i].Edges[j].Norm) CurrentEdge.dLength = LongFaces[i].Edges[j].Len CurrentEdge.dLengthOnX = CurrentEdge.dLength * CurrentEdge.vtToolDirection:getY() CurrentEdge.dElevation = LongFaces[i].Edges[j].Elev CurrentEdge.bIsOpen = LongFaces[i].Edges[j].Open CurrentEdge.bIsStartOpen = LongFaces[i].Edges[nPreviousEdgeIndex].Open CurrentEdge.bIsEndOpen = LongFaces[i].Edges[nNextEdgeIndex].Open if Proc.Topology.sFamily == 'Tunnel' then if CurrentEdge.idAdjacentFace > -1 then table.insert( LongFaces[i].MainEdges.SideEdges, CurrentEdge) LongFaces[i].MainEdges.SideEdges[#LongFaces[i].MainEdges.SideEdges].sType = 'Side' else table.insert( LongFaces[i].MainEdges.OppositeEdges, CurrentEdge) LongFaces[i].MainEdges.OppositeEdges[#LongFaces[i].MainEdges.OppositeEdges].sType = 'Opposite' end else if CurrentEdge.idAdjacentFace == BottomFace.id then LongFaces[i].MainEdges.BottomEdge = CurrentEdge LongFaces[i].MainEdges.BottomEdge.sType = 'Bottom' elseif LongFaces[i].Edges[nNextEdgeIndex].Adj == BottomFace.id then LongFaces[i].MainEdges.SideEdges[1] = CurrentEdge LongFaces[i].MainEdges.SideEdges[1].sType = 'Side' elseif LongFaces[i].Edges[nPreviousEdgeIndex].Adj == BottomFace.id then LongFaces[i].MainEdges.SideEdges[2] = CurrentEdge LongFaces[i].MainEdges.SideEdges[2].sType = 'Side' else table.insert( LongFaces[i].MainEdges.OppositeEdges, CurrentEdge) LongFaces[i].MainEdges.OppositeEdges[#LongFaces[i].MainEdges.OppositeEdges].sType = 'Opposite' end end end end return LongFaces end ------------------------------------------------------------------------------------------------------------- local function GetSideFaces( Proc, MainFaces) local SideFaces = {} if Proc.nFct > 5 then error( 'GetSideFaces : Topology not implemented') end local BottomFace local BottomFaces = MainFaces.BottomFaces or GetBottomFaces( Proc) if BottomFaces and #BottomFaces > 0 then BottomFace = BottomFaces[1] end local LongFaces = MainFaces.LongFaces or GetLongFaces( Proc, MainFaces) local idFirstSideFace = GDB_ID.NULL local idSecondSideFace = GDB_ID.NULL if not LongFaces[1].MainEdges.SideEdges[1].bIsOpen then idFirstSideFace = LongFaces[1].MainEdges.SideEdges[1].idAdjacentFace end if not LongFaces[1].MainEdges.SideEdges[2].bIsOpen then idSecondSideFace = LongFaces[1].MainEdges.SideEdges[2].idAdjacentFace end if idFirstSideFace > -1 and Proc.Faces[idFirstSideFace + 1].bIsOkForMachining then table.insert( SideFaces, Proc.Faces[idFirstSideFace + 1]) SideFaces[#SideFaces].sType = 'Side' end if idSecondSideFace > -1 and Proc.Faces[idSecondSideFace + 1].bIsOkForMachining then table.insert( SideFaces, Proc.Faces[idSecondSideFace + 1]) SideFaces[#SideFaces].sType = 'Side' end for i = 1, #SideFaces do SideFaces[i].sType = 'Side' SideFaces[i].MainEdges = {} SideFaces[i].MainEdges.LongEdges = {} SideFaces[i].MainEdges.OppositeEdges = {} for j = 1, #SideFaces[i].Edges do local nPreviousEdgeIndex = j - 1 if j == 1 then nPreviousEdgeIndex = #SideFaces[1].Edges end local nNextEdgeIndex = j + 1 if j == #SideFaces[i].Edges then nNextEdgeIndex = 1 end local CurrentEdge = {} CurrentEdge.idAdjacentFace = SideFaces[i].Edges[j].Adj CurrentEdge.vtToolDirection = Vector3d( SideFaces[i].Edges[j].Norm) CurrentEdge.dLength = SideFaces[i].Edges[j].Len CurrentEdge.dLengthOnX = CurrentEdge.dLength * CurrentEdge.vtToolDirection:getY() CurrentEdge.dElevation = SideFaces[i].Edges[j].Elev CurrentEdge.bIsOpen = SideFaces[i].Edges[j].Open CurrentEdge.bIsStartOpen = SideFaces[i].Edges[nPreviousEdgeIndex].Open CurrentEdge.bIsEndOpen = SideFaces[i].Edges[nNextEdgeIndex].Open if Proc.Topology.sFamily == 'Tunnel' then if CurrentEdge.idAdjacentFace > -1 then table.insert( SideFaces[i].MainEdges.LongEdges, CurrentEdge) else table.insert( SideFaces[i].MainEdges.OppositeEdges, CurrentEdge) end else if CurrentEdge.idAdjacentFace == BottomFace.id then SideFaces[i].MainEdges.BottomEdge = CurrentEdge SideFaces[i].MainEdges.BottomEdge.sType = 'Bottom' elseif SideFaces[i].Edges[nNextEdgeIndex].Adj == BottomFace.id then SideFaces[i].MainEdges.LongEdges[1] = CurrentEdge SideFaces[i].MainEdges.LongEdges[1].sType = 'Long' elseif SideFaces[i].Edges[nPreviousEdgeIndex].Adj == BottomFace.id then SideFaces[i].MainEdges.LongEdges[2] = CurrentEdge SideFaces[i].MainEdges.LongEdges[2].sType = 'Long' else table.insert( SideFaces[i].MainEdges.OppositeEdges, CurrentEdge) SideFaces[i].MainEdges.OppositeEdges[#SideFaces[i].MainEdges.OppositeEdges].sType = 'Opposite' end end end end return SideFaces end ------------------------------------------------------------------------------------------------------------- -- recupero facce principali della feature, in base alla topologia function FaceData.GetMainFaces( Proc, Part) EgtOutLog( '---MainFaces START---') local MainFaces = {} -- CASO 1 : Feature tipo LapJoint if Proc.Topology.sFamily == 'Rabbet' or Proc.Topology.sFamily == 'VGroove' or Proc.Topology.sFamily == 'Groove' or Proc.Topology.sFamily == 'Pocket' or Proc.Topology.sFamily == 'Tunnel' then if Proc.Topology.bIsThrough and Proc.Topology.bAllRightAngles and Proc.nFct < 5 then MainFaces.TunnelAddedFaces = GetTunnelFaces( Proc, Part) end MainFaces.BottomFaces = GetBottomFaces( Proc) MainFaces.LongFaces = GetLongFaces( Proc, MainFaces) MainFaces.SideFaces = GetSideFaces( Proc, MainFaces) -- scrivo informazioni delle facce nel log if EgtGetDebugLevel() >= 3 then Logs.WriteMainFacesLog( Proc, MainFaces) end else EgtOutLog( '---MainFaces NOT NEEDED---') end EgtOutLog( '---MainFaces END---') return MainFaces end ------------------------------------------------------------------------------------------------------------- return FaceData