版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

291 rivejä
9.7 KiB

  1. using Advantech.Motion;
  2. using Newtonsoft.Json;
  3. using AssistClient.Device;
  4. using AssistClient.Utils;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Drawing.Imaging;
  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 AssistClient.Device.ScannerDev;
  19. namespace AssistClient.UI
  20. {
  21. public partial class UIScannerDev : UserControl
  22. {
  23. SynchronizationContext SyncContext = null;
  24. public Action<string> GetParamsEvent;
  25. public ScannerType devType { get; set; }
  26. private List<string> pathList = new List<string>();
  27. //
  28. private ScannerDevProp prop;
  29. private ScannerDev dev;
  30. public UIScannerDev()
  31. {
  32. InitializeComponent();
  33. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  34. refreshUIState();
  35. //获取UI线程同步上下文
  36. SyncContext = SynchronizationContext.Current;
  37. //init();
  38. }
  39. public string getParamsData()
  40. {
  41. return prop.serialize();
  42. }
  43. public void setParamsData(string json)
  44. {
  45. if (json == "") return;
  46. prop.deserialize(json);
  47. this.propertyGrid1.Refresh();
  48. }
  49. private void refreshUIState()
  50. {
  51. foreach (ToolStripItem item in this.toolStrip1.Items)
  52. {
  53. if (item.Text == "打开设备")
  54. item.Visible = (dev == null || !dev.IsInit);
  55. else
  56. this.propertyGrid1.Enabled = item.Enabled = !(dev == null || !dev.IsInit);
  57. }
  58. }
  59. public void init(ScannerType devType)
  60. {
  61. pathList.Clear();
  62. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  63. prop = new ScannerDevProp(devType);
  64. this.propertyGrid1.SelectedObject = prop;
  65. dev = new ScannerDev(prop.DeviceType);
  66. dev.WarningEvent = (level, info) =>
  67. {
  68. txtLog.Text = $"({level}){info}";
  69. };
  70. //DATA
  71. dev.ScanEventPath += new System.Action<int, string>((num, path) =>
  72. {
  73. //string path = Config.Scnaner_Bmp_SavePath + "\\" + DateTime.Now.Ticks + ".bmp";
  74. //bmp.Save(path, ImageFormat.Bmp);
  75. //MemoryStream ms = new MemoryStream();
  76. //bmp.Save(ms, ImageFormat.Bmp);
  77. //byte[] bs = ms.ToArray(); //GetBuffer();
  78. //ms.Close();
  79. //ms.Dispose();
  80. pathList.Add(path);
  81. prop.ImageList=pathList.ToArray();
  82. this.refreshUI();
  83. if (!dev.isContinuousMode)
  84. updateViewWin(read2Bmp(path));
  85. });
  86. dev.ScanEvent += new System.Action<int, Bitmap>((num, bmp) =>
  87. {
  88. string path;
  89. if(prop.DeviceType== ScannerType.GENTL)
  90. path = Config.Defect_SavePath + "\\" + DateTime.Now.Ticks + ".bmp";
  91. else
  92. path = Config.SizeBmp_Path + "\\" + DateTime.Now.Ticks + ".bmp";
  93. bmp.Save(path, ImageFormat.Bmp);
  94. //MemoryStream ms = new MemoryStream();
  95. //bmp.Save(ms, ImageFormat.Bmp);
  96. //byte[] bs = ms.ToArray(); //GetBuffer();
  97. //ms.Close();
  98. //ms.Dispose();
  99. pathList.Add(path);
  100. prop.ImageList = pathList.ToArray();
  101. this.refreshUI();
  102. if (!dev.isContinuousMode)
  103. updateViewWin(bmp);
  104. });
  105. if (!dev.open())
  106. {
  107. this.closeDev();
  108. return;
  109. }
  110. if (!dev.start(IntPtr.Zero,Config.SizeBmp_Path))
  111. {
  112. this.closeDev();
  113. return;
  114. }
  115. dev.getParam();
  116. prop.ExposureTime = dev.ExposureTime;
  117. prop.Gain = dev.Gain;
  118. prop.ResultingFrameRate = dev.ResultingFrameRate;
  119. refreshUIState();
  120. }
  121. private void updateViewWin(Bitmap bmp)
  122. {
  123. if(frm!=null && API.IsWindow(frm.Handle))
  124. frm.updateBmp(bmp);
  125. }
  126. private Bitmap read2Bmp(string path)
  127. {
  128. MemoryStream ms = new System.IO.MemoryStream(File.ReadAllBytes(path));
  129. Bitmap bmp = new Bitmap(ms);
  130. ms.Close();
  131. ms.Dispose();
  132. return bmp;
  133. }
  134. private void refreshUI()
  135. {
  136. SyncContext.Post(m =>
  137. {
  138. var result = m as string;
  139. propertyGrid1.Refresh();
  140. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  141. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  142. }, "异步操作完成结果");
  143. }
  144. private void closeDev()
  145. {
  146. try
  147. {
  148. if (dev == null)
  149. return;
  150. dev.stop();
  151. dev.close();
  152. }
  153. catch (Exception ex)
  154. {
  155. refreshUIState();
  156. //MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  157. }
  158. }
  159. protected override void OnHandleDestroyed(EventArgs e)
  160. {
  161. base.OnHandleDestroyed(e);
  162. // 在此添加需要手动释放资源的代码
  163. this.closeDev();
  164. }
  165. private void tbtnRun_Click(object sender, EventArgs e)
  166. {
  167. try
  168. {
  169. dev.setParam(prop.ExposureTime,prop.Gain,prop.ResultingFrameRate);
  170. dev.getParam();
  171. prop.ExposureTime = dev.ExposureTime;
  172. prop.Gain = dev.Gain;
  173. prop.ResultingFrameRate = dev.ResultingFrameRate;
  174. }
  175. catch(Exception ex)
  176. {
  177. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  178. }
  179. }
  180. private void tbtnExport_Click(object sender, EventArgs e)
  181. {
  182. string filePath = FileUtil.saveAsFile($"Scanner_config.json", "JSON|*.json");
  183. if (filePath != "")
  184. {
  185. string jsonText = prop.serialize();
  186. File.WriteAllText(filePath, jsonText);
  187. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  188. }
  189. }
  190. private void tbtnImport_Click(object sender, EventArgs e)
  191. {
  192. string filePath = FileUtil.openFile("JSON|*.json");
  193. if (filePath != "")
  194. {
  195. string jsonText = File.ReadAllText(filePath);
  196. prop.deserialize(jsonText);
  197. this.propertyGrid1.Refresh();
  198. //this.propertyGrid1.SelectedObject= prop;
  199. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  200. }
  201. }
  202. private void tbtnScan_Click(object sender, EventArgs e)
  203. {
  204. prop.ImageList = new string[dev.isContinuousMode ? 2 : 1];
  205. dev.scan(prop.ImageList.Length);
  206. }
  207. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  208. {
  209. //其中包含了两个重要的属性:OldValue和ChangeItem。
  210. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  211. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  212. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  213. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  214. var oldValue = e.OldValue;
  215. var newValue = e.ChangedItem.Value;
  216. switch (propertyName)
  217. {
  218. //case "ChannelIndex":
  219. // if((int)newValue <1 || (int)newValue >4)
  220. // {
  221. // prop.ChannelIndex = (int)oldValue;
  222. // this.refreshUI();
  223. // }
  224. // else
  225. // {
  226. // dev.getDigitalValue((int)newValue);
  227. // }
  228. // break;
  229. }
  230. }
  231. private void tbtnMode_Click(object sender, EventArgs e)
  232. {
  233. if (!dev.isContinuousMode)
  234. {
  235. dev.setMode(true);
  236. tbtnMode.Text = "软触发";
  237. updateViewWin(null);
  238. dev.setPreviewWin(frm.picHwnd);
  239. }
  240. else
  241. {
  242. dev.setMode(false);
  243. tbtnMode.Text = "连续";
  244. dev.setPreviewWin(IntPtr.Zero);
  245. }
  246. }
  247. FrmScannerShow frm;
  248. private void tbtnShow_Click(object sender, EventArgs e)
  249. {
  250. if(frm!=null && !frm.IsDisposed)
  251. frm.Close();
  252. frm = new FrmScannerShow(dev.size);
  253. frm.Show();
  254. if(dev.isContinuousMode)
  255. dev.setPreviewWin(frm.picHwnd);
  256. }
  257. private void tbtnSave_Click(object sender, EventArgs e)
  258. {
  259. GetParamsEvent?.Invoke(prop.serialize());
  260. }
  261. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  262. {
  263. init(devType);
  264. }
  265. }
  266. }