版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

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