CorToolForm.cs 9.7 KB

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