using JinYuan.CommunicationLib.Enum; using JinYuan.CommunicationLib.Interface; using PLCCommunication; using JinYuan.Helper; using JinYuan.Models; using JinYuan.VirtualDataLibrary; using SqlSugar; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LargeSquareOne { public partial class FrmRecipe : Form { public FrmRecipe() { InitializeComponent(); ////设置控件样式,去除缓冲 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); } /// /// PLC内批量写\0 /// /// /// public string GetString_0(int Num) { string ff = ""; for (int i = 0; i < Num; i++) { ff = ff.Insert(ff.Length, "\0"); } return ff; } #region 减少闪烁 protected override CreateParams CreateParams { get { CreateParams parss = base.CreateParams; parss.ExStyle |= 0x02000000; return parss; } } #endregion Dictionary disOldValue = new System.Collections.Generic.Dictionary(); /// /// 定义DataGridView数据源 /// private BindingList blPLCConfigBaseList = new BindingList(); private void FrmRecipe_Load(object sender, EventArgs e) { this.dgvPara.AutoGenerateColumns = false; this.dgvPara.DataSource = blPLCConfigBaseList; GetProdModel(); GetChallengeCode(); GetProductParam(0); } private void GetProdModel() { try { var productList = CommonMethods.db.QuerySqlList(CommonMethods.dBSQL.GetProdTypeSql()); dgvProdModel.DataSource = productList; } catch (Exception ex) { MessageBox.Show(ex.Message); } } //获取挑战件条码 private void GetChallengeCode() { try { var ChallengeCodeList = CommonMethods.db.QuerySqlList(CommonMethods.dBSQL.GetChallengeCode()); dgvPara.DataSource = ChallengeCodeList; } catch (Exception ex) { MessageBox.Show(ex.Message); } } #region 选择产品型号 /// /// 点击单元格 /// /// /// private void dgvProdModel_CellClick(object sender, DataGridViewCellEventArgs e) { int row = dgvProdModel.CurrentCell.RowIndex; GetProductParam(row); } public void GetProductParam(int row) { try { if (dgvProdModel.Rows.Count > 0) { string strModelType = dgvProdModel.Rows[row].Cells["ModelName"].Value.ToString(); lb_Model.Text = strModelType; var list = CommonMethods.db.QuerySqlList(CommonMethods.dBSQL.GetProductParaByProdModel(strModelType)); disOldValue.Clear(); for (int i = 0; i < list.Count; i++) { if (!disOldValue.ContainsKey(list[i].Remark)) { disOldValue.Add(list[i].Remark, $"{list[i].ParaValueMin},{list[i].ParaValueMax}"); } } //dgvPara.DataSource = list; } } catch (Exception ex) { MessageBox.Show("加载数据失败" + ex.Message); } } #endregion /// /// 点击极限件单元格 /// /// /// public void dgvPara_CellClick(object sender, DataGridViewCellEventArgs e) { int row = dgvPara.CurrentCell.RowIndex; try { if (dgvPara.Rows.Count > 0) { string strModelType = dgvPara.Rows[row].Cells[0].Value.ToString(); string strModelType2 = dgvPara.Rows[row].Cells[1].Value.ToString(); string strModelType3 = dgvPara.Rows[row].Cells[2].Value.ToString(); ChallengeCodeText.Text = strModelType; comboBox1.Text = strModelType2; comboBox2.Text = strModelType3; var list = CommonMethods.db.QuerySqlList(CommonMethods.dBSQL.GetChallengeCode(strModelType)); //disOldValue.Clear(); //for (int i = 0; i < list.Count; i++) //{ // if (!disOldValue.ContainsKey(list[i].Remark)) // { // disOldValue.Add(list[i].Remark, $"{list[i].ParaValueMin},{list[i].ParaValueMax}"); // } //} //dgvPara.DataSource = list; } } catch (Exception ex) { MessageBox.Show("加载数据失败" + ex.Message); } } /// /// 添加数据 /// /// /// private void btn_Add_Click(object sender, EventArgs e) { var productModel = new ProductModel(); productModel.ModelName = txtProductModel.Text; productModel.MaterialCode = txtMaterialCode.Text; productModel.Remark = txtProductModel.Text; if (string.IsNullOrEmpty(productModel.ModelName)) { new FrmMsgBoxOutWithAck(2, "产品型号不能为空!", "添加型号").Show(); return; } if (string.IsNullOrEmpty(productModel.MaterialCode)) { new FrmMsgBoxOutWithAck(2, "型号对应的物料编码不能为空!", "添加型号").Show(); return; } try { bool b = CommonMethods.db.AddReturnBool(productModel); if (b) { GetProdModel(); CommonMethods.ChangeParamData(); new FrmMsgBoxOutWithAck(1, "添加成功!", "添加型号").Show(); } else { new FrmMsgBoxOutWithAck(3, "添加失败!", "添加型号").Show(); } } catch (Exception ex) { if (ex.Message.Contains("重复键")) { LogHelper.Instance.WriteEX(ex); new FrmMsgBoxOutWithAck(3, "添加失败:已存在改型号,重复新增!", "添加型号").Show(); } else { LogHelper.Instance.WriteEX(ex); new FrmMsgBoxOutWithAck(3, $"添加失败:{ex.Message}", "添加型号").Show(); } } //同步刷新主界面的类型选项 //sendParamIN(); } private void btn_Save_Click(object sender, EventArgs e) { if (dgvProdModel.Rows.Count > 0 && dgvProdModel.CurrentCell.RowIndex > -1) { int row = dgvProdModel.CurrentCell.RowIndex; string strModelType = dgvProdModel.Rows[row].Cells["ModelName"].Value.ToString(); if (string.IsNullOrEmpty(strModelType) || dgvPara.Rows.Count <= 0) { new FrmMsgBoxOutWithAck(2, "产品型号和参数列表不能为空!", "保存参数").Show(); return; } try { List list = (List)dgvPara.DataSource; int f = CommonMethods.db.Delete(t => t.ModelName == strModelType); if (f >= 0) { bool b = CommonMethods.db.AddReturnBool(list); if (b) { GetProdModel(); CommonMethods.ChangeParamData(); new FrmMsgBoxOutWithAck(1, "保存成功!", "保存参数").Show(); } else { new FrmMsgBoxOutWithAck(3, "保存失败!", "保存参数").Show(); } } } catch (Exception ex) { new FrmMsgBoxOutWithAck(3, "保存失败", "保存参数").Show(); } } else { new FrmMsgBoxOutWithAck(1, "请选择产品型号", "保存参数").Show(); } } private void btnDelProductModel_Click(object sender, EventArgs e) { if (dgvProdModel.Rows.Count > 0 && dgvProdModel.CurrentCell.RowIndex > -1) { int row = dgvProdModel.CurrentCell.RowIndex; string strModelType = dgvProdModel.Rows[row].Cells["ModelName"].Value.ToString(); if (string.IsNullOrEmpty(strModelType)) { new FrmMsgBoxOutWithAck(2, "选择要删除的生产型号", "删除型号").Show(); return; } DialogResult dialogResult = new FrmMsgBoxWithAck($"是否要删除型号【{strModelType}】", "删除型号").ShowDialog(); if (dialogResult == DialogResult.OK) { try { int f = CommonMethods.db.Delete(t => t.ModelName == strModelType); if (f > 0) { GetProdModel(); CommonMethods.ChangeParamData(); } new FrmMsgBoxOutWithAck(1, "删除成功!", "删除型号").Show(); } catch (Exception ex) { new FrmMsgBoxOutWithAck(1, $"删除失败:{ex.Message}", "删除型号").Show(); } } } } private void dgvProdModel_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //显示序列号 DataGridViewHelper.DgvRowPostPaint(this.dgvProdModel, e); } private void dgvPara_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //显示序列号 DataGridViewHelper.DgvRowPostPaint(this.dgvPara, e); } private void button1_Click(object sender, EventArgs e) { string Code1 = CodeText1.Text; string Code2 = CodeText2.Text; string Code3 = CodeText3.Text; string Code4 = CodeText4.Text; //根据PLC给的地址长度给二维码补0 string ZeroCode1 = Code1 + GetString_0(40 - Code1.Length); string ZeroCode2 = Code2 + GetString_0(40 - Code2.Length); string ZeroCode3 = Code3 + GetString_0(40 - Code3.Length); string ZeroCode4 = Code4 + GetString_0(40 - Code4.Length); //byte[] Codebytes = Encoding.Default.GetBytes(ZeroCode).ByteReverse(); byte[] Codebytes1 = Encoding.Default.GetBytes(ZeroCode1); byte[] Codebytes2 = Encoding.Default.GetBytes(ZeroCode2); byte[] Codebytes3 = Encoding.Default.GetBytes(ZeroCode3); byte[] Codebytes4 = Encoding.Default.GetBytes(ZeroCode4); //条码地址 int dress1 = 2500; int dress2 = 2520; int dress3 = 2540; int dress4 = 2560; string TouchDress = "D2490";//触发地址 string StrDress1 = "D" + dress1.ToString(); string StrDress2 = "D" + dress2.ToString(); string StrDress3 = "D" + dress3.ToString(); string StrDress4 = "D" + dress4.ToString(); if (string.IsNullOrEmpty(Code1) || string.IsNullOrEmpty(Code2) || string.IsNullOrEmpty(Code3) || string.IsNullOrEmpty(Code4)) { new FrmMsgBoxOutWithAck(2, "上传条码不能为空!", "条码上传").Show(); return; } JYResult result1 = CommonMethods.PlcLink.WriteValue(StrDress1, Codebytes1, Data_Type.ArrByte); JYResult result2 = CommonMethods.PlcLink.WriteValue(StrDress2, Codebytes2, Data_Type.ArrByte); JYResult result3 = CommonMethods.PlcLink.WriteValue(StrDress3, Codebytes3, Data_Type.ArrByte); JYResult result4 = CommonMethods.PlcLink.WriteValue(StrDress4, Codebytes4, Data_Type.ArrByte); System.Threading.Thread.Sleep(500); CommonMethods.PlcLink.WriteValue(TouchDress, 1, Data_Type.Short);//给PLC触发 new FrmMsgBoxOutWithAck(2, "上传成功!", "条码上传").Show(); } private void ChallengeButton_Click(object sender, EventArgs e) { var ChallengeCode = new Challenge(); ChallengeCode.ChallengeBarCode = ChallengeCodeText.Text; ChallengeCode.CodeMessage = comboBox1.Text; ChallengeCode.WeldingCCD = comboBox2.Text; if (string.IsNullOrEmpty(ChallengeCode.ChallengeBarCode)) { new FrmMsgBoxOutWithAck(2, "挑战件条码不能为空!", "添加挑战件条码").Show(); return; } try { bool b = CommonMethods.db.AddReturnBool(ChallengeCode); if (b) { GetChallengeCode(); new FrmMsgBoxOutWithAck(1, "添加成功!", "添加挑战件条码").Show(); } else { new FrmMsgBoxOutWithAck(3, "添加失败!", "添加挑战件条码").Show(); } } catch (Exception ex) { if (ex.Message.Contains("重复键")) { LogHelper.Instance.WriteEX(ex); new FrmMsgBoxOutWithAck(3, "添加失败:已存在该挑战件条码,重复新增!", "添加添加挑战件条码").Show(); } else { LogHelper.Instance.WriteEX(ex); new FrmMsgBoxOutWithAck(3, $"添加失败:{ex.Message}", "添加添加挑战件条码").Show(); } } } } }