版博士V2.0程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

269 řádky
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. string[] tags = picBox.Tag.ToString().Split(',');
  112. this.tagIndex = Convert.ToInt32(tags[0]);
  113. this.Xmm = Convert.ToDouble(tags[1]);
  114. this.Ymm = Convert.ToDouble(tags[2]);
  115. //tsStateMsg.Text = $"索引:{tagIndex}, X:{Xmm}mm, Y:{Ymm}mm";
  116. tsStateStep.Text = $"第{index + 1}/{picBoxList.Count}张";
  117. currBmp = (Bitmap)picBox.Image;
  118. OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(currBmp);//用bitmap转换为mat
  119. this.imageBox1.RefreshWindow(mat);
  120. //
  121. this.tsbtnPre.Enabled = (index > 0);
  122. this.tsbtnNext.Enabled = (index < picBoxList.Count - 1);
  123. if (IsMove)
  124. moveAxis();
  125. }
  126. catch (Exception ex)
  127. {
  128. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  129. }
  130. }
  131. private void ImageShowFrm_Resize(object sender, EventArgs e)
  132. {
  133. if (!IsMove)
  134. {
  135. imageBox2.Visible = false;
  136. imageBox1.Top = 0;
  137. imageBox1.Left = this.panel1.Left;
  138. imageBox1.Width = this.panel1.Width;
  139. imageBox1.Height = this.panel1.Height;
  140. }
  141. else
  142. {
  143. imageBox2.Visible = true;
  144. imageBox1.Top = 0;
  145. imageBox1.Left = this.panel1.Left;
  146. imageBox1.Width = this.panel1.Width / 2 - 5;
  147. imageBox1.Height = this.panel1.Height;
  148. imageBox2.Top = 0;
  149. imageBox2.Left = this.imageBox1.Width + 10;
  150. imageBox2.Width = this.panel1.Width / 2 - 5;
  151. imageBox2.Height = this.panel1.Height;
  152. }
  153. this.Refresh();
  154. }
  155. DebugTestFrm frm;
  156. private void tsbDebug_Click(object sender, EventArgs e)
  157. {
  158. if ((frm == null) || (frm.IsDisposed))
  159. frm = new DebugTestFrm();
  160. //frm.TopMost = true;
  161. frm.Show();
  162. }
  163. private void timer1_Tick(object sender, EventArgs e)
  164. {
  165. SysMgr.Instance.CamDevFront.ClearImageQueue();
  166. Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 600);
  167. if (acq.GrabStatus != "GrabPass")
  168. {
  169. //MessageBox.Show("拍照失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  170. return;
  171. }
  172. if (imageBox2.Image == null)
  173. imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image), ImageBox.ImageModeEnum.Zoom);//可以不显示区域
  174. else
  175. imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image), ImageBox.ImageModeEnum.Part);//可以不显示区域
  176. //imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image));
  177. }
  178. private void ImageShowFrm_FormClosing(object sender, FormClosingEventArgs e)
  179. {
  180. this.timer1.Enabled = false;
  181. }
  182. //移动电机拍照
  183. private void moveAxis()
  184. {
  185. this.tsbtnPre.Enabled = this.tsbtnNext.Enabled = false;
  186. this.logsts.Text = " 正在移动相机...";
  187. // 开启光源
  188. SysMgr.Instance.LightDev.OpenLight((int)ConfMgr.Instance.SysConfigParams.LightCH);
  189. SysMgr.Instance.LightDev.SetLightDigitalValue((int)ConfMgr.Instance.SysConfigParams.LightCH, ConfMgr.Instance.SysConfigParams.LightLut);
  190. //变焦
  191. SysMgr.Instance.LensMotorDev.MoveAbsPulse(0, new VelocityCurveParams(), (int)SmallAxCmdPos.倍率4_5X);
  192. //判断到位
  193. SysMgr.Instance.LensMotorDev.CheckDone(0, 10000);
  194. // 移动电机
  195. if ((SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis0, 60)<0 )||
  196. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 60) < 0) ||
  197. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis3, 60) < 0))
  198. {
  199. MessageBox.Show("轴状态为不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  200. return;
  201. }
  202. var list = SysMgr.Instance.DefectLib.viewTag(tagIndex, Xmm, Ymm, currBmp.Width, currBmp.Height);
  203. double val = SysMgr.Instance.DefaultSpeed;
  204. int st = SysMgr.Instance.MotionST;
  205. this.logsts.Text = $" 图像信息{tagIndex}-{Xmm}-{Ymm}-{currBmp.Width}-{currBmp.Height},正在移动相机{list[0]}-{list[1]}";
  206. //Z移动
  207. VelocityCurveParams Velocity0 = new VelocityCurveParams(100, 100, 0, 10, 2, SysMgr.Instance.GetMMtoPlus(AxisName.Axis3));
  208. SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis3, Velocity0, ConfMgr.Instance.SysConfigParams.LensAxisZPos.Z_4_5X);
  209. //XY移动
  210. VelocityCurveParams Velocity1 = new VelocityCurveParams(val, val, 0, val, st, SysMgr.Instance.GetMMtoPlus(AxisName.Axis0));
  211. VelocityCurveParams Velocity2 = new VelocityCurveParams(val, val, 0, val, st, SysMgr.Instance.GetMMtoPlus(AxisName.Axis2));
  212. SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis0, Velocity1, list[0]);
  213. SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis2, Velocity2, list[1]);
  214. this.logsts.Text = "";
  215. this.tsbtnPre.Enabled = (picIndex > 0);
  216. this.tsbtnNext.Enabled = (picIndex < picBoxList.Count - 1);
  217. if ((SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis0, 60) < 0) ||
  218. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 60) < 0) ||
  219. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis3, 60) < 0))
  220. {
  221. MessageBox.Show("到位失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  222. return;
  223. }
  224. Thread.Sleep(300);
  225. //拍照
  226. SysMgr.Instance.CamDevFront.SetExposure(ConfMgr.Instance.SysConfigParams.CamExposureTime);
  227. this.logsts.Text = $" 拍照{tagIndex}-{Xmm}-{Ymm}-{currBmp.Width}-{currBmp.Height},正在移动相机{list[0]}-{list[1]}";
  228. this.timer1.Enabled = true;
  229. //SysMgr.Instance.CamDevFront.ClearImageQueue();
  230. //Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 6000);
  231. //if (acq.GrabStatus != "GrabPass")
  232. //{
  233. // MessageBox.Show("拍照失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  234. // return;
  235. //}
  236. //imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image));
  237. }
  238. }
  239. }