Form_Main.cs 40 KB

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