FormHOZMain.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. using FileManager;
  2. using MeasureData;
  3. using MeasureThread;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using static MeasureThread.ThreadStatusEventArgs;
  17. namespace HOZProject
  18. {
  19. public partial class FormHOZMain : Form
  20. {
  21. #region 成员变量
  22. private BackgroundWorker m_BackgroundWorker;// 申明后台对象
  23. /// <summary>
  24. /// 测量文件
  25. /// </summary>
  26. public MeasureFile m_MeasureFile;
  27. /// 测量线程
  28. public Measure m_Ms;
  29. /// <summary>
  30. /// 显示日志窗体
  31. /// </summary>
  32. Form m_FormLog = null;
  33. /// <summary>
  34. /// 显示实例化窗体
  35. /// </summary>
  36. Form m_FormInit = null;
  37. /// <summary>
  38. /// 是否已保存
  39. /// </summary>
  40. public bool IsSave = false;
  41. /// <summary>
  42. /// 初始化用户控件
  43. /// </summary>
  44. UControl_Init uControl_Init = null;
  45. //流程控制
  46. public int m_MeasureType = (int)MeasureMsgManage.measureType.FIB;
  47. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  48. #endregion
  49. #region 构造函数
  50. public FormHOZMain()
  51. {
  52. InitializeComponent();
  53. //在线程操作过程中,可以设置控件属性
  54. Control.CheckForIllegalCrossThreadCalls = false;
  55. m_BackgroundWorker = new BackgroundWorker(); // 实例化后台对象
  56. m_BackgroundWorker.WorkerReportsProgress = true; // 设置可以通告进度
  57. m_BackgroundWorker.WorkerSupportsCancellation = true; // 设置可以取消
  58. m_BackgroundWorker.DoWork += new DoWorkEventHandler(DoWork);
  59. m_BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(UpdateProgress);
  60. m_BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork);
  61. LogManager.InitManulLog();
  62. }
  63. #endregion
  64. #region 测量线程
  65. void DoWork(object sender, DoWorkEventArgs e)
  66. {
  67. m_Ms = new Measure(ConfigurationManager.AppSettings["WebServerIP"].ToString(),
  68. ConfigurationManager.AppSettings["WebServerPort"].ToString(),
  69. ConfigurationManager.AppSettings["WebServerUrl"].ToString());
  70. m_MeasureFile.MParam.AutoFocus.UP = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_UP"].ToString());
  71. m_MeasureFile.MParam.AutoFocus.Down = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Down"].ToString());
  72. m_MeasureFile.MParam.AutoFocus.Step = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Step"].ToString());
  73. m_MeasureFile.MParam.AutoFocus.Range = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_Range"].ToString());
  74. m_MeasureFile.MParam.AutoFocus.fStep = Convert.ToSingle(ConfigurationManager.AppSettings["Focus_FStep"].ToString());
  75. m_Ms.InitMeas(m_MeasureFile);
  76. m_Ms.SendThreadStatus += new ThreadStatusHandler(displayMessage); //注册事件
  77. m_Ms.SendCutHolesStatus += new CutHolesStatusHandler(displayCutHoleMessage); //注册事件
  78. //设置控件操作
  79. SetWinControlMeasureState(false);
  80. //自动测量的全过程
  81. m_Ms.DoMeasure();
  82. //定位
  83. //切割
  84. //分析位置
  85. //截面
  86. }
  87. public void displayMessage(object sender, ThreadStatusEventArgs e)
  88. {
  89. //主界面显示内容
  90. this.BeginInvoke((Action)delegate
  91. {
  92. //流程编号
  93. string eCode = e.Step_Code;
  94. //流程开始时间
  95. DateTime eTime = e.Time;
  96. //图片对象
  97. PictureInformation ePictureInformation = e.Picture_Information;
  98. //当前状态
  99. string eState = e.State;
  100. this.listmsg.Items.Add(e.Time.ToString() +"_"+ e.State);
  101. //显示流程中对应编号的内容
  102. MeasureMsgManage.ShowMsgContent(this, e);
  103. ShowLogInfo();
  104. });
  105. }
  106. public void displayCutHoleMessage(object sender, CutHolesStatusEventArgs e)
  107. {
  108. //主界面显示内容
  109. this.BeginInvoke((Action)delegate
  110. {
  111. //this.listmsg.Items.Add(e.HoleName.ToString() + e.State);
  112. ChangeCutHoleState(e.HoleName.ToString(), Convert.ToInt32(e.State));
  113. });
  114. }
  115. void UpdateProgress(object sender, ProgressChangedEventArgs e)
  116. {
  117. }
  118. void CompletedWork(object sender, RunWorkerCompletedEventArgs e)
  119. {
  120. //设置控件操作
  121. SetWinControlMeasureState(true);
  122. }
  123. #endregion
  124. private void FormHOZMain_Load(object sender, EventArgs e)
  125. {
  126. //加载控件的点击事件
  127. this.Click += new EventHandler(FormHOZMain_Click);
  128. plFill.Click += new EventHandler(FormHOZMain_Click);
  129. plTop.Click += new EventHandler(FormHOZMain_Click);
  130. plLeft.Click += new EventHandler(FormHOZMain_Click);
  131. plLeftContent.Click += new EventHandler(FormHOZMain_Click);
  132. pbImage.Click += new EventHandler(FormHOZMain_Click);
  133. //加载窗口移动事件
  134. this.MouseDown += new MouseEventHandler(FormHOZMain_MouseDown);
  135. //新建
  136. NewCreate();
  137. //实例初始化窗体对象
  138. if (uControl_Init == null)
  139. {
  140. uControl_Init = new UControl_Init(this);
  141. uControl_Init.ReloadConfig();
  142. }
  143. }
  144. #region 创建切孔列表信息
  145. /// <summary>
  146. /// 创建切孔列表信息
  147. /// </summary>
  148. /// <param name="ListCutHole"></param>
  149. public void CreateCutHoleList(List<CutHole> ListCutHole)
  150. {
  151. //清空左侧Panel中的切孔控件
  152. ClearPanelControls();
  153. for (int i = ListCutHole.Count - 1; i >= 0; i--)
  154. {
  155. UControl_CutHole ucCutHole = new UControl_CutHole(this);
  156. ucCutHole.Dock = DockStyle.Top;
  157. ucCutHole.CutHoleName = ListCutHole[i].HoleName;
  158. //显示切孔参数信息
  159. if (ucCutHole.UControl_ParaInfo == null)
  160. {
  161. ucCutHole.UControl_ParaInfo = new UControl_ParaInfo(this);
  162. }
  163. plPrarInfo.Width = ucCutHole.UControl_ParaInfo.Width;
  164. plPrarInfo.Height = ucCutHole.UControl_ParaInfo.Height;
  165. //设置当前样品的参数信息
  166. ucCutHole.UControl_ParaInfo.Name = ListCutHole[i].HoleName;
  167. ucCutHole.UControl_ParaInfo.CutHoleName = ListCutHole[i].HoleName;
  168. ucCutHole.UControl_ParaInfo.Position = ListCutHole[i].Position;
  169. ucCutHole.UControl_ParaInfo.StartTime = ListCutHole[i].START.ToString();
  170. ucCutHole.UControl_ParaInfo.EndTime = ListCutHole[i].END.ToString();
  171. ucCutHole.UControl_ParaInfo.State = ListCutHole[i].STATE.ToString();
  172. ucCutHole.UControl_ParaInfo.IsSwitch = ListCutHole[i].SWITCH;
  173. ucCutHole.UControl_ParaInfo.Dock = DockStyle.Fill;
  174. ucCutHole.UControl_ParaInfo.ShowParaInfo();
  175. plPrarInfo.Controls.Add(ucCutHole.UControl_ParaInfo);
  176. plLeftContent.Controls.Add(ucCutHole);
  177. }
  178. plPrarInfo.Visible = false;
  179. }
  180. #endregion
  181. //隐藏处理层
  182. private void FormHOZMain_Click(object sender, EventArgs e)
  183. {
  184. if (plPrarInfo.Visible)
  185. {
  186. plPrarInfo.Visible = false;
  187. }
  188. }
  189. #region 设置控件在线程过程中的编辑状态
  190. /// <summary>
  191. /// 设置控件在线程过程中的编辑状态
  192. /// </summary>
  193. /// <param name="cState"></param>
  194. private void SetWinControlMeasureState(bool cState)
  195. {
  196. pbNew.Enabled = cState;
  197. pbOpen.Enabled = cState;
  198. pbSave.Enabled = cState;
  199. pbInit.Enabled = cState;
  200. pbImportTemplateFile.Enabled = cState;
  201. pbLog.Enabled = cState;
  202. }
  203. #endregion
  204. #region 窗体中 线程开始 停止 最大化 最小化 关闭 按钮事件
  205. private void pbMin_MouseEnter(object sender, EventArgs e)
  206. {
  207. this.pbMin.BackgroundImage = global::HOZProject.Properties.Resources.Min_Blue;
  208. }
  209. private void pbMin_MouseLeave(object sender, EventArgs e)
  210. {
  211. this.pbMin.BackgroundImage = global::HOZProject.Properties.Resources.Min_Gray;
  212. }
  213. private void pbMax_MouseEnter(object sender, EventArgs e)
  214. {
  215. this.pbMax.BackgroundImage = global::HOZProject.Properties.Resources.Max_Blue;
  216. }
  217. private void pbMax_MouseLeave(object sender, EventArgs e)
  218. {
  219. this.pbMax.BackgroundImage = global::HOZProject.Properties.Resources.Max_Gray;
  220. }
  221. private void pbClose_MouseEnter(object sender, EventArgs e)
  222. {
  223. this.pbClose.BackgroundImage = global::HOZProject.Properties.Resources.exit_Red;
  224. }
  225. private void pbClose_MouseLeave(object sender, EventArgs e)
  226. {
  227. this.pbClose.BackgroundImage = global::HOZProject.Properties.Resources.exit_Gray;
  228. }
  229. private void pbStart_MouseEnter(object sender, EventArgs e)
  230. {
  231. this.pbStart.BackgroundImage = global::HOZProject.Properties.Resources.Start_Green;
  232. }
  233. private void pbStart_MouseLeave(object sender, EventArgs e)
  234. {
  235. this.pbStart.BackgroundImage = global::HOZProject.Properties.Resources.Start;
  236. }
  237. private void pbStop_MouseEnter(object sender, EventArgs e)
  238. {
  239. this.pbStop.BackgroundImage = global::HOZProject.Properties.Resources.Stop_Red;
  240. }
  241. private void pbStop_MouseLeave(object sender, EventArgs e)
  242. {
  243. this.pbStop.BackgroundImage = global::HOZProject.Properties.Resources.Stop;
  244. }
  245. private void pbClose_Click(object sender, EventArgs e)
  246. {
  247. this.Close();
  248. }
  249. private void pbMin_Click(object sender, EventArgs e)
  250. {
  251. this.WindowState = FormWindowState.Minimized;
  252. }
  253. private void pbMax_Click(object sender, EventArgs e)
  254. {
  255. if (this.WindowState == FormWindowState.Maximized)
  256. {
  257. this.WindowState = FormWindowState.Normal;
  258. }
  259. else
  260. {
  261. this.WindowState = FormWindowState.Maximized;
  262. }
  263. //窗体据中
  264. this.StartPosition = FormStartPosition.CenterScreen;
  265. }
  266. #endregion
  267. #region 新建、保存、打开、初始化、导入配置、查看日志
  268. private void pbNew_Click(object sender, EventArgs e)
  269. {
  270. NewCreate();
  271. }
  272. private void NewCreate()
  273. {
  274. m_MeasureFile = new MeasureFile();
  275. if (!m_MeasureFile.New())
  276. {
  277. return;
  278. }
  279. else
  280. {
  281. //清空内容容器中的控件
  282. ClearPanelControls();
  283. }
  284. }
  285. private void pbSave_Click(object sender, EventArgs e)
  286. {
  287. if (m_MeasureFile == null)
  288. {
  289. MessageBox.Show("请新建一个测量文件");
  290. }
  291. else
  292. {
  293. //保存测量文件
  294. if (m_MeasureFile.Save())
  295. {
  296. //获取测量文件所在路径
  297. string savePath = m_MeasureFile.FilePath;
  298. //数据库名称
  299. string dbFileName = "MeasureFile.db";
  300. //要复制的文件路径
  301. string pLocalFilePath = Application.StartupPath +"\\"+dbFileName;
  302. string pSaveFilePath = savePath + "\\" + dbFileName;//指定存储的路径
  303. if (File.Exists(pLocalFilePath))//必须判断要复制的文件是否存在
  304. {
  305. //三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换
  306. File.Copy(pLocalFilePath, pSaveFilePath, true);
  307. }
  308. //将初始化中的参数,配置到测量参数中
  309. m_MeasureFile.MParam = uControl_Init.GetMeasureParamInfo();
  310. //设置已保存状态
  311. IsSave = true;
  312. }
  313. }
  314. }
  315. private void pbOpen_Click(object sender, EventArgs e)
  316. {
  317. }
  318. private void pbInit_Click(object sender, EventArgs e)
  319. {
  320. if (uControl_Init == null)
  321. {
  322. uControl_Init = new UControl_Init(this);
  323. uControl_Init.ReloadConfig();
  324. }
  325. if (m_FormInit == null)
  326. {
  327. m_FormInit = new Form();
  328. }
  329. m_FormInit.StartPosition = FormStartPosition.CenterScreen;
  330. m_FormInit.FormBorderStyle = FormBorderStyle.None;
  331. m_FormInit.Width = uControl_Init.Width;
  332. m_FormInit.Height = uControl_Init.Height;
  333. if (m_FormInit.Controls.Count == 0)
  334. {
  335. m_FormInit.Controls.Add(uControl_Init);
  336. }
  337. m_FormInit.ShowDialog();
  338. }
  339. private void pbImportTemplateFile_Click(object sender, EventArgs e)
  340. {
  341. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  342. string m_TemplateFilePath = config.AppSettings.Settings["TemplateFilePath"].Value.ToString();
  343. //判断保存的路径是否存在
  344. if (!Directory.Exists(m_TemplateFilePath))
  345. {
  346. //创建路径
  347. Directory.CreateDirectory(m_TemplateFilePath);
  348. }
  349. //打开默认路径
  350. OpenFileDialog openFileDialog = new OpenFileDialog();
  351. //设置默认打开路径(绝对路径)
  352. openFileDialog.InitialDirectory = m_TemplateFilePath;
  353. openFileDialog.Filter = "样品参数文件|*.cfg";
  354. if (openFileDialog.ShowDialog() == DialogResult.OK)
  355. {
  356. if (uControl_Init == null)
  357. {
  358. uControl_Init = new UControl_Init(this);
  359. }
  360. //设置选择的模板文件
  361. uControl_Init.ReadConfigPath = openFileDialog.FileName;
  362. //获取文件
  363. uControl_Init.ReadConfigInfo();
  364. }
  365. }
  366. private void pbLog_Click(object sender, EventArgs e)
  367. {
  368. m_FormLog = new Form();
  369. m_FormLog.StartPosition = FormStartPosition.CenterScreen;
  370. UControl_Log uControl_Log = new UControl_Log();
  371. m_FormLog.FormBorderStyle = FormBorderStyle.None;
  372. m_FormLog.Width = uControl_Log.Width;
  373. m_FormLog.Height = uControl_Log.Height;
  374. uControl_Log.Name = "UControl_Log";
  375. //获取日志信息
  376. if (listmsg.Items.Count>0)
  377. {
  378. string[] strLog = new string[listmsg.Items.Count];
  379. for (int i = 0; i < listmsg.Items.Count; i++)
  380. {
  381. //赋值给数组
  382. strLog[i] = listmsg.Items[i].ToString();
  383. }
  384. uControl_Log.ShowProcessLogInfo(strLog);
  385. }
  386. m_FormLog.Controls.Add(uControl_Log);
  387. m_FormLog.ShowDialog();
  388. }
  389. //显示日志
  390. private void ShowLogInfo()
  391. {
  392. if (m_FormLog != null)
  393. {
  394. foreach (Control item in m_FormLog.Controls)
  395. {
  396. if (item is UserControl)
  397. {
  398. if (item.Name == "UControl_Log")
  399. {
  400. UControl_Log ucLog = (UControl_Log)item;
  401. //获取日志信息
  402. if (listmsg.Items.Count > 0)
  403. {
  404. string[] strLog = new string[listmsg.Items.Count];
  405. for (int i = 0; i < listmsg.Items.Count; i++)
  406. {
  407. //赋值给数组
  408. strLog[i] = listmsg.Items[i].ToString();
  409. }
  410. ucLog.ShowProcessLogInfo(strLog);
  411. }
  412. }
  413. }
  414. }
  415. }
  416. }
  417. #endregion
  418. #region 修改切孔状态
  419. /// <summary>
  420. /// 修改切孔状态
  421. /// </summary>
  422. /// <param name="cutHoleCode"></param>
  423. /// <param name="State"></param>
  424. private void ChangeCutHoleState(string CutHoleName, int State)
  425. {
  426. foreach (Control item in plLeftContent.Controls)
  427. {
  428. if (item is UserControl)
  429. {
  430. UControl_CutHole cutHole = (UControl_CutHole)item;
  431. if (cutHole.CutHoleName == CutHoleName)
  432. {
  433. Button btnCutHole = (Button)cutHole.Controls.Find("btnCutHole", false)[0];
  434. switch (State)
  435. {
  436. //开始运行
  437. case (int)ThreadState.InProcess:
  438. btnCutHole.BackColor = Color.Yellow;
  439. break;
  440. //完成
  441. case (int)ThreadState.Success:
  442. btnCutHole.BackColor = Color.GreenYellow;
  443. break;
  444. //错误
  445. case (int)ThreadState.Failed:
  446. btnCutHole.BackColor = Color.Red;
  447. break;
  448. }
  449. break;
  450. }
  451. }
  452. }
  453. }
  454. #endregion
  455. #region 拖动无窗体的控件
  456. [DllImport("user32.dll")]//拖动无窗体的控件
  457. public static extern bool ReleaseCapture();
  458. [DllImport("user32.dll")]
  459. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  460. public const int WM_SYSCOMMAND = 0x0112;
  461. public const int SC_MOVE = 0xF010;
  462. public const int HTCAPTION = 0x0002;
  463. private void FormHOZMain_MouseDown(object sender, MouseEventArgs e)
  464. {
  465. if (this.WindowState == FormWindowState.Normal)
  466. {
  467. //拖动窗体
  468. ReleaseCapture();
  469. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  470. }
  471. }
  472. #endregion
  473. #region 开始、结束线程事件
  474. private void pbStart_Click(object sender, EventArgs e)
  475. {
  476. if (m_MeasureFile == null)
  477. {
  478. MessageBox.Show("请新建一个测量文件");
  479. //listmsg.Items.Clear();
  480. }
  481. else
  482. {
  483. if (m_BackgroundWorker.IsBusy)
  484. {
  485. MessageBox.Show("线程已经运行");
  486. return;
  487. }
  488. m_BackgroundWorker.RunWorkerAsync(this);
  489. }
  490. }
  491. private void pbStop_Click(object sender, EventArgs e)
  492. {
  493. if (m_BackgroundWorker.IsBusy)
  494. {
  495. m_BackgroundWorker.CancelAsync();
  496. }
  497. }
  498. #endregion
  499. #region 清空内容容器中的控件
  500. /// <summary>
  501. /// 清空内容容器中的控件
  502. /// </summary>
  503. private void ClearPanelControls()
  504. {
  505. //清空内容容器中的控件
  506. plLeftContent.Controls.Clear();
  507. plPrarInfo.Controls.Clear();
  508. }
  509. #endregion
  510. #region 测试修改切孔中流程状态
  511. private void button1_Click(object sender, EventArgs e)
  512. {
  513. if (plPrarInfo.Controls.Count > 0)
  514. {
  515. string name = textBox1.Text;
  516. string code = textBox2.Text;
  517. int state = Convert.ToInt32(comboBox1.Text);
  518. foreach (Control item in plPrarInfo.Controls)
  519. {
  520. if (item is UserControl)
  521. {
  522. if (item.Name == name)
  523. {
  524. UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
  525. TimeLineItem[] ParaItem = uControl_ParaInfo.TlItem;
  526. foreach (TimeLineItem tlItem in ParaItem)
  527. {
  528. if (tlItem.Code == code)
  529. {
  530. tlItem.State = state;
  531. break;
  532. }
  533. }
  534. uControl_ParaInfo.TimeLineInvalidate();
  535. break;
  536. }
  537. }
  538. }
  539. }
  540. }
  541. #endregion
  542. }
  543. }