版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

248 satır
8.4 KiB

  1. using MaiMuAOI.ImageProcessing;
  2. using MaiMuAOI.SysCtrl;
  3. using MaiMuAOI.SysUI.DefectPicShow;
  4. using MaiMuAOI.SysUI.SysSet;
  5. using Newtonsoft.Json;
  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 System.Xml.Linq;
  18. using static MaiMuAOI.ImageProcessing.SizeLib;
  19. namespace ProductionControl.UI
  20. {
  21. public partial class UISizeLib : UserControl
  22. {
  23. SynchronizationContext SyncContext = null;
  24. public Action<string> GetParamsEvent;
  25. //
  26. private SizeLibProp prop = new SizeLibProp();
  27. private SizeLib _dev;
  28. public UISizeLib(SizeLib dev)
  29. {
  30. InitializeComponent();
  31. _dev = dev;
  32. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  33. refreshUIState();
  34. //获取UI线程同步上下文
  35. SyncContext = SynchronizationContext.Current;
  36. //init();
  37. }
  38. private string szLog;
  39. private void refreshUIState()
  40. {
  41. foreach (ToolStripItem item in this.toolStrip1.Items)
  42. {
  43. if (item.Text == "打开设备")
  44. item.Visible = (_dev == null || !_dev.IsInit);
  45. else
  46. this.propertyGrid1.Enabled = item.Enabled = !(_dev == null || !_dev.IsInit);
  47. }
  48. }
  49. public void init()
  50. {
  51. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  52. this.propertyGrid1.SelectedObject = prop;
  53. //_dev = new SizeLib();
  54. _dev.WarningEvent = (level, info) =>
  55. {
  56. szLog = $"({level}){info}";
  57. txtLog.Text = $"({level}){info}";
  58. };
  59. //DATA
  60. _dev.finishEvent += new System.Action<SizeTask>((task) =>
  61. {
  62. prop.PT1 = task.PT1;
  63. prop.PT2 = task.PT2;
  64. prop.Shanxian = task.Shanxian;
  65. //prop.RowP= task.RowP;
  66. prop.Circle_Ymm = task.Circle_Ymm;
  67. prop.Circle_Xmm = task.Circle_Xmm;
  68. prop.offsetX = task.offsetX;
  69. prop.offsetY = task.offsetY;
  70. prop.MarkPointList = task.MarkPointList;
  71. prop.resultInfo = task.isSucceed ? "成功" : task.resultInfo;
  72. this.refreshUI();
  73. });
  74. //if (!_dev.start(ConfMgr.Instance.SysConfigParams.ImageProcessPath))
  75. //{
  76. // this.closeDev();
  77. // return;
  78. //}
  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. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  98. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  99. }, "异步操作完成结果");
  100. }
  101. private void closeDev()
  102. {
  103. //try
  104. //{
  105. // if (_dev == null)
  106. // return;
  107. // _dev.stop();
  108. //}
  109. //catch (Exception ex)
  110. //{
  111. // refreshUIState();
  112. // MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  113. //}
  114. }
  115. protected override void OnHandleDestroyed(EventArgs e)
  116. {
  117. base.OnHandleDestroyed(e);
  118. // 在此添加需要手动释放资源的代码
  119. this.closeDev();
  120. if(frm!=null)
  121. frm.Close();
  122. }
  123. private void tbtnRun_Click(object sender, EventArgs e)
  124. {
  125. try
  126. {
  127. prop.resultInfo = "";
  128. _dev.add(new SizeTask()
  129. {
  130. engineName = prop.EngineName,
  131. file_path = prop.BmpPath,
  132. //bmp =read2Bmp(prop.BmpPath),
  133. index=prop.Index,
  134. //finishEvent = (res) =>
  135. //{
  136. //}
  137. });
  138. }
  139. catch(Exception ex)
  140. {
  141. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  142. }
  143. }
  144. private void tbtnExport_Click(object sender, EventArgs e)
  145. {
  146. string filePath = ConfMgr.SaveAsFile($"SizeLib Config.json", "JSON|*.json");
  147. if (filePath != "")
  148. {
  149. string jsonText = prop.serialize();
  150. File.WriteAllText(filePath, jsonText);
  151. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  152. }
  153. }
  154. private void tbtnImport_Click(object sender, EventArgs e)
  155. {
  156. string filePath = ConfMgr.SelectFile("JSON|*.json");
  157. if (filePath != "")
  158. {
  159. string jsonText = File.ReadAllText(filePath);
  160. prop.deserialize(jsonText);
  161. this.propertyGrid1.Refresh();
  162. //this.propertyGrid1.SelectedObject= prop;
  163. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  164. }
  165. }
  166. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  167. {
  168. //其中包含了两个重要的属性:OldValue和ChangeItem。
  169. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  170. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  171. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  172. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  173. var oldValue = e.OldValue;
  174. var newValue = e.ChangedItem.Value;
  175. //switch (propertyName)
  176. //{
  177. //case "BmpPath":
  178. // if(newValue)
  179. // {
  180. // prop.BmpPath = (int)oldValue;
  181. // this.refreshUI();
  182. // }
  183. // else
  184. // {
  185. // dev.getDigitalValue((int)newValue);
  186. // }
  187. // break;
  188. //case "DigitalValue":
  189. // if ((int)newValue < 0 || (int)newValue > 100)
  190. // {
  191. // prop.DigitalValue = (int)oldValue;
  192. // this.refreshUI();
  193. // }
  194. // break;
  195. //}
  196. }
  197. private void tbtnSave_Click(object sender, EventArgs e)
  198. {
  199. GetParamsEvent?.Invoke(prop.serialize());
  200. }
  201. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  202. {
  203. this.init();
  204. }
  205. private Bitmap read2Bmp(string path)
  206. {
  207. FileStream fs = new FileStream(path, FileMode.Open);
  208. byte[] bmpdata = new byte[fs.Length];
  209. fs.Read(bmpdata, 0, bmpdata.Length);
  210. Bitmap bmp = new Bitmap(fs);
  211. fs.Close();
  212. return bmp;
  213. }
  214. //2023- 10-27 新增图纸选点
  215. private void tbtnGetPos_Click(object sender, EventArgs e)
  216. {
  217. //FrmGetPosByPic frm = new FrmGetPosByPic(prop);
  218. //frm.ShowDialog();
  219. }
  220. DebugTestFrm frm;
  221. private void tsbDebug_Click(object sender, EventArgs e)
  222. {
  223. if ((frm == null) || (frm.IsDisposed))
  224. frm = new DebugTestFrm();
  225. frm.TopMost = true;
  226. frm.Show();
  227. }
  228. }
  229. }