|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- 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<ImageBox.BaseDrawParam> pickARoi = new List<ImageBox.BaseDrawParam>();
- Mat Img;
-
- string _Path;
- double[] _Points;
-
- Service.ProductService service = new Service.ProductService();
- Models.Product model = new Models.Product();
- List<TestDefectPoints> DefectPoints = new List<TestDefectPoints>();
-
- //测试项名称
- List<string> ItemName = new List<string>();
- //测试项对应颜色
- Color[] pointColors = new Color[30] { 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,
- Color.LightGoldenrodYellow,Color.Navy, Color.Firebrick,Color.Gold, Color.Goldenrod};
-
- 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<TestDefectPoints>();
- }
- 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<ImageBox.BaseDrawParam> 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<ImageBox.ColorDrawParam> lROI = new List<ImageBox.ColorDrawParam>();
- //按点位类型
- var tempStuList = model.TestDefectPointsList.GroupBy(q => q.PointCode);
-
- foreach ( var tempPoint in tempStuList )
- {
- //一个点位类型所有点位数据
- List<TestDefectPoints> 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<TestDefectPoints> 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<TestDefectPoints> 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<ImageBox.ColorDrawParam> lROI = new List<ImageBox.ColorDrawParam>();
- //按点位类型
-
- //一个点位类型所有点位数据
- List<TestDefectPoints> 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<TestDefectPoints> 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<TestDefectPoints> 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<TestDefectPoints> 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<TestDefectPoints> 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<TestDefectPoints> 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
-
-
- }
- }
|