UControl_Init.cs 21 KB

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