geboshi_V1/LeatherProject/GeBoShi/UI/Product/ProductInfoFrm.cs

404 lines
19 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GeBoShi.SysCtrl;
using HZH_Controls.Controls;
using Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace GeBoShi.UI.Product
{
public partial class ProductInfoFrm : Form
{
Service.ProductService service = new Service.ProductService();
Models.Product model = new Models.Product();
public ProductInfoFrm(Models.Product m = null)
{
InitializeComponent();
UIStyle.SetUIStyle(this);
this.uiTitel1.FatherForm = this;
#region dataGridView设置
dataGridView1.AllowUserToAddRows = dataGridView1.AllowUserToDeleteRows = false;//用户添加删除行
dataGridView2.AllowUserToAddRows = dataGridView2.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToResizeRows = dataGridView2.AllowUserToResizeRows = false;//用户调整行大小
//dataGridView1.AllowUserToResizeColumns = false;//用户调整列大小
//显示行号与列宽度自动调整
dataGridView1.RowHeadersVisible = dataGridView2.RowHeadersVisible = true;
dataGridView1.RowHeadersWidth = dataGridView2.RowHeadersWidth = 50;
//dataGridView1.ColumnHeadersHeightSizeMode = dataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridView1.RowHeadersWidthSizeMode = dataGridView2.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
dataGridView1.RowTemplate.Height = dataGridView2.RowTemplate.Height = 30;
#endregion
initData();
if (m != null)
{
model = m;
//显示模型
foreach (string onnxFile in this.cmbDefectModelFile.Items)
if (!string.IsNullOrWhiteSpace(model.ModelName) && onnxFile.ToLower() == model.ModelName.ToLower())
this.cmbDefectModelFile.SelectedItem = model.ModelName;
txtName.Text = model.Name;
cmbClasses.Text = model.Material;
tbColorName.Text = model.ColorName;
int[] rgb = new int[3];
if (!string.IsNullOrEmpty(model.ColorValue))
{
for (int i = 0; i < rgb.Length; i++)
rgb[i] = Convert.ToInt32(model.ColorValue.Split(',')[i]);
tbColorName.ForeColor = Color.FromArgb(rgb[0], rgb[1], rgb[2]);
}
tbSpec.Text = model.Spec;
TrackBarLightValue.Value = model.LightValue;
TrackBarExposureTime.Value = (int)model.ExposureTime;
TrackBarGain.Value = (int)model.Gain;
TrackBarTensionValue.Value = (int)model.TensionValue;
numStopDis.Value = (decimal)model.ThicknessDetectionStopDis;
numReelLen.Value = (decimal)model.residueWarnningLen;
numDefectAreaLimit.Value = (decimal)model.DefectAreaLimit;
numDefectCntLength.Value = (decimal)model.DefectCntLength;
numDefectCountLimit.Value = (decimal)model.DefectCountLimit;
tbWarnDefect.Text = model.WarnDefect;
cbGetHD.Checked = model.OpenThicknessDetection;
numStopDis.Value = (int)model.ThicknessDetectionStopDis;
}
}
private void initData()
{
//模型文件
string strDefectModelFile = ConfMgr.Instance.SysConfigParams.AIModelPath;
if(!Directory.Exists(strDefectModelFile))
{
MessageBox.Show("模型路径错误:" + strDefectModelFile, "警告");
return;
}
string[] onnxFiles = Directory.GetFiles(strDefectModelFile, "*.trt");
//标签文件
string[] labelFiles = Directory.GetFiles(strDefectModelFile, "*.json");
string errorStr = "";
foreach (string onnxFile in onnxFiles)
{
string onlyName;
onlyName = Path.GetFileName(onnxFile);
cmbDefectModelFile.Items.Add(onlyName);
string findname = onlyName.Replace(".trt", ".json");
//存在对应label文件
if (labelFiles.Count(p => Path.GetFileName(p) == findname) <= 0)
{
errorStr += $"{onlyName},";
}
}
if (!string.IsNullOrEmpty(errorStr))
MessageBox.Show("模型缺少词典文件:" + errorStr, "警告");
//加载材质
string configPath = ConfMgr.Instance.ConfigDir + $"\\material.json";
if (File.Exists(configPath))
{
string lsTmp = File.ReadAllText(configPath);
JArray defectItemList = JArray.Parse(lsTmp);
cmbClasses.Items.Clear();
foreach (JObject item in defectItemList)
{
string name = item.Value<string>("name");
cmbClasses.Items.Add(name);
}
}
else
MessageBox.Show("模型材质文件:" + configPath, "警告");
}
private void cmbClasses_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void ProductInfoFrm_Load(object sender, EventArgs e)
{
}
private void ProductInfoFrm_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
this.Top = 0;
this.Left = 0;
this.Width = SystemInformation.WorkingArea.Width;
this.Height = SystemInformation.WorkingArea.Height;
}
}
private void tsbtnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void tsbtnSave_Click(object sender, EventArgs e)
{
try
{
if (this.cmbClasses.SelectedIndex < 0) throw new Exception("请选择材质!");
if (string.IsNullOrEmpty(this.txtName.Text)) throw new Exception("请填写名称!");
if (string.IsNullOrEmpty(this.tbColorName.Text)) throw new Exception("请填写颜色!");
if (this.cmbDefectModelFile.SelectedIndex < 0) throw new Exception("请选择模型!");
model.Name = txtName.Text;
model.Material = cmbClasses.Text;
model.ColorName = tbColorName.Text;
model.ModelName = cmbDefectModelFile.Text;
model.Spec = tbSpec.Text;
model.LightValue = (int)TrackBarLightValue.Value;
model.ExposureTime = (double)TrackBarExposureTime.Value;
model.Gain = (double)TrackBarGain.Value;
model.TensionValue = (double)TrackBarTensionValue.Value;
model.OpenThicknessDetection = cbGetHD.Checked;
model.ThicknessDetectionStopDis = (int)numStopDis.Value;
model.DefectAreaLimit = (int)numDefectAreaLimit.Value;
model.DefectCntLength = (double)numDefectCntLength.Value;
model.DefectCountLimit = (int)numDefectCountLimit.Value;
model.WarnDefect = tbWarnDefect.Text;
model.residueWarnningLen = (double)numReelLen.Value;
//缺陷阈值
if (model.QualifiedLimitList == null)
model.QualifiedLimitList = new List<Models.QualifiedLimit>();
else
model.QualifiedLimitList.Clear();
QualifiedLimit qualifiedLimit = new QualifiedLimit();
string configPath = ConfMgr.Instance.SysConfigParams.AIModelPath + $"\\{tbLabelFile.Text}";
string lsTmp = File.ReadAllText(configPath);
JArray defectItemList = JArray.Parse(lsTmp);
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string code2 = dataGridView1.Rows[i].Cells["Code"].Value.ToString();
string nameCode = defectItemList.FirstOrDefault(x => x.Value<string>("code") == code2).Value<string>("name");
qualifiedLimit = new Models.QualifiedLimit()
{
Code = dataGridView1.Rows[i].Cells["Code"].Value.ToString(),
ZXD = Utils.IsDecimal(dataGridView1.Rows[i].Cells["ZXD"].Value) ? Convert.ToDouble(dataGridView1.Rows[i].Cells["ZXD"].Value) : 0,
Area = Utils.IsDecimal(dataGridView1.Rows[i].Cells["Area"].Value) ? Convert.ToDouble(dataGridView1.Rows[i].Cells["Area"].Value) : 0,
ContrastLower = Utils.IsDecimal(dataGridView1.Rows[i].Cells["DBDL"].Value) ? Utils.PercentToContrast(Convert.ToDouble(dataGridView1.Rows[i].Cells["DBDL"].Value)) : 0,
ContrastTop = Utils.IsDecimal(dataGridView1.Rows[i].Cells["DBDH"].Value) ? Utils.PercentToContrast(Convert.ToDouble(dataGridView1.Rows[i].Cells["DBDH"].Value)) : 0,
IsOR = Convert.ToBoolean(dataGridView1.Rows[i].Cells["OrAnd"].Value),
NameCode = nameCode,
DefectWarnLength = Utils.IsNumber(dataGridView1.Rows[i].Cells["DefectLength"].Value) ? Convert.ToInt32(dataGridView1.Rows[i].Cells["DefectLength"].Value) : 0,
DefectWarnCnt = Utils.IsDecimal(dataGridView1.Rows[i].Cells["DefectWarn"].Value) ? Convert.ToInt32(dataGridView1.Rows[i].Cells["DefectWarn"].Value) : 0,
ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code
};
if (qualifiedLimit.ContrastLower + qualifiedLimit.ContrastTop > 0 && qualifiedLimit.ContrastTop < qualifiedLimit.ContrastLower)
throw new Exception($"检测标准中第{i + 1}行中对比度上限值({qualifiedLimit.ContrastTop})不可小于下限值({qualifiedLimit.ContrastLower})");
model.QualifiedLimitList.Add(qualifiedLimit);
}
//产品等级
if (model.GradeLimitList == null)
model.GradeLimitList = new List<Models.GradeLimit>();
else
model.GradeLimitList.Clear();
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
model.GradeLimitList.Add(
new Models.GradeLimit()
{
Code = dataGridView2.Rows[i].Cells["Code2"].Value.ToString(),
A = Utils.IsNumber(dataGridView2.Rows[i].Cells["A"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["A"].Value) : 0,
B = Utils.IsNumber(dataGridView2.Rows[i].Cells["B"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["B"].Value) : 0,
C = Utils.IsNumber(dataGridView2.Rows[i].Cells["C"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["C"].Value) : 0,
D = Utils.IsNumber(dataGridView2.Rows[i].Cells["D"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["D"].Value) : 0,
E = Utils.IsNumber(dataGridView2.Rows[i].Cells["E"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["E"].Value) : 0,
ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code
});
}
//
model.ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code;
bool result;
if (model.Id == 0)
{
model.CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code;
result = service.InsertNav(model);
}
else
{
result = service.UpdateNav(model);
}
if (!result)
throw new Exception("数据保存失败!");
else
MessageBox.Show("保存成功!", "保存");
}
catch (Exception ex)
{
MessageBox.Show("保存出错:" + ex.Message, "警告");
}
}
private void cmbDefectModelFile_TextChanged(object sender, EventArgs e)
{
try
{
tbLabelFile.Text = cmbDefectModelFile.Text.Replace(".trt", ".json");
//加载缺陷
string configPath = ConfMgr.Instance.SysConfigParams.AIModelPath + $"\\{tbLabelFile.Text}";
string lsTmp = File.ReadAllText(configPath);
JArray defectItemList = JArray.Parse(lsTmp);
//加行
dataGridView1.Rows.Clear();
dataGridView2.Rows.Clear();
cbDefectName.Items.Clear();
foreach (JObject item in defectItemList)
{
string code = item.Value<string>("code");
string name = item.Value<string>("name");
//color = item.Value<string>("color");
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.RowCount - 1].HeaderCell.Value = name;
dataGridView1[0, dataGridView1.RowCount - 1].Value = code;
dataGridView2.Rows.Add();
dataGridView2.Rows[dataGridView2.RowCount - 1].HeaderCell.Value = name;
dataGridView2[0, dataGridView2.RowCount - 1].Value = code;
cbDefectName.Items.Add(name);
}
//模型是否是产品模型
if (model.ModelName == cmbDefectModelFile.Text)
{
//加载参数
string code;
QualifiedLimit item1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
code = dataGridView1.Rows[i].Cells["Code"].Value.ToString();
item1 = model.QualifiedLimitList.FirstOrDefault(m => m.Code == code);
if (item1 != null)
{
dataGridView1.Rows[i].Cells["ZXD"].Value = item1.ZXD;
dataGridView1.Rows[i].Cells["Area"].Value = item1.Area;
dataGridView1.Rows[i].Cells["DBDH"].Value = Utils.ContrastToPercent(item1.ContrastTop);
dataGridView1.Rows[i].Cells["DBDL"].Value = Utils.ContrastToPercent(item1.ContrastLower);
dataGridView1.Rows[i].Cells["OrAnd"].Value = item1.IsOR;
dataGridView1.Rows[i].Cells["DefectLength"].Value = item1.DefectWarnLength;
dataGridView1.Rows[i].Cells["DefectWarn"].Value = item1.DefectWarnCnt;
}
}
GradeLimit item2;
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
code = dataGridView2.Rows[i].Cells["Code2"].Value.ToString();
item2 = model.GradeLimitList.FirstOrDefault(m => m.Code == code);
if (item2 != null)
{
dataGridView2.Rows[i].Cells["A"].Value = item2.A;
dataGridView2.Rows[i].Cells["B"].Value = item2.B;
dataGridView2.Rows[i].Cells["C"].Value = item2.C;
dataGridView2.Rows[i].Cells["D"].Value = item2.D;
dataGridView2.Rows[i].Cells["E"].Value = item2.E;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("载入出错:" + ex.Message, "警告");
}
}
private void btnColor_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
DialogResult result = dlg.ShowDialog();
if(result == DialogResult.OK)
{
// 获取用户所选颜色
Color selectedColor = dlg.Color;
// 在 label1 中显示所选颜色的 RGB 值
tbColorName.ForeColor = selectedColor;
}
}
private void btnClearDefect_Path_Click(object sender, EventArgs e)
{
tbWarnDefect.Text = "";
}
private void btnAddWarn_Click(object sender, EventArgs e)
{
if(tbWarnDefect.Text == "")
{
tbWarnDefect.Text += cbDefectName.Text;
}
else
tbWarnDefect.Text += $",{cbDefectName.Text}";
}
private void TrackBarLightValue_ValueChanged(object sender, EventArgs e)
{
UCTrackBar trackBar = (UCTrackBar)sender;
if(trackBar.Name == "TrackBarLightValue")
{
numericUpDown1.Value = (decimal)trackBar.Value;
}
else if (trackBar.Name == "TrackBarExposureTime")
{
numericUpDown2.Value = (decimal)trackBar.Value;
}
else if (trackBar.Name == "TrackBarGain")
{
numericUpDown3.Value = (decimal)trackBar.Value;
}
else if (trackBar.Name == "TrackBarTensionValue")
{
numericUpDown4.Value = (decimal)trackBar.Value;
}
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
NumericUpDown num = (NumericUpDown)sender;
if (num.Name == "numericUpDown1")
{
TrackBarLightValue.Value = (float)num.Value > TrackBarLightValue.MaxValue? TrackBarLightValue.MaxValue : (float)num.Value;
}
else if (num.Name == "numericUpDown2")
{
TrackBarExposureTime.Value = (float)num.Value > TrackBarExposureTime.MaxValue ? TrackBarExposureTime.MaxValue : (float)num.Value;
}
else if (num.Name == "numericUpDown3")
{
TrackBarGain.Value = (float)num.Value > TrackBarGain.MaxValue ? TrackBarGain.MaxValue : (float)num.Value;
}
else if (num.Name == "numericUpDown4")
{
TrackBarTensionValue.Value = (float)num.Value > TrackBarTensionValue.MaxValue ? TrackBarTensionValue.MaxValue : (float)num.Value;
}
}
}
}