MainForm.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using MeasureData;
  11. using MeasureThread;
  12. using FileManager;
  13. using DBManager;
  14. using System.Data.SQLite;
  15. namespace WindowsFormsApp1
  16. {
  17. public partial class MainForm : Form
  18. {
  19. #region 成员变量
  20. private BackgroundWorker m_BackgroundWorker;// 申明后台对象
  21. /// <summary>
  22. /// 测量文件
  23. /// </summary>
  24. public MeasureFile m_MeasureFile;
  25. /// 测量线程
  26. public Measure m_Ms;
  27. //自动对焦函数
  28. public FocusParam m_focusParam;
  29. enum WorkType
  30. {
  31. AutoFocus = 1,
  32. AllThing = 2
  33. };
  34. WorkType m_work;
  35. #endregion
  36. #region 构造函数
  37. public MainForm()
  38. {
  39. InitializeComponent();
  40. m_BackgroundWorker = new BackgroundWorker(); // 实例化后台对象
  41. m_BackgroundWorker.WorkerReportsProgress = true; // 设置可以通告进度
  42. m_BackgroundWorker.WorkerSupportsCancellation = true; // 设置可以取消
  43. m_BackgroundWorker.DoWork += new DoWorkEventHandler(DoWork);
  44. m_BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(UpdateProgress);
  45. m_BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork);
  46. LogManager.InitManulLog();
  47. m_focusParam = new FocusParam();
  48. m_work = new WorkType();
  49. }
  50. #endregion
  51. #region 测量线程
  52. void DoWork(object sender, DoWorkEventArgs e)
  53. {
  54. m_Ms = new Measure("","","");
  55. m_Ms.InitMeas(m_MeasureFile);
  56. m_Ms.SendThreadStatus += new ThreadStatusHandler(displayMessage); //注册事件
  57. if (m_work == WorkType.AutoFocus)
  58. {
  59. //自动对焦
  60. //int nWd;
  61. //m_Ms.AutoFocus(m_focusParam, out nWd);
  62. //double wd = (double)nWd / 100.0;
  63. //tB_WD.Text = wd.ToString();
  64. }
  65. else
  66. {
  67. //自动测量的全过程
  68. m_Ms.DoMeasure();
  69. }
  70. //定位
  71. //切割
  72. //分析位置
  73. //截面
  74. }
  75. public void displayMessage(object sender, ThreadStatusEventArgs e)
  76. {
  77. //主界面显示内容
  78. this.BeginInvoke((Action)delegate
  79. {
  80. this.listmsg.Items.Add(e.Time.ToString() + e.State);
  81. });
  82. }
  83. void UpdateProgress(object sender, ProgressChangedEventArgs e)
  84. {
  85. }
  86. void CompletedWork(object sender, RunWorkerCompletedEventArgs e)
  87. {
  88. }
  89. #endregion
  90. #region 按钮操作
  91. /// <summary>
  92. /// 新建文件
  93. /// </summary>
  94. /// <param name="sender"></param>
  95. /// <param name="e"></param>
  96. private void btnNewFile_Click(object sender, EventArgs e)
  97. {
  98. m_MeasureFile = new MeasureFile();
  99. if (!m_MeasureFile.New())
  100. {
  101. return;
  102. }
  103. else
  104. {
  105. MessageBox.Show("新建测量文件成功。");
  106. }
  107. }
  108. /// <summary>
  109. /// 保存文件
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="e"></param>
  113. private void btnSaveFile_Click(object sender, EventArgs e)
  114. {
  115. if (m_MeasureFile == null)
  116. {
  117. MessageBox.Show("请新建一个测量文件");
  118. }
  119. else
  120. {
  121. m_MeasureFile.Save();
  122. }
  123. }
  124. /// <summary>
  125. /// 打开文件
  126. /// </summary>
  127. /// <param name="sender"></param>
  128. /// <param name="e"></param>
  129. private void btnOpenFile_Click(object sender, EventArgs e)
  130. {
  131. }
  132. /// <summary>
  133. /// 停止
  134. /// </summary>
  135. /// <param name="sender"></param>
  136. /// <param name="e"></param>
  137. private void button4_Click(object sender, EventArgs e)
  138. {
  139. if (m_BackgroundWorker.IsBusy)
  140. {
  141. m_BackgroundWorker.CancelAsync();
  142. }
  143. }
  144. /// <summary>
  145. /// 加载切割孔
  146. /// </summary>
  147. /// <param name="sender"></param>
  148. /// <param name="e"></param>
  149. private void btnLoadCutHoles_Click(object sender, EventArgs e)
  150. {
  151. if (m_MeasureFile == null)
  152. {
  153. MessageBox.Show("请新建一个测量文件");
  154. }
  155. else
  156. {
  157. if (!m_MeasureFile.GetCutHolesFromFile(""))
  158. {
  159. MessageBox.Show("导入切孔失败");
  160. }
  161. this.CutHoleGridView.Rows.Clear();
  162. List<CutHole> listHoles = m_MeasureFile.ListCutHole;
  163. foreach (CutHole hole in listHoles)
  164. {
  165. //在CutHoleGridView中,添加切孔信息
  166. int index = this.CutHoleGridView.Rows.Add();
  167. this.CutHoleGridView.Rows[index].Cells[0].Value = hole.HoleName;
  168. SemPosition pos = hole.Position;
  169. this.CutHoleGridView.Rows[index].Cells[1].Value = pos.X;
  170. this.CutHoleGridView.Rows[index].Cells[2].Value = pos.Y;
  171. this.CutHoleGridView.Rows[index].Cells[3].Value = pos.Z;
  172. this.CutHoleGridView.Rows[index].Cells[4].Value = pos.M;
  173. this.CutHoleGridView.Rows[index].Cells[5].Value = pos.R;
  174. this.CutHoleGridView.Rows[index].Cells[6].Value = pos.T;
  175. }
  176. }
  177. }
  178. /// <summary>
  179. /// 启动
  180. /// </summary>
  181. /// <param name="sender"></param>
  182. /// <param name="e"></param>
  183. private void button3_Click(object sender, EventArgs e)
  184. {
  185. if (m_MeasureFile == null)
  186. {
  187. MessageBox.Show("请新建一个测量文件");
  188. listmsg.Items.Clear();
  189. }
  190. else
  191. {
  192. if (m_BackgroundWorker.IsBusy)
  193. {
  194. MessageBox.Show("线程已经运行");
  195. return;
  196. }
  197. m_BackgroundWorker.RunWorkerAsync(this);
  198. }
  199. }
  200. /// <summary>
  201. /// 调区FIB模板
  202. /// </summary>
  203. /// <param name="sender"></param>
  204. /// <param name="e"></param>
  205. private void btFIB_Click(object sender, EventArgs e)
  206. {
  207. string FilePathName;
  208. string fileNameWithoutExtension;
  209. //新建一个文件对话框
  210. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  211. //设置对话框标题
  212. pOpenFileDialog.Title = "选择模板文件";
  213. //设置打开文件类型
  214. pOpenFileDialog.Filter = "ely文件(*.ely)|*.ely";
  215. //监测文件是否存在
  216. pOpenFileDialog.CheckFileExists = true;
  217. //文件打开后执行以下程序
  218. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  219. {
  220. FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName); //绝对路径
  221. fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(FilePathName);
  222. this.tBFIBTemp.Text = fileNameWithoutExtension;
  223. }
  224. }
  225. private void btParamOK_Click(object sender, EventArgs e)
  226. {
  227. if (m_MeasureFile == null)
  228. {
  229. MessageBox.Show("请新建一个测量文件");
  230. this.listmsg.Items.Add("请新建一个测量文件");
  231. }
  232. else
  233. {
  234. m_MeasureFile.MParam.PT = this.cBIsPT.Checked;
  235. m_MeasureFile.MParam.SampleName = this.tBSampleName.Text;
  236. m_MeasureFile.MParam.FIBTemp = this.tBFIBTemp.Text;
  237. //m_MeasureFile.MParam.FocusMode = this.cBIsManul.Checked;
  238. MessageBox.Show("参数设置成功");
  239. }
  240. }
  241. #endregion
  242. //定位操作
  243. private void button5_Click(object sender, EventArgs e)
  244. {
  245. }
  246. //切割操作
  247. private void button6_Click(object sender, EventArgs e)
  248. {
  249. }
  250. //分析位置操作
  251. private void button7_Click(object sender, EventArgs e)
  252. {
  253. }
  254. //观测截面
  255. private void button1_Click(object sender, EventArgs e)
  256. {
  257. }
  258. private void btImgPath_Click(object sender, EventArgs e)
  259. {
  260. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  261. dialog.Description = "选择拍图保存的文件夹";
  262. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  263. {
  264. if (string.IsNullOrEmpty(dialog.SelectedPath))
  265. {
  266. MessageBox.Show("图像文件夹不能为空");
  267. return;
  268. }
  269. m_focusParam.Path = dialog.SelectedPath;
  270. tBImgPath.Text = dialog.SelectedPath;
  271. }
  272. }
  273. private void button2_Click(object sender, EventArgs e)
  274. {
  275. if (tBImgPath.Text == "" ||
  276. tB_Up.Text == "" ||
  277. tB_Down.Text == "" ||
  278. tB_Step.Text == "" ||
  279. tB_fRange.Text == "" ||
  280. tB_fStep.Text == "")
  281. {
  282. MessageBox.Show("请输入完整的参数");
  283. return;
  284. }
  285. //保存自动对焦函数
  286. m_focusParam.Path = tBImgPath.Text;
  287. m_focusParam.Step = float.Parse(tB_Step.Text);
  288. m_focusParam.Range = float.Parse(tB_fRange.Text);
  289. m_focusParam.fStep = float.Parse(tB_fStep.Text);
  290. float fUP = float.Parse(tB_Up.Text);
  291. float fDown = float.Parse(tB_Down.Text);
  292. if (fDown <= fUP)
  293. {
  294. MessageBox.Show("请输入下限大于上限的值");
  295. return;
  296. }
  297. m_focusParam.UP = float.Parse(tB_Up.Text);
  298. m_focusParam.Down = float.Parse(tB_Down.Text);
  299. }
  300. private void tB_TextChanged(object sender, EventArgs e)
  301. {
  302. }
  303. //输入限制只能是数字
  304. private void textBox_KeyPress(string text, object sender, KeyPressEventArgs e)
  305. {
  306. //允许输入数字、小数点、删除键和负号
  307. if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-'))
  308. {
  309. MessageBox.Show("请输入正确的数字");
  310. text = "";
  311. e.Handled = true;
  312. }
  313. if (e.KeyChar == (char)('-'))
  314. {
  315. if (text != "")
  316. {
  317. MessageBox.Show("请输入正确的数字");
  318. text = "";
  319. e.Handled = true;
  320. }
  321. }
  322. //小数点只能输入一次
  323. if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
  324. {
  325. MessageBox.Show("请输入正确的数字");
  326. text = "";
  327. e.Handled = true;
  328. }
  329. //第一位不能为小数点
  330. if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
  331. {
  332. MessageBox.Show("请输入正确的数字");
  333. text = "";
  334. e.Handled = true;
  335. }
  336. //第一位是0,第二位必须为小数点
  337. if (e.KeyChar != (char)('.') && ((TextBox)sender).Text == "0")
  338. {
  339. MessageBox.Show("请输入正确的数字");
  340. text = "";
  341. e.Handled = true;
  342. }
  343. //第一位是负号,第二位不能为小数点
  344. if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
  345. {
  346. MessageBox.Show("请输入正确的数字");
  347. text = "";
  348. e.Handled = true;
  349. }
  350. }
  351. private void tB_Up_KeyPress(object sender, KeyPressEventArgs e)
  352. {
  353. textBox_KeyPress(tB_Up.Text, sender, e);
  354. }
  355. private void tB_Down_KeyPress(object sender, KeyPressEventArgs e)
  356. {
  357. textBox_KeyPress(tB_Down.Text, sender, e);
  358. }
  359. private void tB_Step_KeyPress(object sender, KeyPressEventArgs e)
  360. {
  361. textBox_KeyPress(tB_Step.Text, sender, e);
  362. }
  363. private void tB_fRange_KeyPress(object sender, KeyPressEventArgs e)
  364. {
  365. textBox_KeyPress(tB_fRange.Text, sender, e);
  366. }
  367. private void tB_fStep_KeyPress(object sender, KeyPressEventArgs e)
  368. {
  369. textBox_KeyPress(tB_fStep.Text, sender, e);
  370. }
  371. private void tbAutoFocus_Click(object sender, EventArgs e)
  372. {
  373. m_work = WorkType.AutoFocus;
  374. // 调用对焦线程
  375. if (m_MeasureFile == null)
  376. {
  377. MessageBox.Show("请新建一个测量文件");
  378. listmsg.Items.Clear();
  379. }
  380. else
  381. {
  382. if (m_BackgroundWorker.IsBusy)
  383. {
  384. MessageBox.Show("线程已经运行");
  385. return;
  386. }
  387. m_BackgroundWorker.RunWorkerAsync(this);
  388. }
  389. }
  390. private void button8_Click(object sender, EventArgs e)
  391. {
  392. MeasureDB db = new MeasureDB(m_MeasureFile);
  393. }
  394. }
  395. }