UControl_Init.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 = fileNameWithoutExtension;
  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 = fileNameWithoutExtension;
  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. ConfigFile cfm = new ConfigFile();
  138. cfm.Is_Photograph = chkWIsP.Checked;
  139. cfm.PT_Depostion = chkWPT.Checked;
  140. cfm.PT_ELYFile = txtWPTF.Text;
  141. cfm.FIB_ELYFile = 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.Sample_Type = cbbWYP.Text;
  156. cfm.Firm = cbbWCS.Text;
  157. SaveFileDialog saveFileDialog = new SaveFileDialog();
  158. saveFileDialog.Filter = "样品参数文件|*.cfg";
  159. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  160. {
  161. cfm.Save(saveFileDialog.FileName);
  162. }
  163. else
  164. {
  165. return;
  166. }
  167. //编辑Config文件
  168. EditConfig();
  169. }
  170. /// <summary>
  171. /// 编辑confing 文件信息
  172. /// </summary>
  173. public void EditConfig()
  174. {
  175. //设置数据源信息
  176. //样品类型
  177. List<String> _sT = sT.ToList();
  178. if (_sT.IndexOf(cbbWYP.Text) < 0)
  179. {
  180. _sT.Add(cbbWYP.Text);
  181. string wsT = string.Join(",", _sT.ToArray());
  182. config.AppSettings.Settings["Sample_Type"].Value = wsT;
  183. }
  184. //厂商
  185. List<String> _firms = firms.ToList();
  186. if (_firms.IndexOf(cbbWCS.Text) < 0)
  187. {
  188. _firms.Add(cbbWCS.Text);
  189. string wFirms = string.Join(",", _firms.ToArray());
  190. config.AppSettings.Settings["Firm"].Value = wFirms;
  191. }
  192. //拍照电压
  193. List<String> _WPZD = WPZD.ToList();
  194. if (_WPZD.IndexOf(cbbWPZD.Text) < 0)
  195. {
  196. _WPZD.Add(cbbWPZD.Text);
  197. string wWPZD = string.Join(",", _WPZD.ToArray());
  198. config.AppSettings.Settings["WPZD"].Value = wWPZD;
  199. }
  200. //拍照放大位数
  201. List<String> _WPZF = WPZF.ToList();
  202. if (_WPZF.IndexOf(cbbWPZF.Text) < 0)
  203. {
  204. _WPZF.Add(cbbWPZF.Text);
  205. string wWPZF = string.Join(",", _WPZF.ToArray());
  206. config.AppSettings.Settings["WPZF"].Value = wWPZF;
  207. }
  208. //定位切割电压
  209. List<String> _WQGD = WQGD.ToList();
  210. if (_WQGD.IndexOf(cbbWQGD.Text) < 0)
  211. {
  212. _WQGD.Add(cbbWQGD.Text);
  213. string wWQGD = string.Join(",", _WQGD.ToArray());
  214. config.AppSettings.Settings["WQGD"].Value = wWQGD;
  215. }
  216. //定位切割放大位数
  217. List<String> _WQGF = WQGF.ToList();
  218. if (_WQGF.IndexOf(cbbWQGF.Text) < 0)
  219. {
  220. _WQGF.Add(cbbWQGF.Text);
  221. string wWQGF = string.Join(",", _WQGF.ToArray());
  222. config.AppSettings.Settings["WQGF"].Value = wWQGF;
  223. }
  224. //拉直操作放大位数
  225. List<String> _WLZ = WLZ.ToList();
  226. if (_WLZ.IndexOf(cbbWLZ.Text) < 0)
  227. {
  228. _WLZ.Add(cbbWLZ.Text);
  229. string wWLZ = string.Join(",", _WLZ.ToArray());
  230. config.AppSettings.Settings["WLZ"].Value = wWLZ;
  231. }
  232. ConfigFile cfm = new ConfigFile();
  233. cfm.Is_Photograph = chkWIsP.Checked;
  234. cfm.PT_Depostion = chkWPT.Checked;
  235. cfm.PT_ELYFile = txtWPTF.Text;
  236. cfm.FIB_ELYFile = txtWFIBF.Text;
  237. cfm.Stretch_Magnification = Convert.ToDouble(cbbWLZ.Text);
  238. cfm.Location_Magnification = Convert.ToDouble(cbbWQGF.Text);
  239. cfm.Location_Voltage = Convert.ToDouble(cbbWQGD.Text);
  240. cfm.Photograph_Magnification = Convert.ToDouble(cbbWPZF.Text);
  241. cfm.Photograph_Voltage = Convert.ToDouble(cbbWPZD.Text);
  242. if (cbbWXZ.SelectedIndex == 0)
  243. {
  244. cfm.Correction_Angle = 36.0;
  245. }
  246. else
  247. {
  248. cfm.Correction_Angle = 54.0;
  249. }
  250. cfm.Sample_Type = cbbWYP.Text;
  251. cfm.Firm = cbbWCS.Text;
  252. //设置配置文件默认值
  253. config.AppSettings.Settings["Is_Photograph"].Value = cfm.Is_Photograph.ToString();
  254. config.AppSettings.Settings["PT_Depostion"].Value = cfm.PT_Depostion.ToString();
  255. config.AppSettings.Settings["PT_ELYFile"].Value = cfm.PT_ELYFile;
  256. config.AppSettings.Settings["FIB_ELYFile"].Value = cfm.FIB_ELYFile;
  257. config.AppSettings.Settings["Stretch_Magnification"].Value = cfm.Stretch_Magnification.ToString();
  258. config.AppSettings.Settings["Location_Magnification"].Value = cfm.Location_Magnification.ToString();
  259. config.AppSettings.Settings["Location_Voltage"].Value = cfm.Location_Voltage.ToString();
  260. config.AppSettings.Settings["Photograph_Magnification"].Value = cfm.Photograph_Magnification.ToString();
  261. config.AppSettings.Settings["Photograph_Voltage"].Value = cfm.Photograph_Voltage.ToString();
  262. config.AppSettings.Settings["Correction_Angle"].Value = cfm.Correction_Angle.ToString();
  263. config.AppSettings.Settings["Sample_Type"].Value = cfm.Sample_Type;
  264. config.AppSettings.Settings["Firm"].Value = cfm.Firm;
  265. config.Save(ConfigurationSaveMode.Modified);
  266. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  267. ReloadConfig();
  268. }
  269. /// <summary>
  270. /// 删除config文件节点中内容信息
  271. /// </summary>
  272. public bool DelConfigNodeContentInfo(string delControlName, string selName)
  273. {
  274. try
  275. {
  276. switch (delControlName)
  277. {
  278. case "WYP":
  279. //样品类型
  280. List<String> _sT = sT.ToList();
  281. _sT.Remove(selName);
  282. string wsT = string.Join(",", _sT.ToArray());
  283. config.AppSettings.Settings["Sample_Type"].Value = wsT;
  284. break;
  285. case "WCS":
  286. //厂商
  287. List<String> _firms = firms.ToList();
  288. _firms.Remove(selName);
  289. string wFirms = string.Join(",", _firms.ToArray());
  290. config.AppSettings.Settings["Firm"].Value = wFirms;
  291. break;
  292. case "WPZD":
  293. //拍照电压
  294. List<String> _WPZD = WPZD.ToList();
  295. _WPZD.Remove(selName);
  296. string wWPZD = string.Join(",", _WPZD.ToArray());
  297. config.AppSettings.Settings["WPZD"].Value = wWPZD;
  298. break;
  299. case "WPZF":
  300. //拍照放大位数
  301. List<String> _WPZF = WPZF.ToList();
  302. _WPZF.Remove(selName);
  303. string wWPZF = string.Join(",", _WPZF.ToArray());
  304. config.AppSettings.Settings["WPZF"].Value = wWPZF;
  305. break;
  306. case "WQGD"://定位切割电压
  307. List<String> _WQGD = WQGD.ToList();
  308. _WQGD.Remove(selName);
  309. string wWQGD = string.Join(",", _WQGD.ToArray());
  310. config.AppSettings.Settings["WQGD"].Value = wWQGD;
  311. break;
  312. case "WQGF": //定位切割放大位数
  313. List<String> _WQGF = WQGF.ToList();
  314. _WQGF.Remove(selName);
  315. string wWQGF = string.Join(",", _WQGF.ToArray());
  316. config.AppSettings.Settings["WQGF"].Value = wWQGF;
  317. break;
  318. case "WLZ":
  319. //拉直操作放大位数
  320. List<String> _WLZ = WLZ.ToList();
  321. _WLZ.Remove(selName);
  322. string wWLZ = string.Join(",", _WLZ.ToArray());
  323. config.AppSettings.Settings["WLZ"].Value = wWLZ;
  324. break;
  325. }
  326. config.Save(ConfigurationSaveMode.Modified);
  327. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  328. return true;
  329. }
  330. catch (Exception ex)
  331. {
  332. LogManager.LogError(ex.Message);
  333. return true;
  334. }
  335. }
  336. /// <summary>
  337. /// 绑定下拉菜单
  338. /// </summary>
  339. /// <param name="cb"></param>
  340. /// <param name="strGroup"></param>
  341. private void BindComboxData(ComboBox cb, string[] strGroup)
  342. {
  343. for (int i = 0; i < strGroup.Length; i++)
  344. {
  345. if (!strGroup[i].Equals(""))
  346. {
  347. cb.Items.Add(strGroup[i]);
  348. }
  349. }
  350. }
  351. /// <summary>
  352. /// 读取Config文件信息
  353. /// </summary>
  354. public void ReloadConfig()
  355. {
  356. cbbWYP.Items.Clear();
  357. cbbWCS.Items.Clear();
  358. cbbWPZD.Items.Clear();
  359. cbbWPZF.Items.Clear();
  360. cbbWQGD.Items.Clear();
  361. cbbWQGF.Items.Clear();
  362. cbbWLZ.Items.Clear();
  363. string sample_Type = config.AppSettings.Settings["Sample_Type"].ToString();
  364. string firm = config.AppSettings.Settings["Firm"].ToString();
  365. string WPZDTemp = config.AppSettings.Settings["WPZD"].ToString();
  366. string WPZFTemp = config.AppSettings.Settings["WPZF"].ToString();
  367. string WQGDTemp = config.AppSettings.Settings["WQGD"].ToString();
  368. string WQGFTemp = config.AppSettings.Settings["WQGF"].ToString();
  369. string WLZTemp = config.AppSettings.Settings["WLZ"].ToString();
  370. //样品类型
  371. sT = sample_Type.Split(',');
  372. BindComboxData(cbbWYP, sT);
  373. //厂商
  374. firms = firm.Split(',');
  375. BindComboxData(cbbWCS, firms);
  376. //拍照电压
  377. WPZD = WPZDTemp.Split(',');
  378. BindComboxData(cbbWPZD, WPZD);
  379. //拍照放大位数
  380. WPZF = WPZFTemp.Split(',');
  381. BindComboxData(cbbWPZF, WPZF);
  382. //定位切割电压
  383. WQGD = WQGDTemp.Split(',');
  384. BindComboxData(cbbWQGD, WQGD);
  385. //定位切割放大位数
  386. WQGF = WQGFTemp.Split(',');
  387. BindComboxData(cbbWQGF, WQGF);
  388. //拉直操作放大位数
  389. WLZ = WLZTemp.Split(',');
  390. BindComboxData(cbbWLZ, WLZ);
  391. cbbWYP.SelectedIndex = 0;
  392. cbbWCS.SelectedIndex = 0;
  393. cbbWXZ.SelectedIndex = 0;
  394. cbbWPZD.SelectedIndex = 0;
  395. cbbWPZF.SelectedIndex = 0;
  396. cbbWLZ.SelectedIndex = 0;
  397. cbbWQGD.SelectedIndex = 0;
  398. cbbWQGF.SelectedIndex = 0;
  399. }
  400. /// <summary>
  401. /// 根据所选路径,读取模板文件
  402. /// </summary>
  403. public void ReadConfigInfo()
  404. {
  405. if (!ReadConfigPath.Equals(""))
  406. {
  407. ReloadConfig();
  408. ConfigFile cfm = new ConfigFile();
  409. cfm.Read(ReadConfigPath);
  410. chkWIsP.Checked = cfm.Is_Photograph;
  411. chkWPT.Checked = cfm.PT_Depostion;
  412. txtWPTF.Text = cfm.PT_ELYFile;
  413. txtWFIBF.Text = cfm.FIB_ELYFile;
  414. cbbWLZ.Text = cfm.Stretch_Magnification.ToString();
  415. cbbWQGF.Text = cfm.Location_Magnification.ToString();
  416. cbbWQGD.Text = cfm.Location_Voltage.ToString();
  417. cbbWPZF.Text = cfm.Photograph_Magnification.ToString();
  418. cbbWPZD.Text = cfm.Photograph_Voltage.ToString();
  419. cbbWXZ.Text = cfm.Correction_Angle.ToString();
  420. cbbWYP.Text = cfm.Sample_Type;
  421. cbbWCS.Text = cfm.Firm;
  422. }
  423. }
  424. #endregion
  425. #region 删除下拉菜单选择项
  426. private void DelComboBoxItem_Click(object sender, EventArgs e)
  427. {
  428. if (MessageBox.Show("是否删除所选项信息?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
  429. {
  430. PictureBox pbDel = (PictureBox)sender;
  431. string ctrlName = pbDel.Name.Substring(5, pbDel.Name.Length - 5);
  432. if (DelComboBoxItem(ctrlName))
  433. {
  434. //重新加载
  435. ReloadConfig();
  436. }
  437. }
  438. }
  439. /// <summary>
  440. /// 删除下拉菜单选择项
  441. /// </summary>
  442. public bool DelComboBoxItem(string controlName)
  443. {
  444. string delName = controlName;
  445. foreach (Control item in this.Controls)
  446. {
  447. if (item is ComboBox)
  448. {
  449. if (item.Name.Contains(delName))
  450. {
  451. ComboBox cb = (ComboBox)item;
  452. return DelConfigNodeContentInfo(delName, cb.Text);
  453. }
  454. }
  455. }
  456. return false;
  457. }
  458. #endregion
  459. #region 根据是否仅拍照,进行逻辑判断
  460. private void chkWIsP_CheckedChanged(object sender, EventArgs e)
  461. {
  462. if (chkWIsP.Checked)
  463. {
  464. chkWPT.Checked = false;
  465. chkWPT.Visible = false;
  466. }
  467. else
  468. {
  469. chkWPT.Visible = true;
  470. }
  471. }
  472. #endregion
  473. private void btnSaveDefalutPara_Click(object sender, EventArgs e)
  474. {
  475. EditConfig();
  476. }
  477. }
  478. }