UControl_Init.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using MeasureData;
  11. using System.Configuration;
  12. using FileManager;
  13. namespace HOZProject
  14. {
  15. public partial class UControl_Init : UserControl
  16. {
  17. #region 全局变量
  18. String[] sT;
  19. String[] firms;
  20. String[] WPZD;
  21. String[] WPZF;
  22. String[] WQGD;
  23. String[] WQGF;
  24. String[] WLZ;
  25. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  26. private FormHOZMain formHOZMain;
  27. public FormHOZMain FormHOZMainObject { get => formHOZMain; set => formHOZMain = value; }
  28. //模板路径
  29. private string readConfigPath;
  30. public string ReadConfigPath { get => readConfigPath; set => readConfigPath = value; }
  31. #endregion
  32. #region 构造方法
  33. public UControl_Init(FormHOZMain formHOZ)
  34. {
  35. InitializeComponent();
  36. FormHOZMainObject = formHOZ;
  37. }
  38. #endregion
  39. private void UControl_Init_Load(object sender, EventArgs e)
  40. {
  41. }
  42. #region 关闭窗体
  43. private void btnClose_Click(object sender, EventArgs e)
  44. {
  45. Form fParent = this.ParentForm;
  46. fParent.Close();
  47. }
  48. #endregion
  49. #region 加载切孔文件,生成切孔列表
  50. private void pbCutHoleFile_Click(object sender, EventArgs e)
  51. {
  52. if (FormHOZMainObject.m_MeasureFile == null)
  53. {
  54. MessageBox.Show("请新建一个测量文件");
  55. }
  56. else
  57. {
  58. if (!FormHOZMainObject.m_MeasureFile.GetCutHolesFromFile(""))
  59. {
  60. MessageBox.Show("导入切孔失败");
  61. }
  62. else
  63. {
  64. List<CutHole> ListCutHole = FormHOZMainObject.m_MeasureFile.ListCutHole;
  65. //文件路径
  66. string CutHoleFilePath = FormHOZMainObject.m_MeasureFile.CutHoleFilePath;
  67. FormHOZMainObject.CreateCutHoleList(ListCutHole);
  68. //显示导入的切孔数量
  69. lblCutHoleCount.Text = string.Format("成功导入{0}个切孔", ListCutHole.Count);
  70. tbCutHoleFilePath.Text = CutHoleFilePath;
  71. }
  72. }
  73. }
  74. #endregion
  75. #region 根据一个切孔文件,自动计算其他切孔信息
  76. /// <summary>
  77. /// 根据一个切孔,自动计算其他切孔信息
  78. /// </summary>
  79. /// <param name="sender"></param>
  80. /// <param name="e"></param>
  81. private void pbCutHoleAuto_Click(object sender, EventArgs e)
  82. {
  83. }
  84. #endregion
  85. #region 加载PT模板文件
  86. private void pbTemplateFile_Click(object sender, EventArgs e)
  87. {
  88. string FilePathName;
  89. string fileNameWithoutExtension;
  90. //新建一个文件对话框
  91. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  92. //设置对话框标题
  93. pOpenFileDialog.Title = "选择模板文件";
  94. //设置打开文件类型
  95. pOpenFileDialog.Filter = "ely文件(*.ely)|*.ely";
  96. //监测文件是否存在
  97. pOpenFileDialog.CheckFileExists = true;
  98. //文件打开后执行以下程序
  99. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  100. {
  101. FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName); //绝对路径
  102. fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(FilePathName);
  103. this.txtWPTF.Text = FilePathName;
  104. }
  105. }
  106. #endregion
  107. #region 加载FIB模板文件
  108. private void pbFIBTemplateFile_Click(object sender, EventArgs e)
  109. {
  110. string FilePathName;
  111. string fileNameWithoutExtension;
  112. //新建一个文件对话框
  113. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  114. //设置对话框标题
  115. pOpenFileDialog.Title = "选择模板文件";
  116. //设置打开文件类型
  117. pOpenFileDialog.Filter = "ely文件(*.ely)|*.ely";
  118. //监测文件是否存在
  119. pOpenFileDialog.CheckFileExists = true;
  120. //文件打开后执行以下程序
  121. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  122. {
  123. FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName); //绝对路径
  124. fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(FilePathName);
  125. this.txtWFIBF.Text = FilePathName;
  126. }
  127. }
  128. #endregion
  129. #region 创建与读取样品参数、配置文件
  130. private void btnCreateConfig_Click(object sender, EventArgs e)
  131. {
  132. SaveTemplateFile();
  133. }
  134. private void SaveTemplateFile()
  135. {
  136. MeasureParam cfm = new MeasureParam();
  137. cfm.Is_Photograph = chkWIsP.Checked;
  138. cfm.PT = chkWPT.Checked;
  139. cfm.PTTemp = txtWPTF.Text;
  140. cfm.FIBTemp = txtWFIBF.Text;
  141. cfm.Stretch_Magnification = Convert.ToDouble(cbbWLZ.Text);
  142. cfm.Location_Magnification = Convert.ToDouble(cbbWQGF.Text);
  143. cfm.Location_Voltage = Convert.ToDouble(cbbWQGD.Text);
  144. cfm.Photograph_Magnification = Convert.ToDouble(cbbWPZF.Text);
  145. cfm.Photograph_Voltage = Convert.ToDouble(cbbWPZD.Text);
  146. if (cbbWXZ.SelectedIndex == 0)
  147. {
  148. cfm.Correction_Angle = 36.0;
  149. }
  150. else
  151. {
  152. cfm.Correction_Angle = 54.0;
  153. }
  154. cfm.SampleName = cbbWYP.Text;
  155. cfm.Firm = cbbWCS.Text;
  156. ConfigFile cf = new ConfigFile(cfm);
  157. SaveFileDialog saveFileDialog = new SaveFileDialog();
  158. saveFileDialog.Filter = "样品参数文件|*.cfg";
  159. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  160. {
  161. cf.Save(saveFileDialog.FileName);
  162. }
  163. else
  164. {
  165. return;
  166. }
  167. //编辑Config文件
  168. EditConfig();
  169. }
  170. /// <summary>
  171. /// 编辑confing 文件信息
  172. /// </summary>
  173. public bool EditConfig()
  174. {
  175. try
  176. {
  177. //设置数据源信息
  178. //样品类型
  179. List<String> _sT = sT.ToList();
  180. if (_sT.IndexOf(cbbWYP.Text) < 0)
  181. {
  182. _sT.Add(cbbWYP.Text);
  183. string wsT = string.Join(",", _sT.ToArray());
  184. config.AppSettings.Settings["Sample_Type"].Value = wsT;
  185. }
  186. //厂商
  187. List<String> _firms = firms.ToList();
  188. if (_firms.IndexOf(cbbWCS.Text) < 0)
  189. {
  190. _firms.Add(cbbWCS.Text);
  191. string wFirms = string.Join(",", _firms.ToArray());
  192. config.AppSettings.Settings["Firm"].Value = wFirms;
  193. }
  194. //拍照电压
  195. List<String> _WPZD = WPZD.ToList();
  196. if (_WPZD.IndexOf(cbbWPZD.Text) < 0)
  197. {
  198. _WPZD.Add(cbbWPZD.Text);
  199. string wWPZD = string.Join(",", _WPZD.ToArray());
  200. config.AppSettings.Settings["WPZD"].Value = wWPZD;
  201. }
  202. //拍照放大位数
  203. List<String> _WPZF = WPZF.ToList();
  204. if (_WPZF.IndexOf(cbbWPZF.Text) < 0)
  205. {
  206. _WPZF.Add(cbbWPZF.Text);
  207. string wWPZF = string.Join(",", _WPZF.ToArray());
  208. config.AppSettings.Settings["WPZF"].Value = wWPZF;
  209. }
  210. //定位切割电压
  211. List<String> _WQGD = WQGD.ToList();
  212. if (_WQGD.IndexOf(cbbWQGD.Text) < 0)
  213. {
  214. _WQGD.Add(cbbWQGD.Text);
  215. string wWQGD = string.Join(",", _WQGD.ToArray());
  216. config.AppSettings.Settings["WQGD"].Value = wWQGD;
  217. }
  218. //定位切割放大位数
  219. List<String> _WQGF = WQGF.ToList();
  220. if (_WQGF.IndexOf(cbbWQGF.Text) < 0)
  221. {
  222. _WQGF.Add(cbbWQGF.Text);
  223. string wWQGF = string.Join(",", _WQGF.ToArray());
  224. config.AppSettings.Settings["WQGF"].Value = wWQGF;
  225. }
  226. //拉直操作放大位数
  227. List<String> _WLZ = WLZ.ToList();
  228. if (_WLZ.IndexOf(cbbWLZ.Text) < 0)
  229. {
  230. _WLZ.Add(cbbWLZ.Text);
  231. string wWLZ = string.Join(",", _WLZ.ToArray());
  232. config.AppSettings.Settings["WLZ"].Value = wWLZ;
  233. }
  234. MeasureParam cfm = new MeasureParam();
  235. cfm.Is_Photograph = chkWIsP.Checked;
  236. cfm.PT = chkWPT.Checked;
  237. cfm.PTTemp = txtWPTF.Text;
  238. cfm.FIBTemp = txtWFIBF.Text;
  239. cfm.Stretch_Magnification = Convert.ToDouble(cbbWLZ.Text);
  240. cfm.Location_Magnification = Convert.ToDouble(cbbWQGF.Text);
  241. cfm.Location_Voltage = Convert.ToDouble(cbbWQGD.Text);
  242. cfm.Photograph_Magnification = Convert.ToDouble(cbbWPZF.Text);
  243. cfm.Photograph_Voltage = Convert.ToDouble(cbbWPZD.Text);
  244. if (cbbWXZ.SelectedIndex == 0)
  245. {
  246. cfm.Correction_Angle = 36.0;
  247. }
  248. else
  249. {
  250. cfm.Correction_Angle = 54.0;
  251. }
  252. cfm.SampleName = cbbWYP.Text;
  253. cfm.Firm = cbbWCS.Text;
  254. //设置配置文件默认值
  255. config.AppSettings.Settings["Is_Photograph"].Value = cfm.Is_Photograph.ToString();
  256. config.AppSettings.Settings["PT_Depostion"].Value = cfm.PT.ToString();
  257. config.AppSettings.Settings["PT_ELYFile"].Value = cfm.PTTemp;
  258. config.AppSettings.Settings["FIB_ELYFile"].Value = cfm.FIBTemp;
  259. config.AppSettings.Settings["Stretch_Magnification"].Value = cfm.Stretch_Magnification.ToString();
  260. config.AppSettings.Settings["Location_Magnification"].Value = cfm.Location_Magnification.ToString();
  261. config.AppSettings.Settings["Location_Voltage"].Value = cfm.Location_Voltage.ToString();
  262. config.AppSettings.Settings["Photograph_Magnification"].Value = cfm.Photograph_Magnification.ToString();
  263. config.AppSettings.Settings["Photograph_Voltage"].Value = cfm.Photograph_Voltage.ToString();
  264. config.AppSettings.Settings["Correction_Angle"].Value = cfm.Correction_Angle.ToString();
  265. config.AppSettings.Settings["SampleName"].Value = cfm.SampleName;
  266. config.AppSettings.Settings["Firms"].Value = cfm.Firm;
  267. config.Save(ConfigurationSaveMode.Modified);
  268. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  269. return true;
  270. }
  271. catch (Exception ex)
  272. {
  273. LogManager.LogError(ex.Message);
  274. return false;
  275. }
  276. }
  277. /// <summary>
  278. /// 删除config文件节点中内容信息
  279. /// </summary>
  280. public bool DelConfigNodeContentInfo(string delControlName, string selName)
  281. {
  282. try
  283. {
  284. switch (delControlName)
  285. {
  286. case "WYP":
  287. //样品类型
  288. List<String> _sT = sT.ToList();
  289. _sT.Remove(selName);
  290. string wsT = string.Join(",", _sT.ToArray());
  291. config.AppSettings.Settings["Sample_Type"].Value = wsT;
  292. break;
  293. case "WCS":
  294. //厂商
  295. List<String> _firms = firms.ToList();
  296. _firms.Remove(selName);
  297. string wFirms = string.Join(",", _firms.ToArray());
  298. config.AppSettings.Settings["Firm"].Value = wFirms;
  299. break;
  300. case "WPZD":
  301. //拍照电压
  302. List<String> _WPZD = WPZD.ToList();
  303. _WPZD.Remove(selName);
  304. string wWPZD = string.Join(",", _WPZD.ToArray());
  305. config.AppSettings.Settings["WPZD"].Value = wWPZD;
  306. break;
  307. case "WPZF":
  308. //拍照放大位数
  309. List<String> _WPZF = WPZF.ToList();
  310. _WPZF.Remove(selName);
  311. string wWPZF = string.Join(",", _WPZF.ToArray());
  312. config.AppSettings.Settings["WPZF"].Value = wWPZF;
  313. break;
  314. case "WQGD"://定位切割电压
  315. List<String> _WQGD = WQGD.ToList();
  316. _WQGD.Remove(selName);
  317. string wWQGD = string.Join(",", _WQGD.ToArray());
  318. config.AppSettings.Settings["WQGD"].Value = wWQGD;
  319. break;
  320. case "WQGF": //定位切割放大位数
  321. List<String> _WQGF = WQGF.ToList();
  322. _WQGF.Remove(selName);
  323. string wWQGF = string.Join(",", _WQGF.ToArray());
  324. config.AppSettings.Settings["WQGF"].Value = wWQGF;
  325. break;
  326. case "WLZ":
  327. //拉直操作放大位数
  328. List<String> _WLZ = WLZ.ToList();
  329. _WLZ.Remove(selName);
  330. string wWLZ = string.Join(",", _WLZ.ToArray());
  331. config.AppSettings.Settings["WLZ"].Value = wWLZ;
  332. break;
  333. }
  334. config.Save(ConfigurationSaveMode.Modified);
  335. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  336. return true;
  337. }
  338. catch (Exception ex)
  339. {
  340. LogManager.LogError(ex.Message);
  341. return true;
  342. }
  343. }
  344. /// <summary>
  345. /// 绑定下拉菜单
  346. /// </summary>
  347. /// <param name="cb"></param>
  348. /// <param name="strGroup"></param>
  349. private void BindComboxData(ComboBox cb, string[] strGroup)
  350. {
  351. for (int i = 0; i < strGroup.Length; i++)
  352. {
  353. if (!strGroup[i].Equals(""))
  354. {
  355. cb.Items.Add(strGroup[i]);
  356. }
  357. }
  358. }
  359. /// <summary>
  360. /// 读取Config文件信息
  361. /// </summary>
  362. public void ReloadConfig()
  363. {
  364. cbbWYP.Items.Clear();
  365. cbbWCS.Items.Clear();
  366. cbbWPZD.Items.Clear();
  367. cbbWPZF.Items.Clear();
  368. cbbWQGD.Items.Clear();
  369. cbbWQGF.Items.Clear();
  370. cbbWLZ.Items.Clear();
  371. string sample_Type = config.AppSettings.Settings["Sample_Type"].Value.ToString();
  372. string firm = config.AppSettings.Settings["Firm"].Value.ToString();
  373. string WPZDTemp = config.AppSettings.Settings["WPZD"].Value.ToString();
  374. string WPZFTemp = config.AppSettings.Settings["WPZF"].Value.ToString();
  375. string WQGDTemp = config.AppSettings.Settings["WQGD"].Value.ToString();
  376. string WQGFTemp = config.AppSettings.Settings["WQGF"].Value.ToString();
  377. string WLZTemp = config.AppSettings.Settings["WLZ"].Value.ToString();
  378. //样品类型
  379. sT = sample_Type.Split(',');
  380. BindComboxData(cbbWYP, sT);
  381. //厂商
  382. firms = firm.Split(',');
  383. BindComboxData(cbbWCS, firms);
  384. //拍照电压
  385. WPZD = WPZDTemp.Split(',');
  386. BindComboxData(cbbWPZD, WPZD);
  387. //拍照放大位数
  388. WPZF = WPZFTemp.Split(',');
  389. BindComboxData(cbbWPZF, WPZF);
  390. //定位切割电压
  391. WQGD = WQGDTemp.Split(',');
  392. BindComboxData(cbbWQGD, WQGD);
  393. //定位切割放大位数
  394. WQGF = WQGFTemp.Split(',');
  395. BindComboxData(cbbWQGF, WQGF);
  396. //拉直操作放大位数
  397. WLZ = WLZTemp.Split(',');
  398. BindComboxData(cbbWLZ, WLZ);
  399. //设置配置文件默认值
  400. chkWIsP.Checked=Convert.ToBoolean(config.AppSettings.Settings["Is_Photograph"].Value);
  401. chkWPT.Checked = Convert.ToBoolean(config.AppSettings.Settings["PT_Depostion"].Value);
  402. txtWPTF.Text = config.AppSettings.Settings["PT_ELYFile"].Value;
  403. txtWFIBF.Text = config.AppSettings.Settings["FIB_ELYFile"].Value;
  404. cbbWLZ.Text = config.AppSettings.Settings["Stretch_Magnification"].Value;
  405. cbbWQGF.Text = config.AppSettings.Settings["Location_Magnification"].Value;
  406. cbbWQGD.Text = config.AppSettings.Settings["Location_Voltage"].Value;
  407. cbbWPZF.Text = config.AppSettings.Settings["Photograph_Magnification"].Value;
  408. cbbWPZD.Text = config.AppSettings.Settings["Photograph_Voltage"].Value;
  409. string Correction_Angle = config.AppSettings.Settings["Correction_Angle"].Value;
  410. if (Correction_Angle == "36")
  411. {
  412. cbbWXZ.SelectedIndex = 0;
  413. }
  414. else
  415. {
  416. cbbWXZ.SelectedIndex = 1;
  417. }
  418. cbbWYP.Text = config.AppSettings.Settings["SampleName"].Value;
  419. cbbWCS.Text = config.AppSettings.Settings["Firms"].Value;
  420. }
  421. /// <summary>
  422. /// 根据所选路径,读取模板文件
  423. /// </summary>
  424. public void ReadConfigInfo()
  425. {
  426. if (!ReadConfigPath.Equals(""))
  427. {
  428. ReloadConfig();
  429. ConfigFile cfm = new ConfigFile(new MeasureParam());
  430. cfm.Read(ReadConfigPath);
  431. chkWIsP.Checked = cfm.m_Config.Is_Photograph;
  432. chkWPT.Checked = cfm.m_Config.PT;
  433. txtWPTF.Text = cfm.m_Config.PTTemp;
  434. txtWFIBF.Text = cfm.m_Config.FIBTemp;
  435. cbbWLZ.Text = cfm.m_Config.Stretch_Magnification.ToString();
  436. cbbWQGF.Text = cfm.m_Config.Location_Magnification.ToString();
  437. cbbWQGD.Text = cfm.m_Config.Location_Voltage.ToString();
  438. cbbWPZF.Text = cfm.m_Config.Photograph_Magnification.ToString();
  439. cbbWPZD.Text = cfm.m_Config.Photograph_Voltage.ToString();
  440. string Correction_Angle = cfm.m_Config.Correction_Angle.ToString();
  441. if (Correction_Angle == "36")
  442. {
  443. cbbWXZ.SelectedIndex = 0;
  444. }
  445. else
  446. {
  447. cbbWXZ.SelectedIndex = 1;
  448. }
  449. cbbWYP.Text = cfm.m_Config.SampleName;
  450. cbbWCS.Text = cfm.m_Config.Firm;
  451. }
  452. }
  453. /// <summary>
  454. /// 修改配置文件默认值
  455. /// </summary>
  456. /// <param name="sender"></param>
  457. /// <param name="e"></param>
  458. private void btnSaveDefalutPara_Click(object sender, EventArgs e)
  459. {
  460. if (EditConfig())
  461. {
  462. MessageBox.Show("保存成功!", "提示");
  463. }
  464. }
  465. #endregion
  466. #region 删除下拉菜单选择项
  467. private void DelComboBoxItem_Click(object sender, EventArgs e)
  468. {
  469. if (MessageBox.Show("是否删除所选项信息?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
  470. {
  471. PictureBox pbDel = (PictureBox)sender;
  472. string ctrlName = pbDel.Name.Substring(5, pbDel.Name.Length - 5);
  473. if (DelComboBoxItem(ctrlName))
  474. {
  475. //重新加载
  476. ReloadConfig();
  477. }
  478. }
  479. }
  480. /// <summary>
  481. /// 删除下拉菜单选择项
  482. /// </summary>
  483. public bool DelComboBoxItem(string controlName)
  484. {
  485. string delName = controlName;
  486. foreach (Control item in this.Controls)
  487. {
  488. if (item is ComboBox)
  489. {
  490. if (item.Name.Contains(delName))
  491. {
  492. ComboBox cb = (ComboBox)item;
  493. return DelConfigNodeContentInfo(delName, cb.Text);
  494. }
  495. }
  496. }
  497. return false;
  498. }
  499. #endregion
  500. #region 根据是否仅拍照,进行逻辑判断
  501. private void chkWIsP_CheckedChanged(object sender, EventArgs e)
  502. {
  503. if (chkWIsP.Checked)
  504. {
  505. chkWPT.Checked = false;
  506. chkWPT.Visible = false;
  507. }
  508. else
  509. {
  510. chkWPT.Visible = true;
  511. }
  512. }
  513. #endregion
  514. }
  515. }