Form_Main.cs 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. using OTS.WinFormsUI.Docking;
  2. using SourceGrid;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Windows.Forms;
  10. using System.Xml;
  11. using System.Xml.Linq;
  12. namespace OTSPartA_STDEditor
  13. {
  14. public partial class Form_Main : Form
  15. {
  16. public STDRuleslist m_STDRuleslist = null;
  17. public Attributes m_Attributes = null;
  18. public SubMidWindow m_SubMidWindow = null;
  19. /// <summary>
  20. /// 实际存储的字典
  21. /// </summary>
  22. public Dictionary<int, STDdata> STDDictionary = new Dictionary<int, STDdata>();
  23. /// <summary>
  24. /// 原始数据字典用于在关闭时比较是否弹出保存提示
  25. /// </summary>
  26. private Dictionary<int, STDdata> STDDictionaryInitial = new Dictionary<int, STDdata>();
  27. public Dictionary<int, string> GroupIdDictionaryFromId = new Dictionary<int, string>();
  28. public Dictionary<string, int> GroupIdDictionaryFromName = new Dictionary<string, int>();
  29. string STDDBAddress_backupDirectory = Application.StartupPath + "\\Config\\SysData\\LibBackup\\";
  30. public string STDDBAddress = "";
  31. /// <summary>
  32. /// 0:正在打开的标准库路径;1:所增加标准的颗粒所在结果库位置;2:所增加标准的颗粒FieldId;3:所增加标准的颗粒ParticleId;4:所增加标准的颗粒XRayId
  33. /// </summary>
  34. List<string> ImportArgs = new List<string>();
  35. //国际化
  36. public Language lan;
  37. Hashtable table;
  38. //是否做出过更改
  39. public bool IsModified { set; get; }
  40. public bool Isoldversion { set; get; }
  41. 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" };
  42. public Form_Main()
  43. {
  44. InitializeComponent();
  45. m_STDRuleslist = new STDRuleslist(this);
  46. m_Attributes = new Attributes(this);
  47. m_SubMidWindow = new SubMidWindow(this);
  48. }
  49. public Form_Main(string[] ImportStandardLibraryArgs)
  50. {
  51. InitializeComponent();
  52. m_STDRuleslist = new STDRuleslist(this);
  53. m_Attributes = new Attributes(this);
  54. m_SubMidWindow = new SubMidWindow(this);
  55. foreach (var it in ImportStandardLibraryArgs)
  56. {
  57. ImportArgs.Add(it);
  58. }
  59. }
  60. private void Form_ConstantsEditor2_Load(object sender, EventArgs e)
  61. {
  62. lan = new Language(this);
  63. table = lan.GetNameTable("Form_Main");
  64. if (STDDBAddress != "")
  65. {
  66. InitForms(STDDBAddress);
  67. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  68. IsModified = false;
  69. if (ImportArgs.Count > 1)
  70. {
  71. ImportStandardLibraryTypeSelection m_importStandardLibraryTypeSelection = new ImportStandardLibraryTypeSelection();
  72. var result = m_importStandardLibraryTypeSelection.ShowDialog();
  73. if (m_importStandardLibraryTypeSelection.SelectEvent == "expression")
  74. {
  75. int STDId = AddSTDDictionaryItem();
  76. m_STDRuleslist.AddNewRow(STDId, "NewClassify", Attributes.colorHx16toRGB(STDDictionary[STDId].Color));
  77. }
  78. }
  79. }
  80. }
  81. void LoadConstants(string STDDBAddress)
  82. {
  83. try
  84. {
  85. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  86. m_dbConnection.Open();
  87. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from Constants", m_dbConnection);
  88. DataSet ds = new DataSet();
  89. m_dataAdapter.Fill(ds);
  90. DataTable dt = ds.Tables[0];
  91. if (dt.Rows.Count > 0)
  92. {
  93. string ConstantsStr = dt.Rows[0][0].ToString();
  94. ConstantsStr = ConstantsStr.Replace(" ", "");
  95. string[] ConstantsStr2 = ConstantsStr.Split(',');
  96. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Clear();
  97. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.AddRange(ConstantsStr2);
  98. }
  99. m_dbConnection.Close();
  100. }
  101. catch (Exception ee)
  102. {
  103. MessageBox.Show(ee.ToString());
  104. }
  105. }
  106. bool LoadClassifyToDictionary(string DBAddress, ref Dictionary<int, STDdata> STDDictionary)
  107. {
  108. try
  109. {
  110. //STDDictionaryInitial.Clear();
  111. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  112. m_dbConnection.Open();
  113. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD order by ListNum", m_dbConnection);
  114. DataSet ds = new DataSet();
  115. m_dataAdapter.Fill(ds);
  116. DataTable dt = ds.Tables[0];
  117. if (dt != null)
  118. {
  119. if ((dt.Columns.Contains("IfElementAnalysis")))
  120. {
  121. rbSave.Enabled = true;
  122. Isoldversion = false;
  123. }
  124. else
  125. {
  126. MessageBox.Show("该标准库为旧版本,只能进行另存操作!", "注意");
  127. rbSave.Enabled = false;
  128. Isoldversion = true;
  129. }
  130. if (dt.Rows.Count > 0)
  131. {
  132. foreach (DataRow item in dt.Rows)
  133. {
  134. STDdata new_STDdata = new STDdata();
  135. new_STDdata.Hardness = item["Hardness"].ToString();
  136. new_STDdata.Density = item["Density"].ToString();
  137. new_STDdata.Electrical_conductivity = item["Electrical_conductivity"].ToString();
  138. new_STDdata.BSE = item["BSE"].ToString();
  139. new_STDdata.Formula = item["Formula"].ToString();
  140. new_STDdata.Element = item["Element"].ToString();
  141. new_STDdata.StrName = item["StrName"].ToString();
  142. new_STDdata.Expression = item["Expression"].ToString();
  143. new_STDdata.Color = item["Color"].ToString();
  144. new_STDdata.KeyElementList = item["KeyElementList"].ToString();
  145. new_STDdata.SubElementList = item["SubElementList"].ToString();
  146. new_STDdata.GroupId = item["GroupId"].ToString();
  147. new_STDdata.ListNum = item["ListNum"].ToString();
  148. new_STDdata.GroupId = item["GroupId"].ToString();
  149. if (dt.Columns.Contains("IfElementAnalysis"))
  150. {
  151. if (Convert.ToBoolean(item["IfElementAnalysis"]))
  152. {
  153. new_STDdata.IfElementAnalysis = true;
  154. }
  155. else
  156. {
  157. new_STDdata.IfElementAnalysis = false;
  158. }
  159. }
  160. STDDictionary.Add(int.Parse(item["STDId"].ToString()), new_STDdata);
  161. }
  162. }
  163. }
  164. return true;
  165. }
  166. catch /*(Exception ee)*/
  167. {
  168. MessageBox.Show("The selected standard library is formatted incorrectly, please open the correct standard library!", "Tip");
  169. return false;
  170. }
  171. }
  172. public void ChangeSTDEditorAndGrid_Attributes(int STDId)
  173. {
  174. m_SubMidWindow.ChangeText_textbox_STDEditor(STDId);
  175. m_Attributes.SetDatatoGrid_Attributes(STDId);
  176. }
  177. private void rbBackup_Click(object sender, EventArgs e)
  178. {
  179. if (m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row >= 0)
  180. {
  181. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Column);
  182. }
  183. try
  184. {
  185. SaveFileDialog saveFile = new SaveFileDialog();
  186. saveFile.Title = "Please select the path which to save the file";
  187. saveFile.Filter = "Database File(*.db)|*.db";
  188. saveFile.InitialDirectory = Application.StartupPath + "\\Config\\SysData\\";
  189. saveFile.OverwritePrompt = false; //是否覆盖当前文件
  190. if (saveFile.ShowDialog() == DialogResult.OK)
  191. {
  192. if (STDDBAddress == saveFile.FileName)
  193. {
  194. MessageBox.Show("不能另存为同名文件!", "Tip");
  195. return;
  196. }
  197. FormForWaiting forWaiting = new FormForWaiting();
  198. forWaiting.Show();
  199. System.IO.File.Copy(STDDBAddress, saveFile.FileName, true);
  200. bool result = SaveAsDictionaryToClassify(saveFile.FileName);
  201. if (result)
  202. {
  203. STDDictionaryInitial.Clear();
  204. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  205. STDDBAddress = saveFile.FileName;
  206. this.Text = this.Text.Split(' ')[0] + " " + saveFile.FileName;
  207. IsModified = false;
  208. forWaiting.Close();
  209. MessageBox.Show("Save successful!", "Tip");
  210. }
  211. else
  212. {
  213. forWaiting.Close();
  214. MessageBox.Show("Save failed!", "Tip");
  215. }
  216. }
  217. else
  218. {
  219. return;
  220. }
  221. }
  222. catch (Exception ex)
  223. {
  224. MessageBox.Show(ex.Message);
  225. }
  226. }
  227. public bool ClearDb(string DBAddress, string DBTableName)
  228. {
  229. try
  230. {
  231. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  232. m_dbConnection.Open();
  233. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  234. cmm.CommandText = "delete from " + DBTableName;
  235. cmm.ExecuteNonQuery();
  236. m_dbConnection.Close();
  237. return true;
  238. }
  239. catch (Exception ex)
  240. {
  241. MessageBox.Show(ex.ToString());
  242. return false;
  243. }
  244. }
  245. object Clone(Dictionary<int, STDdata> STDDictionary)
  246. {
  247. Dictionary<int, STDdata> STDDictionaryInitial = new Dictionary<int, STDdata>();
  248. foreach (var key in STDDictionary.Keys)
  249. {
  250. STDdata tDdata = new STDdata();
  251. tDdata = tDdata.Clone(STDDictionary[key]) as STDdata;
  252. STDDictionaryInitial.Add(key, tDdata);
  253. }
  254. return STDDictionaryInitial;
  255. }
  256. private void rbSave_Click(object sender, EventArgs e)
  257. {
  258. if (m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row > 0)
  259. {
  260. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  261. }
  262. if (RuleformatcheckAndSave())
  263. {
  264. FormForWaiting forWaiting = new FormForWaiting();
  265. forWaiting.Show();
  266. bool result = SaveDictionaryToClassify(STDDBAddress);
  267. if (result)
  268. {
  269. STDDictionaryInitial.Clear();
  270. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  271. forWaiting.Close();
  272. IsModified = false;
  273. MessageBox.Show("Save successful!", "Tip");
  274. }
  275. else
  276. {
  277. forWaiting.Close();
  278. MessageBox.Show("Save failed!", "Tip");
  279. }
  280. }
  281. else
  282. {
  283. return;
  284. }
  285. }
  286. bool RuleformatcheckAndSave()
  287. {
  288. foreach (var ite in STDDictionary)
  289. {
  290. if (CheckExpression(ite.Value.Expression))
  291. {
  292. }
  293. else
  294. {
  295. Position pos = new Position(Convert.ToInt32(ite.Value.ListNum), 0);
  296. m_STDRuleslist.Grid_Minerals[Convert.ToInt32(ite.Value.ListNum), 0].Grid.Select();
  297. m_STDRuleslist.Grid_Minerals.Selection.Focus(pos, true);
  298. m_STDRuleslist.Grid_Minerals.Refresh();
  299. ChangeSTDEditorAndGrid_Attributes(ite.Key);
  300. m_STDRuleslist.PreRow = Convert.ToInt32(ite.Value.ListNum);
  301. return false;
  302. }
  303. }
  304. return true;
  305. }
  306. /// <summary>
  307. /// [颜色:RGB转成16进制]
  308. /// </summary>
  309. /// <param name="R">红 int</param>
  310. /// <param name="G">绿 int</param>
  311. /// <param name="B">蓝 int</param>
  312. /// <returns></returns>
  313. public static string colorRGBtoHx16(int R, int G, int B)
  314. {
  315. return System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(R, G, B));
  316. }
  317. //表达式规则检查
  318. public bool CheckExpression(string Expression)
  319. {
  320. string str_RemoveBlank = Expression.Replace(" ", "");
  321. if (!string.IsNullOrEmpty(str_RemoveBlank))
  322. {
  323. str_RemoveBlank = str_RemoveBlank.Replace("\r\n", "");
  324. //分割符号检验
  325. string[] Symbol = { "and", "or", "+", "-", "*", "/", "=", ">", "<" };
  326. for (int i = 0; i < Symbol.Length; i++)
  327. {
  328. for (int j = 0; j < Symbol.Length; j++)
  329. {
  330. if ((Symbol[i] + Symbol[j]) != ">=" && (Symbol[i] + Symbol[j]) != "<=")
  331. {
  332. if (str_RemoveBlank.Contains(Symbol[i] + Symbol[j]))
  333. {
  334. MessageBox.Show("Invalid string exists:" + Symbol[i] + Symbol[j], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  335. return false;
  336. }
  337. }
  338. }
  339. if (str_RemoveBlank.Contains(Symbol[i] + ")") || str_RemoveBlank.Contains("(" + Symbol[i]))
  340. {
  341. MessageBox.Show("Invalid string exists:" + Symbol[i] + ")", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  342. return false;
  343. }
  344. if (str_RemoveBlank.Contains("(" + Symbol[i]))
  345. {
  346. MessageBox.Show("Invalid string exists:" + "(" + Symbol[i], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  347. return false;
  348. }
  349. //尾字符检测
  350. int n= Symbol[i].Length;
  351. if (str_RemoveBlank.Length >= n)
  352. {
  353. if (str_RemoveBlank.Substring(str_RemoveBlank.Length-n, n) == Symbol[i])
  354. {
  355. MessageBox.Show("Last character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  356. return false;
  357. }
  358. if (str_RemoveBlank.Substring(0, n) == Symbol[i])
  359. {
  360. MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  361. return false;
  362. }
  363. }
  364. }
  365. //左右括号匹配检验
  366. int BracketsNum = 0;
  367. for (int i = 0; i < str_RemoveBlank.Length; i++)
  368. {
  369. if (str_RemoveBlank[i] == '(') BracketsNum++;
  370. if (str_RemoveBlank[i] == ')') BracketsNum--;
  371. }
  372. if (BracketsNum != 0)
  373. {
  374. MessageBox.Show("Number of left and right parentheses does not match!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  375. return false;
  376. }
  377. ////首字符检测
  378. //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] == '('))
  379. //{
  380. // MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  381. // return false;
  382. //}
  383. //if (str_RemoveBlank.Length >= 3)
  384. //{
  385. // if ((str_RemoveBlank.Substring(0, 3) == "and") || (str_RemoveBlank.Substring(0, 3) == "end"))
  386. // {
  387. // MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  388. // return false;
  389. // }
  390. //}
  391. //forth_elem干扰or分隔符,故先行去掉
  392. //str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  393. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  394. List<string> str_Removeandor = new List<string>();
  395. for (int i = 0; i < str_Removeand.Length; i++)
  396. {
  397. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  398. }
  399. List<string> list_all = new List<string>();
  400. for (int i = 0; i < str_Removeandor.Count; i++)
  401. {
  402. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  403. }
  404. //常量
  405. List<string> Constantslist = new List<string>();
  406. for (int j = 0; j < m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Count; j++)
  407. {
  408. Constantslist.Add(m_SubMidWindow.m_STDEditor.comboBox_Constants.Items[j].ToString().Split('=')[0]);
  409. }
  410. for (int i = 0; i < list_all.Count; i++)
  411. {
  412. //周期元素?
  413. if (this.m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  414. {
  415. continue;
  416. }
  417. //first_elem?
  418. if (this.m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  419. {
  420. continue;
  421. }
  422. //Element1?
  423. if (this.m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  424. {
  425. continue;
  426. }
  427. //其它元素?
  428. if (this.m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  429. {
  430. continue;
  431. }
  432. //常量?
  433. if (Constantslist.Contains(list_all[i]))
  434. {
  435. continue;
  436. }
  437. //数字?
  438. double DNum = 0;
  439. if (double.TryParse(list_all[i], out DNum))
  440. {
  441. continue;
  442. }
  443. //>=? <=?等其它情况
  444. if (list_all[i] == "")
  445. {
  446. continue;
  447. }
  448. //true false?
  449. if ((list_all[i] == "true") || (list_all[i] == "false"))
  450. {
  451. continue;
  452. }
  453. MessageBox.Show("Please check whether the input rules are correct, error characters:" + list_all[i], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  454. return false;
  455. }
  456. //检测是否有<>= true,false
  457. if (!Expression.Contains("<") && !Expression.Contains(">") && !Expression.Contains("=") && Expression.ToLower() != "true" && Expression.ToLower() != "false")
  458. {
  459. MessageBox.Show("Expressions have no effect!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  460. return false;
  461. }
  462. }
  463. else
  464. {
  465. MessageBox.Show("The expresstion is mull!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Error);
  466. return false;
  467. }
  468. return true;
  469. }
  470. /// <summary>
  471. /// 属性检测
  472. /// </summary>
  473. /// <param name="ddata"></param>
  474. /// <returns></returns>
  475. public bool CheckAttributes(STDdata ddata)
  476. {
  477. if (string.IsNullOrEmpty(ddata.StrName))
  478. {
  479. MessageBox.Show("Rule name can not be null !", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  480. }
  481. //if (string.IsNullOrEmpty(ddata.g)
  482. //{
  483. // if (!GroupIdDictionaryFromName.ContainsKey(m_Attributes.Grid_Attributes[11, 1].Value.ToString()))
  484. // {
  485. // Position pos = new Position(11, 1);
  486. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  487. // MessageBox.Show("This group name is not included, please fill in again!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  488. // return false;
  489. // }
  490. //}
  491. #region 化学式和元素暂无限制
  492. //if (m_Attributes.Grid_Attributes[7, 1].Value.ToString())
  493. //{
  494. // Position pos = new Position(7, 1);
  495. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  496. // MessageBox.Show(table["message10"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  497. // return false;
  498. //}
  499. //if (m_Attributes.Grid_Attributes[8, 1].Value.ToString())
  500. //{
  501. // Position pos = new Position(8, 1);
  502. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  503. // MessageBox.Show(table["message10"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  504. // return false;
  505. //}
  506. #endregion
  507. return true;
  508. }
  509. public void SaveDataOfSelRule(int PreRow, int PreColumn)
  510. {
  511. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].StrName = Convert.ToString(m_Attributes.Grid_Attributes[2, 1].Value);
  512. 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);
  513. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].IfElementAnalysis = Convert.ToBoolean(m_Attributes.Grid_Attributes[4, 1].Value);
  514. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Hardness = Convert.ToString(m_Attributes.Grid_Attributes[5, 1].Value);
  515. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Density = Convert.ToString(m_Attributes.Grid_Attributes[6, 1].Value);
  516. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Electrical_conductivity = Convert.ToString(m_Attributes.Grid_Attributes[7, 1].Value);
  517. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].BSE = Convert.ToString(m_Attributes.Grid_Attributes[8, 1].Value);
  518. if (m_Attributes.Grid_Attributes[9, 1].Value != null)
  519. {
  520. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Formula = Convert.ToString(m_Attributes.Grid_Attributes[9, 1].Value);
  521. }
  522. else
  523. {
  524. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Formula = "";
  525. }
  526. if (m_Attributes.Grid_Attributes[10, 1].Value != null)
  527. {
  528. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Element = Convert.ToString(m_Attributes.Grid_Attributes[10, 1].Value);
  529. }
  530. else
  531. {
  532. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Element = "";
  533. }
  534. if (m_Attributes.Grid_Attributes[11, 1].Value != null)
  535. {
  536. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].GroupId = GroupIdDictionaryFromName[Convert.ToString(m_Attributes.Grid_Attributes[11, 1].Value)].ToString();
  537. }
  538. else
  539. {
  540. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].GroupId = "0";
  541. }
  542. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Expression = m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text.ToString();
  543. string sKeyElements = "";
  544. for (int i = 0; i < m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Columns.Count; i++)
  545. {
  546. sKeyElements += m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Rows[0].Cells[i].Value.ToString() + ",";
  547. }
  548. if (sKeyElements.Length > 0)
  549. {
  550. sKeyElements = sKeyElements.Substring(0, sKeyElements.Length - 1);
  551. }
  552. string sSubElements = "";
  553. for (int i = 0; i < m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Columns.Count; i++)
  554. {
  555. sSubElements += m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Rows[0].Cells[i].Value.ToString() + ",";
  556. }
  557. if (sSubElements.Length > 0)
  558. {
  559. sSubElements = sSubElements.Substring(0, sSubElements.Length - 1);
  560. }
  561. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].KeyElementList = sKeyElements;
  562. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].SubElementList = sSubElements;
  563. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].ListNum = PreRow.ToString();
  564. }
  565. /// <summary>
  566. /// 保存ClassifySTD库和MineralElements库
  567. /// </summary>
  568. /// <param name="DBAddress"></param>
  569. /// <returns></returns>
  570. bool SaveDictionaryToClassify(string DBAddress)
  571. {
  572. //保存列表顺序
  573. for (int i = 1; i < m_STDRuleslist.Grid_Minerals.RowsCount; i++)
  574. {
  575. STDDictionary[(int)m_STDRuleslist.Grid_Minerals[i, 0].Tag].ListNum = i.ToString();
  576. }
  577. try
  578. {
  579. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  580. m_dbConnection.Open();
  581. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  582. cmm.CommandText = "delete from ClassifySTD";
  583. //cmm.CommandText = "delete from ClassifySTD_Backup";
  584. try
  585. {
  586. cmm.ExecuteNonQuery();
  587. }
  588. catch (Exception ex)
  589. {
  590. MessageBox.Show(ex.ToString());
  591. return false;
  592. }
  593. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection);
  594. //System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD_Backup", m_dbConnection);
  595. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter);
  596. DataSet ds = new DataSet();
  597. m_dataAdapter.Fill(ds, "ClassifySTD");
  598. DataTable dt = ds.Tables["ClassifySTD"];
  599. //m_dataAdapter.Fill(ds, "ClassifySTD_Backup");
  600. //DataTable dt = ds.Tables["ClassifySTD_Backup"];
  601. dt.Clear();
  602. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  603. {
  604. string UsingElementList = "";
  605. string UsingImgPropertyList = "";
  606. string UsingOtherPropertyList = "";
  607. List<string> UsingElementL = new List<string>();
  608. List<string> UsingImgPropertyL = new List<string>();
  609. List<string> UsingOtherPropertyL = new List<string>();
  610. string str_RemoveBlank = kv.Value.Expression;
  611. ////forth_elem干扰or分隔符,故先行去掉
  612. //if (str_RemoveBlank.Contains("fourth_elem"))
  613. //{
  614. // str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  615. // UsingOtherPropertyList = "fourth_elem";
  616. //}
  617. str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  618. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  619. List<string> str_Removeandor = new List<string>();
  620. for (int i = 0; i < str_Removeand.Length; i++)
  621. {
  622. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  623. }
  624. List<string> list_all = new List<string>();
  625. for (int i = 0; i < str_Removeandor.Count; i++)
  626. {
  627. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  628. }
  629. for (int i = 0; i < list_all.Count; i++)
  630. {
  631. //周期元素?
  632. if (m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  633. {
  634. if (!UsingElementL.Contains(list_all[i]))
  635. {
  636. UsingElementL.Add(list_all[i]);
  637. }
  638. }
  639. //first_elem?
  640. if (m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  641. {
  642. if (!UsingOtherPropertyL.Contains(list_all[i]))
  643. {
  644. UsingOtherPropertyL.Add(list_all[i]);
  645. }
  646. }
  647. //Element1?
  648. if (m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  649. {
  650. if (!UsingOtherPropertyL.Contains(list_all[i]))
  651. {
  652. UsingOtherPropertyL.Add(list_all[i]);
  653. }
  654. }
  655. //其它元素?
  656. if (m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  657. {
  658. if (!UsingImgPropertyL.Contains(list_all[i]))
  659. {
  660. UsingImgPropertyL.Add(list_all[i]);
  661. }
  662. }
  663. }
  664. if (UsingElementL.Count > 0)
  665. {
  666. for (int i = 0; i < UsingElementL.Count - 1; i++)
  667. {
  668. UsingElementList += UsingElementL[i] + ",";
  669. }
  670. UsingElementList += UsingElementL[UsingElementL.Count - 1];
  671. }
  672. if (UsingImgPropertyL.Count > 0)
  673. {
  674. for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  675. {
  676. UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  677. }
  678. UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  679. }
  680. if (UsingOtherPropertyL.Count > 0)
  681. {
  682. for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  683. {
  684. UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  685. }
  686. UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  687. }
  688. DataRow newRow = dt.NewRow();
  689. newRow["STDId"] = kv.Key;
  690. newRow["StrName"] = kv.Value.StrName;
  691. newRow["Color"] = kv.Value.Color;
  692. newRow["KeyElementList"] = kv.Value.KeyElementList;
  693. newRow["SubElementList"] = kv.Value.SubElementList;
  694. newRow["UsingImgPropertyList"] = UsingImgPropertyList;
  695. newRow["UsingOtherPropertyList"] = UsingOtherPropertyList;
  696. newRow["Expression"] = kv.Value.Expression;
  697. newRow["Hardness"] = kv.Value.Hardness;
  698. newRow["Density"] = kv.Value.Density;
  699. newRow["Electrical_conductivity"] = kv.Value.Electrical_conductivity;
  700. newRow["BSE"] = int.Parse(kv.Value.BSE);
  701. newRow["Formula"] = kv.Value.Formula;
  702. newRow["Element"] = kv.Value.Element;
  703. newRow["IfElementAnalysis"] = kv.Value.IfElementAnalysis;
  704. newRow["GroupId"] = int.Parse(kv.Value.GroupId);
  705. newRow["ListNum"] = kv.Value.ListNum;
  706. dt.Rows.Add(newRow);
  707. }
  708. m_dataAdapter.Update(ds, "ClassifySTD");
  709. m_dbConnection.Close();
  710. }
  711. catch (Exception ex)
  712. {
  713. MessageBox.Show(ex.ToString());
  714. return false;
  715. }
  716. return true;
  717. }
  718. bool SaveAsDictionaryToClassify(string DBAddress)
  719. {
  720. ////保存列表顺序
  721. //for (int i = 1; i < m_STDRuleslist.Grid_Minerals.RowsCount; i++)
  722. //{
  723. // STDDictionary[(int)m_STDRuleslist.Grid_Minerals[i, 0].Tag].ListNum = i.ToString();
  724. //}
  725. try
  726. {
  727. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  728. m_dbConnection.Open();
  729. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  730. cmm.CommandText = "drop table ClassifySTD";
  731. cmm.ExecuteNonQuery();
  732. cmm.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)";
  733. cmm.ExecuteNonQuery();
  734. System.Data.Common.DbTransaction trans = m_dbConnection.BeginTransaction();
  735. trans.Commit();
  736. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection);
  737. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter);
  738. DataSet ds = new DataSet();
  739. m_dataAdapter.Fill(ds, "ClassifySTD");
  740. DataTable dt = ds.Tables["ClassifySTD"];
  741. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  742. {
  743. string UsingElementList = "";
  744. string UsingImgPropertyList = "";
  745. string UsingOtherPropertyList = "";
  746. List<string> UsingElementL = new List<string>();
  747. List<string> UsingImgPropertyL = new List<string>();
  748. List<string> UsingOtherPropertyL = new List<string>();
  749. string str_RemoveBlank = kv.Value.Expression;
  750. ////forth_elem干扰or分隔符,故先行去掉
  751. //if (str_RemoveBlank.Contains("fourth_elem"))
  752. //{
  753. // str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  754. // UsingOtherPropertyList = "fourth_elem,";
  755. //}
  756. str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  757. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  758. List<string> str_Removeandor = new List<string>();
  759. for (int i = 0; i < str_Removeand.Length; i++)
  760. {
  761. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  762. }
  763. List<string> list_all = new List<string>();
  764. for (int i = 0; i < str_Removeandor.Count; i++)
  765. {
  766. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  767. }
  768. for (int i = 0; i < list_all.Count; i++)
  769. {
  770. //周期元素?
  771. if (m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  772. {
  773. if (!UsingElementL.Contains(list_all[i]))
  774. {
  775. UsingElementL.Add(list_all[i]);
  776. }
  777. }
  778. //first_elem?
  779. if (m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  780. {
  781. if (!UsingOtherPropertyL.Contains(list_all[i]))
  782. {
  783. UsingOtherPropertyL.Add(list_all[i]);
  784. }
  785. }
  786. //Element1?
  787. if (m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  788. {
  789. if (!UsingOtherPropertyL.Contains(list_all[i]))
  790. {
  791. UsingOtherPropertyL.Add(list_all[i]);
  792. }
  793. }
  794. //其它元素?
  795. if (m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  796. {
  797. if (!UsingImgPropertyL.Contains(list_all[i]))
  798. {
  799. UsingImgPropertyL.Add(list_all[i]);
  800. }
  801. }
  802. }
  803. if (UsingElementL.Count > 0)
  804. {
  805. for (int i = 0; i < UsingElementL.Count - 1; i++)
  806. {
  807. UsingElementList += UsingElementL[i] + ",";
  808. }
  809. UsingElementList += UsingElementL[UsingElementL.Count - 1];
  810. }
  811. if (UsingImgPropertyL.Count > 0)
  812. {
  813. for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  814. {
  815. UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  816. }
  817. UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  818. }
  819. if (UsingOtherPropertyL.Count > 0)
  820. {
  821. for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  822. {
  823. UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  824. }
  825. UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  826. }
  827. DataRow newRow = dt.NewRow();
  828. newRow["STDId"] = kv.Key;
  829. newRow["StrName"] = kv.Value.StrName;
  830. newRow["Color"] = kv.Value.Color;
  831. newRow["KeyElementList"] = kv.Value.KeyElementList;
  832. newRow["SubElementList"] = kv.Value.SubElementList;
  833. newRow["UsingImgPropertyList"] = UsingImgPropertyList;
  834. newRow["UsingOtherPropertyList"] = UsingOtherPropertyList;
  835. newRow["Expression"] = kv.Value.Expression;
  836. newRow["Hardness"] = kv.Value.Hardness;
  837. newRow["Density"] = kv.Value.Density;
  838. newRow["Electrical_conductivity"] = kv.Value.Electrical_conductivity;
  839. newRow["BSE"] = int.Parse(kv.Value.BSE);
  840. newRow["Formula"] = kv.Value.Formula;
  841. newRow["Element"] = kv.Value.Element;
  842. newRow["IfElementAnalysis"] = kv.Value.IfElementAnalysis;
  843. newRow["GroupId"] = int.Parse(kv.Value.GroupId);
  844. newRow["ListNum"] = kv.Value.ListNum;
  845. dt.Rows.Add(newRow);
  846. }
  847. m_dataAdapter.Update(ds, "ClassifySTD");
  848. m_dbConnection.Close();
  849. }
  850. catch (Exception ex)
  851. {
  852. MessageBox.Show(ex.ToString());
  853. return false;
  854. }
  855. return true;
  856. }
  857. public int AddSTDDictionaryItem()
  858. {
  859. STDdata new_STDdata = new STDdata();//定义一个TreeNode节点对象
  860. new_STDdata.Hardness = "1";
  861. new_STDdata.Density = "1";
  862. new_STDdata.Electrical_conductivity = "1";
  863. new_STDdata.BSE = "1";
  864. new_STDdata.Formula = "1";
  865. new_STDdata.Element = "1";
  866. new_STDdata.StrName = "NewRuleName";
  867. //new_STDdata.Expression = "NewRules";
  868. new_STDdata.Expression = "false";
  869. new_STDdata.KeyElementList = "";
  870. new_STDdata.SubElementList = "";
  871. Random random = new Random();
  872. new_STDdata.Color = ColorGroup[random.Next(ColorGroup.Length)];
  873. int STDId = 40000;
  874. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  875. {
  876. if (STDId < kv.Key)
  877. {
  878. STDId = kv.Key;
  879. }
  880. }
  881. STDDictionary.Add(STDId + 1, new_STDdata);
  882. return STDId + 1;
  883. }
  884. public void RemoveSTDDictionaryItem(int STDId)
  885. {
  886. STDDictionary.Remove(STDId);
  887. }
  888. public void ChangeStrName(int RowNum, string NewStrName)
  889. {
  890. STDDictionary[(int)m_STDRuleslist.Grid_Minerals[RowNum, 0].Tag].StrName = NewStrName;
  891. m_Attributes.Grid_Attributes[2, 1].Value = NewStrName;
  892. }
  893. public void ChangeSTDRulesLISTBackColor()
  894. {
  895. int stdid = m_STDRuleslist.ChangeSTDRulesLISTBackColor();
  896. }
  897. public void ChangeSTDRuleName(String RuleName)
  898. {
  899. m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value = RuleName;
  900. }
  901. public void SetNull()
  902. {
  903. m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text = "";
  904. for (int i = 1; i < m_Attributes.Grid_Attributes.RowsCount - 1; i++)
  905. {
  906. if (m_Attributes.Grid_Attributes[i, 1].Value != null)
  907. {
  908. if (m_Attributes.Grid_Attributes[i, 1].View is SourceGrid.Cells.Views.CheckBox)
  909. {
  910. m_Attributes.Grid_Attributes[i, 1].Value = true;
  911. }
  912. else
  913. {
  914. m_Attributes.Grid_Attributes[i, 1].Value = "";
  915. }
  916. }
  917. }
  918. SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell();
  919. view.BackColor = Color.White;
  920. m_Attributes.Grid_Attributes[2, 1].View = view;
  921. m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Rows.Clear();
  922. m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Columns.Clear();
  923. m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Rows.Clear();
  924. m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Columns.Clear();
  925. }
  926. private void ribbon_ESDMaxRules_Click(object sender, EventArgs e)
  927. {
  928. Form_MaxEDSRules form_MaxEDSRules = new Form_MaxEDSRules(STDDBAddress);
  929. form_MaxEDSRules.Show();
  930. }
  931. private void ribbon_ZeroRules_Click(object sender, EventArgs e)
  932. {
  933. Form_ZeroElementRules form_ZeroElementRules = new Form_ZeroElementRules(STDDBAddress);
  934. form_ZeroElementRules.Show();
  935. }
  936. private void ribbon_UserConstants_Click(object sender, EventArgs e)
  937. {
  938. Form_UserConstants form_UserConstants = new Form_UserConstants(STDDBAddress);
  939. DialogResult dialogResult = form_UserConstants.ShowDialog();
  940. if (dialogResult == DialogResult.Yes)
  941. {
  942. try
  943. {
  944. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  945. m_dbConnection.Open();
  946. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select value from Constants", m_dbConnection);
  947. DataSet ds = new DataSet();
  948. m_dataAdapter.Fill(ds);
  949. DataTable dt = ds.Tables[0];
  950. string ConstantsStr = dt.Rows[0][0].ToString();
  951. ConstantsStr = ConstantsStr.Replace(" ", "");
  952. string[] ConstantsStr2 = ConstantsStr.Split(',');
  953. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Clear();
  954. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.AddRange(ConstantsStr2);
  955. m_dbConnection.Close();
  956. }
  957. catch (Exception ee)
  958. {
  959. MessageBox.Show(ee.ToString());
  960. }
  961. }
  962. }
  963. public void AddSTDGroupsToAttribute()
  964. {
  965. try
  966. {
  967. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  968. m_dbConnection.Open();
  969. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select name,id from STDGroups order by iorder", m_dbConnection);
  970. DataSet ds = new DataSet();
  971. m_dataAdapter.Fill(ds);
  972. DataTable dt = ds.Tables[0];
  973. if (dt != null)
  974. {
  975. if (dt.Rows.Count >= 0)
  976. {
  977. GroupIdDictionaryFromId.Clear();
  978. GroupIdDictionaryFromName.Clear();
  979. List<string> ss = new List<string>();
  980. if (dt.Select("id='0'").Length == 0)
  981. {
  982. GroupIdDictionaryFromId.Add(0, "Default");
  983. GroupIdDictionaryFromName.Add("Default", 0);
  984. ss.Add("Default");
  985. }
  986. foreach (DataRow item in dt.Rows)
  987. {
  988. ss.Add(item["name"].ToString());
  989. GroupIdDictionaryFromName.Add(item["name"].ToString(), int.Parse(item["id"].ToString()));
  990. GroupIdDictionaryFromId.Add(int.Parse(item["id"].ToString()), item["name"].ToString());
  991. }
  992. SourceGrid.Cells.Editors.ComboBox GroupIdCBBox = new SourceGrid.Cells.Editors.ComboBox(typeof(string));
  993. GroupIdCBBox.StandardValues = ss;
  994. GroupIdCBBox.EditableMode = SourceGrid.EditableMode.SingleClick | SourceGrid.EditableMode.Focus;
  995. m_Attributes.Grid_Attributes[11, 1] = new SourceGrid.Cells.Cell("Default", GroupIdCBBox);
  996. GroupIdCBBox.Control.DropDownStyle = ComboBoxStyle.DropDownList; //设置下拉框为不可以编辑的状态
  997. }
  998. }
  999. //string ConstantsStr = dt.Rows[0][0].ToString();
  1000. m_dbConnection.Close();
  1001. }
  1002. catch (Exception ee)
  1003. {
  1004. MessageBox.Show(ee.ToString());
  1005. }
  1006. }
  1007. void SetNonexistentGroupsToDefault()
  1008. {
  1009. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  1010. {
  1011. if (!GroupIdDictionaryFromId.Keys.Contains(int.Parse(kv.Value.GroupId)))
  1012. {
  1013. kv.Value.GroupId = "0";
  1014. }
  1015. }
  1016. m_Attributes.Grid_Attributes.Refresh();
  1017. }
  1018. private void ribbon_GroupNameMaintenance_Click(object sender, EventArgs e)
  1019. {
  1020. //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();
  1021. int selGroupId = GroupIdDictionaryFromName[m_Attributes.Grid_Attributes[11, 1].Value.ToString()];
  1022. Form_GroupId form_GroupId = new Form_GroupId(STDDBAddress, this);
  1023. DialogResult dialogResult = form_GroupId.ShowDialog();
  1024. if (dialogResult == DialogResult.Yes)
  1025. {
  1026. AddSTDGroupsToAttribute();
  1027. //SetNonexistentGroupsToDefault();
  1028. m_Attributes.Grid_Attributes[11, 1].Value = GroupIdDictionaryFromId[selGroupId];
  1029. }
  1030. }
  1031. private void ribbon_Exit_Click(object sender, EventArgs e)
  1032. {
  1033. this.Close();
  1034. }
  1035. private void rbClose_Click(object sender, EventArgs e)
  1036. {
  1037. if (!Isoldversion)
  1038. {
  1039. if (!EqualsBetweenDictionary(STDDictionaryInitial, STDDictionary) || IsModified)
  1040. {
  1041. DialogResult dr = MessageBox.Show("Whether to save the current modification?", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1042. if (dr == DialogResult.Yes)
  1043. {
  1044. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1045. {
  1046. if ((m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != ""))
  1047. {
  1048. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  1049. }
  1050. else
  1051. {
  1052. return;
  1053. }
  1054. }
  1055. else
  1056. {
  1057. STDDictionaryInitial.Clear();
  1058. }
  1059. FormForWaiting forWaiting = new FormForWaiting();
  1060. forWaiting.Show();
  1061. bool result = SaveDictionaryToClassify(STDDBAddress);
  1062. if (result)
  1063. {
  1064. forWaiting.Close();
  1065. }
  1066. else
  1067. {
  1068. forWaiting.Close();
  1069. MessageBox.Show("Save failed!", "Tip");
  1070. }
  1071. }
  1072. }
  1073. }
  1074. m_Attributes.Close();
  1075. m_STDRuleslist.Close();
  1076. m_SubMidWindow.m_STDEditor.Close();
  1077. m_SubMidWindow.m_ComparativeLibrary.Close();
  1078. m_SubMidWindow.Close();
  1079. ButtonStatusClose();
  1080. if (this.Text.Split(' ').Count() != 0)
  1081. {
  1082. this.Text = this.Text.Split(' ')[0];
  1083. }
  1084. }
  1085. private void rbOpen_Click(object sender, EventArgs e)
  1086. {
  1087. OpenFileDialog openFileDialog = new OpenFileDialog();
  1088. openFileDialog.Filter = "(*.db)|*.db";
  1089. openFileDialog.RestoreDirectory = true;
  1090. openFileDialog.InitialDirectory = Application.StartupPath + "\\Config\\SysData\\";
  1091. openFileDialog.FilterIndex = 1;
  1092. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1093. {
  1094. try
  1095. {
  1096. STDDBAddress = openFileDialog.FileName;
  1097. InitForms(STDDBAddress);
  1098. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  1099. IsModified = false;
  1100. CheckAccuracyofZeroRules();
  1101. }
  1102. catch(Exception ex)
  1103. {
  1104. MessageBox.Show(ex.ToString(), "Tip");
  1105. }
  1106. }
  1107. }
  1108. void ButtonStatusOpen()
  1109. {
  1110. rbOpen.Enabled = false;
  1111. rbClose.Enabled = true;
  1112. //rbRecover.Enabled = true;
  1113. rbBackup.Enabled = true;
  1114. rbSave.Enabled = true;
  1115. rbNew.Enabled = false;
  1116. ribbon_ZeroRules.Enabled = true;
  1117. ribbon_ESDMaxRules.Enabled = true;
  1118. ribbon_UserConstants.Enabled = true;
  1119. ribbon_GroupNameMaintenance.Enabled = true;
  1120. }
  1121. void ButtonStatusClose()
  1122. {
  1123. rbOpen.Enabled = true;
  1124. rbClose.Enabled = false;
  1125. //rbRecover.Enabled = false;
  1126. rbBackup.Enabled = false;
  1127. rbSave.Enabled = false;
  1128. rbNew.Enabled = true;
  1129. ribbon_ZeroRules.Enabled = false;
  1130. ribbon_ESDMaxRules.Enabled = false;
  1131. ribbon_UserConstants.Enabled = false;
  1132. ribbon_GroupNameMaintenance.Enabled = false;
  1133. }
  1134. void InitForms(string DBAddress)
  1135. {
  1136. STDDictionary.Clear();
  1137. ButtonStatusOpen();
  1138. bool ret = LoadClassifyToDictionary(DBAddress, ref STDDictionary);
  1139. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  1140. if (!ret)
  1141. {
  1142. return;
  1143. }
  1144. m_STDRuleslist = new STDRuleslist(this);
  1145. m_Attributes = new Attributes(this);
  1146. m_SubMidWindow = new SubMidWindow(this);
  1147. m_STDRuleslist.Show(dockPanel1, DockState.DockLeft);
  1148. m_Attributes.Show(dockPanel1, DockState.DockRight);
  1149. m_SubMidWindow.Show(dockPanel1, DockState.Document);
  1150. dockPanel1.DockLeftPortion = 360;
  1151. dockPanel1.DockRightPortion = 381;
  1152. if (m_SubMidWindow.m_STDEditor.m_sc != null)
  1153. {
  1154. m_SubMidWindow.m_STDEditor.m_sc.CloseDB();
  1155. }
  1156. m_SubMidWindow.m_STDEditor.m_sc = new SqlLiteClass(DBAddress);
  1157. LoadConstants(DBAddress);
  1158. AddSTDGroupsToAttribute();
  1159. m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text = "";
  1160. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1161. {
  1162. m_STDRuleslist.button_UpOrder.Enabled = false;
  1163. if (m_STDRuleslist.Grid_Minerals.RowsCount == 2) //就一条时向下按钮置灰
  1164. {
  1165. m_STDRuleslist.button_DownOrder.Enabled = false;
  1166. }
  1167. ChangeSTDEditorAndGrid_Attributes(int.Parse(m_STDRuleslist.Grid_Minerals[1, 0].Tag.ToString()));
  1168. Position pos = new Position(1, 0);
  1169. m_STDRuleslist.Grid_Minerals[1, 0].Grid.Select();
  1170. m_STDRuleslist.Grid_Minerals.Selection.Focus(pos, true);
  1171. }
  1172. }
  1173. bool EqualsBetweenDictionary(Dictionary<int, STDdata> STDDictionaryInitial, Dictionary<int, STDdata> STDDictionary)
  1174. {
  1175. if (STDDictionaryInitial.Count != STDDictionary.Count)
  1176. {
  1177. return false;
  1178. }
  1179. foreach (var key in STDDictionaryInitial.Keys)
  1180. {
  1181. if (STDDictionary.Keys.Contains(key))
  1182. {
  1183. if (!STDDictionaryInitial[key].Equals(STDDictionary[key]))
  1184. {
  1185. return false;
  1186. }
  1187. }
  1188. else
  1189. {
  1190. return false;
  1191. }
  1192. }
  1193. return true;
  1194. }
  1195. private void Form_ConstantsEditor2_FormClosing(object sender, FormClosingEventArgs e)
  1196. {
  1197. if (Isoldversion)
  1198. {
  1199. return;
  1200. }
  1201. if (m_STDRuleslist.Text != "")
  1202. {
  1203. if (!EqualsBetweenDictionary(STDDictionaryInitial, STDDictionary) || IsModified)
  1204. {
  1205. DialogResult dr = MessageBox.Show("Whether to save the current modification", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1206. if (dr == DialogResult.Yes)
  1207. {
  1208. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1209. {
  1210. if (m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != "")
  1211. {
  1212. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  1213. }
  1214. else
  1215. {
  1216. e.Cancel = true;
  1217. return;
  1218. }
  1219. }
  1220. else
  1221. {
  1222. STDDictionaryInitial.Clear();
  1223. }
  1224. FormForWaiting forWaiting = new FormForWaiting();
  1225. forWaiting.Show();
  1226. bool result = SaveDictionaryToClassify(STDDBAddress);
  1227. if (result)
  1228. {
  1229. forWaiting.Close();
  1230. }
  1231. else
  1232. {
  1233. forWaiting.Close();
  1234. MessageBox.Show("Save failed!", "Tip");
  1235. }
  1236. }
  1237. else
  1238. {
  1239. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  1240. {
  1241. if (!GroupIdDictionaryFromId.Keys.Contains(int.Parse(kv.Value.GroupId)))
  1242. {
  1243. MessageBox.Show("There is non-existent grouping informations in the classification, please adjust!");
  1244. e.Cancel = true;
  1245. return;
  1246. }
  1247. }
  1248. }
  1249. }
  1250. }
  1251. }
  1252. private void rbNew_Click(object sender, EventArgs e)
  1253. {
  1254. SaveFileDialog saveFile = new SaveFileDialog();
  1255. saveFile.Title = table["message23"].ToString();
  1256. saveFile.Filter = table["message36"].ToString();
  1257. saveFile.OverwritePrompt = true; //是否覆盖当前文件
  1258. saveFile.RestoreDirectory = true; //还原上次目录
  1259. if (saveFile.ShowDialog() == DialogResult.OK)
  1260. {
  1261. try
  1262. {
  1263. System.Data.SQLite.SQLiteConnection.CreateFile(saveFile.FileName);
  1264. System.Data.SQLite.SQLiteConnection m_OtsDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + saveFile.FileName + "'");
  1265. m_OtsDatabaseConnection.Open();
  1266. System.Data.SQLite.SQLiteCommand OtsDatabaseCommand = m_OtsDatabaseConnection.CreateCommand();
  1267. 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)";
  1268. OtsDatabaseCommand.ExecuteNonQuery();
  1269. OtsDatabaseCommand.CommandText = "CREATE TABLE Constants (value TEXT)";
  1270. OtsDatabaseCommand.ExecuteNonQuery();
  1271. OtsDatabaseCommand.CommandText = "CREATE TABLE MaxEDSRules (MaxEDSTime INT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1272. OtsDatabaseCommand.ExecuteNonQuery();
  1273. OtsDatabaseCommand.CommandText = "CREATE TABLE STDGroups (id INTEGER,name TEXT, color TEXT,iorder INTEGER)";
  1274. OtsDatabaseCommand.ExecuteNonQuery();
  1275. OtsDatabaseCommand.CommandText = "CREATE TABLE STDMinerals (id INTEGER,SPEC BLOB)";
  1276. OtsDatabaseCommand.ExecuteNonQuery();
  1277. OtsDatabaseCommand.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1278. OtsDatabaseCommand.ExecuteNonQuery();
  1279. System.Data.Common.DbTransaction trans = m_OtsDatabaseConnection.BeginTransaction();
  1280. trans.Commit();
  1281. }
  1282. catch (Exception ex)
  1283. {
  1284. MessageBox.Show(ex.ToString());
  1285. return;
  1286. }
  1287. STDDBAddress = saveFile.FileName;
  1288. InitForms(STDDBAddress);
  1289. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  1290. IsModified = false;
  1291. }
  1292. else
  1293. {
  1294. return;
  1295. }
  1296. }
  1297. void CheckAccuracyofZeroRules()
  1298. {
  1299. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  1300. m_dbConnection.Open();
  1301. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ZeroElementRules", m_dbConnection);
  1302. DataSet ds = new DataSet();
  1303. m_dataAdapter.Fill(ds);
  1304. DataTable dt = ds.Tables[0];
  1305. DataTable dt2 = dt.Copy();
  1306. if (dt != null)
  1307. {
  1308. if (dt.Columns[dt.Columns.Count - 1].ColumnName != "Expression")
  1309. {
  1310. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  1311. cmm.CommandText = "CREATE TABLE sqlitestudio_temp_table AS SELECT * FROM ZeroElementRules";
  1312. cmm.ExecuteNonQuery();
  1313. cmm.CommandText = "DROP TABLE ZeroElementRules";
  1314. cmm.ExecuteNonQuery();
  1315. cmm.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT,UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1316. cmm.ExecuteNonQuery();
  1317. cmm.CommandText = @"INSERT INTO ZeroElementRules (
  1318. ZeroElement,
  1319. UsingElementList,
  1320. UsingImgPropertyList,
  1321. UsingOtherPropertyList,
  1322. Expression
  1323. )
  1324. SELECT ZeroElement,
  1325. UsingElementList,
  1326. UsingImgPropertyList,
  1327. UsingOtherPropertyList,
  1328. Expression
  1329. FROM sqlitestudio_temp_table; ";
  1330. cmm.ExecuteNonQuery();
  1331. cmm.CommandText = "DROP TABLE sqlitestudio_temp_table";
  1332. cmm.ExecuteNonQuery();
  1333. m_dbConnection.Close();
  1334. MessageBox.Show("检测到该数据库零元素规则格式错误,已进行修正!");
  1335. }
  1336. }
  1337. }
  1338. }
  1339. }