SettingPage.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.ImageHeight = MessageBus.NowSettingLoadMainThread.cameraConfig.Height.ToString();
  104. ShowMessageBus.ShowBinding.CamUserName = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraName;
  105. ShowMessageBus.ShowBinding.CamOffsetX = MessageBus.NowSettingLoadMainThread.cameraConfig.OffsetX.ToString();
  106. ShowMessageBus.ShowBinding.ExposureTimeValue = MessageBus.NowSettingLoadMainThread.cameraConfig.ExposureTimeValue.ToString();
  107. ShowMessageBus.ShowBinding.AcquistionLineRate = MessageBus.NowSettingLoadMainThread.cameraConfig.AcquistionLineRateValue.ToString();
  108. ShowMessageBus.ShowBinding.CameraSNNum = ThisItemSN;
  109. }
  110. else
  111. {
  112. MessageBox.Show("切换相机失败!");
  113. CamSelectConBox.SelectedItem = MessageBus.NowSettingLoadMainThread.cameraConfig.DeviceName + "_" + MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
  114. return;
  115. }
  116. }
  117. else
  118. {
  119. if (MessageBus.NowSettingLoadMainThread.CameraRunStatic)
  120. {
  121. var confirmResult = MessageBox.Show("检测到正在识别,切换相机需要关闭识别,是否继续切换",
  122. "切换确认", MessageBoxButton.YesNo);
  123. if (confirmResult == MessageBoxResult.No)
  124. {
  125. CamSelectConBox.SelectedValue = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
  126. return;
  127. }
  128. MessageBus.NowSettingLoadMainThread.StopMianThread();
  129. }
  130. else
  131. {
  132. MessageBus.NowSettingLoadMainThread.DisposeCamera();
  133. }
  134. ShowMessageBus.ShowBinding.ImageWidth = "0";
  135. ShowMessageBus.ShowBinding.ImageHeight = "0";
  136. ShowMessageBus.ShowBinding.CamUserName = string.Empty;
  137. ShowMessageBus.ShowBinding.CamOffsetX = "0";
  138. ShowMessageBus.ShowBinding.ExposureTimeValue = "0";
  139. ShowMessageBus.ShowBinding.AcquistionLineRate = "0";
  140. ShowMessageBus.ShowBinding.CameraSNNum = null;
  141. }
  142. }
  143. private void CheckModel1RadBtn_Checked(object sender, RoutedEventArgs e)
  144. {
  145. ShowMessageBus.ShowBinding.PandingCode = "-1";
  146. WHParaPanel.Visibility = Visibility.Collapsed;
  147. AreaParaPanel.Visibility = Visibility.Collapsed;
  148. }
  149. private void CheckModel2RadBtn_Checked(object sender, RoutedEventArgs e)
  150. {
  151. ShowMessageBus.ShowBinding.PandingCode = "0";
  152. WHParaPanel.Visibility = Visibility.Visible;
  153. AreaParaPanel.Visibility = Visibility.Collapsed;
  154. }
  155. private void CheckModel3RadBtn_Checked(object sender, RoutedEventArgs e)
  156. {
  157. ShowMessageBus.ShowBinding.PandingCode = "1";
  158. WHParaPanel.Visibility = Visibility.Collapsed;
  159. AreaParaPanel.Visibility = Visibility.Visible;
  160. }
  161. private void CheckModel4RadBtn_Checked(object sender, RoutedEventArgs e)
  162. {
  163. ShowMessageBus.ShowBinding.PandingCode = "2";
  164. WHParaPanel.Visibility = Visibility.Visible;
  165. AreaParaPanel.Visibility = Visibility.Visible;
  166. }
  167. //private void SaveAllConfigBtn_Click(object sender, RoutedEventArgs e)
  168. //{
  169. // List<SaveConfigModel> SaveConfig = new List<SaveConfigModel>();
  170. // if(MessageBus.MainThreadS.Count <= 0)
  171. // {
  172. // return;
  173. // }
  174. // foreach (var item in MessageBus.MainThreadS)
  175. // {
  176. // SaveConfig.Add(new SaveConfigModel()
  177. // {
  178. // CameraConfig = item.cameraConfig,
  179. // ShuLiConfigClass = item.shuLiConfig
  180. // });
  181. // }
  182. // if (!Directory.Exists(".\\Config\\")) Directory.CreateDirectory(".\\Config\\");
  183. // {
  184. // XmlStorage.SerializeToXml(SaveConfig, ".\\Config\\CCDCountConfig.xml");
  185. // }
  186. //}
  187. /// <summary>
  188. /// 加载配方
  189. /// </summary>
  190. /// <param name="FormulationConfig"></param>
  191. private void LoadFormulation(FormulationConfigClass FormulationConfig)
  192. {
  193. ShowMessageBus.ShowBinding.AcquistionLineRate = FormulationConfig.AcquistionLineRateValue.ToString();
  194. ShowMessageBus.ShowBinding.ExposureTimeValue = FormulationConfig.ExposureTimeValue.ToString();
  195. ShowMessageBus.ShowBinding.Channel = FormulationConfig.Channel.ToString();
  196. ShowMessageBus.ShowBinding.FormulationName = FormulationConfig.FormulationName;
  197. ShowMessageBus.ShowBinding.MAX_OBJECT_LENGTH = FormulationConfig.MAX_Object_LENGTH.ToString();
  198. ShowMessageBus.ShowBinding.MIN_OBJECT_LENGTH = FormulationConfig.MIN_Object_LENGTH.ToString();
  199. ShowMessageBus.ShowBinding.PandingCode = FormulationConfig.PandingCode.ToString();
  200. ShowMessageBus.ShowBinding.RegionThreshold = FormulationConfig.RegionThreshold.ToString();
  201. }
  202. private void ParametricTrainingBtn_Click(object sender, RoutedEventArgs e)
  203. {
  204. if(string.IsNullOrEmpty(KeLiW_TBX.Text)||string.IsNullOrEmpty(KeLiL_TBX.Text))
  205. {
  206. MessageBox.Show("请输入标准颗粒的宽高");
  207. return;
  208. }
  209. int KeliW = Convert.ToInt32(KeLiW_TBX.Text);
  210. int KeliL = Convert.ToInt32(KeLiL_TBX.Text);
  211. var paravalue = MessageBus.NowSettingLoadMainThread.ParameterTrain(KeliW, KeliL);
  212. if(paravalue!=null)
  213. {
  214. ShowMessageBus.ShowBinding.NoiseFilter = paravalue.NoiseFilterThreshold.ToString();
  215. ShowMessageBus.ShowBinding.MaxArea = ((int)(paravalue.MaxArea * 1.1)).ToString();
  216. ShowMessageBus.ShowBinding.MinArea = ((int)(paravalue.MinArea * 0.9)).ToString();
  217. ShowMessageBus.ShowBinding.MAX_OBJECT_LENGTH = ((int)(paravalue.MaxLength)).ToString();
  218. ShowMessageBus.ShowBinding.MIN_OBJECT_LENGTH = ((int)(paravalue.MinLength)).ToString();
  219. }
  220. else
  221. {
  222. MessageBox.Show("Train Error");
  223. }
  224. }
  225. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  226. {
  227. if (e.Key == Key.Enter)
  228. {
  229. TextBox textBox = sender as TextBox;
  230. if (textBox != null)
  231. {
  232. // 强制更新源数据
  233. BindingExpression bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
  234. bindingExpression.UpdateSource();
  235. }
  236. }
  237. }
  238. }
  239. }