MainForm.cs 13 KB

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