CorToolForm.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using DevExpress.Internal.WinApi.Windows.UI.Notifications;
  2. using DevExpress.XtraEditors;
  3. using DevExpress.XtraLayout.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using XdCxRhDW.Core.Api;
  15. namespace XdCxRhDW.App.CorTools
  16. {
  17. public partial class CorToolForm : DevExpress.XtraEditors.XtraForm
  18. {
  19. static readonly string inifile = Path.Combine(Application.StartupPath, "par.ini");
  20. volatile bool beRunning = false;
  21. BindingList<CafResult> gridSource = new BindingList<CafResult>();
  22. public CorToolForm()
  23. {
  24. InitializeComponent();
  25. }
  26. void openfile(object sender)
  27. {
  28. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  29. {
  30. if (sender == btnFile1)
  31. {
  32. btnFile1.Text = openFileDialog1.FileName;
  33. }
  34. else
  35. {
  36. btnFile2.Text = openFileDialog1.FileName;
  37. }
  38. }
  39. }
  40. /// <summary>
  41. /// 读取配置
  42. /// </summary>
  43. void ReadIni()
  44. {
  45. if (File.Exists(inifile))
  46. {
  47. var lines = File.ReadAllLines(inifile);
  48. if (lines.Length != 9)
  49. {
  50. return;
  51. }
  52. btnFile1.Text = lines[0];
  53. btnFile2.Text = lines[1];
  54. teCount.Text = lines[2];
  55. tefs.Text = lines[3];
  56. teCenter.Text = lines[4];
  57. teRange.Text = lines[5];
  58. teSnr.Text = lines[6];
  59. tePos.Text = lines[7];
  60. teDfRange.Text = lines[8];
  61. }
  62. }
  63. /// <summary>
  64. /// 写入配置
  65. /// </summary>
  66. void WriteIni()
  67. {
  68. var lines = new List<String>();
  69. lines.Add(btnFile1.Text);
  70. lines.Add(btnFile2.Text);
  71. lines.Add(teCount.Text);
  72. lines.Add(tefs.Text);
  73. lines.Add(teCenter.Text);
  74. lines.Add(teRange.Text);
  75. lines.Add(teSnr.Text);
  76. lines.Add(tePos.Text);
  77. lines.Add(teDfRange.Text);
  78. File.WriteAllLines(inifile, lines);
  79. }
  80. private void MainForm_Load(object sender, EventArgs e)
  81. {
  82. this.gridControl1.DataSource = gridSource;
  83. ReadIni();
  84. }
  85. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  86. {
  87. //if (!DxHelper.MsgBoxHelper.ShowConfirm("是否退出当前程序"))
  88. //{
  89. // e.Cancel = true;
  90. //}
  91. }
  92. private bool ValidateFile(string filename)
  93. {
  94. if (string.IsNullOrWhiteSpace(filename))
  95. {
  96. DxHelper.MsgBoxHelper.ShowError($"请选择文件!");
  97. return false;
  98. }
  99. if (!File.Exists(filename))
  100. {
  101. DxHelper.MsgBoxHelper.ShowError($"文件【{filename}】不存在");
  102. return false;
  103. }
  104. return true;
  105. }
  106. private async void btnOK_Click(object sender, EventArgs e)
  107. {
  108. if (btnOK.Text == "停止")
  109. {
  110. //XcorrUtils.Stop();
  111. return;
  112. }
  113. //开始计算
  114. //校验文件名称
  115. if (!ValidateFile(btnFile1.Text))
  116. {
  117. return;
  118. }
  119. if (!ValidateFile(btnFile2.Text))
  120. {
  121. return;
  122. }
  123. XcorrStruct xItem = new XcorrStruct();
  124. xItem.file1 = btnFile1.Text;
  125. xItem.file2 = btnFile2.Text;
  126. try
  127. {
  128. xItem.smpCount = int.Parse(teCount.Text);
  129. xItem.samplingRate = Convert.ToInt32(double.Parse(tefs.Text) * 1e6);
  130. xItem.dtCenter = int.Parse(teCenter.Text);
  131. xItem.dtRange = int.Parse(teRange.Text);
  132. xItem.dfRange = int.Parse(teDfRange.Text);
  133. xItem.smpStart = int.Parse(tePos.Text);
  134. xItem.snrThreshold = int.Parse(teSnr.Text);
  135. }
  136. catch
  137. {
  138. DxHelper.MsgBoxHelper.ShowError($"参数错误");
  139. return;
  140. }
  141. WriteIni();
  142. btnOK.Text = "停止";
  143. beRunning = true;
  144. //if (chkDama.Checked is false)
  145. //{
  146. var result = await ExecuteCorAsync(xItem);
  147. if (result != null)
  148. {
  149. gridSource.Add(result);
  150. gridView1.FocusedRowHandle = gridSource.Count - 1;
  151. }
  152. //}
  153. //else
  154. //{
  155. // foreach (var cafItem in gridSource)
  156. // {
  157. // if (!beRunning) continue;
  158. // xItem.smpStart = (int)cafItem.smpstart;
  159. // xItem.smpCount = (int)cafItem.smplen;
  160. // var result = await ExecuteCorAsync(xItem);
  161. // if (result != null)
  162. // {
  163. // cafItem.dt = result.dt;
  164. // cafItem.df = result.df;
  165. // cafItem.snr = result.snr;
  166. // cafItem.tm = result.tm;
  167. // }
  168. // }
  169. // gridControl1.RefreshDataSource();
  170. //}
  171. btnOK.Text = "计算";
  172. beRunning = false;
  173. }
  174. private async Task<CafResult> ExecuteCorAsync(XcorrStruct xItem)
  175. {
  176. XcorrUtils xcorr = new XcorrUtils();
  177. var result = await xcorr.Calc(xItem);
  178. //开始计算
  179. if (result.flag == -2)
  180. {
  181. DxHelper.MsgBoxHelper.ShowError($"内部错误");
  182. btnOK.Text = "计算";
  183. beRunning = false;
  184. return null;
  185. }
  186. else if (result.flag == -1)
  187. {
  188. DxHelper.MsgBoxHelper.ShowError($"计算所需数据超出文件范围");
  189. btnOK.Text = "计算";
  190. beRunning = false;
  191. return null;
  192. }
  193. else if (result.flag == -3)
  194. {
  195. btnOK.Text = "计算";
  196. beRunning = false;
  197. return null;
  198. }
  199. return result;
  200. }
  201. private void ExecuteDamaAsync()
  202. {
  203. }
  204. /// <summary>
  205. /// 网格右键菜单弹出
  206. /// </summary>
  207. /// <param name="sender"></param>
  208. /// <param name="e"></param>
  209. private void gridView1_MouseDown(object sender, MouseEventArgs e)
  210. {
  211. if (e.Button == MouseButtons.Right && !beRunning)
  212. {
  213. popupMenu1.ShowPopup(Cursor.Position);
  214. }
  215. }
  216. /// <summary>
  217. /// 清除网格
  218. /// </summary>
  219. /// <param name="sender"></param>
  220. /// <param name="e"></param>
  221. private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  222. {
  223. gridSource.Clear();
  224. }
  225. /// <summary>
  226. /// 文件拖拽
  227. /// </summary>
  228. /// <param name="sender"></param>
  229. /// <param name="e"></param>
  230. private void btnFile_DragOver(object sender, DragEventArgs e)
  231. {
  232. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None;
  233. }
  234. private void btnFile_DragDrop(object sender, DragEventArgs e)
  235. {
  236. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  237. if (files != null && files.Length != 0)
  238. {
  239. if (sender == btnFile1)
  240. {
  241. btnFile1.Text = files[0];
  242. }
  243. else if (sender == btnFile2)
  244. {
  245. btnFile2.Text = files[0];
  246. }
  247. }
  248. }
  249. private void btnFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  250. {
  251. openfile(sender);
  252. }
  253. private void btnFile_DoubleClick(object sender, EventArgs e)
  254. {
  255. openfile(sender);
  256. }
  257. private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  258. {
  259. SaveFileDialog dialog = new SaveFileDialog();
  260. dialog.Filter = "csv|*.csv";
  261. if (dialog.ShowDialog() == DialogResult.OK)
  262. {
  263. gridView1.ExportToCsv(dialog.FileName);
  264. }
  265. }
  266. private async void btnCheck_Click(object sender, EventArgs e)
  267. {
  268. //if (!ValidateFile(btnFile1.Text))
  269. //{
  270. // return;
  271. //}
  272. //var dmcResult = await XcorrUtils.DmcCheckAsync(btnFile1.Text, double.Parse(tefs.Text));
  273. //gridSource.Clear();
  274. //foreach (var dmcItem in dmcResult)
  275. //{
  276. // gridSource.Add(new CafResult()
  277. // {
  278. // file1 = btnFile1.Text,
  279. // file2 = btnFile2.Text,
  280. // smpstart = dmcItem.Start,
  281. // smplen = dmcItem.Length
  282. // });
  283. //}
  284. }
  285. private void chkDama_CheckedChanged(object sender, EventArgs e)
  286. {
  287. //lciBtnCheck.Visibility = chkDama.Checked ? LayoutVisibility.Always : LayoutVisibility.Never;
  288. //lciStartPos.Visibility = lciSampleLength.Visibility = chkDama.Checked ? LayoutVisibility.Never : LayoutVisibility.Always;
  289. //layoutControl1.BestFit();
  290. }
  291. }
  292. }