Form_Main.cs 38 KB

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