版博士V2.0程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

244 lines
8.7 KiB

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