Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5
Imports EgtBEAMWALL.Core

Public Class CALCPanelVM
   Inherits VMBase

   Private m_CALCPanel_IsEnabled As Boolean = True
   Public Property CALCPanel_IsEnabled As Boolean
      Get
         Return m_CALCPanel_IsEnabled
      End Get
      Set(value As Boolean)
         m_CALCPanel_IsEnabled = value
      End Set
   End Property

   Private m_bMachineLog As Boolean = False
   Public Property bMachineLog As Boolean
      Get
         Return m_bMachineLog
      End Get
      Set(value As Boolean)
         m_bMachineLog = value
         If value Then
            Map.refProjectVM.SetSelTabPage(ProjectVM.TabPages.MACHINELOG)
         ElseIf Not IsNothing(Map.refLeftPanelVM.SelOPMode) AndAlso Map.refLeftPanelVM.SelOPMode.Id = OPModes.Mdi Then
            Map.refProjectVM.SetSelTabPage(ProjectVM.TabPages.MDI)
         Else
            Map.refProjectVM.SetSelTabPage(ProjectVM.TabPages.SCENE)
         End If
      End Set
   End Property
   Friend Sub SetMachineLog(value As Boolean)
      m_bMachineLog = value
      NotifyPropertyChanged(NameOf(bMachineLog))
   End Sub

   ' Definizione comandi
   Private m_cmdVerify As ICommand
   Private m_cmdVerifyAll As ICommand
   Private m_cmdSimulate As ICommand

   Private m_ProcessResultList As New List(Of Integer)

#Region "Messages"

   Public ReadOnly Property Verify_ToolTip As String
      Get
         Return EgtMsg(61901)
      End Get
   End Property

   Public ReadOnly Property Simulate_ToolTip As String
      Get
         Return EgtMsg(61902)
      End Get
   End Property

#End Region    ' Messages

#Region "CONSTRUCTORS"

   Sub New()
      ' imposto riferimento in Map
      Map.SetRefCALCPanelVM(Me)
      Core.CalcIntegration.SetMaxCamInstances(1)
      AddHandler Core.CalcIntegration.Calc_ProcessResult, AddressOf Calc_ProcessResult
      AddHandler Core.CalcIntegration.Calc_Ended, AddressOf Calc_Ended
      AddHandler Core.CalcIntegration.Calc_ProcessEnd, AddressOf Calc_ProcessEnded
   End Sub

#End Region    ' CONSTRUCTORS

#Region "METHODS"

   Friend Sub SetCalcPanelIsEnabled(bIsEnabled As Boolean)
      m_CALCPanel_IsEnabled = bIsEnabled
      NotifyPropertyChanged(NameOf(CALCPanel_IsEnabled))
   End Sub

   Private Sub Calc_ProcessResult(sender As Object, e As CalcResultEventArgs)
      m_ProcessResultList.Add(e.m_Result.nBarId)
      ProcessResults(e.m_Result)
      Dim nRawid As Integer = e.m_Result.nBarId
      If nRawid <= 0 Then Return
      Dim Machgroup As MyMachGroupVM = GetMachgroupVMFromMachgroupId(nRawid)
      Machgroup.CALCEnd()
   End Sub

   Private Sub Calc_Ended(sender As Object, e As CalcEndEventArgs)
      If e.End_ <> CalcEndEventArgs.Results.OK Then
         MessageBox.Show("Execution error (all processes are disabled)")
      End If
      ' ciclo sui pezzi
      Dim bNotOkFound As Boolean = False
      Dim MachGroupList As List(Of MachGroupVM) = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Where(Function(x) m_ProcessResultList.Contains(x.Id)).ToList()
      For Each MachGroup As MyMachGroupVM In MachGroupList
         ' se trovo pezzo errato, resetto tutti i successivi
         If Not bNotOkFound AndAlso MachGroup.MyMachGroupM.m_nCALC_GlobalState <> CalcStates.OK AndAlso MachGroup.MyMachGroupM.m_nCALC_GlobalState <> CalcStates.INFO Then
            bNotOkFound = True
         End If
         If bNotOkFound Then MachGroup.ResetProduce()
      Next
      m_ProcessResultList = New List(Of Integer)
      Map.refProjectVM.ManageIsEnabled(True)
      Map.refMyStatusBarVM.ResetStopProgress()
      ' riabilito bottoni di comunicazione a fine invio programma
      Map.refMachCommandMessagePanelVM.SetCommBtnIsEnabled(True)
   End Sub

   Private Sub Calc_ProcessEnded(sender As Object, e As CalcProcessEndEventArgs)
      'Dim nRawid As Integer = e.m_Result.nBarId
      'If nRawid <= 0 Then Return
      'Dim Machgroup As MyMachGroupVM = GetMachgroupVMFromMachgroupId(nRawid)
      'Machgroup.CALCEnd()
   End Sub

   Private Shared Sub ProcessResults(Bar As Bar)
      If IsNothing(Bar) Then Return

      ' disabilito impostazione modificato
      Dim DisableMgr As New DisableModifiedMgr

      Dim BtlPath As String = Bar.sBarPath
      Dim ResPath As String = Path.ChangeExtension(BtlPath, ".txt")
      Dim bErrors As Boolean = False
      Dim nLastErr As Integer = 0
      Dim sLastMsg As String = ""
      Dim nCurrCutId As Integer = 0
      Dim nTotTime As Integer = 0

      If File.Exists(ResPath) Then

         Dim ProcessResultList As New List(Of ProcessResult)
         Dim nErr As Integer = 0
         Dim sMsg As String = ""
         Dim dRot As Double = 0
         Dim nFall As Integer = 0
         Dim dPartRot As Double = 0
         Dim dTotRot As Double = 0
         Dim cutId As Integer = 0
         Dim taskId As Integer = 0
         Dim prevCutId As Integer = GDB_ID.NULL

         Dim lines As String() = System.IO.File.ReadAllLines(ResPath)
         For Each line As String In lines

            If line.StartsWith("ERR=") Then
               Dim nVal As Integer? = GetVal(line, "ERR")
               nErr = (If(Not IsNothing(nVal), nVal.Value, 0))
               sMsg = ""
               dRot = 0
               nFall = 0
               cutId = 0
               taskId = 0
            ElseIf line.StartsWith("ROT=") Then
               Dim nVal As Integer? = GetVal(line, "ROT")
               Dim nRot As Integer = (If(Not IsNothing(nVal), nVal.Value, 0))
               dRot = Math.Abs(((4 - nRot) Mod 4) * 90)
               dPartRot = Math.Max(dPartRot, dRot)
               dTotRot = Math.Max(dTotRot, dRot)
            ElseIf line.StartsWith("CUTID=") Then
               Dim nVal As Integer? = GetVal(line, "CUTID")
               cutId = If(Not IsNothing(nVal), nVal.Value, 0)
            ElseIf line.StartsWith("TASKID=") Then
               Dim nVal As Integer? = GetVal(line, "TASKID")
               taskId = (If(Not IsNothing(nVal), nVal.Value, 0))
               ProcessResultAdd(ProcessResultList, ProcessResult.CreateTaskResult(cutId, taskId, nErr, sMsg, dRot), Bar)
            ElseIf line.StartsWith("FALL=") Then
               Dim nVal As Integer? = GetVal(line, "FALL")
               nFall = (If(Not IsNothing(nVal), nVal.Value, 0))
               ProcessResultList.Add(ProcessResult.CreateFallResult(cutId, nErr, sMsg, nFall))
            ElseIf line.StartsWith("TIME=") Then
               Dim nVal As Integer? = GetVal(line, "TIME")
               nTotTime = (If(Not IsNothing(nVal), nVal.Value, 0))
               ProcessResultList.Add(ProcessResult.CreateTimeResult(cutId, nTotTime))
               'If Bar.nBarType = ProjectType.PROJ AndAlso Not IsNothing(currBTLPart) Then
               '   EgtSetInfo(currBTLPart.nPartId, If(Bar.nBarType = ProjectType.PROJ, ITG_PROJ_TIME, ITG_PROD_TIME), nTotTime, True)
               'End If
            ElseIf Not String.IsNullOrWhiteSpace(line) AndAlso line <> "---" Then
               sMsg = line
            End If
         Next

         If ProcessResultList.Count > 0 Then
            ' Inizializzo i pezzi del gruppo di lavoro in verifica
            Dim Machgroup As MyMachGroupVM = GetMachgroupVMFromMachgroupId(Bar.nBarId)
            For Each Part In Machgroup.PartVMList
               Part.CalcPartUpdate(0, 0, "")
            Next
            Machgroup.CalcMachGroupUpdate(0, 0, "")
            ' Ciclo ...
            Dim nCUTID As Integer = -1
            Dim CurrPartVM As PartVM = Nothing
            Dim CurrMachgroupVM As MyMachGroupVM = Nothing
            Dim CurrBTLFeatureVM As BTLFeatureVM = Nothing
            For Each Line In ProcessResultList
               ' se necessario recupero part
               If Line.nCUTID <> 0 Then
                  If Line.nCUTID <> nCUTID Then
                     nCUTID = Line.nCUTID
                     ' aggiorno nuovo pezzo
                     CurrPartVM = GetPartVMFromPartId(Line.nCUTID)
                  End If
               Else
                  ' se CutId = 0 
                  ' riporto errore barra
                  CurrMachgroupVM = GetMachgroupVMFromMachgroupId(Bar.nBarId)
               End If
               ' se necessario recupero feature
               If Line.nTASKID <> 0 Then
                  CurrBTLFeatureVM = GetFeatureVMFromPartId(CurrPartVM, Line.nTASKID)
               Else
                  CurrBTLFeatureVM = Nothing
               End If
               Select Case Line.Type
                  Case ProcessResult.ProcessResultTypes.BAR
                     If Not IsNothing(CurrMachgroupVM) Then CurrMachgroupVM.CalcMachGroupUpdate(Line.nERR, Line.dROT, Line.sMSG)
                  Case ProcessResult.ProcessResultTypes.PART
                     If Not IsNothing(CurrPartVM) Then CurrPartVM.CalcPartUpdate(Line.nERR, Line.dROT, Line.sMSG)
                  Case ProcessResult.ProcessResultTypes.TASKID
                     If Not IsNothing(CurrBTLFeatureVM) Then CurrBTLFeatureVM.CalcFeatureUpdate(Line.nERR, Line.dROT, Line.sMSG)
                  Case ProcessResult.ProcessResultTypes.FALL
                     If Not IsNothing(CurrPartVM) Then CurrPartVM.CalcFallUpdate(Line.nFALL)
                  Case ProcessResult.ProcessResultTypes.TIME
                     If IsNothing(CurrMachgroupVM) Then CurrMachgroupVM = GetMachgroupVMFromMachgroupId(Bar.nBarId)
                     If Not IsNothing(CurrMachgroupVM) Then CurrMachgroupVM.CalcTimeUpdate(Line.nTIME)
               End Select
            Next
            Machgroup = GetMachgroupVMFromMachgroupId(Bar.nBarId)
            If Not IsNothing(Machgroup) Then
               For Each Part In Machgroup.PartVMList
                  Part.CalcGlobalUpdate()
               Next
               Machgroup.CalcGlobalUpdate()
            End If

         End If

         ' mando notifica aggiornamento ad ottimizzatore
         DbControllers.m_StatusMapController.UpdateAction(DbControllers.m_SupervisorId, Map.refSupervisorManagerVM.CurrProd.nProdId, Bar.nBarId, StatusMapItemType.MachGroup, StatusMapOpType.MachGroupValidationUpdate, "")

      Else
         bErrors = True
         nLastErr = 25
         sLastMsg = "Execution Error"
      End If

      ' ripristino precedente impostazione modificato
      DisableMgr.ReEnable()
   End Sub

   Private Shared Function GetVal(sText As String, sKey As String) As Integer?
      Dim sParts As String() = sText.Split("="c)
      If String.Compare(sParts(0), sKey) <> 0 Then Return Nothing
      Dim nVal As Integer = Nothing

      If Not Integer.TryParse(sParts(1), nVal) Then
         Return Nothing
      Else
         Return nVal
      End If
   End Function

   Private Shared Sub ProcessResultAdd(PRList As List(Of ProcessResult), NewPR As ProcessResult, Bar As Bar)
      Dim Prev As ProcessResult = Nothing
      Select Case NewPR.Type
         Case ProcessResult.ProcessResultTypes.BAR
            If Bar.nProgramPage = ProjectType.PROJ Then
               Prev = PRList.FirstOrDefault(Function(x) x.Type = ProcessResult.ProcessResultTypes.PART)
            Else
               Prev = PRList.FirstOrDefault(Function(x) x.Type = ProcessResult.ProcessResultTypes.BAR)
            End If
         Case ProcessResult.ProcessResultTypes.PART
            Prev = PRList.FirstOrDefault(Function(x) x.Type = ProcessResult.ProcessResultTypes.PART AndAlso x.nCUTID = NewPR.nCUTID)
         Case ProcessResult.ProcessResultTypes.TASKID
            Prev = PRList.FirstOrDefault(Function(x) x.Type = ProcessResult.ProcessResultTypes.TASKID AndAlso x.nCUTID = NewPR.nCUTID AndAlso x.nTASKID = NewPR.nTASKID)
      End Select
      If IsNothing(Prev) Then
         PRList.Add(NewPR)
      ElseIf NewPR.nERR = 22 Or (NewPR.nERR > 0 And Prev.nERR <= 0) Or (NewPR.nERR < 0 And Prev.nERR = 0) Then
         If NewPR.dROT = 0 And Prev.dROT <> NewPR.dROT Then
            NewPR.SetROT(Prev.dROT)
         End If
         PRList.Remove(Prev)
         PRList.Add(NewPR)
      ElseIf Prev.dROT = 0 And Prev.dROT <> NewPR.dROT Then
         Prev.SetROT(NewPR.dROT)
      End If
   End Sub


   Private Shared Function GetPartVMFromPartId(nPartId As Integer) As PartVM
      For Each MachGroupVM As MyMachGroupVM In Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList
         Dim PartVM As PartVM = MachGroupVM.PartVMList.FirstOrDefault(Function(x) x.nPartId = nPartId)
         If Not IsNothing(PartVM) Then Return PartVM
      Next
      Return Nothing
   End Function

   Private Shared Function GetMachgroupVMFromMachgroupId(nMachGroupId As Integer) As MyMachGroupVM
      Return Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.FirstOrDefault(Function(x) x.Id = nMachGroupId)
   End Function

   Private Shared Function GetFeatureVMFromPartId(PartVM As PartVM, nFeatureId As Integer) As Core.BTLFeatureVM
      Return PartVM.FeatureVMList.FirstOrDefault(Function(x) x.nFeatureId = nFeatureId)
   End Function

#End Region    ' METHODS

#Region "COMMANDS"

#Region "Verify"

   ''' <summary>
   ''' Returns a command that do Open.
   ''' </summary>
   Public ReadOnly Property Verify_Command As ICommand
      Get
         If m_cmdVerify Is Nothing Then
            m_cmdVerify = New Command(AddressOf Verify)
         End If
         Return m_cmdVerify
      End Get
   End Property

   ''' <summary>
   ''' Execute the Open.  This method is invoked by the OpenCommand.
   ''' </summary>
   Friend Sub Verify()
      If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return
      Dim ProjType As BWType = Map.refSupervisorManagerVM.CurrProd.nType
      Dim BarList() As EgtBEAMWALL.Core.Bar
      If Not IsNothing(Map.refSupervisorMachGroupPanelVM.SelectedMachGroup) Then
         Dim TempBarList(0) As EgtBEAMWALL.Core.Bar
         Dim MachineName As String = ""
         EgtGetMachGroupMachineName(Map.refSupervisorMachGroupPanelVM.SelectedMachGroup.Id, MachineName)
         Dim Bar As New EgtBEAMWALL.Core.Bar With {.nBarId = Map.refSupervisorMachGroupPanelVM.SelectedMachGroup.Id,
                                                   .nProgramPage = ProjectType.PROD,
                                                   .nProjType = ProjType,
                                                   .bBarOk = True,
                                                   .nMachineName = MachineName}
         Dim SelMachGroup As MyMachGroupVM = Map.refSupervisorMachGroupPanelVM.SelectedMachGroup
         Select Case SelMachGroup.nGlobalState
            Case Core.CalcStates.OK, Core.CalcStates.INFO
               Bar.nCmdType = CalcIntegration.CmdTypes.GENERATE
            Case Else
               Dim sMachGroupFilePath As String = Map.refSupervisorManagerVM.CurrProd.sProdDirPath & "\" & SelMachGroup.Name.ToString() & ".ori.bwe"
               If File.Exists(sMachGroupFilePath) Then File.Delete(sMachGroupFilePath)
               Bar.nCmdType = CalcIntegration.CmdTypes.CHECKGEN
         End Select
         TempBarList(0) = Bar
         BarList = TempBarList
      Else
         Return
      End If
      ' disabilito interfaccia
      Map.refProjectVM.ManageIsEnabled(False)
      ' lancio calcolo
      EgtBEAMWALL.Core.CalcIntegration.Run(BarList, Map.refSupervisorManagerVM.CurrProd.sProdDirPath, AddressOf ManageCalc)
   End Sub

   Friend Sub VerifyMachGroup(MachGroup As MachGroupVM)
      VerifyMachGroup(New List(Of MachGroupVM)({MachGroup}))
   End Sub

   Friend Sub VerifyMachGroup(MachGroupList As List(Of MachGroupVM))
      If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return
      Dim ProjType As BWType = Map.refSupervisorManagerVM.CurrProd.nType
      Dim BarList() As EgtBEAMWALL.Core.Bar
      If Not IsNothing(MachGroupList) AndAlso MachGroupList.Count > 0 Then
         Dim TempBarList(MachGroupList.Count() - 1) As EgtBEAMWALL.Core.Bar
         For MachGroupIndex = 0 To MachGroupList.Count - 1
            Dim MachGroup As MyMachGroupVM = MachGroupList(MachGroupIndex)
            Dim MachineName As String = ""
            EgtGetMachGroupMachineName(MachGroup.Id, MachineName)
            Dim Bar As New EgtBEAMWALL.Core.Bar With {.nBarId = MachGroup.Id,
                                                      .nProgramPage = ProjectType.PROD,
                                                      .nProjType = ProjType,
                                                      .bBarOk = True,
                                                      .nMachineName = MachineName,
                                                      .nGlobState = MachGroup.nGlobalState}
            Select Case MachGroup.nGlobalState
               Case Core.CalcStates.OK, Core.CalcStates.INFO
                  Bar.nCmdType = CalcIntegration.CmdTypes.GENERATE
               Case Else
                  Bar.nCmdType = CalcIntegration.CmdTypes.CHECKGEN
            End Select
            TempBarList(MachGroupIndex) = Bar
         Next
         BarList = TempBarList
      Else
         Return
      End If
      ' disabilito interfaccia
      Map.refProjectVM.ManageIsEnabled(False)
      ' lancio calcolo
      EgtBEAMWALL.Core.CalcIntegration.Run(BarList, Map.refSupervisorManagerVM.CurrProd.sProdDirPath, AddressOf ManageCalc, True)
   End Sub

   ' funzione che gestisce risposta da thread di verifica, aggiorna progress e segnala comando di interruzione
   Private Sub ManageCalc(dProgress As Double, sProgress As String, ByRef bCancel As Boolean)
      If dProgress = 0 Then
         Map.refMyStatusBarVM.SetLoadingProgress_Visibility(True)
         Map.refMyStatusBarVM.SetStopProgress_IsActive(True)
         Map.refMyStatusBarVM.SetStopProgress_IsEnabled(True)
         Map.refMyStatusBarVM.SetOutputMessage(sProgress)
         Map.refMyStatusBarVM.SetStopProgress_IsActive(True)
         Map.refMyStatusBarVM.SetStopProgress_IsEnabled(True)
      ElseIf dProgress = 1 Then
         Map.refMyStatusBarVM.SetLoadingProgress_Visibility(False)
         Map.refMyStatusBarVM.SetStopProgress_IsActive(False)
         Map.refMyStatusBarVM.SetStopProgress_IsEnabled(False)
         Map.refMyStatusBarVM.SetOutputMessage(sProgress, 3)
      End If
      bCancel = Map.refMyStatusBarVM.bStopProgress
      Map.refMyStatusBarVM.SetLoadingProgress(dProgress * 100)
      Map.refMyStatusBarVM.SetOutputMessage(sProgress)
   End Sub

#End Region ' Verify

#Region "VerifyAll"

   ''' <summary>
   ''' Returns a command that do Open.
   ''' </summary>
   Public ReadOnly Property VerifyAll_Command As ICommand
      Get
         If m_cmdVerifyAll Is Nothing Then
            m_cmdVerifyAll = New Command(AddressOf VerifyAll)
         End If
         Return m_cmdVerifyAll
      End Get
   End Property

   ''' <summary>
   ''' Execute the Open.  This method is invoked by the OpenCommand.
   ''' </summary>
   Friend Sub VerifyAll()
      If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return
      Dim ProjType As BWType = Map.refSupervisorManagerVM.CurrProd.nType
      Dim BarList() As EgtBEAMWALL.Core.Bar
      Dim TempBarList(Map.refMachGroupPanelVM.MachGroupVMList.Count - 1) As EgtBEAMWALL.Core.Bar
      For PartIndex = 0 To Map.refMachGroupPanelVM.MachGroupVMList.Count - 1
         Dim CurrMachGroup As MyMachGroupVM = Map.refMachGroupPanelVM.MachGroupVMList(PartIndex)
         Dim MachineName As String = ""
         EgtGetMachGroupMachineName(CurrMachGroup.Id, MachineName)
         Dim Bar As New EgtBEAMWALL.Core.Bar With {.nBarId = CurrMachGroup.Id,
                                                      .nProgramPage = ProjectType.PROD,
                                                      .nProjType = ProjType,
                                                      .bBarOk = True,
                                                      .nMachineName = MachineName}
         Select Case CurrMachGroup.nGlobalState
            Case Core.CalcStates.OK, Core.CalcStates.INFO
               Bar.nCmdType = CalcIntegration.CmdTypes.GENERATE
            Case Else
               Dim sMachGroupFilePath As String = Map.refSupervisorManagerVM.CurrProd.sProdDirPath & "\" & CurrMachGroup.Name.ToString() & ".ori.bwe"
               If File.Exists(sMachGroupFilePath) Then File.Delete(sMachGroupFilePath)
               Bar.nCmdType = CalcIntegration.CmdTypes.CHECKGEN
         End Select
         TempBarList(PartIndex) = Bar
      Next
      BarList = TempBarList
      ' disabilito interfaccia
      Map.refProjectVM.ManageIsEnabled(False)
      ' lancio calcolo
      EgtBEAMWALL.Core.CalcIntegration.Run(BarList, Map.refSupervisorManagerVM.CurrProd.sProdDirPath, AddressOf ManageCalc)
   End Sub

#End Region ' VerifyAll

#Region "Simulate"

   ''' <summary>
   ''' Returns a command that do Open.
   ''' </summary>
   Public ReadOnly Property Simulate_Command As ICommand
      Get
         If m_cmdSimulate Is Nothing Then
            m_cmdSimulate = New Command(AddressOf Simulate)
         End If
         Return m_cmdSimulate
      End Get
   End Property

   ''' <summary>
   ''' Execute the Open.  This method is invoked by the OpenCommand.
   ''' </summary>
   Friend Sub Simulate()
      If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return
      Dim ProjType As BWType = Map.refSupervisorManagerVM.CurrProd.nType
      Dim BarList() As EgtBEAMWALL.Core.Bar
      Dim SelMachGroup As MyMachGroupVM = Map.refSupervisorMachGroupPanelVM.SelectedMachGroup
      If Not IsNothing(SelMachGroup) Then
         Dim TempBarList(0) As EgtBEAMWALL.Core.Bar
         Dim MachineName As String = ""
         EgtGetMachGroupMachineName(SelMachGroup.Id, MachineName)
         Dim Bar As New EgtBEAMWALL.Core.Bar With {.nBarId = SelMachGroup.Id,
                                                  .nProgramPage = ProjectType.PROD,
                                                  .nProjType = ProjType,
                                                  .bBarOk = True,
                                                  .nMachineName = MachineName,
                                                  .nCmdType = CalcIntegration.CmdTypes.SIMULATE}
         If SelMachGroup.nGlobalState = CalcStates.NOTCALCULATED Then
            Dim sMachGroupFilePath As String = Map.refSupervisorManagerVM.CurrProd.sProdDirPath & "\" & SelMachGroup.Name.ToString() & ".ori.bwe"
            If File.Exists(sMachGroupFilePath) Then File.Delete(sMachGroupFilePath)
         End If
         TempBarList(0) = Bar
         BarList = TempBarList
      Else
         Return
      End If
      ' disabilito interfaccia
      Map.refProjectVM.ManageIsEnabled(False)
      ' lancio simulazione
      EgtBEAMWALL.Core.CalcIntegration.Run(BarList, Map.refSupervisorManagerVM.CurrProd.sProdDirPath, AddressOf ManageCalc)
   End Sub

#End Region ' Simulate

#End Region    ' COMMANDS

End Class
