/// <summary>
 /// Initializes a new <see cref="PickAndPlaceLib.FootprintForm"/>
 /// </summary>
 public FootprintForm()
 {
     InitializeComponent();
     cbxNozzle.DataSource = Enum.GetValues(typeof(Nozzle));
     foreach (StackType stackType_ in Enum.GetValues(typeof(StackType)))
     {
         cbxStackType.Items.Add(PNPconverterTools.StackTypeToString(stackType_));
     }
     cbxStackType.SelectedIndex = 0;
     try
     {
         AutoCompleteStringCollection source = new AutoCompleteStringCollection();
         source.AddRange(DatabaseOperations.GetFootprintList().ToArray());
         tbxMPN.AutoCompleteCustomSource = source;
         tbxMPN.AutoCompleteMode         = AutoCompleteMode.Suggest;
         tbxMPN.AutoCompleteSource       = AutoCompleteSource.CustomSource;
         tbxMPN.Leave += tbxMPN_Leave;
     }
     catch
     {
         //failed to load the database
         tbxMPN.AutoCompleteMode   = AutoCompleteMode.None;
         tbxMPN.AutoCompleteSource = AutoCompleteSource.None;
     }
 }
 /*-------------------------------------------------------------*/
 private void cbxStackType_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (cbxStackType.SelectedItem.ToString() == PNPconverterTools.StackTypeToString(StackType.Tray18mm))
     {
         nudFeedRate.Value   = 18;
         nudFeedRate.Enabled = false;
     }
     else
     {
         nudFeedRate.Enabled = true;
     }
 }
 /// <summary>
 /// Sets the footprint to be edited by displaying its properties
 /// </summary>
 /// <param name="footprint">Footprint to be edited</param>
 public void SetFootprint(Footprint footprint)
 {
     tbxMPN.Text               = footprint.ManufacturerPartNumber;
     nudHeight.Value           = (decimal)footprint.Height;
     nudLength.Value           = (decimal)footprint.Length;
     nudWidth.Value            = (decimal)footprint.Width;
     nudRotation.Value         = footprint.Rotation;
     bscOffset.ValueX          = (decimal)footprint.OffsetStackX;
     bscOffset.ValueY          = (decimal)footprint.OffsetStackY;
     cbxNozzle.SelectedItem    = footprint.Nozzle;
     cbxStackType.SelectedItem = PNPconverterTools.StackTypeToString(footprint.StackType);
     nudFeedRate.Value         = (decimal)footprint.FeedRate;
 }
 /*-------------------------------------------------------------*/
 private void btnSave_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrWhiteSpace(tbxMPN.Text))
     {
         MessageBox.Show("Please enter a valid manufacturer part number", "Error: invalid manufacturer part number", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     else if (tbxMPN.Text.Contains(','))
     {
         MessageBox.Show("manufacturer part number contains a ','" + Environment.NewLine + "this is not allowed.", "Error: Comma not allowed", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     try
     {
         //Generate new footprint
         string    manufacturerPartNumber = tbxMPN.Text;
         float     width     = (float)nudWidth.Value;
         float     length    = (float)nudLength.Value;
         float     height    = (float)nudHeight.Value;
         int       rotation  = Convert.ToInt32(nudRotation.Value);
         float     offsetX   = (float)bscOffset.ValueX;
         float     offsetY   = (float)bscOffset.ValueY;
         float     feedRate  = (float)nudFeedRate.Value;
         StackType stackType = PNPconverterTools.StringToStackType(cbxStackType.SelectedItem.ToString());
         footPrint_        = new Footprint(manufacturerPartNumber, width, length, height, rotation, offsetX, offsetY, feedRate, (Nozzle)cbxNozzle.SelectedItem, stackType);
         newFootprintMade_ = !DatabaseOperations.FootprintExists(footPrint_.ManufacturerPartNumber);
         if (newFootprintMade_)
         {
             //new footprint
             DatabaseOperations.AddNewFootprint(footPrint_);
         }
         else
         {
             //change of values
             DatabaseOperations.UpdateFootprint(footPrint_);
         }
         this.DialogResult = DialogResult.Yes;
         this.Close();
     }
     catch (Exception exc)
     {
         MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
        /// <summary>
        /// Get the footprint from the database specified by the connectionstring
        /// </summary>
        /// <param name="manufacturerPartNumber">Manufacturer part number to be loaded</param>
        /// <param name="connectionString_">Connectionstring for the sqlite database</param>
        /// <returns>Footprint of the manufacturere part number</returns>
        private static Footprint GetFootprint(string manufacturerPartNumber, string connectionString_)
        {
            Footprint result    = null;
            string    sqlString = @"SELECT f.manufacturerPartNumber,f.width,f.length,f.height,f.rotation,f.offsetStackX,f.offsetStackY,f.feedRate,n.name as nozzle,s.name as stackType
                                FROM footprints f, nozzles n,stackTypes s
                                WHERE n.id = f.nozzle_id
                                AND s.id = f.StackType_id
                                AND f.manufacturerPartNumber = @manufacturerPartNumber";

            using (SqliteConnection dbConnection = new SqliteConnection(connectionString_))
                using (SqliteCommand dbCommand = new SqliteCommand(sqlString, dbConnection))
                {
                    dbConnection.Open();
                    dbCommand.Parameters.AddWithValue("manufacturerPartNumber", manufacturerPartNumber);
                    using (SqliteDataReader reader = dbCommand.ExecuteReader())
                    {
                        reader.Read();
                        if (reader.HasRows)
                        {
                            string    partNumber_ = reader["manufacturerPartNumber"].ToString();
                            float     width_      = float.Parse(reader["width"].ToString());
                            float     length_     = float.Parse(reader["length"].ToString());
                            float     height_     = float.Parse(reader["height"].ToString());
                            int       rotation_   = Int32.Parse(reader["rotation"].ToString());
                            float     offsetX_    = float.Parse(reader["offsetStackX"].ToString());
                            float     offsetY_    = float.Parse(reader["offsetStackY"].ToString());
                            float     feedRate    = float.Parse(reader["feedRate"].ToString());
                            Nozzle    nozzle      = PNPconverterTools.StringToNozzle(reader["nozzle"].ToString());
                            StackType stackType   = PNPconverterTools.StringToStackType(reader["stackType"].ToString());

                            result = new Footprint(partNumber_, width_, length_, height_, rotation_, offsetX_, offsetY_, feedRate, nozzle, stackType);
                        }
                    }
                    dbConnection.Close();
                }
            return(result);
        }