版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

274 líneas
11 KiB

  1. using Google.Protobuf.Collections;
  2. using HalconDotNet;
  3. using ImageToolKits;
  4. using MaiMuAOI.SysCtrl;
  5. using MaiMuControl.Device;
  6. using MaiMuControl.Device.AxisDev;
  7. using MaiMuControl.Device.CamDev;
  8. using MaiMuControl.Device.LightDev;
  9. using OpenCvSharp.Flann;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Drawing;
  15. using System.Drawing.Imaging;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Web;
  21. using System.Windows.Forms;
  22. namespace MaiMuAOI.SysUI.DefectPicShow
  23. {
  24. public partial class ImageShowFrm : Form
  25. {
  26. private Control.ControlCollection picBoxList;
  27. private int picIndex;
  28. private int tagIndex;
  29. private double Xmm, Ymm;
  30. private Bitmap currBmp;
  31. private bool IsMove;
  32. public ImageShowFrm(Control.ControlCollection picBoxColl, int index, bool isDev = false)
  33. {
  34. InitializeComponent();
  35. UIStyle.SetUIStyle(this);
  36. this.uiTitel1.FatherForm = this;
  37. this.picBoxList = picBoxColl;
  38. this.picIndex = index;
  39. logsts.BackColor = Color.White;
  40. logsts.ForeColor = Color.Black;
  41. tsStateStep.BackColor = Color.White;
  42. tsStateStep.ForeColor = Color.Black;
  43. IsMove = isDev;
  44. }
  45. private void ImageShowFrm_Shown(object sender, EventArgs e)
  46. {
  47. showPic(this.picIndex);
  48. }
  49. private void tsbtnPre_Click(object sender, EventArgs e)
  50. {
  51. showPic(--this.picIndex);
  52. }
  53. private void tsbtnNext_Click(object sender, EventArgs e)
  54. {
  55. showPic(++this.picIndex);
  56. }
  57. private void ImageShowFrm_Load(object sender, EventArgs e)
  58. {
  59. if (!IsMove)
  60. {
  61. imageBox2.Visible = false;
  62. imageBox1.Top = 0;
  63. imageBox1.Left = this.panel1.Left;
  64. imageBox1.Width = this.panel1.Width;
  65. imageBox1.Height = this.panel1.Height;
  66. tsbDebug.Visible = false;
  67. }
  68. else
  69. {
  70. tsbDebug.Visible=true;
  71. imageBox2.Visible = true;
  72. imageBox1.Top = 0;
  73. imageBox1.Left = this.panel1.Left;
  74. imageBox1.Width = this.panel1.Width / 2 - 5;
  75. imageBox1.Height = this.panel1.Height;
  76. imageBox2.Top = 0;
  77. imageBox2.Left = this.imageBox1.Width + 10;
  78. imageBox2.Width = this.panel1.Width / 2 - 5;
  79. imageBox2.Height = this.panel1.Height;
  80. }
  81. PictureBox picBox = picBoxList[this.picIndex] as PictureBox;
  82. this.ActiveControl = this.imageBox1; // 设置焦点
  83. this.logsts.Text = "";
  84. this.Refresh();
  85. }
  86. private void FrmPhoto_KeyDown(object sender, KeyEventArgs e)
  87. {
  88. switch (e.KeyCode)
  89. {
  90. case Keys.Up:
  91. case Keys.Left:
  92. if (!this.tsbtnPre.Enabled) return;
  93. tsbtnPre_Click(null, null);
  94. break;
  95. case Keys.Down:
  96. case Keys.Right:
  97. if (!this.tsbtnNext.Enabled) return;
  98. tsbtnNext_Click(null, null);
  99. break;
  100. }
  101. }
  102. private void tsbtnExit_Click(object sender, EventArgs e)
  103. {
  104. this.Close();
  105. }
  106. private void showPic(int index)
  107. {
  108. try
  109. {
  110. PictureBox picBox = picBoxList[index] as PictureBox;
  111. try
  112. {
  113. string[] tags = picBox.Tag.ToString().Split(',');
  114. this.tagIndex = Convert.ToInt32(tags[0]);
  115. this.Xmm = Convert.ToDouble(tags[1]);
  116. this.Ymm = Convert.ToDouble(tags[2]);
  117. }
  118. catch { }
  119. //tsStateMsg.Text = $"索引:{tagIndex}, X:{Xmm}mm, Y:{Ymm}mm";
  120. tsStateStep.Text = $"第{index + 1}/{picBoxList.Count}张";
  121. currBmp = (Bitmap)picBox.Image;
  122. OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(currBmp);//用bitmap转换为mat
  123. this.imageBox1.RefreshWindow(mat);
  124. //
  125. this.tsbtnPre.Enabled = (index > 0);
  126. this.tsbtnNext.Enabled = (index < picBoxList.Count - 1);
  127. if (IsMove)
  128. moveAxis();
  129. }
  130. catch (Exception ex)
  131. {
  132. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  133. }
  134. }
  135. private void ImageShowFrm_Resize(object sender, EventArgs e)
  136. {
  137. if (!IsMove)
  138. {
  139. imageBox2.Visible = false;
  140. imageBox1.Top = 0;
  141. imageBox1.Left = this.panel1.Left;
  142. imageBox1.Width = this.panel1.Width;
  143. imageBox1.Height = this.panel1.Height;
  144. }
  145. else
  146. {
  147. imageBox2.Visible = true;
  148. imageBox1.Top = 0;
  149. imageBox1.Left = this.panel1.Left;
  150. imageBox1.Width = this.panel1.Width / 2 - 5;
  151. imageBox1.Height = this.panel1.Height;
  152. imageBox2.Top = 0;
  153. imageBox2.Left = this.imageBox1.Width + 10;
  154. imageBox2.Width = this.panel1.Width / 2 - 5;
  155. imageBox2.Height = this.panel1.Height;
  156. }
  157. this.Refresh();
  158. }
  159. DebugTestFrm frm;
  160. private void tsbDebug_Click(object sender, EventArgs e)
  161. {
  162. if ((frm == null) || (frm.IsDisposed))
  163. frm = new DebugTestFrm();
  164. //frm.TopMost = true;
  165. frm.Show();
  166. }
  167. private void timer1_Tick(object sender, EventArgs e)
  168. {
  169. SysMgr.Instance.CamDevFront.ClearImageQueue();
  170. Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 600);
  171. if (acq.GrabStatus != "GrabPass")
  172. {
  173. //MessageBox.Show("拍照失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  174. return;
  175. }
  176. if (imageBox2.Image == null)
  177. imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image), ImageBox.ImageModeEnum.Zoom);//可以不显示区域
  178. else
  179. imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image), ImageBox.ImageModeEnum.Part);//可以不显示区域
  180. //imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image));
  181. }
  182. private void ImageShowFrm_FormClosing(object sender, FormClosingEventArgs e)
  183. {
  184. this.timer1.Enabled = false;
  185. }
  186. //移动电机拍照
  187. private void moveAxis()
  188. {
  189. this.tsbtnPre.Enabled = this.tsbtnNext.Enabled = false;
  190. this.logsts.Text = " 正在移动相机...";
  191. // 开启光源
  192. SysMgr.Instance.LightDev.OpenLight((int)ConfMgr.Instance.SysConfigParams.LightCH);
  193. SysMgr.Instance.LightDev.SetLightDigitalValue((int)ConfMgr.Instance.SysConfigParams.LightCH, ConfMgr.Instance.SysConfigParams.LightLut);
  194. //变焦
  195. SysMgr.Instance.LensMotorDev.MoveAbsPulse(0, new VelocityCurveParams(), (int)SmallAxCmdPos.倍率4_5X);
  196. //判断到位
  197. SysMgr.Instance.LensMotorDev.CheckDone(0, 10000);
  198. // 移动电机
  199. if ((SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis0, 60)<0 )||
  200. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 60) < 0) ||
  201. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis3, 60) < 0))
  202. {
  203. MessageBox.Show("轴状态为不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  204. return;
  205. }
  206. var list = SysMgr.Instance.DefectLib.viewTag(tagIndex, Xmm, Ymm, currBmp.Width, currBmp.Height);
  207. double val = SysMgr.Instance.DefaultSpeed;
  208. int st = SysMgr.Instance.MotionST;
  209. this.logsts.Text = $" 图像信息{tagIndex}-{Xmm}-{Ymm}-{currBmp.Width}-{currBmp.Height},正在移动相机{list[0]}-{list[1]}";
  210. //Z移动
  211. VelocityCurveParams Velocity0 = new VelocityCurveParams(100, 100, 0, 10, 2, SysMgr.Instance.GetMMtoPlus(AxisName.Axis3));
  212. SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis3, Velocity0, ConfMgr.Instance.SysConfigParams.LensAxisZPos.Z_4_5X);
  213. //XY移动
  214. VelocityCurveParams Velocity1 = new VelocityCurveParams(val, val, 0, val, st, SysMgr.Instance.GetMMtoPlus(AxisName.Axis0));
  215. VelocityCurveParams Velocity2 = new VelocityCurveParams(val, val, 0, val, st, SysMgr.Instance.GetMMtoPlus(AxisName.Axis2));
  216. SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis0, Velocity1, list[0]);
  217. SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis2, Velocity2, list[1]);
  218. this.logsts.Text = "";
  219. this.tsbtnPre.Enabled = (picIndex > 0);
  220. this.tsbtnNext.Enabled = (picIndex < picBoxList.Count - 1);
  221. if ((SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis0, 60) < 0) ||
  222. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 60) < 0) ||
  223. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis3, 60) < 0))
  224. {
  225. MessageBox.Show("到位失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  226. return;
  227. }
  228. Thread.Sleep(300);
  229. //拍照
  230. SysMgr.Instance.CamDevFront.SetExposure(ConfMgr.Instance.SysConfigParams.CamExposureTime);
  231. this.logsts.Text = $" 拍照{tagIndex}-{Xmm}-{Ymm}-{currBmp.Width}-{currBmp.Height},正在移动相机{list[0]}-{list[1]}";
  232. this.timer1.Enabled = true;
  233. //SysMgr.Instance.CamDevFront.ClearImageQueue();
  234. //Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 6000);
  235. //if (acq.GrabStatus != "GrabPass")
  236. //{
  237. // MessageBox.Show("拍照失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  238. // return;
  239. //}
  240. //imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image));
  241. }
  242. }
  243. }