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. var result = await XcorrUtils.Calc(xItem);
  176. //开始计算
  177. if (result.flag == -2)
  178. {
  179. DxHelper.MsgBoxHelper.ShowError($"内部错误");
  180. btnOK.Text = "计算";
  181. beRunning = false;
  182. return null;
  183. }
  184. else if (result.flag == -1)
  185. {
  186. DxHelper.MsgBoxHelper.ShowError($"计算所需数据超出文件范围");
  187. btnOK.Text = "计算";
  188. beRunning = false;
  189. return null;
  190. }
  191. else if (result.flag == -3)
  192. {
  193. btnOK.Text = "计算";
  194. beRunning = false;
  195. return null;
  196. }
  197. return result;
  198. }
  199. private void ExecuteDamaAsync()
  200. {
  201. }
  202. /// <summary>
  203. /// 网格右键菜单弹出
  204. /// </summary>
  205. /// <param name="sender"></param>
  206. /// <param name="e"></param>
  207. private void gridView1_MouseDown(object sender, MouseEventArgs e)
  208. {
  209. if (e.Button == MouseButtons.Right && !beRunning)
  210. {
  211. popupMenu1.ShowPopup(Cursor.Position);
  212. }
  213. }
  214. /// <summary>
  215. /// 清除网格
  216. /// </summary>
  217. /// <param name="sender"></param>
  218. /// <param name="e"></param>
  219. private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  220. {
  221. gridSource.Clear();
  222. }
  223. /// <summary>
  224. /// 文件拖拽
  225. /// </summary>
  226. /// <param name="sender"></param>
  227. /// <param name="e"></param>
  228. private void btnFile_DragOver(object sender, DragEventArgs e)
  229. {
  230. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None;
  231. }
  232. private void btnFile_DragDrop(object sender, DragEventArgs e)
  233. {
  234. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  235. if (files != null && files.Length != 0)
  236. {
  237. if (sender == btnFile1)
  238. {
  239. btnFile1.Text = files[0];
  240. }
  241. else if (sender == btnFile2)
  242. {
  243. btnFile2.Text = files[0];
  244. }
  245. }
  246. }
  247. private void btnFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  248. {
  249. openfile(sender);
  250. }
  251. private void btnFile_DoubleClick(object sender, EventArgs e)
  252. {
  253. openfile(sender);
  254. }
  255. private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  256. {
  257. SaveFileDialog dialog = new SaveFileDialog();
  258. dialog.Filter = "csv|*.csv";
  259. if (dialog.ShowDialog() == DialogResult.OK)
  260. {
  261. gridView1.ExportToCsv(dialog.FileName);
  262. }
  263. }
  264. private async void btnCheck_Click(object sender, EventArgs e)
  265. {
  266. //if (!ValidateFile(btnFile1.Text))
  267. //{
  268. // return;
  269. //}
  270. //var dmcResult = await XcorrUtils.DmcCheckAsync(btnFile1.Text, double.Parse(tefs.Text));
  271. //gridSource.Clear();
  272. //foreach (var dmcItem in dmcResult)
  273. //{
  274. // gridSource.Add(new CafResult()
  275. // {
  276. // file1 = btnFile1.Text,
  277. // file2 = btnFile2.Text,
  278. // smpstart = dmcItem.Start,
  279. // smplen = dmcItem.Length
  280. // });
  281. //}
  282. }
  283. private void chkDama_CheckedChanged(object sender, EventArgs e)
  284. {
  285. //lciBtnCheck.Visibility = chkDama.Checked ? LayoutVisibility.Always : LayoutVisibility.Never;
  286. //lciStartPos.Visibility = lciSampleLength.Visibility = chkDama.Checked ? LayoutVisibility.Never : LayoutVisibility.Always;
  287. //layoutControl1.BestFit();
  288. }
  289. }
  290. }