Imports EgtBEAMWALL.Core
Imports EgtBEAMWALL.DataLayer.Controllers
Imports EgtUILib
Imports EgtWPFLib5

Module LuaExec

   Friend Function ExecBeam(sFile As String, sMachine As String, nFlag As Integer, bBtl As Boolean) As Boolean
      EgtOutLog("-- Start ExecBeam --")
      ' Recupero lo script da eseguire
      Dim sExecPath As String = ""
      Dim sExecName As String = ""
      GetMainPrivateProfileString(S_BEAM, K_BEAMBWEEXEC, "", sExecName)
      sExecPath = (Map.refMainWindowVM.MainWindowM.sBeamRoot & "\" & sExecName).TrimEnd({"\"c})
      If Not My.Computer.FileSystem.FileExists(sExecPath) Then
         EgtOutLog("Not found BeamExec script " & sExecPath)
         Return False
      End If
      ' Assegno i dati
      EgtLuaCreateGlobTable("BEAM")
      EgtLuaSetGlobStringVar("BEAM.BASEDIR", Map.refMainWindowVM.MainWindowM.sBeamRoot)
      EgtLuaSetGlobStringVar("BEAM.FILE", sFile)
      EgtLuaSetGlobStringVar("BEAM.MACHINE", sMachine)
      EgtLuaSetGlobIntVar("BEAM.FLAG", nFlag)
      ' Eseguo lo script
      Dim bOk As Boolean = False
      If EgtLuaExecFile(sExecPath) Then
         ' Recupero i risultati
         Dim nErr As Integer = 999
         EgtLuaGetGlobIntVar("BEAM.ERR", nErr)
         bOk = (nErr <= 0)
         If Not bOk Then EgtOutLog("BeamExec Err=" & nErr.ToString())
      Else
         EgtOutLog("Error executing Beam Exec script " & sExecPath)
         Return False
      End If
      ' Cancello tavola globale
      EgtLuaResetGlobVar("BEAM")
      Return bOk
   End Function

   Friend Function ExecWall(sFile As String, sMachine As String, nFlag As Integer, bBtl As Boolean, bNestingCornerBL As Boolean) As Boolean
      EgtOutLog("-- Start ExecWall --")
      ' Recupero lo script da eseguire
      Dim sExecPath As String = ""
      Dim sExecName As String = ""
      GetMainPrivateProfileString(S_WALL, K_WALLBWEEXEC, "", sExecName)
      sExecPath = (Map.refMainWindowVM.MainWindowM.sWallRoot & "\" & sExecName).TrimEnd({"\"c})
      If Not My.Computer.FileSystem.FileExists(sExecPath) Then
         EgtOutLog("Not found WallExec script " & sExecPath)
         Return False
      End If
      ' Assegno i dati
      EgtLuaCreateGlobTable("WALL")
      EgtLuaSetGlobStringVar("WALL.BASEDIR", Map.refMainWindowVM.MainWindowM.sWallRoot)
      EgtLuaSetGlobStringVar("WALL.FILE", sFile)
      EgtLuaSetGlobStringVar("WALL.MACHINE", sMachine)
      EgtLuaSetGlobIntVar("WALL.FLAG", nFlag)
      EgtLuaSetGlobBoolVar("WALL.NESTINGCORNERBL", bNestingCornerBL)
      ' Eseguo lo script
      Dim bOk As Boolean = False
      If EgtLuaExecFile(sExecPath) Then
         ' Recupero i risultati
         Dim nErr As Integer = 999
         EgtLuaGetGlobIntVar("WALL.ERR", nErr)
         bOk = (nErr <= 0)
         If Not bOk Then EgtOutLog("WallExec Err=" & nErr.ToString())
      Else
         bOk = False
         EgtOutLog("Error executing Wall Exec script " & sExecPath)
      End If
      ' Cancello tavola globale
      EgtLuaResetGlobVar("WALL")
      Return bOk
   End Function

   Friend Function ExecNesting(sFile As String, sMachine As String, PartList As List(Of BTLPartM), SectionList As List(Of SParam), dStartOffset As Double, dOffset As Double, dKerf As Double, bLDIntersOther As Boolean, nMinScore As Integer, nNestTime As Integer) As Boolean
      If PartList.Count = 0 Then
         EgtOutLog("Exit nesting: no parts received!")
         Return True
      End If
      EgtOutLog("-- Start ExecNest --")
      ' Recupero lo script da eseguire
      Dim sExecPath As String = ""
      Dim sExecName As String = ""
      GetMainPrivateProfileString(S_NEST, K_NESTEXEC, "", sExecName)
      Dim sRoot As String = ""
      If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
         sRoot = Map.refMainWindowVM.MainWindowM.sBeamRoot
      ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
         sRoot = Map.refMainWindowVM.MainWindowM.sWallRoot
      End If
      sExecPath = (sRoot & "\" & sExecName).TrimEnd({"\"c})
      If Not My.Computer.FileSystem.FileExists(sExecPath) Then
         EgtOutLog("Not found NestExec script " & sExecPath)
         Return False
      End If
      ' Assegno i dati
      EgtLuaCreateGlobTable("NEST")
      EgtLuaSetGlobStringVar("NEST.BASEDIR", sRoot)
      EgtLuaSetGlobStringVar("NEST.FILE", sFile)
      EgtLuaSetGlobStringVar("NEST.MACHINE", sMachine)
      EgtLuaSetGlobNumVar("NEST.STARTOFFSET", dStartOffset)
      EgtLuaSetGlobNumVar("NEST.OFFSET", dOffset)
      EgtLuaSetGlobNumVar("NEST.KERF", dKerf)
      EgtLuaSetGlobNumVar("NEST.DRILL_MACH_AREA", If(bLDIntersOther, 1, 0))
      EgtLuaSetGlobNumVar("NEST.MIN_ANGLE_PNT", nMinScore)
      Dim nNestCorner As Integer
      Select Case CurrentMachine.NestingCorner
         Case MCH_CR.TL
            nNestCorner = 1
         Case MCH_CR.TR
            nNestCorner = 3
         Case MCH_CR.BL
            nNestCorner = 0
         Case MCH_CR.BR
            nNestCorner = 2
      End Select
      EgtLuaSetGlobNumVar("NEST.CORNER", nNestCorner)
      EgtLuaSetGlobStringVar("NEST.MATERIAL", PartList(0).sMATERIAL)
      EgtLuaSetGlobNumVar("NEST.TIME", nNestTime)
      EgtLuaSetGlobNumVar("NEST.PRODID", Map.refProdManagerVM.CurrProd.nProdId)
      EgtLuaCreateGlobTable("PART")
      For PartIndex = 0 To PartList.Count - 1
         EgtLuaSetGlobIntVar("PART." & PartList(PartIndex).nPartId.ToString(), PartList(PartIndex).m_nCNT + PartList(PartIndex).m_nADDED - PartList(PartIndex).nINPROD)
      Next
      EgtLuaCreateGlobTable("LEN")
      For SectionIndex = 0 To SectionList.Count - 1
         EgtLuaSetGlobNumVar("LEN." & (SectionIndex + 1).ToString(), SectionList(SectionIndex).dL)
      Next
      EgtLuaCreateGlobTable("WIDTH")
      For SectionIndex = 0 To SectionList.Count - 1
         EgtLuaSetGlobNumVar("WIDTH." & (SectionIndex + 1).ToString(), SectionList(SectionIndex).dW)
      Next
      EgtLuaCreateGlobTable("QTY")
      For SectionIndex = 0 To SectionList.Count - 1
         EgtLuaSetGlobNumVar("QTY." & (SectionIndex + 1).ToString(), SectionList(SectionIndex).nQuantity)
      Next
      EgtLuaCreateGlobTable("MATERIAL")
      For SectionIndex = 0 To SectionList.Count - 1
         EgtLuaSetGlobStringVar("MATERIAL." & (SectionIndex + 1).ToString(), SectionList(SectionIndex).SectXMat.sSectionXMaterial)
      Next
      ' Eseguo lo script
      Dim bOk As Boolean = False
      If EgtLuaExecFile(sExecPath) Then
         ' Recupero i risultati
         Dim nErr As Integer = 999
         EgtLuaGetGlobIntVar("NEST.ERR", nErr)
         bOk = (nErr <= 0)
         If Not bOk Then EgtOutLog("NestExec Err=" & nErr.ToString())
      Else
         bOk = False
         EgtOutLog("Error executing Nest Exec script " & sExecPath)
      End If
      ' Cancello tavola globale
      EgtLuaResetGlobVar("NEST")
      EgtLuaResetGlobVar("PART")
      EgtLuaResetGlobVar("LEN")
      EgtLuaResetGlobVar("WIDTH")
      EgtLuaResetGlobVar("QTY")
      EgtLuaResetGlobVar("MATERIAL")
      Return bOk
   End Function

   Friend Function ExecRotFlip(nPartId As Integer) As Boolean
      If IsNothing(nPartId) OrElse nPartId = GDB_ID.NULL Then
         EgtOutLog("Exit nesting: no parts received!")
         Return True
      End If
      EgtOutLog("-- Start ExecFlipRot --")
      ' Recupero lo script da eseguire
      Dim sExecPath As String = ""
      Dim sExecName As String = ""
      GetMainPrivateProfileString(S_NEST, K_FLIPROT, "", sExecName)
      Dim sRoot As String = ""
      If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
         sRoot = Map.refMainWindowVM.MainWindowM.sBeamRoot
      ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
         sRoot = Map.refMainWindowVM.MainWindowM.sWallRoot
      End If
      sExecPath = (sRoot & "\" & sExecName).TrimEnd({"\"c})
      If Not My.Computer.FileSystem.FileExists(sExecPath) Then
         EgtOutLog("Not found NestExec script " & sExecPath)
         Return False
      End If
      ' Assegno i dati
      EgtLuaCreateGlobTable("NFAR")
      EgtLuaSetGlobStringVar("NFAR.BASEDIR", sRoot)
      EgtLuaSetGlobStringVar("NFAR.PARTID", nPartId)
      ' Eseguo lo script
      Dim bOk As Boolean = False
      If EgtLuaExecFile(sExecPath) Then
         ' Recupero i risultati
         Dim nErr As Integer = 999
         EgtLuaGetGlobIntVar("NFAR.ERR", nErr)
         bOk = (nErr <= 0)
         If Not bOk Then EgtOutLog("FlipRot Err=" & nErr.ToString())
      Else
         bOk = False
         EgtOutLog("Error executing Nest FlipRot script " & sExecPath)
      End If
      ' Cancello tavola globale
      EgtLuaResetGlobVar("NFAR")
      Return bOk
   End Function

   ' Special Panel
   Friend Sub ExecScript(sFilePath As String, Optional bRaiseEvent As Boolean = False)
      EgtLuaExecFile(sFilePath)

      'Dim bMachiningMode As Boolean = EgtGetCurrMachGroup() <> GDB_ID.NULL
      'If Not bMachiningMode And EgtGetCurrLayer() = GDB_ID.NULL Then
      '   Dim nCurrPart As Integer = EgtGetCurrPart()
      '   If nCurrPart = GDB_ID.NULL Or Not EgtSetCurrPartLayer(nCurrPart, EgtGetFirstLayer(nCurrPart, True)) Then
      '      EgtResetCurrPartLayer()
      '   End If
      'End If
   End Sub

End Module
