Form_Main.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. using OTS.WinFormsUI.Docking;
  2. using OTSPartA_STDEditor.BaseClass;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Windows.Forms;
  10. namespace OTSPartA_STDEditor
  11. {
  12. public partial class Form_Main : Form
  13. {
  14. public STDRuleslist m_STDRuleslist = null;
  15. public Attributes m_Attributes = null;
  16. public SubMidWindow m_SubMidWindow = null;
  17. public string STDDBAddress = "";
  18. public STDInfo _sTDEditor=null;
  19. /// <summary>
  20. /// 0:正在打开的标准库路径;1:所增加标准的颗粒所在结果库位置;2:所增加标准的颗粒FieldId;3:所增加标准的颗粒ParticleId;4:所增加标准的颗粒XRayId
  21. /// </summary>
  22. List<string> ImportArgs = new List<string>();
  23. //国际化
  24. public Language lan;
  25. Hashtable table;
  26. //是否做出过更改
  27. public static readonly string[] ColorGroup = { "#FFB6C1", "#FFC0CB", "#DC143C", "#FFF0F5", "#DB7093", "#FF69B4", "#FF1493", "#C71585", "#DA70D6", "#D8BFD8", "#DDA0DD", "#EE82EE", "#FF00FF", "#8B008B", "#800080", "#BA55D3", "#9400D3", "#9932CC", "#4B0082", "#8A2BE2", "#9370DB","#7B68EE", "#6A5ACD", "#483D8B", "#E6E6FA", "#F8F8FF", "#0000FF", "#0000CD", "#191970", "#00008B", "#000080", "#4169E1" , "#6495ED", "#B0C4DE", "#778899", "#708090", "#1E90FF", "#F0F8FF", "#00FFFF" };
  28. public Form_Main()
  29. {
  30. InitializeComponent();
  31. m_STDRuleslist = new STDRuleslist(this);
  32. m_Attributes = new Attributes(this);
  33. m_SubMidWindow = new SubMidWindow(this);
  34. }
  35. public Form_Main(string[] ImportStandardLibraryArgs)
  36. {
  37. InitializeComponent();
  38. m_STDRuleslist = new STDRuleslist(this);
  39. m_Attributes = new Attributes(this);
  40. m_SubMidWindow = new SubMidWindow(this);
  41. foreach (var it in ImportStandardLibraryArgs)
  42. {
  43. ImportArgs.Add(it);
  44. }
  45. }
  46. private void Form_ConstantsEditor2_Load(object sender, EventArgs e)
  47. {
  48. lan = new Language(this);
  49. table = lan.GetNameTable("Form_Main");
  50. if (STDDBAddress != "")
  51. {
  52. InitForms(STDDBAddress);
  53. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  54. }
  55. }
  56. public void ChangeSTDEditorAndGrid_Attributes(STDdata ddata)
  57. {
  58. m_SubMidWindow.ChangeText_textbox_STDEditor(ddata);
  59. m_Attributes.SetDatatoGrid_Attributes(ddata);
  60. }
  61. private void rbBackup_Click(object sender, EventArgs e)
  62. {
  63. if (m_STDRuleslist.treeView_G.SelectedNode != null && m_STDRuleslist.treeView_G.SelectedNode.Level == 1)
  64. {
  65. STDdata sTDdata = (STDdata)m_STDRuleslist.treeView_G.SelectedNode.Tag;
  66. if (sTDdata != null)
  67. {
  68. SaveDataOfSelRule(sTDdata.STDId);
  69. }
  70. }
  71. m_STDRuleslist.ConvertToRuleViewDic();
  72. try
  73. {
  74. SaveFileDialog saveFile = new SaveFileDialog();
  75. saveFile.Title = "Please select the path which to save the file";
  76. saveFile.Filter = "Database File(*.db)|*.db";
  77. saveFile.InitialDirectory = Application.StartupPath + "\\Config\\SysData\\";
  78. saveFile.OverwritePrompt = false; //是否覆盖当前文件
  79. if (saveFile.ShowDialog() == DialogResult.OK)
  80. {
  81. if (STDDBAddress == saveFile.FileName)
  82. {
  83. MessageBox.Show("Cannot save as same name!", "Tip");
  84. return;
  85. }
  86. FormForWaiting forWaiting = new FormForWaiting();
  87. forWaiting.Show();
  88. this.Refresh();
  89. if (Ruleformatcheck())
  90. {
  91. System.IO.File.Copy(STDDBAddress, saveFile.FileName, true);
  92. bool result = _sTDEditor.SaveAsDictionaryToClassify(saveFile.FileName);
  93. if (result)
  94. {
  95. _sTDEditor.UpdateSTDDictionaryInitial();
  96. STDDBAddress = saveFile.FileName;
  97. this.Text = this.Text.Split(' ')[0] + " " + saveFile.FileName;
  98. forWaiting.Close();
  99. MessageBox.Show("Save successful!", "Tip");
  100. }
  101. else
  102. {
  103. forWaiting.Close();
  104. MessageBox.Show("Save failed!", "Tip");
  105. }
  106. }
  107. else
  108. {
  109. forWaiting.Close();
  110. MessageBox.Show("Save failed!", "Tip");
  111. }
  112. }
  113. else
  114. {
  115. return;
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. MessageBox.Show(ex.Message);
  121. }
  122. }
  123. private void rbSave_Click(object sender, EventArgs e)
  124. {
  125. if (m_STDRuleslist.treeView_G.SelectedNode != null && m_STDRuleslist.treeView_G.SelectedNode.Level == 1)
  126. {
  127. STDdata sTDdata = (STDdata)m_STDRuleslist.treeView_G.SelectedNode.Tag;
  128. if (sTDdata != null)
  129. {
  130. SaveDataOfSelRule(sTDdata.STDId);
  131. }
  132. }
  133. m_STDRuleslist.ConvertToRuleViewDic();
  134. Save();
  135. }
  136. bool Ruleformatcheck()
  137. {
  138. foreach (var ite in _sTDEditor.STDDictionary)
  139. {
  140. if (CheckExpression(ite.Value.Expression))
  141. {
  142. }
  143. else
  144. {
  145. foreach (TreeNode treeNode in m_STDRuleslist.treeView_G.Nodes)
  146. {
  147. foreach (TreeNode treeNode1 in treeNode.Nodes)
  148. {
  149. if (((STDdata)treeNode1.Tag).STDId == ite.Key)
  150. {
  151. m_STDRuleslist.treeView_G.SelectedNode = treeNode1;
  152. ChangeSTDEditorAndGrid_Attributes(ite.Value);
  153. m_STDRuleslist.Predata = ite.Value;
  154. break;
  155. }
  156. }
  157. }
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. public bool Save()
  164. {
  165. if (Ruleformatcheck())
  166. {
  167. FormForWaiting forWaiting = new FormForWaiting();
  168. forWaiting.Show();
  169. this.Refresh();
  170. bool result = _sTDEditor.SaveDictionaryToClassify();
  171. if (result)
  172. {
  173. _sTDEditor.UpdateSTDDictionaryInitial();
  174. MessageBox.Show("Save successful!", "Tip");
  175. }
  176. else
  177. {
  178. MessageBox.Show("Save failed!", "Tip");
  179. }
  180. forWaiting.Close();
  181. return result;
  182. }
  183. else
  184. {
  185. return false;
  186. }
  187. }
  188. /// <summary>
  189. /// [颜色:RGB转成16进制]
  190. /// </summary>
  191. /// <param name="R">红 int</param>
  192. /// <param name="G">绿 int</param>
  193. /// <param name="B">蓝 int</param>
  194. /// <returns></returns>
  195. public static string colorRGBtoHx16(int R, int G, int B)
  196. {
  197. return System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(R, G, B));
  198. }
  199. //表达式规则检查
  200. public bool CheckExpression(string Expression)
  201. {
  202. string str_RemoveBlank = Expression.Replace(" ", "");
  203. if (!string.IsNullOrEmpty(str_RemoveBlank))
  204. {
  205. str_RemoveBlank = str_RemoveBlank.Replace("\r\n", "");
  206. //分割符号检验
  207. string[] Symbol = { "and", "or", "+", "-", "*", "/", "=", ">", "<" };
  208. for (int i = 0; i < Symbol.Length; i++)
  209. {
  210. for (int j = 0; j < Symbol.Length; j++)
  211. {
  212. if ((Symbol[i] + Symbol[j]) != ">=" && (Symbol[i] + Symbol[j]) != "<=")
  213. {
  214. if (str_RemoveBlank.Contains(Symbol[i] + Symbol[j]))
  215. {
  216. MessageBox.Show("Invalid string exists:" + Symbol[i] + Symbol[j], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  217. return false;
  218. }
  219. }
  220. }
  221. if (str_RemoveBlank.Contains(Symbol[i] + ")") || str_RemoveBlank.Contains("(" + Symbol[i]))
  222. {
  223. MessageBox.Show("Invalid string exists:" + Symbol[i] + ")", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  224. return false;
  225. }
  226. if (str_RemoveBlank.Contains("(" + Symbol[i]))
  227. {
  228. MessageBox.Show("Invalid string exists:" + "(" + Symbol[i], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  229. return false;
  230. }
  231. //尾字符检测
  232. int n= Symbol[i].Length;
  233. if (str_RemoveBlank.Length >= n)
  234. {
  235. if (str_RemoveBlank.Substring(str_RemoveBlank.Length-n, n) == Symbol[i])
  236. {
  237. MessageBox.Show("Last character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  238. return false;
  239. }
  240. if (str_RemoveBlank.Substring(0, n) == Symbol[i])
  241. {
  242. MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  243. return false;
  244. }
  245. }
  246. }
  247. //左右括号匹配检验
  248. int BracketsNum = 0;
  249. for (int i = 0; i < str_RemoveBlank.Length; i++)
  250. {
  251. if (str_RemoveBlank[i] == '(') BracketsNum++;
  252. if (str_RemoveBlank[i] == ')') BracketsNum--;
  253. }
  254. if (BracketsNum != 0)
  255. {
  256. MessageBox.Show("Number of left and right parentheses does not match!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  257. return false;
  258. }
  259. ////首字符检测
  260. //if ((str_RemoveBlank[0] == '+') || (str_RemoveBlank[0] == '-') || (str_RemoveBlank[0] == '*') || (str_RemoveBlank[0] == '/') || (str_RemoveBlank[0] == '>') || (str_RemoveBlank[0] == '<') || (str_RemoveBlank[0] == '=') || (str_RemoveBlank[0] == ')') || (str_RemoveBlank[str_RemoveBlank.Length - 1] == '('))
  261. //{
  262. // MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  263. // return false;
  264. //}
  265. //if (str_RemoveBlank.Length >= 3)
  266. //{
  267. // if ((str_RemoveBlank.Substring(0, 3) == "and") || (str_RemoveBlank.Substring(0, 3) == "end"))
  268. // {
  269. // MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  270. // return false;
  271. // }
  272. //}
  273. //forth_elem干扰or分隔符,故先行去掉
  274. //str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  275. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  276. List<string> str_Removeandor = new List<string>();
  277. for (int i = 0; i < str_Removeand.Length; i++)
  278. {
  279. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  280. }
  281. List<string> list_all = new List<string>();
  282. for (int i = 0; i < str_Removeandor.Count; i++)
  283. {
  284. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  285. }
  286. //常量
  287. List<string> Constantslist = new List<string>();
  288. for (int j = 0; j < m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Count; j++)
  289. {
  290. Constantslist.Add(m_SubMidWindow.m_STDEditor.comboBox_Constants.Items[j].ToString().Split('=')[0]);
  291. }
  292. for (int i = 0; i < list_all.Count; i++)
  293. {
  294. //周期元素?
  295. if (this.m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  296. {
  297. continue;
  298. }
  299. //first_elem?
  300. if (this.m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  301. {
  302. continue;
  303. }
  304. //Element1?
  305. if (this.m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  306. {
  307. continue;
  308. }
  309. //其它元素?
  310. if (this.m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  311. {
  312. continue;
  313. }
  314. //常量?
  315. if (Constantslist.Contains(list_all[i]))
  316. {
  317. continue;
  318. }
  319. //数字?
  320. double DNum = 0;
  321. if (double.TryParse(list_all[i], out DNum))
  322. {
  323. continue;
  324. }
  325. //>=? <=?等其它情况
  326. if (list_all[i] == "")
  327. {
  328. continue;
  329. }
  330. //true false?
  331. if ((list_all[i] == "true") || (list_all[i] == "false"))
  332. {
  333. continue;
  334. }
  335. MessageBox.Show("Please check whether the input rules are correct, error characters:" + list_all[i], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  336. return false;
  337. }
  338. //检测是否有<>= true,false
  339. if (!Expression.Contains("<") && !Expression.Contains(">") && !Expression.Contains("=") && Expression.ToLower() != "true" && Expression.ToLower() != "false")
  340. {
  341. MessageBox.Show("Expressions have no effect!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  342. return false;
  343. }
  344. }
  345. else
  346. {
  347. MessageBox.Show("The expresstion is mull!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Error);
  348. return false;
  349. }
  350. return true;
  351. }
  352. /// <summary>
  353. /// 属性检测
  354. /// </summary>
  355. /// <param name="ddata"></param>
  356. /// <returns></returns>
  357. public bool CheckAttributes(STDdata ddata)
  358. {
  359. if (string.IsNullOrEmpty(ddata.StrName))
  360. {
  361. MessageBox.Show("Rule name can not be null !", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  362. }
  363. return true;
  364. }
  365. public void SaveDataOfSelRule(int stdid)
  366. {
  367. foreach (int grpid in m_STDRuleslist.GroupViewDic.Keys)
  368. {
  369. foreach (STDdata ddata in m_STDRuleslist.GroupViewDic[grpid].ContainSTD)
  370. {
  371. if (ddata.STDId == stdid)
  372. {
  373. ddata.StrName = Convert.ToString(m_Attributes.Grid_Attributes[2, 1].Value);
  374. ddata.Color = colorRGBtoHx16(m_Attributes.Grid_Attributes[3, 1].View.BackColor.R, m_Attributes.Grid_Attributes[3, 1].View.BackColor.G, m_Attributes.Grid_Attributes[3, 1].View.BackColor.B);
  375. ddata.IfElementAnalysis = Convert.ToBoolean(m_Attributes.Grid_Attributes[4, 1].Value);
  376. ddata.Hardness = Convert.ToString(m_Attributes.Grid_Attributes[5, 1].Value);
  377. ddata.Density = Convert.ToString(m_Attributes.Grid_Attributes[6, 1].Value);
  378. ddata.Electrical_conductivity = Convert.ToString(m_Attributes.Grid_Attributes[7, 1].Value);
  379. ddata.BSE = Convert.ToString(m_Attributes.Grid_Attributes[8, 1].Value);
  380. if (m_Attributes.Grid_Attributes[9, 1].Value != null)
  381. {
  382. ddata.Formula = Convert.ToString(m_Attributes.Grid_Attributes[9, 1].Value);
  383. }
  384. else
  385. {
  386. ddata.Formula = "";
  387. }
  388. if (m_Attributes.Grid_Attributes[10, 1].Value != null)
  389. {
  390. ddata.Element = Convert.ToString(m_Attributes.Grid_Attributes[10, 1].Value);
  391. }
  392. else
  393. {
  394. ddata.Element = "";
  395. }
  396. if (m_Attributes.Grid_Attributes[11, 1].Value != null)
  397. {
  398. ddata.GroupId = _sTDEditor.GroupIdDictionaryFromName[Convert.ToString(m_Attributes.Grid_Attributes[11, 1].Value)];
  399. }
  400. else
  401. {
  402. ddata.GroupId = -1;
  403. }
  404. ddata.Expression = m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text.ToString();
  405. string sKeyElements = "";
  406. for (int i = 0; i < m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Columns.Count; i++)
  407. {
  408. sKeyElements += m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Rows[0].Cells[i].Value.ToString() + ",";
  409. }
  410. if (sKeyElements.Length > 0)
  411. {
  412. sKeyElements = sKeyElements.Substring(0, sKeyElements.Length - 1);
  413. }
  414. string sSubElements = "";
  415. for (int i = 0; i < m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Columns.Count; i++)
  416. {
  417. sSubElements += m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Rows[0].Cells[i].Value.ToString() + ",";
  418. }
  419. if (sSubElements.Length > 0)
  420. {
  421. sSubElements = sSubElements.Substring(0, sSubElements.Length - 1);
  422. }
  423. ddata.KeyElementList = sKeyElements;
  424. ddata.SubElementList = sSubElements;
  425. //ddata.ListNum = "0";
  426. break;
  427. }
  428. }
  429. }
  430. }
  431. public void RemoveSTDDictionaryItem(int STDId)
  432. {
  433. _sTDEditor.STDDictionary.Remove(STDId);
  434. }
  435. public void ChangeSTDRulesLISTBackColor()
  436. {
  437. int stdid = m_STDRuleslist.ChangeSTDRulesLISTBackColor();
  438. m_STDRuleslist.treeView_G.Focus();
  439. }
  440. public void ChangeSTDRuleName(String RuleName)
  441. {
  442. if(m_STDRuleslist.treeView_G.SelectedNode==null)
  443. {
  444. return;
  445. }
  446. if (m_STDRuleslist.treeView_G.SelectedNode.Level == 0)
  447. {
  448. return;
  449. }
  450. else
  451. {
  452. if (RuleName == null || RuleName == "")
  453. {
  454. MessageBox.Show("RuleName Cannot Be Null!", "Tip");
  455. m_Attributes.Grid_Attributes[2, 1].Value= ((STDdata)m_STDRuleslist.treeView_G.SelectedNode.Tag).StrName;
  456. return;
  457. }
  458. STDdata sT = (STDdata)m_STDRuleslist.treeView_G.SelectedNode.Tag;
  459. foreach (int grpid in m_STDRuleslist.GroupViewDic.Keys)
  460. {
  461. foreach (STDdata ddata in m_STDRuleslist.GroupViewDic[grpid].ContainSTD)
  462. {
  463. if (ddata.STDId == ((STDdata)m_STDRuleslist.treeView_G.SelectedNode.Tag).STDId)
  464. {
  465. ddata.StrName = RuleName;
  466. m_STDRuleslist.treeView_G.SelectedNode.Text = RuleName;
  467. break;
  468. }
  469. }
  470. }
  471. }
  472. m_STDRuleslist.treeView_G.Focus();
  473. }
  474. public void SetNull()
  475. {
  476. m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text = "";
  477. SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell();
  478. view.BackColor = Color.White;
  479. m_Attributes.Grid_Attributes[3, 1].View = view;
  480. for (int i = 1; i < m_Attributes.Grid_Attributes.RowsCount - 1; i++)
  481. {
  482. if (m_Attributes.Grid_Attributes[i, 1].Value != null)
  483. {
  484. if (m_Attributes.Grid_Attributes[i, 1].View is SourceGrid.Cells.Views.CheckBox)
  485. {
  486. m_Attributes.Grid_Attributes[i, 1].Value = true;
  487. }
  488. else
  489. {
  490. m_Attributes.Grid_Attributes[i, 1].Value = "";
  491. }
  492. }
  493. }
  494. m_Attributes.Grid_Attributes.Refresh();
  495. m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Rows.Clear();
  496. m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Columns.Clear();
  497. m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Rows.Clear();
  498. m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Columns.Clear();
  499. }
  500. private void ribbon_ESDMaxRules_Click(object sender, EventArgs e)
  501. {
  502. Form_MaxEDSRules form_MaxEDSRules = new Form_MaxEDSRules(STDDBAddress);
  503. form_MaxEDSRules.Show();
  504. }
  505. private void ribbon_ZeroRules_Click(object sender, EventArgs e)
  506. {
  507. Form_ZeroElementRules form_ZeroElementRules = new Form_ZeroElementRules(STDDBAddress);
  508. form_ZeroElementRules.Show();
  509. }
  510. private void ribbon_UserConstants_Click(object sender, EventArgs e)
  511. {
  512. Form_UserConstants form_UserConstants = new Form_UserConstants(STDDBAddress);
  513. DialogResult dialogResult = form_UserConstants.ShowDialog();
  514. if (dialogResult == DialogResult.Yes)
  515. {
  516. try
  517. {
  518. _sTDEditor.LoadConstants();
  519. string[] ConstantsStr2 = _sTDEditor.ConstantsStr;
  520. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Clear();
  521. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.AddRange(ConstantsStr2);
  522. }
  523. catch (Exception ee)
  524. {
  525. MessageBox.Show(ee.ToString());
  526. }
  527. }
  528. }
  529. private void ribbon_GroupNameMaintenance_Click(object sender, EventArgs e)
  530. {
  531. int selGroupId = _sTDEditor.GroupIdDictionaryFromName[m_Attributes.Grid_Attributes[11, 1].Value.ToString()];
  532. Form_GroupId form_GroupId = new Form_GroupId(STDDBAddress, this);
  533. DialogResult dialogResult = form_GroupId.ShowDialog();
  534. if (dialogResult == DialogResult.Yes)
  535. {
  536. m_Attributes.AddSTDGroupsToAttribute();
  537. m_Attributes.Grid_Attributes[11, 1].Value = _sTDEditor.GroupIdDictionaryFromId[selGroupId];
  538. }
  539. }
  540. private void ribbon_Exit_Click(object sender, EventArgs e)
  541. {
  542. this.Close();
  543. }
  544. private void rbClose_Click(object sender, EventArgs e)
  545. {
  546. bool expressionright = true;
  547. if (!_sTDEditor.Isoldversion)
  548. {
  549. if (!EqualsBetweenDictionary())
  550. {
  551. DialogResult dr = MessageBox.Show("Whether to save the current modification?", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  552. if (dr == DialogResult.Yes)
  553. {
  554. expressionright = Save();
  555. }
  556. }
  557. }
  558. if (expressionright)
  559. {
  560. m_Attributes.Close();
  561. m_STDRuleslist.Close();
  562. m_SubMidWindow.m_STDEditor.Close();
  563. m_SubMidWindow.m_ComparativeLibrary.Close();
  564. m_SubMidWindow.Close();
  565. ButtonStatusClose();
  566. if (this.Text.Split(' ').Count() != 0)
  567. {
  568. this.Text = this.Text.Split(' ')[0];
  569. }
  570. }
  571. }
  572. private void rbOpen_Click(object sender, EventArgs e)
  573. {
  574. OpenFileDialog openFileDialog = new OpenFileDialog();
  575. openFileDialog.Filter = "(*.db)|*.db";
  576. openFileDialog.RestoreDirectory = true;
  577. openFileDialog.InitialDirectory = Application.StartupPath + "\\Config\\SysData\\";
  578. openFileDialog.FilterIndex = 1;
  579. if (openFileDialog.ShowDialog() == DialogResult.OK)
  580. {
  581. try
  582. {
  583. STDDBAddress = openFileDialog.FileName;
  584. InitForms(STDDBAddress);
  585. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  586. CheckAccuracyofZeroRules();
  587. }
  588. catch(Exception ex)
  589. {
  590. MessageBox.Show(ex.ToString(), "Tip");
  591. }
  592. }
  593. }
  594. void ButtonStatusOpen()
  595. {
  596. rbOpen.Enabled = false;
  597. rbClose.Enabled = true;
  598. //rbRecover.Enabled = true;
  599. rbBackup.Enabled = true;
  600. rbSave.Enabled = true;
  601. rbNew.Enabled = false;
  602. ribbon_ZeroRules.Enabled = true;
  603. ribbon_ESDMaxRules.Enabled = true;
  604. ribbon_UserConstants.Enabled = true;
  605. ribbon_GroupNameMaintenance.Enabled = false;
  606. }
  607. void ButtonStatusClose()
  608. {
  609. rbOpen.Enabled = true;
  610. rbClose.Enabled = false;
  611. //rbRecover.Enabled = false;
  612. rbBackup.Enabled = false;
  613. rbSave.Enabled = false;
  614. rbNew.Enabled = true;
  615. ribbon_ZeroRules.Enabled = false;
  616. ribbon_ESDMaxRules.Enabled = false;
  617. ribbon_UserConstants.Enabled = false;
  618. ribbon_GroupNameMaintenance.Enabled = false;
  619. }
  620. void InitForms(string DBAddress)
  621. {
  622. ButtonStatusOpen();
  623. _sTDEditor = new STDInfo(DBAddress);
  624. if (_sTDEditor.STDDictionary == null|| _sTDEditor.GroupDictionary==null)
  625. {
  626. return;
  627. }
  628. m_STDRuleslist = new STDRuleslist(this);
  629. m_Attributes = new Attributes(this);
  630. m_SubMidWindow = new SubMidWindow(this);
  631. m_STDRuleslist.Show(dockPanel1, DockState.DockLeft);
  632. m_Attributes.Show(dockPanel1, DockState.DockRight);
  633. m_SubMidWindow.Show(dockPanel1, DockState.Document);
  634. dockPanel1.DockLeftPortion = 360;
  635. dockPanel1.DockRightPortion = 381;
  636. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Clear();
  637. if(_sTDEditor.ConstantsStr != null)
  638. {
  639. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.AddRange(_sTDEditor.ConstantsStr);
  640. }
  641. m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text = "";
  642. }
  643. private void Form_ConstantsEditor2_FormClosing(object sender, FormClosingEventArgs e)
  644. {
  645. if(_sTDEditor==null)
  646. {
  647. return;
  648. }
  649. if (_sTDEditor.Isoldversion)
  650. {
  651. return;
  652. }
  653. if (m_STDRuleslist.Text != "")
  654. {
  655. bool expressionright = true;
  656. if (!EqualsBetweenDictionary())
  657. {
  658. DialogResult dr = MessageBox.Show("Whether to save the current modification", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  659. if (dr == DialogResult.Yes)
  660. {
  661. expressionright = Save();
  662. if (!expressionright)
  663. { e.Cancel = true; return; }
  664. }
  665. }
  666. }
  667. }
  668. bool EqualsBetweenDictionary()
  669. {
  670. if (m_STDRuleslist.treeView_G.SelectedNode != null && m_STDRuleslist.treeView_G.SelectedNode.Level == 1)
  671. {
  672. STDdata sTDdata = (STDdata)m_STDRuleslist.treeView_G.SelectedNode.Tag;
  673. if (sTDdata != null)
  674. {
  675. SaveDataOfSelRule(sTDdata.STDId);
  676. }
  677. }
  678. m_STDRuleslist.ConvertToRuleViewDic();
  679. return _sTDEditor.EqualsBetweenDictionary();
  680. }
  681. private void rbNew_Click(object sender, EventArgs e)
  682. {
  683. SaveFileDialog saveFile = new SaveFileDialog();
  684. saveFile.Title = table["message23"].ToString();
  685. saveFile.Filter = table["message36"].ToString();
  686. saveFile.OverwritePrompt = true; //是否覆盖当前文件
  687. saveFile.RestoreDirectory = true; //还原上次目录
  688. if (saveFile.ShowDialog() == DialogResult.OK)
  689. {
  690. try
  691. {
  692. System.Data.SQLite.SQLiteConnection.CreateFile(saveFile.FileName);
  693. System.Data.SQLite.SQLiteConnection m_OtsDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + saveFile.FileName + "'");
  694. m_OtsDatabaseConnection.Open();
  695. System.Data.SQLite.SQLiteCommand OtsDatabaseCommand = m_OtsDatabaseConnection.CreateCommand();
  696. OtsDatabaseCommand.CommandText = "CREATE TABLE ClassifySTD (STDId INTEGER,StrName TEXT, Color TEXT,KeyElementList TEXT,SubElementList TEXT,UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT,Hardness TEXT,Density TEXT,Electrical_conductivity TEXT,BSE INTEGER,Formula TEXT,Element TEXT,IfElementAnalysis BOOLEAN,ListNum INT,GroupId INT)";
  697. OtsDatabaseCommand.ExecuteNonQuery();
  698. OtsDatabaseCommand.CommandText = "CREATE TABLE Constants (value TEXT)";
  699. OtsDatabaseCommand.ExecuteNonQuery();
  700. OtsDatabaseCommand.CommandText = "CREATE TABLE MaxEDSRules (MaxEDSTime INT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  701. OtsDatabaseCommand.ExecuteNonQuery();
  702. OtsDatabaseCommand.CommandText = "CREATE TABLE STDGroups (id INTEGER,name TEXT, color TEXT,iorder INTEGER)";
  703. OtsDatabaseCommand.ExecuteNonQuery();
  704. OtsDatabaseCommand.CommandText = "CREATE TABLE STDMinerals (id INTEGER,SPEC BLOB)";
  705. OtsDatabaseCommand.ExecuteNonQuery();
  706. OtsDatabaseCommand.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  707. OtsDatabaseCommand.ExecuteNonQuery();
  708. System.Data.Common.DbTransaction trans = m_OtsDatabaseConnection.BeginTransaction();
  709. trans.Commit();
  710. }
  711. catch (Exception ex)
  712. {
  713. MessageBox.Show(ex.ToString());
  714. return;
  715. }
  716. STDDBAddress = saveFile.FileName;
  717. InitForms(STDDBAddress);
  718. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  719. }
  720. else
  721. {
  722. return;
  723. }
  724. }
  725. void CheckAccuracyofZeroRules()
  726. {
  727. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  728. m_dbConnection.Open();
  729. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ZeroElementRules", m_dbConnection);
  730. DataSet ds = new DataSet();
  731. m_dataAdapter.Fill(ds);
  732. DataTable dt = ds.Tables[0];
  733. DataTable dt2 = dt.Copy();
  734. if (dt != null)
  735. {
  736. if (dt.Columns[dt.Columns.Count - 1].ColumnName != "Expression")
  737. {
  738. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  739. cmm.CommandText = "CREATE TABLE sqlitestudio_temp_table AS SELECT * FROM ZeroElementRules";
  740. cmm.ExecuteNonQuery();
  741. cmm.CommandText = "DROP TABLE ZeroElementRules";
  742. cmm.ExecuteNonQuery();
  743. cmm.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT,UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  744. cmm.ExecuteNonQuery();
  745. cmm.CommandText = @"INSERT INTO ZeroElementRules (
  746. ZeroElement,
  747. UsingElementList,
  748. UsingImgPropertyList,
  749. UsingOtherPropertyList,
  750. Expression
  751. )
  752. SELECT ZeroElement,
  753. UsingElementList,
  754. UsingImgPropertyList,
  755. UsingOtherPropertyList,
  756. Expression
  757. FROM sqlitestudio_temp_table; ";
  758. cmm.ExecuteNonQuery();
  759. cmm.CommandText = "DROP TABLE sqlitestudio_temp_table";
  760. cmm.ExecuteNonQuery();
  761. m_dbConnection.Close();
  762. MessageBox.Show("检测到该数据库零元素规则格式错误,已进行修正!");
  763. }
  764. }
  765. }
  766. }
  767. }