Public Class Side_Rectangle
    Private Shared buttonOffset As Integer
    Private Shared sideSquareValues(3) As Integer
    Private Shared numOfSideRectangles As Integer = 0

    ' --- Create textboxes when user clicks the rectangle side recess button ---
    Public Shared Sub SquareSideButtton(sender As Object, e As EventArgs)
        Individual.Panel_Recesses.Controls("Button_Angle_Corner").Enabled = False
        Individual.Panel_Recesses.Controls("Button_Angle_Side").Enabled = False
        Individual.Panel_Recesses.Controls("Button_Square_Corner").Enabled = False
        Individual.Panel_Recesses.Controls("Button_Square_Side").Enabled = False
        Individual.Panel_Recesses.Controls("Button_Square_Middle").Enabled = False

        Dim buttonX As Integer = Individual.Panel_Recesses.Controls("Button_Square_Side").Location.X + Individual.Panel_Recesses.Controls("Button_Square_Side").Width
        Dim buttonY As Integer = Individual.Panel_Recesses.Controls("Button_Square_Side").Location.Y

        Individual.Create_TextBox_Recesses("Y_TextBox", "", buttonX + 60, buttonY - 7, 60)
        AddHandler Individual.Panel_Recesses.Controls("Y_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        AddHandler Individual.Panel_Recesses.Controls("Y_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Create_Label_Recesses("Y_Label", "Width", buttonX + 18, buttonY - 4)

        Individual.Create_TextBox_Recesses("X_TextBox", "", buttonX + 60, buttonY + 23, 60)
        AddHandler Individual.Panel_Recesses.Controls("X_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        AddHandler Individual.Panel_Recesses.Controls("X_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Create_Label_Recesses("X_Label", "Length", buttonX + 10, buttonY + 26)

        Individual.Create_TextBox_Recesses("Off_TextBox", "", buttonX + 60, buttonY + 53, 60)
        AddHandler Individual.Panel_Recesses.Controls("Off_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        AddHandler Individual.Panel_Recesses.Controls("Off_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Create_Label_Recesses("Off_Label", "Offset", buttonX + 16, buttonY + 56)

        buttonOffset = 130
        Individual.Panel_Recesses.Controls("Button_Square_Middle").Left = Individual.Panel_Recesses.Controls("Button_Square_Middle").Left + buttonOffset
        Individual.Panel_Recesses.Controls("Button_Radius").Left = Individual.Panel_Recesses.Controls("Button_Radius").Left + buttonOffset

        Individual.Button_Square_Side.FlatStyle = FlatStyle.Flat
        Individual.Button_Square_Side.FlatAppearance.BorderColor = Color.Red

    End Sub

    ' --- Check the key pressed by the user ---
    Private Shared Sub FunctionText_KeyPress(sender As Object, e As KeyPressEventArgs)
        User_Input.Check_IfNumber(e)
    End Sub

    ' --- Adds clickable buttons for all sides if conditions are met ---
    Private Shared Sub FunctionTextSide_Changed(sender As TextBox, e As EventArgs)
        Try
            sideSquareValues(0) = CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text)
            sideSquareValues(1) = CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text)
            sideSquareValues(2) = CInt(Individual.Panel_Recesses.Controls("Off_TextBox").Text)
        Catch ex As Exception
            sideSquareValues(0) = 0
            sideSquareValues(1) = 0
            sideSquareValues(2) = 0
        End Try

        If sideSquareValues(0) > 0 AndAlso sideSquareValues(1) > 0 AndAlso sideSquareValues(2) > 0 Then
            If Individual.Panel_Grating.Controls.Count = 2 Then
                For i = 1 To 4
                    Dim sidePoints As New List(Of String)
                    If i = 1 Then
                        Create_SideButton(sideSquareValues(0) / (Data.scaleDiff), 30,
                                          Data.grossAreaPoints.Rows(0)("GUI X") + sideSquareValues(2) / (Data.scaleDiff),
                                          Data.grossAreaPoints.Rows(0)("GUI Y") - 30 / 2,
                                          "Button_" & i)
                    ElseIf i = 2 Then
                        Create_SideButton(30, sideSquareValues(1) / (Data.scaleDiff),
                                              Data.grossAreaPoints.Rows(1)("GUI X") - 30 / 2,
                                              Data.grossAreaPoints.Rows(1)("GUI Y") + sideSquareValues(2) / (Data.scaleDiff), "Button_" & i)
                    ElseIf i = 3 Then
                        Create_SideButton(sideSquareValues(0) / (Data.scaleDiff), 30,
                                              Data.grossAreaPoints.Rows(2)("GUI X") - sideSquareValues(2) / (Data.scaleDiff) - sideSquareValues(0) / (Data.scaleDiff),
                                              Data.grossAreaPoints.Rows(2)("GUI Y") - 30 / 2,
                                              "Button_" & i)
                    Else
                        Create_SideButton(30, sideSquareValues(1) / (Data.scaleDiff),
                                              Data.grossAreaPoints.Rows(3)("GUI X") - 30 / 2,
                                              Data.grossAreaPoints.Rows(3)("GUI Y") - sideSquareValues(2) / (Data.scaleDiff) - sideSquareValues(1) / (Data.scaleDiff),
                                              "Button_" & i)
                    End If
                Next
            Else
                ' Update button size and position
                For i = 2 To Individual.Panel_Grating.Controls.Count - 1
                    Dim buttonSide As Integer = CInt(Individual.Panel_Grating.Controls(i).Name.Split("_")(1))
                    If buttonSide = 1 Then
                        Individual.Panel_Grating.Controls(i).Width = sideSquareValues(0) / (Data.scaleDiff)
                        Individual.Panel_Grating.Controls(i).Left = Data.grossAreaPoints.Rows(0)("GUI X") + sideSquareValues(2) / (Data.scaleDiff)
                    ElseIf buttonSide = 2 Then
                        Individual.Panel_Grating.Controls(i).Height = sideSquareValues(1) / (Data.scaleDiff)
                        Individual.Panel_Grating.Controls(i).Top = Data.grossAreaPoints.Rows(1)("GUI Y") + sideSquareValues(2) / (Data.scaleDiff)
                    ElseIf buttonSide = 3 Then
                        Individual.Panel_Grating.Controls(i).Width = sideSquareValues(0) / (Data.scaleDiff)
                        Individual.Panel_Grating.Controls(i).Left = Data.grossAreaPoints.Rows(2)("GUI X") - sideSquareValues(2) / (Data.scaleDiff) - Individual.Panel_Grating.Controls(i).Width
                    Else
                        Individual.Panel_Grating.Controls(i).Height = sideSquareValues(1) / (Data.scaleDiff)
                        Individual.Panel_Grating.Controls(i).Top = Data.grossAreaPoints.Rows(3)("GUI Y") - sideSquareValues(2) / (Data.scaleDiff) - Individual.Panel_Grating.Controls(i).Height
                    End If

                Next

            End If

        ElseIf Individual.Panel_Grating.Controls.Count > 2 Then
            For i = 2 To Individual.Panel_Grating.Controls.Count - 1
                RemoveHandler Individual.Panel_Grating.Controls(2).Click, AddressOf SideButton_Click
                Individual.Panel_Grating.Controls.RemoveAt(2)
            Next
        End If
    End Sub

    ' --- Template to create corner buttons ---
    Private Shared Sub Create_SideButton(bWidth As Integer, bHeight As Integer, bLeft As Integer, bTop As Integer, bName As String)
        Dim sideButton As New Button With {
            .Width = bWidth,
            .Height = bHeight,
            .Left = bLeft,
            .Top = bTop,
            .Name = bName,
            .Text = ""
        }

        sideButton.BackColor = Color.FromArgb(50, Color.Red)
        sideButton.FlatStyle = FlatStyle.Flat
        sideButton.FlatAppearance.BorderSize = 0
        sideButton.FlatAppearance.MouseOverBackColor = Color.Red
        sideButton.FlatAppearance.MouseDownBackColor = Color.DarkRed

        Individual.Panel_Grating.Controls.Add(sideButton)

        AddHandler sideButton.Click, AddressOf SideButton_Click
    End Sub

    ' --- When a side function button is pressed ---
    Private Shared Sub SideButton_Click(sender As Button, e As EventArgs)
        'Determine which side is being pressed
        Dim sidePressed As Integer = CInt(sender.Name.Split("_")(1))

        ' Remove point buttons
        For i = 2 To Individual.Panel_Grating.Controls.Count - 1
            RemoveHandler Individual.Panel_Grating.Controls(2).Click, AddressOf SideButton_Click
            Individual.Panel_Grating.Controls.RemoveAt(2)
        Next

        Dim sidePoints As New List(Of Integer)
        Dim index As Integer

        If sidePressed = 1 Then
            For j = 0 To Data.gratingPoints.Rows.Count - 1
                If Data.gratingPoints.Rows(j)("GUI Y") = Data.grossAreaPoints.Rows(0)("GUI Y") Then
                    sidePoints.Add(j)
                End If
            Next
            index = sidePoints(0)
            If sidePoints.Count > 2 Then
                For i = sidePoints.Count / 2 To 2 Step -1
                    If Data.gratingPoints.Rows(sidePoints(i * 2 - 2))("GUI X") <
                        Data.grossAreaPoints.Rows(0)("GUI X") + sideSquareValues(2) / Data.scaleDiff Then
                        index = sidePoints(i * 2 - 2)
                        Exit For
                    End If
                Next
            End If

        ElseIf sidePressed = 2 Then
            For j = 1 To Data.gratingPoints.Rows.Count - 1
                If Data.gratingPoints.Rows(j)("GUI X") = Data.grossAreaPoints.Rows(1)("GUI X") Then
                    sidePoints.Add(j)
                End If
            Next
            index = sidePoints(0)
            If sidePoints.Count > 2 Then
                For i = sidePoints.Count / 2 To 2 Step -1
                    If Data.gratingPoints.Rows(sidePoints(i * 2 - 2))("GUI Y") <
                        Data.grossAreaPoints.Rows(1)("GUI Y") + sideSquareValues(2) / Data.scaleDiff Then
                        index = sidePoints(i * 2 - 2)
                        Exit For
                    End If
                Next
            End If

        ElseIf sidePressed = 3 Then
            For j = 2 To Data.gratingPoints.Rows.Count - 1
                If Data.gratingPoints.Rows(j)("GUI Y") = Data.grossAreaPoints.Rows(2)("GUI Y") Then
                    sidePoints.Add(j)
                End If
            Next
            index = sidePoints(0)
            If sidePoints.Count > 2 Then
                For i = sidePoints.Count / 2 To 2 Step -1
                    If Data.gratingPoints.Rows(sidePoints(i * 2 - 2))("GUI X") >
                        Data.grossAreaPoints.Rows(2)("GUI X") - sideSquareValues(2) / Data.scaleDiff Then
                        index = sidePoints(i * 2 - 2)
                        Exit For
                    End If
                Next
            End If

        Else
            For j = 3 To Data.gratingPoints.Rows.Count - 1
                If Data.gratingPoints.Rows(j)("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X") Then
                    sidePoints.Add(j)
                End If
            Next
            sidePoints.Add(0)
            index = sidePoints(0)
            If sidePoints.Count > 2 Then
                For i = sidePoints.Count / 2 To 2 Step -1
                    If Data.gratingPoints.Rows(sidePoints(i * 2 - 2))("GUI Y") >
                        Data.grossAreaPoints.Rows(3)("GUI Y") - sideSquareValues(2) / Data.scaleDiff Then
                        index = sidePoints(i * 2 - 2)
                        Exit For
                    End If
                Next
            End If
        End If

        For i = 2 To Data.pointsMeasurements.Count - 1
            Dim mesName As String = Data.pointsMeasurements.Keys(i)
            If mesName.Split("_")(0) = "SS1" AndAlso mesName.Split("_")(0) <> "MS" AndAlso mesName.Split("_")(0) <> "MS1" Then
                If index + 2 = Data.pointsMeasurements(mesName)(1) Then
                    If Draw_Grating.measureLabels(mesName)(3) = 1 Then
                        Draw_Grating.measureLabels(mesName)(0) = Data.gratingPoints.Rows(index + 1)("X") * 1000 -
                        Data.grossAreaPoints.Rows(0)("X") * 1000 - (sideSquareValues(2) + sideSquareValues(0))

                        Draw_Grating.sideSquareMesLine(mesName)(0) = Data.grossAreaPoints.Rows(0)("GUI X") + (sideSquareValues(2) + sideSquareValues(0)) / Data.scaleDiff

                        Draw_Grating.measureLabels(mesName)(1) = (Draw_Grating.sideSquareMesLine(mesName)(0) + Draw_Grating.sideSquareMesLine(mesName)(2)) / 2 - 8

                    ElseIf Draw_Grating.measureLabels(mesName)(3) = 2 Then
                        Draw_Grating.measureLabels(mesName)(0) = Data.grossAreaPoints.Rows(1)("Y") * 1000 - Data.gratingPoints.Rows(index + 1)("Y") * 1000 -
                         (sideSquareValues(2) + sideSquareValues(1))

                        Draw_Grating.sideSquareMesLine(mesName)(1) = Data.grossAreaPoints.Rows(1)("GUI Y") + (sideSquareValues(2) + sideSquareValues(1)) / Data.scaleDiff

                        Draw_Grating.measureLabels(mesName)(2) = (Draw_Grating.sideSquareMesLine(mesName)(1) + Draw_Grating.sideSquareMesLine(mesName)(3)) / 2 - 6

                    ElseIf Draw_Grating.measureLabels(mesName)(3) = 3 Then
                        Draw_Grating.measureLabels(mesName)(0) = Data.grossAreaPoints.Rows(2)("X") * 1000 - Data.gratingPoints.Rows(index + 1)("X") * 1000 -
                        (sideSquareValues(2) + sideSquareValues(0))

                        Draw_Grating.sideSquareMesLine(mesName)(0) = Data.grossAreaPoints.Rows(2)("GUI X") - (sideSquareValues(2) + sideSquareValues(0)) / Data.scaleDiff

                        Draw_Grating.measureLabels(mesName)(1) = (Draw_Grating.sideSquareMesLine(mesName)(0) + Draw_Grating.sideSquareMesLine(mesName)(2)) / 2 - 8

                    Else
                        Draw_Grating.measureLabels(mesName)(0) = Data.gratingPoints.Rows(index + 1)("Y") * 1000 -
                        Data.grossAreaPoints.Rows(3)("Y") * 1000 - (sideSquareValues(2) + sideSquareValues(1))

                        Draw_Grating.sideSquareMesLine(mesName)(1) = Data.grossAreaPoints.Rows(3)("GUI Y") - (sideSquareValues(2) + sideSquareValues(1)) / Data.scaleDiff

                        Draw_Grating.measureLabels(mesName)(2) = (Draw_Grating.sideSquareMesLine(mesName)(1) + Draw_Grating.sideSquareMesLine(mesName)(3)) / 2 - 6

                    End If
                End If
            End If
        Next

        For i = 2 To Data.pointsMeasurements.Count - 1
            Dim mesName As String = Data.pointsMeasurements.Keys(i)
            If mesName.Split("_")(0) <> "SS1" AndAlso index + 1 < Data.pointsMeasurements(mesName)(0) Then
                If mesName.Split("_")(0) <> "MS" AndAlso mesName.Split("_")(0) <> "MS1" Then
                    Data.pointsMeasurements(mesName)(0) = Data.pointsMeasurements(mesName)(0) + 4
                    Data.pointsMeasurements(mesName)(1) = Data.pointsMeasurements(mesName)(1) + 4
                ElseIf mesName.Split("_")(0) = "MS1" Then
                    Data.pointsMeasurements(mesName)(0) = Data.pointsMeasurements(mesName)(0) + 4
                End If
            ElseIf mesName.Split("_")(0) = "SS1" Then
                If index + 2 <= Data.pointsMeasurements(mesName)(0) Then
                    If mesName.Split("_")(0) <> "MS" AndAlso mesName.Split("_")(0) <> "MS1" Then
                        Data.pointsMeasurements(mesName)(0) = Data.pointsMeasurements(mesName)(0) + 4
                        Data.pointsMeasurements(mesName)(1) = Data.pointsMeasurements(mesName)(1) + 4
                    ElseIf mesName.Split("_")(0) = "MS1" Then
                        Data.pointsMeasurements(mesName)(0) = Data.pointsMeasurements(mesName)(0) + 4
                    End If
                ElseIf index + 2 <= Data.pointsMeasurements(mesName)(1) Then
                    If mesName.Split("_")(0) <> "MS" AndAlso mesName.Split("_")(0) <> "MS1" Then
                        Data.pointsMeasurements(mesName)(1) = Data.pointsMeasurements(mesName)(1) + 4
                    End If
                End If
            End If
        Next

        Dim mesNum As Integer = Data.pointsMeasurements.Count - 1

        Data.recessData.Rows.Add()
        Data.recessData.Rows(Data.recessData.Rows.Count - 1)("NAME") = "CS" & numOfSideRectangles

        Dim pointRow1 As DataRow = Data.gratingPoints.NewRow
        Dim pointRow2 As DataRow = Data.gratingPoints.NewRow
        Dim pointRow3 As DataRow = Data.gratingPoints.NewRow
        Dim pointRow4 As DataRow = Data.gratingPoints.NewRow

        pointRow1("NAME") = "CS" & numOfSideRectangles & "_" & 1
        pointRow1("QUADRANT") = 1
        pointRow1("RECESS OK") = False

        pointRow2("NAME") = "CS" & numOfSideRectangles & "_" & 2
        pointRow2("QUADRANT") = 1
        pointRow2("RECESS OK") = False

        pointRow3("NAME") = "CS" & numOfSideRectangles & "_" & 3
        pointRow3("QUADRANT") = 1
        pointRow3("RECESS OK") = False

        pointRow4("NAME") = "CS" & numOfSideRectangles & "_" & 4
        pointRow4("QUADRANT") = 1
        pointRow4("RECESS OK") = False
        If sidePressed = 1 Then
            ' Add the new points
            pointRow1("X") = Data.grossAreaPoints.Rows(0)("X") + sideSquareValues(2) / 1000
            pointRow1("Y") = Data.grossAreaPoints.Rows(0)("Y")
            pointRow1("GUI X") = Data.grossAreaPoints.Rows(0)("GUI X") + sideSquareValues(2) / Data.scaleDiff
            pointRow1("GUI Y") = Data.grossAreaPoints.Rows(0)("GUI Y")

            pointRow2("X") = Data.grossAreaPoints.Rows(0)("X") + sideSquareValues(2) / 1000
            pointRow2("Y") = Data.grossAreaPoints.Rows(0)("Y") - sideSquareValues(1) / 1000
            pointRow2("GUI X") = Data.grossAreaPoints.Rows(0)("GUI X") + sideSquareValues(2) / Data.scaleDiff
            pointRow2("GUI Y") = Data.grossAreaPoints.Rows(0)("GUI Y") + sideSquareValues(1) / Data.scaleDiff

            pointRow3("X") = Data.grossAreaPoints.Rows(0)("X") + (sideSquareValues(2) + sideSquareValues(0)) / 1000
            pointRow3("Y") = Data.grossAreaPoints.Rows(0)("Y") - sideSquareValues(1) / 1000
            pointRow3("GUI X") = Data.grossAreaPoints.Rows(0)("GUI X") + (sideSquareValues(2) + sideSquareValues(0)) / Data.scaleDiff
            pointRow3("GUI Y") = Data.grossAreaPoints.Rows(0)("GUI Y") + sideSquareValues(1) / Data.scaleDiff

            pointRow4("X") = Data.grossAreaPoints.Rows(0)("X") + (sideSquareValues(2) + sideSquareValues(0)) / 1000
            pointRow4("Y") = Data.grossAreaPoints.Rows(0)("Y")
            pointRow4("GUI X") = Data.grossAreaPoints.Rows(0)("GUI X") + (sideSquareValues(2) + sideSquareValues(0)) / Data.scaleDiff
            pointRow4("GUI Y") = Data.grossAreaPoints.Rows(0)("GUI Y")

            ' Update outer measuements (SW)
            Data.pointsMeasurements("Lmes")(1) = Data.pointsMeasurements("Lmes")(1) + 4
            Data.pointsMeasurements("Wmes")(0) = Data.pointsMeasurements("Wmes")(0) + 4
            Data.pointsMeasurements("Wmes")(1) = Data.pointsMeasurements("Wmes")(1) + 4

            ' Add recess measuements (SW)
            Data.pointsMeasurements.Add("SS1_" & mesNum, {sidePoints(0) + 1, index + 2, 1})
            Data.pointsMeasurements.Add("SS_" & mesNum + 1, {index + 2, index + 5, 1})
            Data.pointsMeasurements.Add("SS_" & mesNum + 2, {index + 2, index + 3, 4})

            ' Add support line data for sidesquare measurement
            Draw_Grating.sideSquareMesLine.Add("SS1_" & mesNum, {Data.gratingPoints.Rows(index)("GUI X"),
                                                    Data.grossAreaPoints.Rows(0)("GUI Y") - 15,
                                                    Data.grossAreaPoints.Rows(0)("GUI X") + sideSquareValues(2) / Data.scaleDiff,
                                                    Data.grossAreaPoints.Rows(0)("GUI Y") - 15})

            ' Add recess measurments (GUI)
            Draw_Grating.measureLabels.Add("SS1_" & mesNum, {CInt(Individual.Panel_Recesses.Controls("Off_TextBox").Text) - (Data.gratingPoints.Rows(index)("X") * 1000 + Data.gratingL / 2),
                                                (Data.gratingPoints.Rows(index)("GUI X") + Data.grossAreaPoints.Rows(0)("GUI X") + sideSquareValues(2) / Data.scaleDiff) / 2 - 8,
                                                Data.grossAreaPoints.Rows(0)("GUI Y") - 15 - 12 - 3,
                                                1}) '15 är linjen, 12 är textens höjd, 3 är lite extra
            Draw_Grating.measureLabels.Add("SS_" & mesNum + 1, {CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text),
                                                Data.grossAreaPoints.Rows(0)("GUI X") + sideSquareValues(2) / (Data.scaleDiff) + CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text) / (2 * Data.scaleDiff) - 8,
                                                Data.grossAreaPoints.Rows(0)("GUI Y") + sideSquareValues(1) / (Data.scaleDiff) + 3,
                                                1})
            Draw_Grating.measureLabels.Add("SS_" & mesNum + 2, {CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text),
                                                Data.grossAreaPoints.Rows(0)("GUI X") + sideSquareValues(2) / Data.scaleDiff - 18 - 3,
                                                Data.grossAreaPoints.Rows(0)("GUI Y") + CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text) / (2 * Data.scaleDiff) - 4,
                                                1})

            ' Recess Data
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("RECESS TYPE") = "SIDE SQUARE"
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("SIDE") = 1
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("WIDTH") = CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text)
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("LENGTH") = CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text)
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("OFFSET") = CInt(Individual.Panel_Recesses.Controls("Off_TextBox").Text)

        ElseIf sidePressed = 2 Then
            ' Add the new points
            pointRow1("X") = Data.grossAreaPoints.Rows(1)("X")
            pointRow1("Y") = Data.grossAreaPoints.Rows(1)("Y") - sideSquareValues(2) / 1000
            pointRow1("GUI X") = Data.grossAreaPoints.Rows(1)("GUI X")
            pointRow1("GUI Y") = Data.grossAreaPoints.Rows(1)("GUI Y") + sideSquareValues(2) / Data.scaleDiff

            pointRow2("X") = Data.grossAreaPoints.Rows(1)("X") - sideSquareValues(0) / 1000
            pointRow2("Y") = Data.grossAreaPoints.Rows(1)("Y") - sideSquareValues(2) / 1000
            pointRow2("GUI X") = Data.grossAreaPoints.Rows(1)("GUI X") - sideSquareValues(0) / Data.scaleDiff
            pointRow2("GUI Y") = Data.grossAreaPoints.Rows(1)("GUI Y") + sideSquareValues(2) / Data.scaleDiff

            pointRow3("X") = Data.grossAreaPoints.Rows(1)("X") - sideSquareValues(0) / 1000
            pointRow3("Y") = Data.grossAreaPoints.Rows(1)("Y") - (sideSquareValues(2) + sideSquareValues(1)) / 1000
            pointRow3("GUI X") = Data.grossAreaPoints.Rows(1)("GUI X") - sideSquareValues(0) / Data.scaleDiff
            pointRow3("GUI Y") = Data.grossAreaPoints.Rows(1)("GUI Y") + (sideSquareValues(2) + sideSquareValues(1)) / Data.scaleDiff

            pointRow4("X") = Data.grossAreaPoints.Rows(1)("X")
            pointRow4("Y") = Data.grossAreaPoints.Rows(1)("Y") - (sideSquareValues(2) + sideSquareValues(1)) / 1000
            pointRow4("GUI X") = Data.grossAreaPoints.Rows(1)("GUI X")
            pointRow4("GUI Y") = Data.grossAreaPoints.Rows(1)("GUI Y") + (sideSquareValues(2) + sideSquareValues(1)) / Data.scaleDiff

            ' Update outer measuements (SW)
            Data.pointsMeasurements("Wmes")(1) = Data.pointsMeasurements("Wmes")(1) + 4

            ' Add recess measuements (SW)
            Data.pointsMeasurements.Add("SS1_" & mesNum, {sidePoints(0) + 1, index + 2, 2})
            Data.pointsMeasurements.Add("SS_" & mesNum + 1, {index + 2, index + 5, 2})
            Data.pointsMeasurements.Add("SS_" & mesNum + 2, {index + 2, index + 3, 3})

            ' Add support line data for sidesquare measurement
            Draw_Grating.sideSquareMesLine.Add("SS1_" & mesNum, {Data.grossAreaPoints.Rows(1)("GUI X") + 15,
                                                    Data.gratingPoints.Rows(index)("GUI Y"),
                                                    Data.grossAreaPoints.Rows(1)("GUI X") + 15,
                                                    Data.grossAreaPoints.Rows(1)("GUI Y") + sideSquareValues(2) / Data.scaleDiff})

            ' Add recess measurments (GUI)
            Draw_Grating.measureLabels.Add("SS1_" & mesNum, {CInt(Individual.Panel_Recesses.Controls("Off_TextBox").Text) - (Data.gratingW / 2 - Data.gratingPoints.Rows(index)("Y") * 1000),
                                                Data.grossAreaPoints.Rows(1)("GUI X") + 15 + 3,
                                                (Data.gratingPoints.Rows(index)("GUI Y") + Data.grossAreaPoints.Rows(1)("GUI Y") + sideSquareValues(2) / Data.scaleDiff) / 2 - 4,
                                                2})

            Draw_Grating.measureLabels.Add("SS_" & mesNum + 1, {CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text),
                                                Data.grossAreaPoints.Rows(1)("GUI X") - sideSquareValues(0) / Data.scaleDiff - 18 - 3,
                                                Data.grossAreaPoints.Rows(1)("GUI Y") + (sideSquareValues(2) + sideSquareValues(1) / 2) / Data.scaleDiff - 4,
                                                2})

            Draw_Grating.measureLabels.Add("SS_" & mesNum + 2, {CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text),
                                                Data.grossAreaPoints.Rows(1)("GUI X") - sideSquareValues(0) / (2 * Data.scaleDiff) - 8,
                                                Data.grossAreaPoints.Rows(1)("GUI Y") + sideSquareValues(2) / Data.scaleDiff - 15 - 3,
                                                2})

            ' Recess Data
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("RECESS TYPE") = "SIDE SQUARE"
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("SIDE") = 2
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("WIDTH") = CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text)
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("LENGTH") = CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text)
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("OFFSET") = CInt(Individual.Panel_Recesses.Controls("Off_TextBox").Text)

        ElseIf sidePressed = 3 Then
            ' Add the new points
            pointRow1("X") = Data.grossAreaPoints.Rows(2)("X") - sideSquareValues(2) / 1000
            pointRow1("Y") = Data.grossAreaPoints.Rows(2)("Y")
            pointRow1("GUI X") = Data.grossAreaPoints.Rows(2)("GUI X") - sideSquareValues(2) / Data.scaleDiff
            pointRow1("GUI Y") = Data.grossAreaPoints.Rows(2)("GUI Y")

            pointRow2("X") = Data.grossAreaPoints.Rows(2)("X") - sideSquareValues(2) / 1000
            pointRow2("Y") = Data.grossAreaPoints.Rows(2)("Y") + sideSquareValues(1) / 1000
            pointRow2("GUI X") = Data.grossAreaPoints.Rows(2)("GUI X") - sideSquareValues(2) / Data.scaleDiff
            pointRow2("GUI Y") = Data.grossAreaPoints.Rows(2)("GUI Y") - sideSquareValues(1) / Data.scaleDiff

            pointRow3("X") = Data.grossAreaPoints.Rows(2)("X") - (sideSquareValues(2) + sideSquareValues(0)) / 1000
            pointRow3("Y") = Data.grossAreaPoints.Rows(2)("Y") + sideSquareValues(1) / 1000
            pointRow3("GUI X") = Data.grossAreaPoints.Rows(2)("GUI X") - (sideSquareValues(2) + sideSquareValues(0)) / Data.scaleDiff
            pointRow3("GUI Y") = Data.grossAreaPoints.Rows(2)("GUI Y") - sideSquareValues(1) / Data.scaleDiff

            pointRow4("X") = Data.grossAreaPoints.Rows(2)("X") - (sideSquareValues(2) + sideSquareValues(0)) / 1000
            pointRow4("Y") = Data.grossAreaPoints.Rows(2)("Y")
            pointRow4("GUI X") = Data.grossAreaPoints.Rows(2)("GUI X") - (sideSquareValues(2) + sideSquareValues(0)) / Data.scaleDiff
            pointRow4("GUI Y") = Data.grossAreaPoints.Rows(2)("GUI Y")

            ' Add recess measuements (SW)
            Data.pointsMeasurements.Add("SS1_" & mesNum, {sidePoints(0) + 1, index + 2, 3})
            Data.pointsMeasurements.Add("SS_" & mesNum + 1, {index + 2, index + 5, 3})
            Data.pointsMeasurements.Add("SS_" & mesNum + 2, {index + 2, index + 3, 4})

            ' Add support line data for sidesquare measurement
            Draw_Grating.sideSquareMesLine.Add("SS1_" & mesNum, {Data.gratingPoints.Rows(index)("GUI X"),
                                                    Data.grossAreaPoints.Rows(2)("GUI Y") + 15,
                                                    Data.grossAreaPoints.Rows(2)("GUI X") - sideSquareValues(2) / Data.scaleDiff,
                                                    Data.grossAreaPoints.Rows(2)("GUI Y") + 15})

            ' Add recess measurments (GUI)
            Draw_Grating.measureLabels.Add("SS1_" & mesNum, {CInt(Individual.Panel_Recesses.Controls("Off_TextBox").Text) - (Data.gratingL / 2 - Data.gratingPoints.Rows(index)("X") * 1000),
                                                (Data.gratingPoints.Rows(index)("GUI X") + Data.grossAreaPoints.Rows(2)("GUI X") - sideSquareValues(2) / Data.scaleDiff) / 2 - 8,
                                                Data.grossAreaPoints.Rows(2)("GUI Y") + 15 + 3,
                                                3}) '15 är linjen, 12 är textens höjd, 3 är lite extra
            Draw_Grating.measureLabels.Add("SS_" & mesNum + 1, {CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text),
                                                Data.grossAreaPoints.Rows(2)("GUI X") - sideSquareValues(2) / (Data.scaleDiff) - CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text) / (2 * Data.scaleDiff) - 8,
                                                Data.grossAreaPoints.Rows(2)("GUI Y") - sideSquareValues(1) / (Data.scaleDiff) - 12 - 3,
                                                3})
            Draw_Grating.measureLabels.Add("SS_" & mesNum + 2, {CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text),
                                                Data.grossAreaPoints.Rows(2)("GUI X") - sideSquareValues(2) / Data.scaleDiff + 3,
                                                Data.grossAreaPoints.Rows(2)("GUI Y") - CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text) / (2 * Data.scaleDiff) - 4,
                                                3})

            ' Recess Data
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("RECESS TYPE") = "SIDE SQUARE"
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("SIDE") = 3
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("WIDTH") = CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text)
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("LENGTH") = CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text)
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("OFFSET") = CInt(Individual.Panel_Recesses.Controls("Off_TextBox").Text)

        Else
            ' Add the new points
            pointRow1("X") = Data.grossAreaPoints.Rows(3)("X")
            pointRow1("Y") = Data.grossAreaPoints.Rows(3)("Y") + sideSquareValues(2) / 1000
            pointRow1("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X")
            pointRow1("GUI Y") = Data.grossAreaPoints.Rows(3)("GUI Y") - sideSquareValues(2) / Data.scaleDiff

            pointRow2("X") = Data.grossAreaPoints.Rows(3)("X") + sideSquareValues(0) / 1000
            pointRow2("Y") = Data.grossAreaPoints.Rows(3)("Y") + sideSquareValues(2) / 1000
            pointRow2("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X") + sideSquareValues(0) / Data.scaleDiff
            pointRow2("GUI Y") = Data.grossAreaPoints.Rows(3)("GUI Y") - sideSquareValues(2) / Data.scaleDiff

            pointRow3("X") = Data.grossAreaPoints.Rows(3)("X") + sideSquareValues(0) / 1000
            pointRow3("Y") = Data.grossAreaPoints.Rows(3)("Y") + (sideSquareValues(2) + sideSquareValues(1)) / 1000
            pointRow3("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X") + sideSquareValues(0) / Data.scaleDiff
            pointRow3("GUI Y") = Data.grossAreaPoints.Rows(3)("GUI Y") - (sideSquareValues(2) + sideSquareValues(1)) / Data.scaleDiff

            pointRow4("X") = Data.grossAreaPoints.Rows(3)("X")
            pointRow4("Y") = Data.grossAreaPoints.Rows(3)("Y") + (sideSquareValues(2) + sideSquareValues(1)) / 1000
            pointRow4("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X")
            pointRow4("GUI Y") = Data.grossAreaPoints.Rows(3)("GUI Y") - (sideSquareValues(2) + sideSquareValues(1)) / Data.scaleDiff

            ' Add recess measuements (SW)
            Data.pointsMeasurements.Add("SS1_" & mesNum, {sidePoints(0) + 1, index + 2, 4})
            Data.pointsMeasurements.Add("SS_" & mesNum + 1, {index + 2, index + 5, 4})
            Data.pointsMeasurements.Add("SS_" & mesNum + 2, {index + 2, index + 3, 3})

            ' Add support line data for sidesquare measurement
            Draw_Grating.sideSquareMesLine.Add("SS1_" & mesNum, {Data.grossAreaPoints.Rows(3)("GUI X") - 15,
                                                    Data.gratingPoints.Rows(index)("GUI Y"),
                                                    Data.grossAreaPoints.Rows(3)("GUI X") - 15,
                                                    Data.grossAreaPoints.Rows(3)("GUI Y") - sideSquareValues(2) / Data.scaleDiff})

            ' Add recess measurments (GUI)
            Draw_Grating.measureLabels.Add("SS1_" & mesNum, {CInt(Individual.Panel_Recesses.Controls("Off_TextBox").Text) - (Data.gratingW / 2 + Data.gratingPoints.Rows(index)("Y") * 1000),
                                                Data.grossAreaPoints.Rows(3)("GUI X") - 15 - 18 - 3,
                                                (Data.gratingPoints.Rows(index)("GUI Y") + Data.grossAreaPoints.Rows(3)("GUI Y") - sideSquareValues(2) / Data.scaleDiff) / 2 - 4,
                                                4})

            Draw_Grating.measureLabels.Add("SS_" & mesNum + 1, {CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text),
                                                Data.grossAreaPoints.Rows(3)("GUI X") + sideSquareValues(0) / Data.scaleDiff + 3,
                                                Data.grossAreaPoints.Rows(3)("GUI Y") - (sideSquareValues(2) + sideSquareValues(1) / 2) / Data.scaleDiff - 4,
                                                4})

            Draw_Grating.measureLabels.Add("SS_" & mesNum + 2, {CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text),
                                                Data.grossAreaPoints.Rows(3)("GUI X") + sideSquareValues(0) / (2 * Data.scaleDiff) - 8,
                                                Data.grossAreaPoints.Rows(3)("GUI Y") - sideSquareValues(2) / Data.scaleDiff + 3,
                                                4})

            ' Recess Data
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("RECESS TYPE") = "SIDE SQUARE"
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("SIDE") = 4
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("WIDTH") = CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text)
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("LENGTH") = CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text)
            Data.recessData.Rows(Data.recessData.Rows.Count - 1)("OFFSET") = CInt(Individual.Panel_Recesses.Controls("Off_TextBox").Text)
        End If

        Data.gratingPoints.Rows.InsertAt(pointRow1, index + 1)
        Data.gratingPoints.Rows.InsertAt(pointRow2, index + 2)
        Data.gratingPoints.Rows.InsertAt(pointRow3, index + 3)
        Data.gratingPoints.Rows.InsertAt(pointRow4, index + 4)

        numOfSideRectangles += 1

        'Redraw grating
        Individual.Panel_Grating.Refresh()

        RemoveHandler Individual.Panel_Recesses.Controls("X_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        RemoveHandler Individual.Panel_Recesses.Controls("X_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Panel_Recesses.Controls.RemoveByKey("X_TextBox")

        RemoveHandler Individual.Panel_Recesses.Controls("Y_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        RemoveHandler Individual.Panel_Recesses.Controls("Y_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Panel_Recesses.Controls.RemoveByKey("Y_TextBox")

        RemoveHandler Individual.Panel_Recesses.Controls("Off_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        RemoveHandler Individual.Panel_Recesses.Controls("Off_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Panel_Recesses.Controls.RemoveByKey("Off_TextBox")

        Individual.Panel_Recesses.Controls.RemoveByKey("X_Label")
        Individual.Panel_Recesses.Controls.RemoveByKey("Y_Label")
        Individual.Panel_Recesses.Controls.RemoveByKey("Off_Label")

        Individual.Panel_Recesses.Controls("Button_Angle_Corner").Enabled = True
        Individual.Panel_Recesses.Controls("Button_Angle_Side").Enabled = True
        Individual.Panel_Recesses.Controls("Button_Square_Corner").Enabled = True
        Individual.Panel_Recesses.Controls("Button_Square_Side").Enabled = True
        Individual.Panel_Recesses.Controls("Button_Square_Middle").Enabled = True


        Individual.Panel_Recesses.Controls("Button_Square_Middle").Left = Individual.Panel_Recesses.Controls("Button_Square_Middle").Left - buttonOffset
        Individual.Panel_Recesses.Controls("Button_Radius").Left = Individual.Panel_Recesses.Controls("Button_Radius").Left - buttonOffset

        Individual.Button_Square_Side.FlatAppearance.BorderColor = Color.Black

    End Sub
End Class
