STDInfo.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. namespace OTSPartA_STDEditor.BaseClass
  7. {
  8. public class STDInfo
  9. {
  10. public Dictionary<int, STDdata> STDDictionary = new Dictionary<int, STDdata>();
  11. /// <summary>
  12. /// 原始数据字典用于在关闭时比较是否弹出保存提示
  13. /// </summary>
  14. private Dictionary<int, STDdata> STDDictionaryInitial = new Dictionary<int, STDdata>();
  15. public Dictionary<int, STDGroups> GroupDictionary = new Dictionary<int, STDGroups>();
  16. public string[] ConstantsStr = null;
  17. public Dictionary<int, string> GroupIdDictionaryFromId = new Dictionary<int, string>();
  18. public Dictionary<string, int> GroupIdDictionaryFromName = new Dictionary<string, int>();
  19. public bool Isoldversion { set; get; }
  20. public bool rbSaveStatus { set; get; }
  21. 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" };
  22. public static string[] PeriodicTable = { "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Unb" };
  23. public static string[] Elem1 = { "first_elem", "second_elem", "third_elem", "fourth_elem", "fifth_elem", "sixth_elem", "seventh_elem", "eighth_elem", "ninth_elem", "tenth_elem" };
  24. public static string[] Elem2 = { "Element#1", "Element#2", "Element#3", "Element#4", "Element#5", "Element#6", "Element#7", "Element#8", "Element#9", "Element#10"};
  25. public static string[] ImgProperty = { "Dmax", "Dmin", "Aspect", "Dperp", "Dmean", "Area", "Dferet", "Width", "Height", "Perimeter", "Dinscr", "Orientation", "Delong", "Aspectelong", "Dequalcircle", "Vedio" };
  26. string _DBAddress;
  27. public STDInfo(string DBAddress)
  28. {
  29. _DBAddress=DBAddress;
  30. bool aa = LoadClassifyToDictionary();
  31. if (!aa)
  32. {
  33. STDDictionary=null;
  34. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  35. }
  36. aa = LoadGroupDataFromDb();
  37. if (!aa)
  38. {
  39. GroupDictionary = null;
  40. }
  41. LoadConstants();
  42. }
  43. bool LoadClassifyToDictionary()
  44. {
  45. try
  46. {
  47. STDDictionaryInitial.Clear();
  48. STDDictionary.Clear();
  49. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + _DBAddress + "'");
  50. m_dbConnection.Open();
  51. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD order by ListNum", m_dbConnection);
  52. DataSet ds = new DataSet();
  53. m_dataAdapter.Fill(ds);
  54. DataTable dt = ds.Tables[0];
  55. if (dt != null)
  56. {
  57. if ((dt.Columns.Contains("IfElementAnalysis")))
  58. {
  59. rbSaveStatus = true;
  60. Isoldversion = false;
  61. }
  62. else
  63. {
  64. rbSaveStatus = false;
  65. Isoldversion = true;
  66. }
  67. if (dt.Rows.Count > 0)
  68. {
  69. foreach (DataRow item in dt.Rows)
  70. {
  71. STDdata new_STDdata = new STDdata();
  72. new_STDdata.STDId = int.Parse(item["STDId"].ToString());
  73. new_STDdata.Hardness = item["Hardness"].ToString();
  74. new_STDdata.Density = item["Density"].ToString();
  75. new_STDdata.Electrical_conductivity = item["Electrical_conductivity"].ToString();
  76. new_STDdata.BSE = item["BSE"].ToString();
  77. new_STDdata.Formula = item["Formula"].ToString();
  78. new_STDdata.Element = item["Element"].ToString();
  79. new_STDdata.StrName = item["StrName"].ToString();
  80. new_STDdata.Expression = item["Expression"].ToString();
  81. new_STDdata.Color = item["Color"].ToString();
  82. new_STDdata.KeyElementList = item["KeyElementList"].ToString();
  83. new_STDdata.SubElementList = item["SubElementList"].ToString();
  84. new_STDdata.GroupId = int.Parse(item["GroupId"].ToString());
  85. new_STDdata.ListNum = item["ListNum"].ToString();
  86. if (dt.Columns.Contains("IfElementAnalysis"))
  87. {
  88. if (Convert.ToBoolean(item["IfElementAnalysis"]))
  89. {
  90. new_STDdata.IfElementAnalysis = true;
  91. }
  92. else
  93. {
  94. new_STDdata.IfElementAnalysis = false;
  95. }
  96. }
  97. STDDictionary.Add(int.Parse(item["STDId"].ToString()), new_STDdata);
  98. STDDictionaryInitial.Add(int.Parse(item["STDId"].ToString()), new_STDdata);
  99. }
  100. }
  101. }
  102. return true;
  103. }
  104. catch /*(Exception ee)*/
  105. {
  106. MessageBox.Show("The selected standard library is formatted incorrectly, please open the correct standard library!", "Tip");
  107. return false;
  108. }
  109. }
  110. bool LoadGroupDataFromDb()
  111. {
  112. try
  113. {
  114. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + _DBAddress + "'");
  115. m_dbConnection.Open();
  116. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups order by iorder", m_dbConnection);
  117. DataSet ds = new DataSet();
  118. m_dataAdapter.Fill(ds);
  119. DataTable dt = ds.Tables[0];
  120. if (dt != null)
  121. {
  122. GroupDictionary.Clear();
  123. GroupIdDictionaryFromId.Clear();
  124. GroupIdDictionaryFromName.Clear();
  125. STDGroups new_data = new STDGroups();
  126. new_data.name = "Default";
  127. new_data.color = "#C9C9C9";
  128. new_data.iorder = 10000;
  129. GroupDictionary.Add(0, new_data);
  130. if (dt.Select("id='0'").Length == 0)
  131. {
  132. GroupIdDictionaryFromId.Add(0, "Default");
  133. GroupIdDictionaryFromName.Add("Default", 0);
  134. }
  135. if (dt.Rows.Count > 0)
  136. {
  137. foreach (DataRow item in dt.Rows)
  138. {
  139. if (item["name"].ToString() != "Default")
  140. {
  141. STDGroups new_STDdata = new STDGroups();
  142. new_STDdata.id = int.Parse(item["id"].ToString());
  143. new_STDdata.name = item["name"].ToString();
  144. new_STDdata.color = item["color"].ToString();
  145. new_STDdata.iorder = int.Parse(item["iorder"].ToString());
  146. GroupDictionary.Add(int.Parse(item["id"].ToString()), new_STDdata);
  147. }
  148. GroupIdDictionaryFromName.Add(item["name"].ToString(), int.Parse(item["id"].ToString()));
  149. GroupIdDictionaryFromId.Add(int.Parse(item["id"].ToString()), item["name"].ToString());
  150. }
  151. }
  152. }
  153. m_dbConnection.Close();
  154. m_dbConnection.Close();
  155. return true;
  156. }
  157. catch (Exception ee)
  158. {
  159. MessageBox.Show(ee.ToString());
  160. return false;
  161. }
  162. }
  163. public void LoadConstants()
  164. {
  165. try
  166. {
  167. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + _DBAddress + "'");
  168. m_dbConnection.Open();
  169. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from Constants", m_dbConnection);
  170. DataSet ds = new DataSet();
  171. m_dataAdapter.Fill(ds);
  172. DataTable dt = ds.Tables[0];
  173. if (dt.Rows.Count > 0)
  174. {
  175. string ConstantsStr2 = dt.Rows[0][0].ToString();
  176. ConstantsStr2 = ConstantsStr2.Replace(" ", "");
  177. ConstantsStr = ConstantsStr2.Split(',');
  178. }
  179. else
  180. {
  181. ConstantsStr = null;
  182. }
  183. m_dbConnection.Close();
  184. }
  185. catch (Exception ee)
  186. {
  187. MessageBox.Show(ee.ToString());
  188. }
  189. }
  190. public bool EqualsBetweenDictionary()
  191. {
  192. if (STDDictionaryInitial.Count != STDDictionary.Count)
  193. {
  194. return false;
  195. }
  196. foreach (var key in STDDictionaryInitial.Keys)
  197. {
  198. if (STDDictionary.Keys.Contains(key))
  199. {
  200. if (!STDDictionaryInitial[key].Equals(STDDictionary[key]))
  201. {
  202. return false;
  203. }
  204. }
  205. else
  206. {
  207. return false;
  208. }
  209. }
  210. return true;
  211. }
  212. public bool ClearDb(string DBAddress, string DBTableName)
  213. {
  214. try
  215. {
  216. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  217. m_dbConnection.Open();
  218. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  219. cmm.CommandText = "delete from " + DBTableName;
  220. cmm.ExecuteNonQuery();
  221. m_dbConnection.Close();
  222. return true;
  223. }
  224. catch (Exception ex)
  225. {
  226. MessageBox.Show(ex.ToString());
  227. return false;
  228. }
  229. }
  230. public STDdata AddSTDDictionaryItem()
  231. {
  232. STDdata new_STDdata = new STDdata();//定义一个TreeNode节点对象
  233. new_STDdata.Hardness = "1";
  234. new_STDdata.Density = "1";
  235. new_STDdata.Electrical_conductivity = "1";
  236. new_STDdata.BSE = "1";
  237. new_STDdata.Formula = "1";
  238. new_STDdata.Element = "1";
  239. new_STDdata.StrName = "NewRuleName";
  240. new_STDdata.Expression = "false";
  241. new_STDdata.KeyElementList = "";
  242. new_STDdata.SubElementList = "";
  243. Random random = new Random();
  244. new_STDdata.Color = ColorGroup[random.Next(ColorGroup.Length)];
  245. return new_STDdata;
  246. }
  247. object Clone(Dictionary<int, STDdata> STDDictionary)
  248. {
  249. Dictionary<int, STDdata> STDDictionaryInitial = new Dictionary<int, STDdata>();
  250. foreach (var key in STDDictionary.Keys)
  251. {
  252. STDdata tDdata = new STDdata();
  253. tDdata = tDdata.Clone(STDDictionary[key]) as STDdata;
  254. STDDictionaryInitial.Add(key, tDdata);
  255. }
  256. return STDDictionaryInitial;
  257. }
  258. /// <summary>
  259. /// 保存ClassifySTD库和MineralElements库
  260. /// </summary>
  261. /// <param name="DBAddress"></param>
  262. /// <returns></returns>
  263. public bool SaveDictionaryToClassify()
  264. {
  265. //保存列表顺序
  266. //for (int i = 1; i < m_STDRuleslist.Grid_Minerals.RowsCount; i++)
  267. //{
  268. // STDDictionary[(int)m_STDRuleslist.Grid_Minerals[i, 0].Tag].ListNum = i.ToString();
  269. //}
  270. try
  271. {
  272. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + _DBAddress + "'");
  273. m_dbConnection.Open();
  274. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  275. cmm.CommandText = "delete from ClassifySTD";
  276. //cmm.CommandText = "delete from ClassifySTD_Backup";
  277. try
  278. {
  279. cmm.ExecuteNonQuery();
  280. }
  281. catch (Exception ex)
  282. {
  283. MessageBox.Show(ex.ToString());
  284. return false;
  285. }
  286. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection);
  287. //System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD_Backup", m_dbConnection);
  288. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter);
  289. DataSet ds = new DataSet();
  290. m_dataAdapter.Fill(ds, "ClassifySTD");
  291. DataTable dt = ds.Tables["ClassifySTD"];
  292. //m_dataAdapter.Fill(ds, "ClassifySTD_Backup");
  293. //DataTable dt = ds.Tables["ClassifySTD_Backup"];
  294. dt.Clear();
  295. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  296. {
  297. string UsingElementList = "";
  298. string UsingImgPropertyList = "";
  299. string UsingOtherPropertyList = "";
  300. List<string> UsingElementL = new List<string>();
  301. List<string> UsingImgPropertyL = new List<string>();
  302. List<string> UsingOtherPropertyL = new List<string>();
  303. string str_RemoveBlank = kv.Value.Expression;
  304. ////forth_elem干扰or分隔符,故先行去掉
  305. //if (str_RemoveBlank.Contains("fourth_elem"))
  306. //{
  307. // str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  308. // UsingOtherPropertyList = "fourth_elem";
  309. //}
  310. str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  311. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  312. List<string> str_Removeandor = new List<string>();
  313. for (int i = 0; i < str_Removeand.Length; i++)
  314. {
  315. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  316. }
  317. List<string> list_all = new List<string>();
  318. for (int i = 0; i < str_Removeandor.Count; i++)
  319. {
  320. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  321. }
  322. for (int i = 0; i < list_all.Count; i++)
  323. {
  324. //周期元素?
  325. if (PeriodicTable.Contains(list_all[i]))
  326. {
  327. if (!UsingElementL.Contains(list_all[i]))
  328. {
  329. UsingElementL.Add(list_all[i]);
  330. }
  331. }
  332. //first_elem?
  333. if (Elem1.Contains(list_all[i]))
  334. {
  335. if (!UsingOtherPropertyL.Contains(list_all[i]))
  336. {
  337. UsingOtherPropertyL.Add(list_all[i]);
  338. }
  339. }
  340. //Element1?
  341. if (Elem2.Contains(list_all[i]))
  342. {
  343. if (!UsingOtherPropertyL.Contains(list_all[i]))
  344. {
  345. UsingOtherPropertyL.Add(list_all[i]);
  346. }
  347. }
  348. //其它元素?
  349. if (ImgProperty.Contains(list_all[i]))
  350. {
  351. if (!UsingImgPropertyL.Contains(list_all[i]))
  352. {
  353. UsingImgPropertyL.Add(list_all[i]);
  354. }
  355. }
  356. }
  357. if (UsingElementL.Count > 0)
  358. {
  359. for (int i = 0; i < UsingElementL.Count - 1; i++)
  360. {
  361. UsingElementList += UsingElementL[i] + ",";
  362. }
  363. UsingElementList += UsingElementL[UsingElementL.Count - 1];
  364. }
  365. if (UsingImgPropertyL.Count > 0)
  366. {
  367. for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  368. {
  369. UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  370. }
  371. UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  372. }
  373. if (UsingOtherPropertyL.Count > 0)
  374. {
  375. for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  376. {
  377. UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  378. }
  379. UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  380. }
  381. DataRow newRow = dt.NewRow();
  382. newRow["STDId"] = kv.Key;
  383. newRow["StrName"] = kv.Value.StrName;
  384. newRow["Color"] = kv.Value.Color;
  385. newRow["KeyElementList"] = kv.Value.KeyElementList;
  386. newRow["SubElementList"] = kv.Value.SubElementList;
  387. newRow["UsingImgPropertyList"] = UsingImgPropertyList;
  388. newRow["UsingOtherPropertyList"] = UsingOtherPropertyList;
  389. newRow["Expression"] = kv.Value.Expression;
  390. newRow["Hardness"] = kv.Value.Hardness;
  391. newRow["Density"] = kv.Value.Density;
  392. newRow["Electrical_conductivity"] = kv.Value.Electrical_conductivity;
  393. newRow["BSE"] = int.Parse(kv.Value.BSE);
  394. newRow["Formula"] = kv.Value.Formula;
  395. newRow["Element"] = kv.Value.Element;
  396. newRow["IfElementAnalysis"] = kv.Value.IfElementAnalysis;
  397. newRow["GroupId"] = kv.Value.GroupId;
  398. newRow["ListNum"] = kv.Value.ListNum;
  399. dt.Rows.Add(newRow);
  400. }
  401. m_dataAdapter.Update(ds, "ClassifySTD");
  402. System.Data.SQLite.SQLiteCommand tr = m_dbConnection.CreateCommand();
  403. cmm.CommandText = "delete from STDGroups";
  404. try
  405. {
  406. cmm.ExecuteNonQuery();
  407. }
  408. catch (Exception ex)
  409. {
  410. MessageBox.Show(ex.ToString());
  411. return false;
  412. }
  413. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter_grp = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups", m_dbConnection);
  414. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder_grp = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter_grp);
  415. DataSet ds_grp = new DataSet();
  416. m_dataAdapter_grp.Fill(ds_grp, "STDGroups");
  417. DataTable dt_grp = ds_grp.Tables["STDGroups"];
  418. dt_grp.Clear();
  419. foreach (STDGroups kv in GroupDictionary.Values)
  420. {
  421. DataRow newRow = dt_grp.NewRow();
  422. newRow["id"] = kv.id;
  423. newRow["name"] = kv.name;
  424. newRow["color"] = kv.color;
  425. newRow["iorder"] = kv.iorder;
  426. if (kv.name == "Default")
  427. {
  428. newRow["iorder"] = 10000;
  429. newRow["color"] = "#C9C9C9";
  430. }
  431. dt_grp.Rows.Add(newRow);
  432. }
  433. m_dataAdapter_grp.Update(ds_grp, "STDGroups");
  434. m_dbConnection.Close();
  435. }
  436. catch (Exception ex)
  437. {
  438. MessageBox.Show(ex.ToString());
  439. return false;
  440. }
  441. return true;
  442. }
  443. public bool SaveAsDictionaryToClassify(string DBAddress)
  444. {
  445. ////保存列表顺序
  446. //for (int i = 1; i < m_STDRuleslist.Grid_Minerals.RowsCount; i++)
  447. //{
  448. // STDDictionary[(int)m_STDRuleslist.Grid_Minerals[i, 0].Tag].ListNum = i.ToString();
  449. //}
  450. try
  451. {
  452. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  453. m_dbConnection.Open();
  454. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  455. cmm.CommandText = "drop table ClassifySTD";
  456. cmm.ExecuteNonQuery();
  457. 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)";
  458. cmm.ExecuteNonQuery();
  459. System.Data.Common.DbTransaction trans = m_dbConnection.BeginTransaction();
  460. trans.Commit();
  461. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection);
  462. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter);
  463. DataSet ds = new DataSet();
  464. m_dataAdapter.Fill(ds, "ClassifySTD");
  465. DataTable dt = ds.Tables["ClassifySTD"];
  466. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  467. {
  468. string UsingElementList = "";
  469. string UsingImgPropertyList = "";
  470. string UsingOtherPropertyList = "";
  471. List<string> UsingElementL = new List<string>();
  472. List<string> UsingImgPropertyL = new List<string>();
  473. List<string> UsingOtherPropertyL = new List<string>();
  474. string str_RemoveBlank = kv.Value.Expression;
  475. ////forth_elem干扰or分隔符,故先行去掉
  476. //if (str_RemoveBlank.Contains("fourth_elem"))
  477. //{
  478. // str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  479. // UsingOtherPropertyList = "fourth_elem,";
  480. //}
  481. str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  482. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  483. List<string> str_Removeandor = new List<string>();
  484. for (int i = 0; i < str_Removeand.Length; i++)
  485. {
  486. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  487. }
  488. List<string> list_all = new List<string>();
  489. for (int i = 0; i < str_Removeandor.Count; i++)
  490. {
  491. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  492. }
  493. for (int i = 0; i < list_all.Count; i++)
  494. {
  495. //周期元素?
  496. if (PeriodicTable.Contains(list_all[i]))
  497. {
  498. if (!UsingElementL.Contains(list_all[i]))
  499. {
  500. UsingElementL.Add(list_all[i]);
  501. }
  502. }
  503. //first_elem?
  504. if (Elem1.Contains(list_all[i]))
  505. {
  506. if (!UsingOtherPropertyL.Contains(list_all[i]))
  507. {
  508. UsingOtherPropertyL.Add(list_all[i]);
  509. }
  510. }
  511. //Element1?
  512. if (Elem2.Contains(list_all[i]))
  513. {
  514. if (!UsingOtherPropertyL.Contains(list_all[i]))
  515. {
  516. UsingOtherPropertyL.Add(list_all[i]);
  517. }
  518. }
  519. //其它元素?
  520. if (ImgProperty.Contains(list_all[i]))
  521. {
  522. if (!UsingImgPropertyL.Contains(list_all[i]))
  523. {
  524. UsingImgPropertyL.Add(list_all[i]);
  525. }
  526. }
  527. }
  528. if (UsingElementL.Count > 0)
  529. {
  530. for (int i = 0; i < UsingElementL.Count - 1; i++)
  531. {
  532. UsingElementList += UsingElementL[i] + ",";
  533. }
  534. UsingElementList += UsingElementL[UsingElementL.Count - 1];
  535. }
  536. if (UsingImgPropertyL.Count > 0)
  537. {
  538. for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  539. {
  540. UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  541. }
  542. UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  543. }
  544. if (UsingOtherPropertyL.Count > 0)
  545. {
  546. for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  547. {
  548. UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  549. }
  550. UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  551. }
  552. DataRow newRow = dt.NewRow();
  553. newRow["STDId"] = kv.Key;
  554. newRow["StrName"] = kv.Value.StrName;
  555. newRow["Color"] = kv.Value.Color;
  556. newRow["KeyElementList"] = kv.Value.KeyElementList;
  557. newRow["SubElementList"] = kv.Value.SubElementList;
  558. newRow["UsingImgPropertyList"] = UsingImgPropertyList;
  559. newRow["UsingOtherPropertyList"] = UsingOtherPropertyList;
  560. newRow["Expression"] = kv.Value.Expression;
  561. newRow["Hardness"] = kv.Value.Hardness;
  562. newRow["Density"] = kv.Value.Density;
  563. newRow["Electrical_conductivity"] = kv.Value.Electrical_conductivity;
  564. newRow["BSE"] = int.Parse(kv.Value.BSE);
  565. newRow["Formula"] = kv.Value.Formula;
  566. newRow["Element"] = kv.Value.Element;
  567. newRow["IfElementAnalysis"] = kv.Value.IfElementAnalysis;
  568. newRow["GroupId"] = kv.Value.GroupId;
  569. newRow["ListNum"] = kv.Value.ListNum;
  570. dt.Rows.Add(newRow);
  571. }
  572. m_dataAdapter.Update(ds, "ClassifySTD");
  573. System.Data.SQLite.SQLiteCommand tr = m_dbConnection.CreateCommand();
  574. cmm.CommandText = "delete from STDGroups";
  575. try
  576. {
  577. cmm.ExecuteNonQuery();
  578. }
  579. catch (Exception ex)
  580. {
  581. MessageBox.Show(ex.ToString());
  582. return false;
  583. }
  584. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter_grp = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups", m_dbConnection);
  585. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder_grp = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter_grp);
  586. DataSet ds_grp = new DataSet();
  587. m_dataAdapter_grp.Fill(ds_grp, "STDGroups");
  588. DataTable dt_grp = ds_grp.Tables["STDGroups"];
  589. dt_grp.Clear();
  590. foreach (STDGroups kv in GroupDictionary.Values)
  591. {
  592. DataRow newRow = dt_grp.NewRow();
  593. newRow["id"] = kv.id;
  594. newRow["name"] = kv.name;
  595. newRow["color"] = kv.color;
  596. newRow["iorder"] = 0;
  597. if (kv.name == "Default")
  598. {
  599. newRow["iorder"] = 10000;
  600. }
  601. dt_grp.Rows.Add(newRow);
  602. }
  603. m_dataAdapter_grp.Update(ds_grp, "STDGroups");
  604. m_dbConnection.Close();
  605. }
  606. catch (Exception ex)
  607. {
  608. MessageBox.Show(ex.ToString());
  609. return false;
  610. }
  611. return true;
  612. }
  613. public void UpdateSTDDictionaryInitial()
  614. {
  615. STDDictionaryInitial.Clear();
  616. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  617. }
  618. public void SetNonexistentGroupsToDefault()
  619. {
  620. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  621. {
  622. if (!GroupIdDictionaryFromId.Keys.Contains(kv.Value.GroupId))
  623. {
  624. kv.Value.GroupId = -1;
  625. }
  626. }
  627. }
  628. }
  629. }