using CCWin.Win32; using CCWin.Win32.Const; using ImageToolKits; using MaiMuAOI.SysCtrl; using MaiMuAOI.SysUI; using Models; using OpenCvSharp; using OpenCvSharp.Flann; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static ImageToolKits.ImageBox; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header; namespace ProductionControl.UI { public partial class FrmGetPosByPic : Form { List pickARoi = new List(); Mat Img; string _Path; double[] _Points; Service.ProductService service = new Service.ProductService(); Models.Product model = new Models.Product(); List DefectPoints = new List(); //测试项名称 List ItemName = new List(); //测试项对应颜色 Color[] pointColors = new Color[25] { Color.Red, Color.Blue, Color.Green, Color.YellowGreen, Color.Orange, Color.Peru, Color.DarkRed,Color.AliceBlue, Color.Pink , Color.Brown, Color.CadetBlue, Color.OrangeRed, Color.Cyan, Color.Lime, Color.Magenta, Color.Tan, Color.Sienna, Color.AliceBlue, Color.SaddleBrown, Color.DarkBlue, Color.Firebrick,Color.Gainsboro,Color.Honeydew,Color.Khaki,Color.Lavender}; public FrmGetPosByPic(Models.Product m, string map, double[] pointlist) { _Path = map; _Points = pointlist; pickARoi.Clear(); InitializeComponent(); UIStyle.SetUIStyle(this); this.uiTitel1.FatherForm = this; //lbPoslist.Items.Clear(); //this.imageBox1.SetLowLevelMode(14); this.imageBox1.SetPointsLevelMode(); imageBox1.DrawOverAllEventHandler += imgBox_DrawOverAllEventHandler; //显示行号与列宽度自动调整 dgvPointTable.DefaultCellStyle.ForeColor = Color.Black; dgvPointTable.RowHeadersVisible = true; dgvPointTable.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders; dgvPointTable.RowPostPaint += (sender, e) => { SysMgr.showRowNum_onDataGrid_RowPostPaint(this.dgvPointTable, sender, e); }; model = m; if((model != null )&&(model.TestDefectPointsList != null)) { DefectPoints = model.TestDefectPointsList; } else if((model != null) && (model.TestDefectPointsList == null)) { model.TestDefectPointsList = new List(); } cbPointName.SelectedIndex = 0; //加载 cbPointName.Items.Clear(); //反射 循环 获取数据 Type t = typeof(PointTestType); FieldInfo[] fieldInfos = t.GetFields(); foreach (var item in fieldInfos) { //不是枚举字段不处理 if (item.FieldType.IsEnum) { //名称可以直接获取 ItemName.Add(item.Name); cbPointName.Items.Add(item.Name); } } //cbPointName.Items.AddRange(fieldInfos); } public string GetMapPath() { return _Path; } public double[] GetPoints() { return _Points; } void imgBox_DrawOverAllEventHandler(object sender, List e) { if (e.Count > 0) { imageBox1.RefreshWindow(imageBox1.Image, e, ImageBox.ImageModeEnum.Part); } } private void FrmGetPosByPic_Load(object sender, EventArgs e) { imageBox1.Select(); if (File.Exists(_Path)) { Img = new Mat(_Path); imageBox1.RefreshWindow(Img); InitViewPoints(); ShowPointCount(); } else { string maop_path = ConfMgr.SelectFile(); if (File.Exists(maop_path)) { _Path = maop_path; Img = new Mat(maop_path); this.imageBox1.RefreshWindow(Img); } else { MessageBox.Show("无图纸图片加载!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } //imageBox1.Refresh(); //imageBox1.DisplayROIs(roi); //this.Refresh(); } void InitViewPoints() { List lROI = new List(); //按点位类型 var tempStuList = model.TestDefectPointsList.GroupBy(q => q.PointCode); foreach ( var tempPoint in tempStuList ) { //一个点位类型所有点位数据 List t = model.TestDefectPointsList.Where(q => q.PointCode == tempPoint.Key).ToList(); for (int i = 0; i < t.Count; i++) { ColorDrawParam param = new ColorDrawParam(); param.baseDraw = new ImageBox.Point((float)t[i].Y, (float)t[i].X); int index = 0; for (int j = 0; j < pointColors.Length; j++) { if (this.cbPointName.Items[j].ToString() == tempPoint.Key) { index = j; break; } } param.color = pointColors[index]; lROI.Add(param); } } imageBox1.RefreshWindow(imageBox1.Image, lROI); } void ShowPointCount() { //按点位类型 var tempStuList = model.TestDefectPointsList.GroupBy(q => q.PointCode); int[] count = new int[ItemName.Count]; foreach (var tempPoint in tempStuList) { //一个点位类型所有点位数据 List t = model.TestDefectPointsList.Where(q => q.PointCode == tempPoint.Key).ToList(); for (int j = 0; j < pointColors.Length; j++) { if (this.cbPointName.Items[j].ToString() == tempPoint.Key) { count[j] = t.Count; break; } } } //stLabel.Text = $"Y轴方向PT值检测({count[0]}),线宽正面({count[1]}),反面检测({count[2]}),X轴方向PT值检测({count[3]})," + // $"主栅连接线检测(主栅间隔)({count[4]}),主栅宽度检测({count[5]}),主栅间距({count[6]}),细栅间距检测({count[7]}),\r\n背极宽度({count[8]}),"+ // $"主栅长度检测({count[9]}),Mark点横向间距({count[10]}),Mark点竖向间距({count[11]}),鱼叉口长({count[12]}),鱼叉口宽({count[13]}),鱼叉口间距({count[14]})" + // $",蜈蚣角长({count[15]}),蜈蚣角宽({count[16]})"; string showPointNum = ""; for (int i = 0; i < ItemName.Count; i++) { showPointNum += $"{ItemName[i]}({count[i]}),"; if ((i+1) % 8 == 0) showPointNum += "\r\n"; } stLabel.Text = showPointNum.Remove(showPointNum.Length - 1); } #region 工具栏 private void tsbtnClear_Click(object sender, EventArgs e) { if (MessageBox.Show("是否清空所有点位?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) { pickARoi.Clear(); DefectPoints.Clear(); this.imageBox1.ClearColorPoints(); this.imageBox1.RefreshWindow(Img); ShowPointCount(); ShowPoints(); ReflashTable(); this.Refresh(); } } private void tsbtnShowAll_Click(object sender, EventArgs e) { InitViewPoints(); ShowPointCount(); } private void tsbtnClose_Click(object sender, EventArgs e) { this.Close(); } private void tsbtnSave_Click(object sender, EventArgs e) { //pickARoi = this.imageBox1.GetLowPoints(); //if (pickARoi.Count == 14) //{ // _Points = new double[14 * 2]; // for (int i = 0; i < 14; i++) // { // _Points[2 * i] = Math.Round((pickARoi[i] as ImageBox.Point).Column, 3); // _Points[2 * i + 1] = Math.Round((pickARoi[i] as ImageBox.Point).Row, 3); // } // this.DialogResult = DialogResult.OK; //} //else if (pickARoi.Count >0) // MessageBox.Show("点位设置错误,点数不对!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error); //else //{ // _Points = new double[14 * 2]; // for (int i = 0; i < 14; i++) // { // _Points[2 * i] = 0; // _Points[2 * i + 1] = 0; // } //} try { //老点位使用 bool result; if (model.Id == 0) { model.TestDefectPointsList = DefectPoints; result = service.InsertNav(model); } else { model.TestDefectPointsList = DefectPoints; result = service.UpdateNav(model); } if (!result) throw new Exception("保存失败!"); MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion #region 工具按钮 private void btnAdd_Click(object sender, EventArgs e) { try { //List t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList(); //DefectPoints.Add(point); DefectPoints.Add( new Models.TestDefectPoints() { PointCode = this.cbPointName.Text, X = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Column, 3), Y = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Row,3), ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code, CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code }); ShowPoints(); ReflashTable(); ShowPointCount(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ShowPoints() { List lROI = new List(); //按点位类型 //一个点位类型所有点位数据 List t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList(); for (int i = 0; i < t.Count; i++) { ColorDrawParam param = new ColorDrawParam(); param.baseDraw = new ImageBox.Point((float)t[i].Y, (float)t[i].X); param.color = pointColors[this.cbPointName.SelectedIndex]; lROI.Add(param); } if(imageBox1.Image == null) imageBox1.RefreshWindow(imageBox1.Image, lROI); else imageBox1.RefreshWindow(imageBox1.Image, lROI, ImageModeEnum.Part); } private void cbPointName_SelectedIndexChanged(object sender, EventArgs e) { ShowPoints(); ReflashTable(); } private void ReflashTable() { List t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList(); dgvPointTable.Rows.Clear(); for (int i = 0; i < t.Count; i++) { ColorDrawParam param = new ColorDrawParam(); param.baseDraw = new ImageBox.Point((float)t[i].Y, (float)t[i].X); param.color = pointColors[this.cbPointName.SelectedIndex]; int index = dgvPointTable.Rows.Add(); dgvPointTable.Rows[index].Cells[0].Value = t[i].PointCode; dgvPointTable.Rows[index].Cells[1].Value = t[i].X; dgvPointTable.Rows[index].Cells[2].Value = t[i].Y; } } private void btnInsert_Click(object sender, EventArgs e) { try { if (this.dgvPointTable.SelectedRows.Count != 1) throw new Exception("请选择要插入的位置!"); int liInsertIndex = this.dgvPointTable.Rows.Count; if (this.dgvPointTable.SelectedRows.Count > 0) liInsertIndex = this.dgvPointTable.SelectedRows[0].Index; List t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList(); int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liInsertIndex].X && temp.Y == t[liInsertIndex].Y) ); //DefectPoints.Add(point); DefectPoints.Insert(findIndex, new Models.TestDefectPoints() { PointCode = this.cbPointName.Text, X = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Column, 3), Y = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Row, 3), ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code, CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code }); ShowPoints(); ReflashTable(); ShowPointCount(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnDel_Click(object sender, EventArgs e) { try { if (this.dgvPointTable.SelectedRows.Count != 1) throw new Exception("请选择要删除的位置!"); int liRowIndex = this.dgvPointTable.SelectedRows[0].Index; List t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList(); int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liRowIndex].X && temp.Y == t[liRowIndex].Y)); DefectPoints.RemoveAt(findIndex); ReflashTable(); if (liRowIndex >= dgvPointTable.Rows.Count - 1) liRowIndex = dgvPointTable.Rows.Count - 1; dgvPointTable.CurrentCell = dgvPointTable.Rows[liRowIndex].Cells[1]; if (dgvPointTable.Rows.Count < 1) return; if (liRowIndex >= dgvPointTable.Rows.Count) liRowIndex = dgvPointTable.Rows.Count - 1; dgvPointTable.CurrentCell = dgvPointTable.Rows[liRowIndex].Cells[1]; ShowPoints(); ShowPointCount(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnUp_Click(object sender, EventArgs e) { try { if (this.dgvPointTable.SelectedRows.Count != 1) throw new Exception("请选择要调整顺序的位置!"); int liSelIndex = this.dgvPointTable.SelectedRows[0].Index; if (liSelIndex == 0) return; List t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList(); int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex].X && temp.Y == t[liSelIndex].Y)); int findIndexPre = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex - 1].X && temp.Y == t[liSelIndex - 1].Y)); DefectPoints.Insert(findIndexPre, DefectPoints[findIndex]); DefectPoints.RemoveAt(findIndex + 1); ReflashTable(); this.dgvPointTable.Rows[liSelIndex - 1].Selected = true; dgvPointTable.CurrentCell = dgvPointTable.Rows[liSelIndex - 1].Cells[1]; ShowPoints(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnDown_Click(object sender, EventArgs e) { try { if (this.dgvPointTable.SelectedRows.Count != 1) throw new Exception("请选择要调整顺序的位置!"); int liSelIndex = this.dgvPointTable.SelectedRows[0].Index; if (liSelIndex == this.dgvPointTable.Rows.Count - 1) return; List t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList(); int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex].X && temp.Y == t[liSelIndex].Y)); int findIndexAft = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex + 1].X && temp.Y == t[liSelIndex + 1].Y)); DefectPoints.Insert(findIndexAft + 1, DefectPoints[findIndex]); DefectPoints.RemoveAt(findIndex); ReflashTable(); this.dgvPointTable.Rows[liSelIndex + 1].Selected = true; dgvPointTable.CurrentCell = dgvPointTable.Rows[liSelIndex + 1].Cells[1]; ShowPoints(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion } }