版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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