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

262 строки
9.6 KiB

  1. using MaiMuAOI.ImageProcessing;
  2. using MaiMuAOI.SysCtrl;
  3. using MaiMuAOI.SysUI.DefectPicShow;
  4. using MaiMuAOI.SysUI.SysSet;
  5. using MaiMuControl.Utils;
  6. using Newtonsoft.Json;
  7. using OpenCvSharp;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Windows.Forms;
  19. using static MaiMuAOI.ImageProcessing.DefectLib;
  20. namespace ProductionControl.UI
  21. {
  22. public partial class UIDefectLib : UserControl
  23. {
  24. SynchronizationContext SyncContext = null;
  25. public Action<string> GetParamsEvent;
  26. //
  27. private DefectLibProp prop = new DefectLibProp(ConfMgr.Instance.SysConfigParams.Defect_CutSize,
  28. ConfMgr.Instance.SysConfigParams.Defect_ReSize, ConfMgr.Instance.SysConfigParams.Defect_Thresholds);
  29. private DefectLib _dev;
  30. public UIDefectLib(DefectLib dev)
  31. {
  32. InitializeComponent();
  33. _dev = dev;
  34. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  35. refreshUIState();
  36. //获取UI线程同步上下文
  37. SyncContext = SynchronizationContext.Current;
  38. //init();
  39. }
  40. private string szLog;
  41. private void refreshUIState()
  42. {
  43. foreach (ToolStripItem item in this.toolStrip1.Items)
  44. {
  45. if (item.Text == "打开设备")
  46. item.Visible = (_dev == null || !_dev.IsInit);
  47. else
  48. this.propertyGrid1.Enabled = item.Enabled = !(_dev == null || !_dev.IsInit);
  49. }
  50. }
  51. public void init()
  52. {
  53. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  54. this.propertyGrid1.SelectedObject = prop;
  55. //_dev = new DefectLib();
  56. _dev.WarningEvent = (level, info) =>
  57. {
  58. szLog = $"({level}){info}";
  59. txtLog.Text = $"({level}){info}";
  60. };
  61. //DATA
  62. _dev.finishEvent += new System.Action<DefectTask>((task) =>
  63. {
  64. DateTime t1= DateTime.Now;
  65. var t2=t1-task.nowTime;
  66. var t3= t2.Milliseconds;
  67. prop.InformationList = JsonConvert.SerializeObject(task.informationList);
  68. prop.resultInfo = task.isSucceed ? "成功" : task.resultInfo;
  69. this.refreshUI();
  70. });
  71. //if (!_dev.start())
  72. //{
  73. // this.closeDev();
  74. // return;
  75. //}
  76. //string onnxFile = $"{Application.StartupPath}\\onnxFiles\\default.onnx";
  77. string onnxFile = $"{ConfMgr.Instance.SysConfigParams.AIModelPath}\\default.onnx";
  78. _dev.loadModelFile(onnxFile);
  79. refreshUIState();
  80. }
  81. public string getParamsData()
  82. {
  83. return prop.serialize();
  84. }
  85. public void setParamsData(string json)
  86. {
  87. if (json == "") return;
  88. prop.deserialize(json);
  89. this.propertyGrid1.Refresh();
  90. }
  91. private void refreshUI()
  92. {
  93. SyncContext.Post(m =>
  94. {
  95. var result = m as string;
  96. propertyGrid1.Refresh();
  97. //txtLog.Text = szLog;//不显示
  98. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  99. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  100. }, "异步操作完成结果");
  101. }
  102. private void closeDev()
  103. {
  104. //try
  105. //{
  106. // if (_dev == null)
  107. // return;
  108. // _dev.stop();
  109. //}
  110. //catch (Exception ex)
  111. //{
  112. // refreshUIState();
  113. // MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  114. //}
  115. }
  116. protected override void OnHandleDestroyed(EventArgs e)
  117. {
  118. base.OnHandleDestroyed(e);
  119. // 在此添加需要手动释放资源的代码
  120. this.closeDev();
  121. if(frm !=null)
  122. frm.Close();
  123. }
  124. private void tbtnRun_Click(object sender, EventArgs e)
  125. {
  126. if (!Util.IsNumber(prop.ThresholdsClass.Replace("-", "").Replace(",", "").Replace(";", "").Replace(".", "")))
  127. {
  128. txtLog.Text = "种类阀值只可输入数字!";
  129. return;
  130. }
  131. if (!verifyThresholdsCount(prop.ThresholdsClass, prop.ThresholdsClassCount))
  132. {
  133. txtLog.Text = "输入种类阀值与数量不一致!";
  134. return;
  135. }
  136. try
  137. {
  138. _dev.loadModelFile(prop.ModelPath);
  139. prop.resultInfo = "";
  140. _dev.add(new DefectTask()
  141. {
  142. nowTime= DateTime.Now,
  143. bmp=new Mat(prop.BmpPath),
  144. cut_size=prop.CutSize,
  145. resize=prop.Resize,
  146. thresholds=prop.Thresholds,
  147. thresholdsClass = prop.ThresholdsClass,
  148. //finishEvent = (res) =>
  149. //{
  150. //}
  151. });
  152. }
  153. catch(Exception ex)
  154. {
  155. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  156. }
  157. }
  158. private void tbtnExport_Click(object sender, EventArgs e)
  159. {
  160. string filePath = ConfMgr.SaveAsFile($"DefectLib Config.json", "JSON|*.json");
  161. if (filePath != "")
  162. {
  163. string jsonText = prop.serialize();
  164. File.WriteAllText(filePath, jsonText);
  165. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  166. }
  167. }
  168. private void tbtnImport_Click(object sender, EventArgs e)
  169. {
  170. string filePath = ConfMgr.SelectFile("JSON|*.json");
  171. if (filePath != "")
  172. {
  173. string jsonText = File.ReadAllText(filePath);
  174. prop.deserialize(jsonText);
  175. this.propertyGrid1.Refresh();
  176. //this.propertyGrid1.SelectedObject= prop;
  177. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  178. }
  179. }
  180. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  181. {
  182. //其中包含了两个重要的属性:OldValue和ChangeItem。
  183. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  184. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  185. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  186. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  187. var oldValue = e.OldValue;
  188. var newValue = e.ChangedItem.Value;
  189. switch (propertyName)
  190. {
  191. case "ThresholdsClass":
  192. if(!Util.IsNumber(newValue.ToString().Replace("-","").Replace(",", "").Replace(";", "").Replace(".", "")))
  193. txtLog.Text = "种类阀值只可输入数字!";
  194. else if (!verifyThresholdsCount(newValue.ToString(), prop.ThresholdsClassCount))
  195. txtLog.Text = "输入种类阀值与数量不一致!";
  196. else
  197. txtLog.Text = "";
  198. break;
  199. case "ThresholdsClassCount":
  200. if (!verifyThresholdsCount(prop.ThresholdsClass, (int)newValue))
  201. txtLog.Text = "输入种类阀值与数量不一致!";
  202. else
  203. txtLog.Text = "";
  204. break;
  205. }
  206. }
  207. private void tbtnSave_Click(object sender, EventArgs e)
  208. {
  209. if (!Util.IsNumber(prop.ThresholdsClass.Replace("-", "").Replace(",", "").Replace(";", "").Replace(".", "")))
  210. {
  211. txtLog.Text = "种类阀值只可输入数字!";
  212. return;
  213. }
  214. if (!verifyThresholdsCount(prop.ThresholdsClass, prop.ThresholdsClassCount))
  215. {
  216. txtLog.Text = "输入种类阀值与数量不一致!";
  217. return;
  218. }
  219. GetParamsEvent?.Invoke(prop.serialize());
  220. }
  221. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  222. {
  223. szLog = txtLog.Text = "";
  224. this.init();
  225. }
  226. private bool verifyThresholdsCount(string szThresholdsClass,int iThresholdsClassCount)
  227. {
  228. return szThresholdsClass.Trim().TrimEnd(new char[] { ',', ';' })
  229. .Split(new char[] { ',', ';' }).Count() == iThresholdsClassCount;
  230. }
  231. DebugTestFrm frm;
  232. private void tsbDebug_Click(object sender, EventArgs e)
  233. {
  234. if ((frm == null) || (frm.IsDisposed))
  235. frm = new DebugTestFrm();
  236. frm.TopMost = true;
  237. frm.Show();
  238. }
  239. }
  240. }