SettingPage.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using CCDCount.DLL;
  2. using CCDCount.MODEL.CameraClass;
  3. using CCDCount.MODEL.ConfigModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Input;
  11. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
  12. namespace CCDCountWpf.WpfPage
  13. {
  14. /// <summary>
  15. /// SettingPage.xaml 的交互逻辑
  16. /// </summary>
  17. public partial class SettingPage : Page
  18. {
  19. public SettingPage()
  20. {
  21. InitializeComponent();
  22. DataContext = ShowMessageBus.ShowBinding;
  23. }
  24. private void Page_Loaded(object sender, RoutedEventArgs e)
  25. {
  26. if (MessageBus.NowSettingLoadMainThread != null)
  27. {
  28. CamSelectConBox.SelectedValue = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
  29. if(MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode == -1)
  30. {
  31. CheckModel1RadBtn.IsChecked = true;
  32. }
  33. else if(MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode == 0)
  34. {
  35. CheckModel2RadBtn.IsChecked = true;
  36. }
  37. else if (MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode == 1)
  38. {
  39. CheckModel3RadBtn.IsChecked = true;
  40. }
  41. else if (MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode == 2)
  42. {
  43. CheckModel4RadBtn.IsChecked = true;
  44. }
  45. }
  46. else
  47. {
  48. CamSelectConBox.SelectedValue = "";
  49. }
  50. }
  51. private void CameraSettingBtn_Click(object sender, RoutedEventArgs e)
  52. {
  53. CameraSettingGrid.Visibility = Visibility.Visible;
  54. ShuLiSettingGrid.Visibility = Visibility.Collapsed;
  55. }
  56. private void ShuLiSettingBtn_Click(object sender, RoutedEventArgs e)
  57. {
  58. CameraSettingGrid.Visibility = Visibility.Collapsed;
  59. ShuLiSettingGrid.Visibility = Visibility.Visible;
  60. }
  61. private void CamListFlushBtn_Click(object sender, RoutedEventArgs e)
  62. {
  63. List<CameraInfoClass> CameraInfoList = new List<CameraInfoClass>();
  64. MessageBus.CameraClass.GetCameraList(out CameraInfoList);
  65. if (CameraInfoList.Count > 0)
  66. {
  67. ShowMessageBus.ShowBinding.CameraItems.Clear();
  68. ShowMessageBus.ShowBinding.CameraItems.Add(new CameraCoboxItem() { Name = "请选择相机", SNValue = "" });
  69. foreach (var item in CameraInfoList)
  70. {
  71. ShowMessageBus.ShowBinding.CameraItems.Add(new CameraCoboxItem()
  72. {
  73. Name = item.DeviceName + "_" + item.DeviceSN,
  74. SNValue = item.DeviceSN
  75. });
  76. }
  77. CamSelectConBox.SelectedValue = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
  78. }
  79. }
  80. private void CamSelectConBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  81. {
  82. if(ShowMessageBus.ShowBinding.CameraItems.Count == 0) return;
  83. //string ThisItemSN = ShowMessageBus.ShowBinding.CameraSNNum;
  84. string ThisItemSN = CamSelectConBox.SelectedValue.ToString();
  85. if (MessageBus.NowSettingLoadMainThread == null || ThisItemSN == MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum) return;
  86. if (ThisItemSN != string.Empty && ThisItemSN != null)
  87. {
  88. if (MessageBus.NowSettingLoadMainThread.CameraRunStatic)
  89. {
  90. var confirmResult = MessageBox.Show("检测到正在识别,切换相机需要关闭识别,是否继续切换",
  91. "切换确认", MessageBoxButton.YesNo);
  92. if (confirmResult == MessageBoxResult.No)
  93. {
  94. CamSelectConBox.SelectedValue = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
  95. return;
  96. }
  97. MessageBus.NowSettingLoadMainThread.StopMianThread();
  98. }
  99. if (MessageBus.NowSettingLoadMainThread.ReLoadCamera(ThisItemSN))
  100. {
  101. MessageBus.NowSettingLoadMainThread.GetCameraConfig();
  102. ShowMessageBus.ShowBinding.ImageWidth = MessageBus.NowSettingLoadMainThread.cameraConfig.Width.ToString();
  103. ShowMessageBus.ShowBinding.CamUserName = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraName;
  104. ShowMessageBus.ShowBinding.CamOffsetX = MessageBus.NowSettingLoadMainThread.cameraConfig.OffsetX.ToString();
  105. ShowMessageBus.ShowBinding.ExposureTimeValue = MessageBus.NowSettingLoadMainThread.cameraConfig.ExposureTimeValue.ToString();
  106. ShowMessageBus.ShowBinding.AcquistionLineRate = MessageBus.NowSettingLoadMainThread.cameraConfig.AcquistionLineRateValue.ToString();
  107. ShowMessageBus.ShowBinding.CameraSNNum = ThisItemSN;
  108. }
  109. else
  110. {
  111. MessageBox.Show("切换相机失败!");
  112. CamSelectConBox.SelectedItem = MessageBus.NowSettingLoadMainThread.cameraConfig.DeviceName + "_" + MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
  113. return;
  114. }
  115. }
  116. else
  117. {
  118. if (MessageBus.NowSettingLoadMainThread.CameraRunStatic)
  119. {
  120. var confirmResult = MessageBox.Show("检测到正在识别,切换相机需要关闭识别,是否继续切换",
  121. "切换确认", MessageBoxButton.YesNo);
  122. if (confirmResult == MessageBoxResult.No)
  123. {
  124. CamSelectConBox.SelectedValue = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
  125. return;
  126. }
  127. MessageBus.NowSettingLoadMainThread.StopMianThread();
  128. }
  129. else
  130. {
  131. MessageBus.NowSettingLoadMainThread.DisposeCamera();
  132. }
  133. ShowMessageBus.ShowBinding.ImageWidth = "0";
  134. ShowMessageBus.ShowBinding.CamUserName = string.Empty;
  135. ShowMessageBus.ShowBinding.CamOffsetX = "0";
  136. ShowMessageBus.ShowBinding.ExposureTimeValue = "0";
  137. ShowMessageBus.ShowBinding.AcquistionLineRate = "0";
  138. ShowMessageBus.ShowBinding.CameraSNNum = null;
  139. }
  140. }
  141. private void CheckModel1RadBtn_Checked(object sender, RoutedEventArgs e)
  142. {
  143. ShowMessageBus.ShowBinding.PandingCode = "-1";
  144. WHParaPanel.Visibility = Visibility.Collapsed;
  145. AreaParaPanel.Visibility = Visibility.Collapsed;
  146. }
  147. private void CheckModel2RadBtn_Checked(object sender, RoutedEventArgs e)
  148. {
  149. ShowMessageBus.ShowBinding.PandingCode = "0";
  150. WHParaPanel.Visibility = Visibility.Visible;
  151. AreaParaPanel.Visibility = Visibility.Collapsed;
  152. }
  153. private void CheckModel3RadBtn_Checked(object sender, RoutedEventArgs e)
  154. {
  155. ShowMessageBus.ShowBinding.PandingCode = "1";
  156. WHParaPanel.Visibility = Visibility.Collapsed;
  157. AreaParaPanel.Visibility = Visibility.Visible;
  158. }
  159. private void CheckModel4RadBtn_Checked(object sender, RoutedEventArgs e)
  160. {
  161. ShowMessageBus.ShowBinding.PandingCode = "2";
  162. WHParaPanel.Visibility = Visibility.Visible;
  163. AreaParaPanel.Visibility = Visibility.Visible;
  164. }
  165. //private void SaveAllConfigBtn_Click(object sender, RoutedEventArgs e)
  166. //{
  167. // List<SaveConfigModel> SaveConfig = new List<SaveConfigModel>();
  168. // if(MessageBus.MainThreadS.Count <= 0)
  169. // {
  170. // return;
  171. // }
  172. // foreach (var item in MessageBus.MainThreadS)
  173. // {
  174. // SaveConfig.Add(new SaveConfigModel()
  175. // {
  176. // CameraConfig = item.cameraConfig,
  177. // ShuLiConfigClass = item.shuLiConfig
  178. // });
  179. // }
  180. // if (!Directory.Exists(".\\Config\\")) Directory.CreateDirectory(".\\Config\\");
  181. // {
  182. // XmlStorage.SerializeToXml(SaveConfig, ".\\Config\\CCDCountConfig.xml");
  183. // }
  184. //}
  185. /// <summary>
  186. /// 加载配方
  187. /// </summary>
  188. /// <param name="FormulationConfig"></param>
  189. private void LoadFormulation(FormulationConfigClass FormulationConfig)
  190. {
  191. ShowMessageBus.ShowBinding.AcquistionLineRate = FormulationConfig.AcquistionLineRateValue.ToString();
  192. ShowMessageBus.ShowBinding.ExposureTimeValue = FormulationConfig.ExposureTimeValue.ToString();
  193. ShowMessageBus.ShowBinding.Channel = FormulationConfig.Channel.ToString();
  194. ShowMessageBus.ShowBinding.FormulationName = FormulationConfig.FormulationName;
  195. ShowMessageBus.ShowBinding.MAX_OBJECT_LENGTH = FormulationConfig.MAX_Object_LENGTH.ToString();
  196. ShowMessageBus.ShowBinding.MIN_OBJECT_LENGTH = FormulationConfig.MIN_Object_LENGTH.ToString();
  197. ShowMessageBus.ShowBinding.PandingCode = FormulationConfig.PandingCode.ToString();
  198. ShowMessageBus.ShowBinding.RegionThreshold = FormulationConfig.RegionThreshold.ToString();
  199. }
  200. private void ParametricTrainingBtn_Click(object sender, RoutedEventArgs e)
  201. {
  202. if(string.IsNullOrEmpty(KeLiW_TBX.Text)||string.IsNullOrEmpty(KeLiL_TBX.Text))
  203. {
  204. MessageBox.Show("请输入标准颗粒的宽高");
  205. return;
  206. }
  207. int KeliW = Convert.ToInt32(KeLiW_TBX.Text);
  208. int KeliL = Convert.ToInt32(KeLiL_TBX.Text);
  209. var paravalue = MessageBus.NowSettingLoadMainThread.ParameterTrain(KeliW, KeliL);
  210. ShowMessageBus.ShowBinding.NoiseFilter = paravalue.NoiseFilterThreshold.ToString();
  211. ShowMessageBus.ShowBinding.MaxArea = ((int)(paravalue.MaxArea*1.1)).ToString();
  212. ShowMessageBus.ShowBinding.MinArea = ((int)(paravalue.MinArea*0.9)).ToString();
  213. ShowMessageBus.ShowBinding.MAX_OBJECT_LENGTH = ((int)(paravalue.MaxLength)).ToString();
  214. ShowMessageBus.ShowBinding.MIN_OBJECT_LENGTH = ((int)(paravalue.MinLength)).ToString();
  215. }
  216. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  217. {
  218. if (e.Key == Key.Enter)
  219. {
  220. TextBox textBox = sender as TextBox;
  221. if (textBox != null)
  222. {
  223. // 强制更新源数据
  224. BindingExpression bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
  225. bindingExpression.UpdateSource();
  226. }
  227. }
  228. }
  229. }
  230. }