Form_Main.cs 37 KB

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