UCConfig.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using Ookii.Dialogs.Wpf;
  17. using System.Collections.ObjectModel;
  18. using MeterVision.FreeAi;
  19. using System.IO;
  20. using MeterVision.db;
  21. using MeterVision.Dlg;
  22. namespace MeterVision.Config
  23. {
  24. /// <summary>
  25. /// UCConfig.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class UCConfig : UserControl, INotifyPropertyChanged
  28. {
  29. public event PropertyChangedEventHandler PropertyChanged;
  30. protected void OnPropertyChanged(string propertyName)
  31. {
  32. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  33. }
  34. public ObservableCollection<string> AiDlls { get; set; }
  35. public ObservableCollection<string> Onnxs { get; set; }
  36. public CfginiItem _cfginiItem;
  37. public CfginiItem mConfigItem
  38. {
  39. get => _cfginiItem;
  40. set
  41. {
  42. if(_cfginiItem != value)
  43. {
  44. _cfginiItem = value;
  45. OnPropertyChanged(nameof(mConfigItem));
  46. }
  47. }
  48. }
  49. //private ConfigItem _curConfigItem;
  50. //public ConfigItem CurConfigItem
  51. //{
  52. // get => _curConfigItem;
  53. // set
  54. // {
  55. // if(_curConfigItem != value)
  56. // {
  57. // _curConfigItem = value;
  58. // OnPropertyChanged(nameof(CurConfigItem));
  59. // }
  60. // }
  61. //}
  62. //private RulerDigitTypeEnum _selecteRulerDigitType;
  63. //public RulerDigitTypeEnum SelectedRulerDigitType
  64. //{
  65. // get { return _selecteRulerDigitType; }
  66. // set
  67. // {
  68. // if (_selecteRulerDigitType != value)
  69. // {
  70. // _selecteRulerDigitType = value;
  71. // OnPropertyChanged(nameof(SelectedRulerDigitType));
  72. // ConfigItem.SaveRulerDigitType((int)value);
  73. // }
  74. // }
  75. //}
  76. //private RulerDigitPointerTypeEnum _selecteRulerDigitPointerType;
  77. //public RulerDigitPointerTypeEnum SelectedRulerDigitPointerType
  78. //{
  79. // get { return _selecteRulerDigitPointerType; }
  80. // set
  81. // {
  82. // if (_selecteRulerDigitPointerType != value)
  83. // {
  84. // _selecteRulerDigitPointerType = value;
  85. // OnPropertyChanged(nameof(SelectedRulerDigitPointerType));
  86. // ConfigItem.SaveRulerDigitPointerType((int)value);
  87. // }
  88. // }
  89. //}
  90. //private RulerPointerTypeEnum _selecteRulerPointerType;
  91. //public RulerPointerTypeEnum SelectedRulerPointerType
  92. //{
  93. // get { return _selecteRulerPointerType; }
  94. // set
  95. // {
  96. // if (_selecteRulerPointerType != value)
  97. // {
  98. // _selecteRulerPointerType = value;
  99. // OnPropertyChanged(nameof(SelectedRulerPointerType));
  100. // ConfigItem.SaveRulerPointerType((int)value);
  101. // }
  102. // }
  103. //}
  104. public UCConfig()
  105. {
  106. InitializeComponent();
  107. /*Window parentWindow = Window.GetWindow(this);
  108. if (parentWindow is MainWindow mainWindow)
  109. {
  110. CurConfigItem = mainWindow.mConfigItem;
  111. }*/
  112. //CurConfigItem = ConfigItem.GetConfigItem();
  113. mConfigItem = CfginiItem.GetConfigItem();
  114. this.DataContext = this;
  115. //this.InitUI();
  116. FindFreeAIDllFiles();
  117. FindOnnxFiles();
  118. //CurAiDll = CurConfigItem.AiDll;
  119. this.Loaded += UCConfig_Loaded;
  120. }
  121. private void UCConfig_Loaded(object sender, RoutedEventArgs e)
  122. {
  123. //throw new NotImplementedException();
  124. //FindFreeAIDllFiles();
  125. //FindOnnxFiles();
  126. }
  127. //private void InitUI()
  128. //{
  129. // SelectedRulerDigitType = (RulerDigitTypeEnum)CurConfigItem.RulerDigitType;
  130. // SelectedRulerDigitPointerType = (RulerDigitPointerTypeEnum)CurConfigItem.RulerDigitPointerType;
  131. // SelectedRulerPointerType = (RulerPointerTypeEnum)CurConfigItem.RulerPointerType;
  132. //}
  133. // 查询 EXE 目录下所有包含 "freeAI" 且后缀为 ".dll" 的文件
  134. private void FindFreeAIDllFiles()
  135. {
  136. // 获取当前应用程序的 EXE 文件路径
  137. string exeDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  138. // 使用通配符查询符合条件的 DLL 文件
  139. string[] files = Directory.GetFiles(exeDirectory, "*freeAI*.dll");
  140. if(AiDlls == null)
  141. {
  142. AiDlls = new ObservableCollection<string>();
  143. }
  144. else
  145. {
  146. AiDlls.Clear();
  147. }
  148. // 输出找到的所有文件
  149. foreach (var file in files)
  150. {
  151. //Console.WriteLine(file);
  152. // 获取文件名(包含扩展名)
  153. string fileNameWithExtension = Path.GetFileName(file);
  154. // 获取文件名(不包含扩展名)
  155. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
  156. AiDlls.Add(fileNameWithExtension);
  157. }
  158. }
  159. private void FindOnnxFiles()
  160. {
  161. // 获取当前应用程序的 EXE 文件路径
  162. string exeDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  163. // 使用通配符查询符合条件的 DLL 文件
  164. string[] files = Directory.GetFiles(exeDirectory, "*.onnx");
  165. if (Onnxs == null)
  166. {
  167. Onnxs = new ObservableCollection<string>();
  168. }
  169. else
  170. {
  171. Onnxs.Clear();
  172. }
  173. // 输出找到的所有文件
  174. foreach (var file in files)
  175. {
  176. //Console.WriteLine(file);
  177. // 获取文件名(包含扩展名)
  178. string fileNameWithExtension = Path.GetFileName(file);
  179. // 获取文件名(不包含扩展名)
  180. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
  181. Onnxs.Add(fileNameWithExtension);
  182. }
  183. }
  184. private void BtnSelectOnnx_Click(object sender, RoutedEventArgs e)
  185. {
  186. // 创建一个 OpenFileDialog 实例
  187. OpenFileDialog openFileDialog = new OpenFileDialog();
  188. // 设置对话框的标题
  189. openFileDialog.Title = "选择文件";
  190. // 设置默认文件夹(可选)
  191. //openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  192. // 设置过滤器,以便只显示特定类型的文件(可选)
  193. openFileDialog.Filter = "AI模型文件 (*.onnx)|*.onnx|所有文件 (*.*)|*.*";
  194. // 显示对话框并检查用户是否点击了“确定”
  195. if (openFileDialog.ShowDialog() == true)
  196. {
  197. // 获取所选文件的全路径
  198. string filePath = openFileDialog.FileName;
  199. // 在界面上显示文件路径
  200. //txtFilePath.Text = $"选择的文件路径: {filePath}";
  201. //if (mConfigItem != null)
  202. //{
  203. // mConfigItem.OnnxPath = filePath;
  204. // setOnnxPath(filePath);
  205. //}
  206. //---------------
  207. //ConfigItem.SaveOnnxPath(filePath);
  208. ////获取父窗体
  209. //Window parentWindow = Window.GetWindow(this);
  210. //if(parentWindow is MainWindow mainWindow)
  211. //{
  212. // mainWindow.mConfigItem.OnnxPath = filePath;
  213. //}
  214. mConfigItem.OnnxPath = filePath;
  215. }
  216. }
  217. private void BtnDstImgPath_Click(object sender, RoutedEventArgs e)
  218. {
  219. // 创建 VistaFolderBrowserDialog 实例
  220. var dialog = new VistaFolderBrowserDialog();
  221. //dialog.Description = "请选择目标图像输出文件夹";
  222. dialog.Description = "请选择识别数据输出文件夹";
  223. dialog.UseDescriptionForTitle = true; // 使用 Description 作为窗口标题
  224. // 显示对话框并检查用户是否点击了“确定”
  225. if (dialog.ShowDialog() == true)
  226. {
  227. // 获取用户选择的文件夹路径
  228. string selectedFolderPath = dialog.SelectedPath;
  229. //------------------
  230. //ConfigItem.SaveDstImgPath(selectedFolderPath);
  231. ////获取父窗体
  232. //Window parentWindow = Window.GetWindow(this);
  233. //if (parentWindow is MainWindow mainWindow)
  234. //{
  235. // mainWindow.mConfigItem.DstImgPath = selectedFolderPath;
  236. //}
  237. mConfigItem.Output = selectedFolderPath;
  238. }
  239. }
  240. private async void BtnFreeSpace_Click(object sender, RoutedEventArgs e)
  241. {
  242. string titleInfo = "正在释放数据库空间占用,请稍候...";
  243. WaitWindow waitWindow = new WaitWindow(titleInfo)
  244. {
  245. Owner = Application.Current.MainWindow,
  246. WindowStartupLocation = WindowStartupLocation.CenterOwner
  247. };
  248. waitWindow.Show();
  249. //DBStand.FreeDatabase();
  250. try
  251. {
  252. await Task.Run(() =>
  253. {
  254. DBStand.FreeDatabase();
  255. });
  256. }
  257. catch (Exception ex)
  258. {
  259. MessageBox.Show(Application.Current.MainWindow, $"失败:{ex.Message}", "错误",
  260. MessageBoxButton.OK, MessageBoxImage.Error);
  261. }
  262. finally
  263. {
  264. //关闭等待窗口
  265. waitWindow.Close();
  266. }
  267. }
  268. //private void BtnAiRunLog_Click(object sender, RoutedEventArgs e)
  269. //{
  270. // // 创建 VistaFolderBrowserDialog 实例
  271. // var dialog = new VistaFolderBrowserDialog();
  272. // dialog.Description = "请选择AI运行日志文件夹";
  273. // dialog.UseDescriptionForTitle = true; // 使用 Description 作为窗口标题
  274. // // 显示对话框并检查用户是否点击了“确定”
  275. // if (dialog.ShowDialog() == true)
  276. // {
  277. // // 获取用户选择的文件夹路径
  278. // string selectedFolderPath = dialog.SelectedPath;
  279. // ConfigItem.SaveAiLogPath(selectedFolderPath);
  280. // //获取父窗体
  281. // Window parentWindow = Window.GetWindow(this);
  282. // if (parentWindow is MainWindow mainWindow)
  283. // {
  284. // mainWindow.mConfigItem.AiLogPath = selectedFolderPath;
  285. // }
  286. // }
  287. //}
  288. //private void BtnTransImgPath_Click(object sender, RoutedEventArgs e)
  289. //{
  290. // var dialog = new VistaFolderBrowserDialog();
  291. // dialog.Description = "请选择图像转换后的存储文件夹";
  292. // dialog.UseDescriptionForTitle = true; // 使用 Description 作为窗口标题
  293. // // 显示对话框并检查用户是否点击了“确定”
  294. // if (dialog.ShowDialog() == true)
  295. // {
  296. // // 获取用户选择的文件夹路径
  297. // string selectedFolderPath = dialog.SelectedPath;
  298. // ConfigItem.SaveTransImgPath(selectedFolderPath);
  299. // //获取父窗体
  300. // Window parentWindow = Window.GetWindow(this);
  301. // if (parentWindow is MainWindow mainWindow)
  302. // {
  303. // mainWindow.mConfigItem.TransImgPath = selectedFolderPath;
  304. // }
  305. // }
  306. //}
  307. /////////////////////////////////////////////////////////////////////////
  308. }
  309. }