private void CheckTextCamera2(HObject logo1Region, HObject logo2Region)
        {
            try
            {
                HOperatorSet.RotateImage(hoImageCamera2Rotated, out HObject hImageRotated, -90, "constant");

                HalconProcedures.CheckText(hImageRotated,
                                           selectedProgram.ColorCamera2,
                                           selectedProgram.NumberOfLogosCamera2,
                                           logo1Region,
                                           logo2Region,
                                           out HObject textLines,
                                           out HTuple singleCharacters);

                hWindowControlCamera2.HalconWindow.SetColor("blue");
                hWindowControlCamera2.HalconWindow.DispObj(textLines);

                HOperatorSet.SmallestRectangle1(textLines, out HTuple row1, out HTuple column1, out HTuple row2, out HTuple column2);

                hWindowControlCamera2.HalconWindow.SetColor("green");
                hWindowControlCamera2.HalconWindow.SetFont("Arial-Bold-18");

                for (int i = 0; i < singleCharacters.Length; i++)
                {
                    hWindowControlCamera2.HalconWindow.SetTposition(row2[i] + 20, column1[i]);
                    hWindowControlCamera2.HalconWindow.WriteString(singleCharacters[i].S);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
        private void CheckColorCamera2()
        {
            try
            {
                HOperatorSet.RotateImage(hoImageCamera2Rotated, out HObject hImageRotated, -90, "constant");

                string colorDetected = HalconProcedures.CheckColor(hImageRotated);

                if (colorDetected == string.Empty)
                {
                    Console.WriteLine("No color detected");
                    return;
                }

                if (colorDetected == selectedProgram.ColorCamera2)
                {
                    Console.WriteLine("Camera 2 color OK");
                }
                else
                {
                    Console.WriteLine("Camera 2 color NOK");
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("HALCON error #4056"))
                {
                    MessageBox.Show("No image camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void CheckShapesCamera1(out HObject shapesRegion, out HTuple checkedShapes)
        {
            HOperatorSet.GenEmptyObj(out shapesRegion);

            checkedShapes = new HTuple();

            try
            {
                HOperatorSet.RotateImage(hoImageCamera1Rotated, out HObject hImageRotated, -90, "constant");

                HalconProcedures.CheckShapes(hImageRotated,
                                             selectedProgram.ColorCamera1,
                                             selectedProgram.ShapeTypeCamera1,
                                             out HObject selectedShapes,
                                             out HTuple checkShapes);

                shapesRegion  = selectedShapes;
                checkedShapes = checkShapes;

                hWindowControlCamera1.HalconWindow.SetColor("yellow");
                hWindowControlCamera1.HalconWindow.DispObj(selectedShapes);

                HOperatorSet.CountObj(selectedShapes, out HTuple numberOfShapes);

                hWindowControlCamera1.HalconWindow.SetColor("coral");
                hWindowControlCamera1.HalconWindow.SetFont("Arial-Bold-18");

                for (int i = 1; i <= numberOfShapes; i++)
                {
                    HOperatorSet.SelectObj(selectedShapes, out HObject selectedShape, i);
                    HOperatorSet.SmallestRectangle1(selectedShape, out HTuple row1, out HTuple column1, out HTuple row2, out HTuple column2);

                    hWindowControlCamera1.HalconWindow.SetTposition(row2 + 20, column1 + 30);

                    if (checkedShapes[i - 1] == 1)
                    {
                        hWindowControlCamera1.HalconWindow.WriteString("OK");
                    }
                    else
                    {
                        hWindowControlCamera1.HalconWindow.WriteString("NOK");
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("HALCON error #4056"))
                {
                    MessageBox.Show("No image camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
        private void ButtonDetectColorCamera2Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.RotateImage(hoImageCamera2Rotated, out HObject hImageRotated, -90, "constant");

                string colorDetected = HalconProcedures.CheckColor(hImageRotated);

                comboBoxColorsCamera2.SelectedIndex = comboBoxColorsCamera2.FindStringExact(colorDetected.ToUpper());
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("HALCON error #4056"))
                {
                    MessageBox.Show("No image camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void CheckTextCamera1(HObject logo1Region, HObject logo2Region)
        {
            try
            {
                HOperatorSet.RotateImage(hoImageCamera1Rotated, out HObject hImageRotated, -90, "constant");

                HalconProcedures.CheckText(hImageRotated,
                                           selectedProgram.ColorCamera1,
                                           selectedProgram.NumberOfLogosCamera1,
                                           logo1Region,
                                           logo2Region,
                                           out HObject textLines,
                                           out HTuple singleCharacters);

                hWindowControlCamera1.HalconWindow.SetColor("blue");
                hWindowControlCamera1.HalconWindow.DispObj(textLines);

                HOperatorSet.SmallestRectangle1(textLines, out HTuple row1, out HTuple column1, out HTuple row2, out HTuple column2);

                hWindowControlCamera1.HalconWindow.SetColor("green");
                hWindowControlCamera1.HalconWindow.SetFont("Arial-Bold-18");

                for (int i = 0; i < singleCharacters.Length; i++)
                {
                    hWindowControlCamera1.HalconWindow.SetTposition(row2[i] + 20, column1[i]);
                    hWindowControlCamera1.HalconWindow.WriteString(singleCharacters[i].S);
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("HALCON error #4056"))
                {
                    MessageBox.Show("No image camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
        private void CheckColorCamera1()
        {
            try
            {
                HOperatorSet.RotateImage(hoImageCamera1Rotated, out HObject hImageRotated, -90, "constant");

                string colorDetected = HalconProcedures.CheckColor(hImageRotated);

                hWindowControlCamera1.HalconWindow.SetFont("Arial-Bold-30");

                if (colorDetected == string.Empty)
                {
                    hWindowControlCamera1.HalconWindow.DispText("NO COLOR DETECTED", "image", 20, 20, "black", new HTuple(), new HTuple());
                    Console.WriteLine("No color detected");
                    return;
                }

                if (colorDetected == selectedProgram.ColorCamera1)
                {
                    hWindowControlCamera1.HalconWindow.DispText("COLOR OK", "image", 20, 20, "green", new HTuple(), new HTuple());
                    Console.WriteLine("Camera 1 color OK");
                }
                else
                {
                    hWindowControlCamera1.HalconWindow.DispText("COLOR NOK", "image", 20, 20, "red", new HTuple(), new HTuple());

                    Console.WriteLine("Camera 1 color NOK");
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("HALCON error #4056"))
                {
                    MessageBox.Show("No image camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void CheckLogosCamera2(out HObject logo1RegionCheckLogos, out HObject logo2RegionCheckLogos)
        {
            HOperatorSet.GenEmptyObj(out logo1RegionCheckLogos);
            HOperatorSet.GenEmptyObj(out logo2RegionCheckLogos);

            try
            {
                HOperatorSet.RotateImage(hoImageCamera2Rotated, out HObject hImageRotated, -90, "constant");

                HalconProcedures.CheckLogos(hImageRotated,
                                            selectedProgram.NumberOfLogosCamera2,
                                            selectedProgram.ColorCamera2,
                                            selectedProgram.Logo1Camera2SaveLocation.Replace(' ', '\\'),
                                            selectedProgram.Logo2Camera2SaveLocation.Replace(' ', '\\'),
                                            out HObject logo1Region,
                                            out HObject logo2Region,
                                            out HObject logo1Model,
                                            out HObject logo2Model,
                                            out double logo1Confidence,
                                            out double logo2Confidence);

                logo1RegionCheckLogos = logo1Region;
                logo2RegionCheckLogos = logo2Region;

                hWindowControlCamera2.HalconWindow.SetColor("red");
                hWindowControlCamera2.HalconWindow.SetLineWidth(2);

                if (selectedProgram.NumberOfLogosCamera2 == 1)
                {
                    HOperatorSet.AreaCenter(logo1Region, out HTuple areaLogo1, out HTuple rowLogo1, out HTuple columnLogo1);

                    hWindowControlCamera2.HalconWindow.DispObj(logo1Model);

                    HOperatorSet.SmallestRectangle1(logo1Region, out HTuple row1Logo1, out HTuple column1Logo1, out HTuple row2Logo1, out HTuple column2Logo1);

                    hWindowControlCamera2.HalconWindow.SetTposition(row2Logo1 + 20, column1Logo1);
                    hWindowControlCamera2.HalconWindow.WriteString(logo1Confidence);
                }
                else if (selectedProgram.NumberOfLogosCamera2 == 2)
                {
                    HOperatorSet.AreaCenter(logo1Region, out HTuple areaLogo1, out HTuple rowLogo1, out HTuple columnLogo1);
                    HOperatorSet.AreaCenter(logo2Region, out HTuple areaLogo2, out HTuple rowLogo2, out HTuple columnLogo2);

                    hWindowControlCamera2.HalconWindow.DispObj(logo1Model);
                    hWindowControlCamera2.HalconWindow.DispObj(logo2Model);

                    HOperatorSet.SmallestRectangle1(logo1Region, out HTuple row1Logo1, out HTuple column1Logo1, out HTuple row2Logo1, out HTuple column2Logo1);

                    hWindowControlCamera2.HalconWindow.SetTposition(row2Logo1 + 50, column1Logo1);
                    hWindowControlCamera2.HalconWindow.WriteString(logo1Confidence);

                    HOperatorSet.SmallestRectangle1(logo2Region, out HTuple row1Logo2, out HTuple column1Logo2, out HTuple row2Logo2, out HTuple column2Logo2);

                    hWindowControlCamera2.HalconWindow.SetTposition(row2Logo2 + 50, column1Logo2);
                    hWindowControlCamera2.HalconWindow.WriteString(logo2Confidence);
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("HALCON error #4056"))
                {
                    MessageBox.Show("No image camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void ButtonOkClick(object sender, EventArgs e)
        {
            ProgramEntry programToCheck;

            string shapeTypeCamera1 = string.Empty;
            string shapeTypeCamera2 = string.Empty;

            if (comboBoxPrograms.SelectedIndex != -1)
            {
                programToCheck = SqlCommunication.CheckProgram(comboBoxPrograms.SelectedItem.ToString());
            }
            else
            {
                MessageBox.Show("No program selected", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (startWastingOffset == 0 || startWastingOffset == 0 || wasteOffset == 0 || logo1Tolerance == 0 || logo2Tolerance == 0 || machine == string.Empty)
            {
                MessageBox.Show("Product configuration values not set", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!Directory.Exists(Directory.GetCurrentDirectory() + $"\\Programs\\{comboBoxPrograms.SelectedItem}"))
            {
                Directory.CreateDirectory(Directory.GetCurrentDirectory() + $"\\Programs\\{comboBoxPrograms.SelectedItem}");
            }

            if (!checkBoxCamera1.Checked && !checkBoxCamera2.Checked)
            {
                MessageBox.Show("At least one camera must be activated", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (checkBoxCamera1.Checked)
            {
                if (comboBoxColorsCamera1.SelectedIndex == -1)
                {
                    MessageBox.Show("No color selected for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (checkBoxLogoCamera1.Checked)
                {
                    if (numericUpDownLogosCamera1.Value == 0)
                    {
                        MessageBox.Show("Camera 1 logos activated but no logo set", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else if (numericUpDownLogosCamera1.Value == 1)
                    {
                        if (logo1Camera1RegionCoords[0] == 0 && logo1Camera1RegionCoords[1] == 0 && logo1Camera1RegionCoords[2] == 0 && logo1Camera1RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 1 region not set for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else if (numericUpDownLogosCamera1.Value == 2)
                    {
                        if (logo1Camera1RegionCoords[0] == 0 && logo1Camera1RegionCoords[1] == 0 && logo1Camera1RegionCoords[2] == 0 && logo1Camera1RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 1 region not set for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        if (logo2Camera1RegionCoords[0] == 0 && logo2Camera1RegionCoords[1] == 0 && logo2Camera1RegionCoords[2] == 0 && logo2Camera1RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 2 region not set for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (!Directory.Exists(Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera1"))
                    {
                        Directory.CreateDirectory(Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera1");
                    }
                    HOperatorSet.RotateImage(hoImageCamera1Rotated, out HObject hImageRotatedCamera1, -90, "constant");

                    HalconProcedures.CreateLogos(hImageRotatedCamera1,
                                                 Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera1\Logo1.shm",
                                                 Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera1\Logo2.shm",
                                                 comboBoxColorsCamera1.SelectedItem.ToString().ToLower(),
                                                 Convert.ToInt32(numericUpDownLogosCamera1.Value),
                                                 logo1Camera1RegionCoords[0],
                                                 logo1Camera1RegionCoords[1],
                                                 logo1Camera1RegionCoords[2],
                                                 logo1Camera1RegionCoords[3],
                                                 logo2Camera1RegionCoords[0],
                                                 logo2Camera1RegionCoords[1],
                                                 logo2Camera1RegionCoords[2],
                                                 logo2Camera1RegionCoords[3]);
                }

                if (checkBoxShapeCamera1.Checked)
                {
                    if (comboBoxShapeTypeCamera1.SelectedIndex == -1)
                    {
                        MessageBox.Show("No shape type selected for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        shapeTypeCamera1 = comboBoxShapeTypeCamera1.SelectedItem.ToString();
                    }
                }

                if (textBoxOcrCamera1.Text == string.Empty)
                {
                    MessageBox.Show("No OCR text for camera 1", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            if (checkBoxCamera2.Checked)
            {
                if (comboBoxColorsCamera2.SelectedIndex == -1)
                {
                    MessageBox.Show("No color selected for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (checkBoxLogoCamera2.Checked)
                {
                    if (numericUpDownLogosCamera2.Value == 0)
                    {
                        MessageBox.Show("Camera 2 logos activated but no logo set", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else if (numericUpDownLogosCamera2.Value == 1)
                    {
                        if (logo1Camera2RegionCoords[0] == 0 && logo1Camera2RegionCoords[1] == 0 && logo1Camera2RegionCoords[2] == 0 && logo1Camera2RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 1 region not set for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else if (numericUpDownLogosCamera2.Value == 2)
                    {
                        if (logo1Camera2RegionCoords[0] == 0 && logo1Camera2RegionCoords[1] == 0 && logo1Camera2RegionCoords[2] == 0 && logo1Camera2RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 1 region not set for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        if (logo2Camera2RegionCoords[0] == 0 && logo2Camera2RegionCoords[1] == 0 && logo2Camera2RegionCoords[2] == 0 && logo2Camera2RegionCoords[3] == 0)
                        {
                            MessageBox.Show("Logo 2 region not set for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (!Directory.Exists(Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera2"))
                    {
                        Directory.CreateDirectory(Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera2");
                    }

                    HOperatorSet.RotateImage(hoImageCamera2Rotated, out HObject hImageRotatedCamera2, -90, "constant");

                    HalconProcedures.CreateLogos(hImageRotatedCamera2,
                                                 Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera2\Logo1.shm",
                                                 Directory.GetCurrentDirectory() + $@"\Programs\{comboBoxPrograms.SelectedItem}\Camera2\Logo2.shm",
                                                 comboBoxColorsCamera2.SelectedItem.ToString().ToLower(),
                                                 Convert.ToInt32(numericUpDownLogosCamera2.Value),
                                                 logo1Camera2RegionCoords[0],
                                                 logo1Camera2RegionCoords[1],
                                                 logo1Camera2RegionCoords[2],
                                                 logo1Camera2RegionCoords[3],
                                                 logo2Camera2RegionCoords[0],
                                                 logo2Camera2RegionCoords[1],
                                                 logo2Camera2RegionCoords[2],
                                                 logo2Camera2RegionCoords[3]);
                }

                if (checkBoxShapeCamera2.Checked)
                {
                    if (comboBoxShapeTypeCamera2.SelectedIndex == -1)
                    {
                        MessageBox.Show("No shape type selected for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        shapeTypeCamera2 = comboBoxShapeTypeCamera2.SelectedItem.ToString();
                    }
                }

                if (textBoxOcrCamera2.Text == string.Empty)
                {
                    MessageBox.Show("No OCR text for camera 2", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            if (programToCheck.Name == null)
            {
                string colorCamera1 = string.Empty;
                string colorCamera2 = string.Empty;

                if (comboBoxColorsCamera1.SelectedIndex != -1)
                {
                    colorCamera1 = comboBoxColorsCamera1.SelectedItem.ToString().ToLower();
                }

                if (comboBoxColorsCamera2.SelectedIndex != -1)
                {
                    colorCamera2 = comboBoxColorsCamera2.SelectedItem.ToString().ToLower();
                }

                SqlCommunication.InsertProgram(comboBoxPrograms.SelectedItem.ToString(),
                                               triggerOffset,
                                               startWastingOffset,
                                               wasteOffset,
                                               colorCamera1,
                                               colorCamera2,
                                               checkBoxCamera1.Checked,
                                               checkBoxCamera2.Checked,
                                               checkBoxLogoCamera1.Checked,
                                               checkBoxLogoCamera2.Checked,
                                               Convert.ToInt32(numericUpDownLogosCamera1.Value),
                                               Convert.ToInt32(numericUpDownLogosCamera2.Value),
                                               $"C:\\Hirschmann\\Hirschmann\\bin\\Debug\\Programs\\{comboBoxPrograms.SelectedItem}\\Camera1\\Logo1.shm".Replace('\\', ' '),
                                               $"C:\\Hirschmann\\Hirschmann\\bin\\Debug\\Programs\\{comboBoxPrograms.SelectedItem}\\Camera1\\Logo2.shm".Replace('\\', ' '),
                                               $"C:\\Hirschmann\\Hirschmann\\bin\\Debug\\Programs\\{comboBoxPrograms.SelectedItem}\\Camera2\\Logo1.shm".Replace('\\', ' '),
                                               $"C:\\Hirschmann\\Hirschmann\\bin\\Debug\\Programs\\{comboBoxPrograms.SelectedItem}\\Camera2\\Logo2.shm".Replace('\\', ' '),
                                               checkBoxShapeCamera1.Checked,
                                               checkBoxShapeCamera2.Checked,
                                               shapeTypeCamera1.ToLower(),
                                               shapeTypeCamera2.ToLower(),
                                               textBoxOcrCamera1.Text.Replace(" ", string.Empty),
                                               textBoxOcrCamera2.Text.Replace(" ", string.Empty)
                                               );

                AddLog($"Manage Programs: Program {comboBoxPrograms.SelectedItem.ToString()} created");
            }
            else
            {
                string colorCamera1 = string.Empty;
                string colorCamera2 = string.Empty;

                if (comboBoxColorsCamera1.SelectedIndex != -1)
                {
                    colorCamera1 = comboBoxColorsCamera1.SelectedItem.ToString().ToLower();
                }

                if (comboBoxColorsCamera2.SelectedIndex != -1)
                {
                    colorCamera2 = comboBoxColorsCamera2.SelectedItem.ToString().ToLower();
                }

                SqlCommunication.UpdateProgramManageProgramsForm(comboBoxPrograms.SelectedItem.ToString(),
                                                                 triggerOffset,
                                                                 startWastingOffset,
                                                                 wasteOffset,
                                                                 colorCamera1,
                                                                 colorCamera2,
                                                                 checkBoxCamera1.Checked,
                                                                 checkBoxCamera2.Checked,
                                                                 checkBoxLogoCamera1.Checked,
                                                                 checkBoxLogoCamera2.Checked,
                                                                 Convert.ToInt32(numericUpDownLogosCamera1.Value),
                                                                 Convert.ToInt32(numericUpDownLogosCamera2.Value),
                                                                 checkBoxShapeCamera1.Checked,
                                                                 checkBoxShapeCamera2.Checked,
                                                                 shapeTypeCamera1.ToLower(),
                                                                 shapeTypeCamera2.ToLower(),
                                                                 textBoxOcrCamera1.Text.Replace(" ", string.Empty),
                                                                 textBoxOcrCamera2.Text.Replace(" ", string.Empty)
                                                                 );

                AddLog($"Manage Programs: Program {comboBoxPrograms.SelectedItem.ToString()} updated");
            }

            Close();
        }