Imports System.ComponentModel
Imports EgtUILib

Public Class MachineStatusUC

   ' Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Private m_MachinePage As MachinePageUC
   ' Flag che indica se è stato premuto questo controllo per chiamare la pagina con lista errori
   Friend m_bIsClicked As Boolean = False
   ' Oggetto per comunicare con il CN
   Friend m_CN As CN_generico
   ' Flag per tipo visualizzazione assi
   Private m_bShowTipAxes As Boolean = False
   ' numero di assi da visualizzare
   Private m_AxesNumber As Integer = 5

   Private Sub MachineStatus_Initialized(sender As Object, e As EventArgs)
      Dim PcType As Integer = GetPrivateProfileInt(S_GENERAL, K_SIEMENSPC, 0, m_MainWindow.GetIniFile())
      m_AxesNumber = GetPrivateProfileInt(S_AXES, K_AXESNUM, 5, m_MainWindow.GetMachIniFile())
      If m_MainWindow.m_dMWinScale < 1 Then
         MachineStatusImage.Height *= m_MainWindow.m_dMWinScale
         MachineStatusImage.Width *= m_MainWindow.m_dMWinScale
         Axis1NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis1TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis1TxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis2NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis2TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis2TxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis3NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis3TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis3TxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis4NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis4TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis4TxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis5NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis5TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis5TxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis6NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis6TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis6TxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis7NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis7TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis7TxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis8NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis8TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis8TxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis9NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis9TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis9TxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis10NameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         Axis10TxBx.FontSize *= m_MainWindow.m_dMWinScale
         DeltaAxis10TxBx.FontSize *= m_MainWindow.m_dMWinScale
         FeedNameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         FeedTxBx.FontSize *= m_MainWindow.m_dMWinScale
         FeedOverrideNameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         FeedOverrideTxBx.FontSize *= m_MainWindow.m_dMWinScale
         SpeedNameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         SpeedTxBx.FontSize *= m_MainWindow.m_dMWinScale
         SpeedOverrideNameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         SpeedOverrideTxBx.FontSize *= m_MainWindow.m_dMWinScale
         ConsumptionNameTxBx.FontSize *= m_MainWindow.m_dMWinScale
         ConsumptionTxBx.FontSize *= m_MainWindow.m_dMWinScale
         AOverrideTxBx.FontSize *= m_MainWindow.m_dMWinScale
      End If
      If PcType <> 0 Then
         MachineStatusGrid.ColumnDefinitions.Clear()
         MachineStatusGrid.RowDefinitions.Clear()
         Dim SeparatorColumn As New ColumnDefinition()
         SeparatorColumn.Width = New GridLength(2, GridUnitType.Pixel)
         LongSeparator.SetValue(Grid.ColumnSpanProperty, 1)
         LongSeparator.SetValue(Grid.RowSpanProperty, 2)
         MachineStatusGrid.ColumnDefinitions.Add(SeparatorColumn)
         Dim BaseColumn As New ColumnDefinition()
         BaseColumn.Width = New GridLength(1, GridUnitType.Star)
         MachineStatusGrid.ColumnDefinitions.Add(BaseColumn)
         Dim VerticalRow As New RowDefinition
         VerticalRow.Height = New GridLength(m_AxesNumber + 2, GridUnitType.Star)
         MachineStatusGrid.RowDefinitions.Add(VerticalRow)
         Dim StatusRow As New RowDefinition
         StatusRow.Height = New GridLength(1, GridUnitType.Star)
         MachineStatusGrid.RowDefinitions.Add(StatusRow)
         ' rimuovo A dallo UniformGrid degli assi
         AxisUniformGrid.Children.Remove(ConsumptionGrid)
         AxisUniformGrid.SetValue(Grid.RowProperty, 0)
         ' porto A in stato macchina
         CurrMachineStatusGrid.SetValue(Grid.ColumnProperty, 1)
         CurrMachineStatusGrid.SetValue(Grid.RowProperty, 1)
         CurrMachineStatusGrid.ColumnDefinitions.Clear()
         CurrMachineStatusGrid.RowDefinitions.Clear()
         Dim SingleColumn As New ColumnDefinition()
         SingleColumn.Width = New GridLength(1, GridUnitType.Star)
         CurrMachineStatusGrid.ColumnDefinitions.Add(SingleColumn)
         Dim DoubleColumn As New ColumnDefinition()
         DoubleColumn.Width = New GridLength(1.5, GridUnitType.Star)
         CurrMachineStatusGrid.ColumnDefinitions.Add(DoubleColumn)
         Dim StatusBorderRow As New RowDefinition()
         StatusBorderRow.Height = New GridLength(2, GridUnitType.Pixel)
         CurrMachineStatusGrid.RowDefinitions.Add(StatusBorderRow)
         Dim SingleRow As New RowDefinition()
         SingleRow.Height = New GridLength(1, GridUnitType.Star)
         CurrMachineStatusGrid.RowDefinitions.Add(SingleRow)
         ConsumptionGrid.SetValue(Grid.ColumnProperty, 1)
         CurrMachineStatusGrid.Children.Add(ConsumptionGrid)
         Dim StatusBorder As New Border
         StatusBorder.Background = Application.Current.FindResource("OmagCut_White")
         StatusBorder.SetValue(Grid.ColumnSpanProperty, 2)
         CurrMachineStatusGrid.Children.Add(StatusBorder)
         MachineStatusImage.SetValue(Grid.RowProperty, 1)
         ConsumptionGrid.SetValue(Grid.RowProperty, 1)
         ' modifico uniformgrid
         AxisUniformGrid.Columns = 1
         AxisUniformGrid.Rows = m_AxesNumber + 2
         For Index As Integer = 0 To 12 - 1
            Dim HorizontalGrid As Grid = DirectCast(AxisUniformGrid.Children(Index), Grid)
            HorizontalGrid.ColumnDefinitions.Clear()
            HorizontalGrid.RowDefinitions.Clear()
            If Index < m_AxesNumber Then
               HorizontalGrid.Visibility = Windows.Visibility.Visible
            End If
            If HorizontalGrid.Name = "B" And m_AxesNumber = 4 Then
               ' se macchina a 4 assi allora nascondo le info dell'asse B
               HorizontalGrid.Visibility = Windows.Visibility.Collapsed
               Continue For
            End If
            Dim BorderRow As New RowDefinition
            BorderRow.Height = New GridLength(2, GridUnitType.Pixel)
            HorizontalGrid.RowDefinitions.Add(BorderRow)
            Dim AxisGrid As New RowDefinition
            AxisGrid.Height = New GridLength(1, GridUnitType.Star)
            HorizontalGrid.RowDefinitions.Add(AxisGrid)

            For Each Children As UIElement In HorizontalGrid.Children
               If TypeOf Children Is System.Windows.Controls.Primitives.UniformGrid Then
                  Dim AxisUniformGrid As System.Windows.Controls.Primitives.UniformGrid = DirectCast(Children, System.Windows.Controls.Primitives.UniformGrid)
                  AxisUniformGrid.SetValue(Grid.RowProperty, 1)
               End If
            Next
         Next
      End If

   End Sub

   Private Sub MachineStatus_Loaded(sender As Object, e As RoutedEventArgs)
      m_MachinePage = m_MainWindow.m_MachinePageUC
      m_bShowTipAxes = GetPrivateProfileInt(S_GENERAL, K_SHOWTIPAXES, 0, m_MainWindow.GetIniFile()) <> 0
   End Sub

   Private Sub MachineStatus_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseDown
      If Not GetPrivateProfileInt(S_NUMERICALCONTROL, K_TYPE, 0, m_MainWindow.GetMachIniFile()) = 0 Then
         MachinePageClick()
      End If
   End Sub

   Sub MachinePageClick()
      ' Emulo il click della pagina Macchina
      Select Case m_MainWindow.m_ActivePage
         Case MainWindow.Pages.WorkInProgress
            m_MainWindow.WorkInProgressBtn.IsChecked = False
            m_MainWindow.MachineBtn.IsChecked = True
            m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_WorkInProgressPageUC)
            m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_MachinePageUC)
            m_MainWindow.m_ActivePage = MainWindow.Pages.Machine
            m_bIsClicked = True
         Case MainWindow.Pages.DirectCut
            m_MainWindow.DirectCutBtn.IsChecked = False
            m_MainWindow.MachineBtn.IsChecked = True
            m_MainWindow.m_CurrentProjectPageUC.CurrentProjectPageGrid.Children.Remove(m_MainWindow.m_DirectCutPageUC)
            m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_CurrentProjectPageUC)
            m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_MachinePageUC)
            m_MainWindow.m_ActivePage = MainWindow.Pages.Machine
            m_bIsClicked = True
         Case MainWindow.Pages.CadCut
            m_MainWindow.CadCutBtn.IsChecked = False
            m_MainWindow.MachineBtn.IsChecked = True
            m_MainWindow.m_CurrentProjectPageUC.CurrentProjectPageGrid.Children.Remove(m_MainWindow.m_CadCutPageUC)
            m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_CurrentProjectPageUC)
            m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_MachinePageUC)
            m_MainWindow.m_ActivePage = MainWindow.Pages.Machine
            m_bIsClicked = True
         Case MainWindow.Pages.RawPart
            m_MainWindow.CadCutBtn.IsChecked = True
            m_MainWindow.MachineBtn.IsChecked = False
         Case MainWindow.Pages.Simulation
            m_MainWindow.CadCutBtn.IsChecked = True
            m_MainWindow.MachineBtn.IsChecked = False
         Case MainWindow.Pages.Draw
            m_MainWindow.CadCutBtn.IsChecked = True
            m_MainWindow.MachineBtn.IsChecked = False
         Case MainWindow.Pages.Import
            m_MainWindow.CadCutBtn.IsChecked = True
            m_MainWindow.MachineBtn.IsChecked = False
         Case MainWindow.Pages.FrameCut
            m_MainWindow.FrameCutBtn.IsChecked = False
            m_MainWindow.MachineBtn.IsChecked = True
            m_MainWindow.m_CurrentProjectPageUC.CurrentProjectPageGrid.Children.Remove(m_MainWindow.m_FrameCutPageUC)
            m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_CurrentProjectPageUC)
            m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_MachinePageUC)
            m_MainWindow.m_ActivePage = MainWindow.Pages.Machine
            m_bIsClicked = True
         Case MainWindow.Pages.Machine
            m_MainWindow.MachineBtn.IsChecked = True
            m_bIsClicked = True
         Case MainWindow.Pages.Options
            m_MainWindow.MachineBtn.IsChecked = True
            m_MainWindow.OptionsBtn.IsChecked = False
            m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_OptionsPageUC)
            m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_MachinePageUC)
            m_MainWindow.m_ActivePage = MainWindow.Pages.Machine
            m_bIsClicked = True
      End Select
   End Sub

   Sub DisplayPositionName()
      ' Recupero i nomi degli assi macchina
      Dim sA1 As String = String.Empty
      Dim sA2 As String = String.Empty
      Dim sA3 As String = String.Empty
      Dim sA4 As String = String.Empty
      Dim sA5 As String = String.Empty
      Dim sA6 As String = String.Empty
      Dim sA7 As String = String.Empty
      Dim sA8 As String = String.Empty
      Dim sA9 As String = String.Empty
      Dim sA10 As String = String.Empty
      m_MainWindow.m_CNCommunication.GetAxesNames(sA1, sA2, sA3, sA4, sA5, sA6, sA7, sA8, sA9, sA10)
      Axis1NameTxBx.Text = sA1
      Axis2NameTxBx.Text = sA2
      Axis3NameTxBx.Text = sA3
      Axis4NameTxBx.Text = sA4
      Axis5NameTxBx.Text = sA5
      Axis6NameTxBx.Text = sA6
      Axis7NameTxBx.Text = sA7
      Axis8NameTxBx.Text = sA8
      Axis9NameTxBx.Text = sA9
      Axis10NameTxBx.Text = sA10
   End Sub

   Sub DisplayPosition()
      ' Recupero le posizioni macchina
      Dim dL1, dL2, dL3, dR1, dR2, dA6, dA7, dA8, dA9, dA10 As Double
      m_MainWindow.m_CNCommunication.GetAxesPositions(dL1, dL2, dL3, dR1, dR2, dA6, dA7, dA8, dA9, dA10)
      ' Se richiesto, le converto sul tip dell'utensile corrente rispetto allo Zero tavola
      Dim ptTip As New Point3d
      If m_bShowTipAxes AndAlso m_MainWindow.m_WorkInProgressPageUC.GetTipFromPositions(dL1, dL2, dL3, dR1, dR2, ptTip) Then
         dL1 = ptTip.x
         dL2 = ptTip.y
         dL3 = ptTip.z
      End If
      ' Visualizzazione
      Axis1TxBx.Text = LenToString(dL1, -3)
      Axis2TxBx.Text = LenToString(dL2, -3)
      Axis3TxBx.Text = LenToString(dL3, -3)
      Axis4TxBx.Text = DoubleToString(dR1, -3)
      Axis5TxBx.Text = DoubleToString(dR2, -3)
      If m_AxesNumber >= 6 Then Axis6TxBx.Text = DoubleToString(dA6, -3)
      If m_AxesNumber >= 7 Then Axis7TxBx.Text = LenToString(dA7, -3)
      If m_AxesNumber >= 8 Then Axis8TxBx.Text = LenToString(dA8, -3)
      If m_AxesNumber >= 9 Then Axis9TxBx.Text = LenToString(dA9, -3)
      If m_AxesNumber >= 10 Then Axis10TxBx.Text = LenToString(dA10, -3)
   End Sub

   Sub DisplayPositionDelta()
      ' Recupero le posizioni macchina
      Dim dL1, dL2, dL3, dR1, dR2, dA6, dA7, dA8, dA9, dA10 As Double
      m_MainWindow.m_CNCommunication.GetAxesDeltas(dL1, dL2, dL3, dR1, dR2, dA6, dA7, dA8, dA9, dA10)
      ' Visualizzazione
      DeltaAxis1TxBx.Text = LenToString(dL1, -3)
      DeltaAxis2TxBx.Text = LenToString(dL2, -3)
      DeltaAxis3TxBx.Text = LenToString(dL3, -3)
      DeltaAxis4TxBx.Text = DoubleToString(dR1, -3)
      DeltaAxis5TxBx.Text = DoubleToString(dR2, -3)
      If m_AxesNumber >= 6 Then DeltaAxis6TxBx.Text = DoubleToString(dA6, -3)
      If m_AxesNumber >= 7 Then DeltaAxis7TxBx.Text = LenToString(dA7, -3)
      If m_AxesNumber >= 8 Then DeltaAxis8TxBx.Text = LenToString(dA8, -3)
      If m_AxesNumber >= 9 Then DeltaAxis9TxBx.Text = LenToString(dA9, -3)
      If m_AxesNumber >= 10 Then DeltaAxis10TxBx.Text = LenToString(dA10, -3)
   End Sub

   Sub DisplayFeed()
      Dim dRealFeed As Double = m_CN.d_DInterpo_Prog_Feed * m_CN.d_DInterpo_Feed_override / 100
      'Feed = DoubleToString(dRealFeed, 0)
      FeedTxBx.Text = LenToString(dRealFeed, 0)
      'FeedOverride = DoubleToString(m_CN.d_DInterpo_Feed_override, 0) & "%"
      Dim dFeedOverride As Double = m_CN.d_DInterpo_Feed_override
      FeedOverrideTxBx.Text = DoubleToString(dFeedOverride, 0) & "%"
   End Sub

   Sub DisplaySpeed()
      If m_MainWindow.m_CNCommunication.m_nNCType = 3 Then     'se controllo Siemens
         SpeedTxBx.Text = DoubleToString(m_CN.d_spindle_prog(0), 0)
      Else
         Dim dSpeed As Double = m_CN.d_spindle_eff(0)
         SpeedTxBx.Text = DoubleToString(dSpeed, 0)
      End If
      Dim dSpindleOverride As Integer = m_CN.n_spindle_override(0)
      SpeedOverrideTxBx.Text = DoubleToString(dSpindleOverride, 0) & "%"
   End Sub

   Sub DisplayPower()
      Dim dSpindlePower As Double
      Dim dSpindleOvr As Double
      ' se attivi nuovi comandi MDI su siemens
      If m_MainWindow.m_CNCommunication.m_nNCType = 3 AndAlso m_MainWindow.m_CNCommunication.m_CN.m_IsSiemensOne Then
         dSpindlePower = m_CN.d_data(0)
         dSpindleOvr = m_CN.d_data(1)
      Else
         dSpindlePower = m_CN.d_spindle_power
         dSpindleOvr = If(m_CN.bPowerOvr > 0, m_CN.d_spindle_power / m_CN.bPowerOvr * 100, 0)
      End If

      ConsumptionTxBx.Text = DoubleToString(dSpindlePower, -2)
      AOverrideTxBx.Text = DoubleToString(dSpindleOvr, 0) & "%"
      ' AOverrideLbl
   End Sub

   Sub DisplayActiveMode(Mode As Integer)
      Select Case Mode
      Case 0
         MachineStatusImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("\Resources\MachineStatusImage\Auto.png", UriKind.Relative))
      Case 1
         MachineStatusImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("\Resources\MachineStatusImage\Single.png", UriKind.Relative))
      Case 2
         MachineStatusImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("\Resources\MachineStatusImage\Mdi.png", UriKind.Relative))
      Case 7
         MachineStatusImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("\Resources\MachineStatusImage\Manual.png", UriKind.Relative))
      Case 8
         MachineStatusImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("\Resources\MachineStatusImage\Home.png", UriKind.Relative))
      End Select
   End Sub

   Sub DisplayVar()

      'For n As Short = 0 To 5 - 1

      '    lbl_var_values(n).Text = cn.d_Dvariable_values(n * 10).ToString

      'Next

   End Sub

End Class
