Imports EgtUILib

Module GeomCalc

   Public Function GetRegionOutLoop(nRegId As Integer, nDestGrpId As Integer) As Integer
      Dim nCount As Integer = 0
      Dim nOutId As Integer = EgtExtractSurfFrChunkLoops(nRegId, 0, nDestGrpId, nCount)
      If nOutId <> GDB_ID.NULL Then
         For nId As Integer = nOutId + 1 To nOutId + nCount - 1
            EgtErase(nId)
         Next
      End If
      Return nOutId
   End Function

   Public Function CreateSolidFromRegion(nRegId As Integer, vtExtr As Vector3d, nDestGrpId As Integer) As Integer
      Dim nOutId As Integer = GetRegionOutLoop(nRegId, nDestGrpId)
      If nOutId = GDB_ID.NULL Then Return GDB_ID.NULL
      Dim nSolId As Integer = EgtCreateSurfTmByRegionExtrusion(nDestGrpId, 1, {nOutId}, vtExtr,
                                                               100 * EPS_SMALL, GDB_RT.GLOB)
      EgtErase(nOutId)
      Return nSolId
   End Function

   Friend Function UpdateSolidForDrip(nPartId As Integer) As Boolean
      ' se non ci sono lavorazioni da sotto nel pezzo si esce
      If EgtGetGroupObjs(EgtGetFirstNameInGroup(nPartId, NAME_DRIPCUT)) = 0 AndAlso
         EgtGetGroupObjs(EgtGetFirstNameInGroup(nPartId, NAME_UNDERDRILL)) = 0 Then Return True
      ' Recupero lo spessore del grezzo
      Dim dThick As Double = GetRawHeight()
      ' Recupero layer Regione
      Dim nGrpRegId As Integer = EgtGetFirstNameInGroup(nPartId, NAME_REGION)
      ' Elimino eventuale vecchio solido
      EgtErase(EgtGetFirstNameInGroup(nGrpRegId, NAME_DRIP_SOLID))
      ' Recupero la regione del pezzo
      Dim nRegId As Integer = EgtGetFirstInGroup(nGrpRegId)
      While nRegId <> GDB_ID.NULL
         If EgtGetType(nRegId) = GDB_TY.SRF_FRGN Then Exit While
         nRegId = EgtGetNext(nRegId)
      End While
      ' Ne derivo il solido di estrusione
      Dim nSolId As Integer = CreateSolidFromRegion(nRegId, -dThick * Vector3d.Z_AX(), nGrpRegId)
      EgtSetName(nSolId, NAME_DRIP_SOLID)
      EgtSetColor(nSolId, New Color3d(0, 255, 255, 25))
      EgtSetStatus(nSolId, GDB_ST.OFF)
      Return nSolId <> GDB_ID.NULL
   End Function

   Friend Function EraseSolidForDrip(nPartId As Integer) As Boolean
      ' Recupero layer Regione
      Dim nGrpRegId As Integer = EgtGetFirstNameInGroup(nPartId, NAME_REGION)
      ' Elimino eventuale vecchio solido
      EgtErase(EgtGetFirstNameInGroup(nGrpRegId, NAME_DRIP_SOLID))
      Return True
   End Function

   Friend Function GetPartArea( nPartid As Integer) As Double
      Dim nRegId As Integer = EgtGetFirstNameInGroup( nPartId, NAME_REGION)
      Dim nSurfId As Integer = EgtGetFirstInGroup( nRegId)
      While nSurfId <> GDB_ID.NULL and EgtGetType( nSurfId) <> GDB_TY.SRF_FRGN
         nSurfId = EgtGetNext( nSurfId)
      End While
      Dim dArea As Double = 0
      EgtSurfFrGrossArea( nSurfId, dArea)
      Return dArea
   End Function

End Module
