MainForm.cs 14 KB

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