STDInfo.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. }
  35. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  36. aa = LoadGroupDataFromDb();
  37. if (!aa)
  38. {
  39. GroupDictionary = null;
  40. }
  41. LoadConstants();
  42. }
  43. bool LoadClassifyToDictionary()
  44. {
  45. try
  46. {
  47. STDDictionary.Clear();
  48. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + _DBAddress + "'");
  49. m_dbConnection.Open();
  50. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD order by ListNum", m_dbConnection);
  51. DataSet ds = new DataSet();
  52. m_dataAdapter.Fill(ds);
  53. DataTable dt = ds.Tables[0];
  54. if (dt != null)
  55. {
  56. if ((dt.Columns.Contains("IfElementAnalysis")))
  57. {
  58. rbSaveStatus = true;
  59. Isoldversion = false;
  60. }
  61. else
  62. {
  63. rbSaveStatus = false;
  64. Isoldversion = true;
  65. }
  66. if (dt.Rows.Count > 0)
  67. {
  68. foreach (DataRow item in dt.Rows)
  69. {
  70. STDdata new_STDdata = new STDdata();
  71. new_STDdata.STDId = int.Parse(item["STDId"].ToString());
  72. new_STDdata.Hardness = item["Hardness"].ToString();
  73. new_STDdata.Density = item["Density"].ToString();
  74. new_STDdata.Electrical_conductivity = item["Electrical_conductivity"].ToString();
  75. new_STDdata.BSE = item["BSE"].ToString();
  76. new_STDdata.Formula = item["Formula"].ToString();
  77. new_STDdata.Element = item["Element"].ToString();
  78. new_STDdata.StrName = item["StrName"].ToString();
  79. new_STDdata.Expression = item["Expression"].ToString();
  80. new_STDdata.Color = item["Color"].ToString();
  81. new_STDdata.KeyElementList = item["KeyElementList"].ToString();
  82. new_STDdata.SubElementList = item["SubElementList"].ToString();
  83. new_STDdata.GroupId = int.Parse(item["GroupId"].ToString());
  84. new_STDdata.ListNum = item["ListNum"].ToString();
  85. if (dt.Columns.Contains("IfElementAnalysis"))
  86. {
  87. if (Convert.ToBoolean(item["IfElementAnalysis"]))
  88. {
  89. new_STDdata.IfElementAnalysis = true;
  90. }
  91. else
  92. {
  93. new_STDdata.IfElementAnalysis = false;
  94. }
  95. }
  96. STDDictionary.Add(int.Parse(item["STDId"].ToString()), new_STDdata);
  97. }
  98. }
  99. }
  100. return true;
  101. }
  102. catch /*(Exception ee)*/
  103. {
  104. MessageBox.Show("The selected standard library is formatted incorrectly, please open the correct standard library!", "Tip");
  105. return false;
  106. }
  107. }
  108. bool LoadGroupDataFromDb()
  109. {
  110. try
  111. {
  112. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + _DBAddress + "'");
  113. m_dbConnection.Open();
  114. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups order by iorder", m_dbConnection);
  115. DataSet ds = new DataSet();
  116. m_dataAdapter.Fill(ds);
  117. DataTable dt = ds.Tables[0];
  118. if (dt != null)
  119. {
  120. GroupDictionary.Clear();
  121. GroupIdDictionaryFromId.Clear();
  122. GroupIdDictionaryFromName.Clear();
  123. STDGroups new_data = new STDGroups();
  124. new_data.name = "Default";
  125. new_data.color = "#C9C9C9";
  126. new_data.iorder = 10000;
  127. GroupDictionary.Add(0, new_data);
  128. if (dt.Select("id='0'").Length == 0)
  129. {
  130. GroupIdDictionaryFromId.Add(0, "Default");
  131. GroupIdDictionaryFromName.Add("Default", 0);
  132. }
  133. if (dt.Rows.Count > 0)
  134. {
  135. foreach (DataRow item in dt.Rows)
  136. {
  137. if (item["name"].ToString() != "Default")
  138. {
  139. STDGroups new_STDdata = new STDGroups();
  140. new_STDdata.id = int.Parse(item["id"].ToString());
  141. new_STDdata.name = item["name"].ToString();
  142. new_STDdata.color = item["color"].ToString();
  143. new_STDdata.iorder = int.Parse(item["iorder"].ToString());
  144. GroupDictionary.Add(int.Parse(item["id"].ToString()), new_STDdata);
  145. }
  146. GroupIdDictionaryFromName.Add(item["name"].ToString(), int.Parse(item["id"].ToString()));
  147. GroupIdDictionaryFromId.Add(int.Parse(item["id"].ToString()), item["name"].ToString());
  148. }
  149. }
  150. }
  151. m_dbConnection.Close();
  152. m_dbConnection.Close();
  153. return true;
  154. }
  155. catch (Exception ee)
  156. {
  157. MessageBox.Show(ee.ToString());
  158. return false;
  159. }
  160. }
  161. public void LoadConstants()
  162. {
  163. try
  164. {
  165. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + _DBAddress + "'");
  166. m_dbConnection.Open();
  167. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from Constants", m_dbConnection);
  168. DataSet ds = new DataSet();
  169. m_dataAdapter.Fill(ds);
  170. DataTable dt = ds.Tables[0];
  171. if (dt.Rows.Count > 0)
  172. {
  173. string ConstantsStr2 = dt.Rows[0][0].ToString();
  174. ConstantsStr2 = ConstantsStr2.Replace(" ", "");
  175. ConstantsStr = ConstantsStr2.Split(',');
  176. }
  177. else
  178. {
  179. ConstantsStr = null;
  180. }
  181. m_dbConnection.Close();
  182. }
  183. catch (Exception ee)
  184. {
  185. MessageBox.Show(ee.ToString());
  186. }
  187. }
  188. public bool EqualsBetweenDictionary()
  189. {
  190. if (STDDictionaryInitial.Count != STDDictionary.Count)
  191. {
  192. return false;
  193. }
  194. foreach (var key in STDDictionaryInitial.Keys)
  195. {
  196. if (STDDictionary.Keys.Contains(key))
  197. {
  198. if (!STDDictionaryInitial[key].Equals(STDDictionary[key]))
  199. {
  200. return false;
  201. }
  202. }
  203. else
  204. {
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. public bool ClearDb(string DBAddress, string DBTableName)
  211. {
  212. try
  213. {
  214. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  215. m_dbConnection.Open();
  216. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  217. cmm.CommandText = "delete from " + DBTableName;
  218. cmm.ExecuteNonQuery();
  219. m_dbConnection.Close();
  220. return true;
  221. }
  222. catch (Exception ex)
  223. {
  224. MessageBox.Show(ex.ToString());
  225. return false;
  226. }
  227. }
  228. public STDdata AddSTDDictionaryItem()
  229. {
  230. STDdata new_STDdata = new STDdata();//定义一个TreeNode节点对象
  231. new_STDdata.Hardness = "1";
  232. new_STDdata.Density = "1";
  233. new_STDdata.Electrical_conductivity = "1";
  234. new_STDdata.BSE = "1";
  235. new_STDdata.Formula = "1";
  236. new_STDdata.Element = "1";
  237. new_STDdata.StrName = "NewRuleName";
  238. new_STDdata.Expression = "false";
  239. new_STDdata.KeyElementList = "";
  240. new_STDdata.SubElementList = "";
  241. Random random = new Random();
  242. new_STDdata.Color = ColorGroup[random.Next(ColorGroup.Length)];
  243. return new_STDdata;
  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. /// <summary>
  257. /// 保存ClassifySTD库和MineralElements库
  258. /// </summary>
  259. /// <param name="DBAddress"></param>
  260. /// <returns></returns>
  261. public bool SaveDictionaryToClassify()
  262. {
  263. //保存列表顺序
  264. //for (int i = 1; i < m_STDRuleslist.Grid_Minerals.RowsCount; i++)
  265. //{
  266. // STDDictionary[(int)m_STDRuleslist.Grid_Minerals[i, 0].Tag].ListNum = i.ToString();
  267. //}
  268. try
  269. {
  270. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + _DBAddress + "'");
  271. m_dbConnection.Open();
  272. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  273. cmm.CommandText = "delete from ClassifySTD";
  274. //cmm.CommandText = "delete from ClassifySTD_Backup";
  275. try
  276. {
  277. cmm.ExecuteNonQuery();
  278. }
  279. catch (Exception ex)
  280. {
  281. MessageBox.Show(ex.ToString());
  282. return false;
  283. }
  284. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection);
  285. //System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD_Backup", m_dbConnection);
  286. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter);
  287. DataSet ds = new DataSet();
  288. m_dataAdapter.Fill(ds, "ClassifySTD");
  289. DataTable dt = ds.Tables["ClassifySTD"];
  290. //m_dataAdapter.Fill(ds, "ClassifySTD_Backup");
  291. //DataTable dt = ds.Tables["ClassifySTD_Backup"];
  292. dt.Clear();
  293. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  294. {
  295. string UsingElementList = "";
  296. string UsingImgPropertyList = "";
  297. string UsingOtherPropertyList = "";
  298. List<string> UsingElementL = new List<string>();
  299. List<string> UsingImgPropertyL = new List<string>();
  300. List<string> UsingOtherPropertyL = new List<string>();
  301. string str_RemoveBlank = kv.Value.Expression;
  302. ////forth_elem干扰or分隔符,故先行去掉
  303. //if (str_RemoveBlank.Contains("fourth_elem"))
  304. //{
  305. // str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  306. // UsingOtherPropertyList = "fourth_elem";
  307. //}
  308. str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  309. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  310. List<string> str_Removeandor = new List<string>();
  311. for (int i = 0; i < str_Removeand.Length; i++)
  312. {
  313. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  314. }
  315. List<string> list_all = new List<string>();
  316. for (int i = 0; i < str_Removeandor.Count; i++)
  317. {
  318. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  319. }
  320. for (int i = 0; i < list_all.Count; i++)
  321. {
  322. //周期元素?
  323. if (PeriodicTable.Contains(list_all[i]))
  324. {
  325. if (!UsingElementL.Contains(list_all[i]))
  326. {
  327. UsingElementL.Add(list_all[i]);
  328. }
  329. }
  330. //first_elem?
  331. if (Elem1.Contains(list_all[i]))
  332. {
  333. if (!UsingOtherPropertyL.Contains(list_all[i]))
  334. {
  335. UsingOtherPropertyL.Add(list_all[i]);
  336. }
  337. }
  338. //Element1?
  339. if (Elem2.Contains(list_all[i]))
  340. {
  341. if (!UsingOtherPropertyL.Contains(list_all[i]))
  342. {
  343. UsingOtherPropertyL.Add(list_all[i]);
  344. }
  345. }
  346. //其它元素?
  347. if (ImgProperty.Contains(list_all[i]))
  348. {
  349. if (!UsingImgPropertyL.Contains(list_all[i]))
  350. {
  351. UsingImgPropertyL.Add(list_all[i]);
  352. }
  353. }
  354. }
  355. if (UsingElementL.Count > 0)
  356. {
  357. for (int i = 0; i < UsingElementL.Count - 1; i++)
  358. {
  359. UsingElementList += UsingElementL[i] + ",";
  360. }
  361. UsingElementList += UsingElementL[UsingElementL.Count - 1];
  362. }
  363. if (UsingImgPropertyL.Count > 0)
  364. {
  365. for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  366. {
  367. UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  368. }
  369. UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  370. }
  371. if (UsingOtherPropertyL.Count > 0)
  372. {
  373. for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  374. {
  375. UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  376. }
  377. UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  378. }
  379. DataRow newRow = dt.NewRow();
  380. newRow["STDId"] = kv.Key;
  381. newRow["StrName"] = kv.Value.StrName;
  382. newRow["Color"] = kv.Value.Color;
  383. newRow["KeyElementList"] = kv.Value.KeyElementList;
  384. newRow["SubElementList"] = kv.Value.SubElementList;
  385. newRow["UsingImgPropertyList"] = UsingImgPropertyList;
  386. newRow["UsingOtherPropertyList"] = UsingOtherPropertyList;
  387. newRow["Expression"] = kv.Value.Expression;
  388. newRow["Hardness"] = kv.Value.Hardness;
  389. newRow["Density"] = kv.Value.Density;
  390. newRow["Electrical_conductivity"] = kv.Value.Electrical_conductivity;
  391. newRow["BSE"] = int.Parse(kv.Value.BSE);
  392. newRow["Formula"] = kv.Value.Formula;
  393. newRow["Element"] = kv.Value.Element;
  394. newRow["IfElementAnalysis"] = kv.Value.IfElementAnalysis;
  395. newRow["GroupId"] = kv.Value.GroupId;
  396. newRow["ListNum"] = kv.Value.ListNum;
  397. dt.Rows.Add(newRow);
  398. }
  399. m_dataAdapter.Update(ds, "ClassifySTD");
  400. System.Data.SQLite.SQLiteCommand tr = m_dbConnection.CreateCommand();
  401. cmm.CommandText = "delete from STDGroups";
  402. try
  403. {
  404. cmm.ExecuteNonQuery();
  405. }
  406. catch (Exception ex)
  407. {
  408. MessageBox.Show(ex.ToString());
  409. return false;
  410. }
  411. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter_grp = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups", m_dbConnection);
  412. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder_grp = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter_grp);
  413. DataSet ds_grp = new DataSet();
  414. m_dataAdapter_grp.Fill(ds_grp, "STDGroups");
  415. DataTable dt_grp = ds_grp.Tables["STDGroups"];
  416. dt_grp.Clear();
  417. foreach (STDGroups kv in GroupDictionary.Values)
  418. {
  419. DataRow newRow = dt_grp.NewRow();
  420. newRow["id"] = kv.id;
  421. newRow["name"] = kv.name;
  422. newRow["color"] = kv.color;
  423. newRow["iorder"] = kv.iorder;
  424. if (kv.name == "Default")
  425. {
  426. newRow["iorder"] = 10000;
  427. newRow["color"] = "#C9C9C9";
  428. }
  429. dt_grp.Rows.Add(newRow);
  430. }
  431. m_dataAdapter_grp.Update(ds_grp, "STDGroups");
  432. m_dbConnection.Close();
  433. }
  434. catch (Exception ex)
  435. {
  436. MessageBox.Show(ex.ToString());
  437. return false;
  438. }
  439. return true;
  440. }
  441. public bool SaveAsDictionaryToClassify(string DBAddress)
  442. {
  443. ////保存列表顺序
  444. //for (int i = 1; i < m_STDRuleslist.Grid_Minerals.RowsCount; i++)
  445. //{
  446. // STDDictionary[(int)m_STDRuleslist.Grid_Minerals[i, 0].Tag].ListNum = i.ToString();
  447. //}
  448. try
  449. {
  450. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  451. m_dbConnection.Open();
  452. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  453. cmm.CommandText = "drop table ClassifySTD";
  454. cmm.ExecuteNonQuery();
  455. 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)";
  456. cmm.ExecuteNonQuery();
  457. System.Data.Common.DbTransaction trans = m_dbConnection.BeginTransaction();
  458. trans.Commit();
  459. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection);
  460. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter);
  461. DataSet ds = new DataSet();
  462. m_dataAdapter.Fill(ds, "ClassifySTD");
  463. DataTable dt = ds.Tables["ClassifySTD"];
  464. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  465. {
  466. string UsingElementList = "";
  467. string UsingImgPropertyList = "";
  468. string UsingOtherPropertyList = "";
  469. List<string> UsingElementL = new List<string>();
  470. List<string> UsingImgPropertyL = new List<string>();
  471. List<string> UsingOtherPropertyL = new List<string>();
  472. string str_RemoveBlank = kv.Value.Expression;
  473. ////forth_elem干扰or分隔符,故先行去掉
  474. //if (str_RemoveBlank.Contains("fourth_elem"))
  475. //{
  476. // str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  477. // UsingOtherPropertyList = "fourth_elem,";
  478. //}
  479. str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  480. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  481. List<string> str_Removeandor = new List<string>();
  482. for (int i = 0; i < str_Removeand.Length; i++)
  483. {
  484. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  485. }
  486. List<string> list_all = new List<string>();
  487. for (int i = 0; i < str_Removeandor.Count; i++)
  488. {
  489. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  490. }
  491. for (int i = 0; i < list_all.Count; i++)
  492. {
  493. //周期元素?
  494. if (PeriodicTable.Contains(list_all[i]))
  495. {
  496. if (!UsingElementL.Contains(list_all[i]))
  497. {
  498. UsingElementL.Add(list_all[i]);
  499. }
  500. }
  501. //first_elem?
  502. if (Elem1.Contains(list_all[i]))
  503. {
  504. if (!UsingOtherPropertyL.Contains(list_all[i]))
  505. {
  506. UsingOtherPropertyL.Add(list_all[i]);
  507. }
  508. }
  509. //Element1?
  510. if (Elem2.Contains(list_all[i]))
  511. {
  512. if (!UsingOtherPropertyL.Contains(list_all[i]))
  513. {
  514. UsingOtherPropertyL.Add(list_all[i]);
  515. }
  516. }
  517. //其它元素?
  518. if (ImgProperty.Contains(list_all[i]))
  519. {
  520. if (!UsingImgPropertyL.Contains(list_all[i]))
  521. {
  522. UsingImgPropertyL.Add(list_all[i]);
  523. }
  524. }
  525. }
  526. if (UsingElementL.Count > 0)
  527. {
  528. for (int i = 0; i < UsingElementL.Count - 1; i++)
  529. {
  530. UsingElementList += UsingElementL[i] + ",";
  531. }
  532. UsingElementList += UsingElementL[UsingElementL.Count - 1];
  533. }
  534. if (UsingImgPropertyL.Count > 0)
  535. {
  536. for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  537. {
  538. UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  539. }
  540. UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  541. }
  542. if (UsingOtherPropertyL.Count > 0)
  543. {
  544. for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  545. {
  546. UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  547. }
  548. UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  549. }
  550. DataRow newRow = dt.NewRow();
  551. newRow["STDId"] = kv.Key;
  552. newRow["StrName"] = kv.Value.StrName;
  553. newRow["Color"] = kv.Value.Color;
  554. newRow["KeyElementList"] = kv.Value.KeyElementList;
  555. newRow["SubElementList"] = kv.Value.SubElementList;
  556. newRow["UsingImgPropertyList"] = UsingImgPropertyList;
  557. newRow["UsingOtherPropertyList"] = UsingOtherPropertyList;
  558. newRow["Expression"] = kv.Value.Expression;
  559. newRow["Hardness"] = kv.Value.Hardness;
  560. newRow["Density"] = kv.Value.Density;
  561. newRow["Electrical_conductivity"] = kv.Value.Electrical_conductivity;
  562. newRow["BSE"] = int.Parse(kv.Value.BSE);
  563. newRow["Formula"] = kv.Value.Formula;
  564. newRow["Element"] = kv.Value.Element;
  565. newRow["IfElementAnalysis"] = kv.Value.IfElementAnalysis;
  566. newRow["GroupId"] = kv.Value.GroupId;
  567. newRow["ListNum"] = kv.Value.ListNum;
  568. dt.Rows.Add(newRow);
  569. }
  570. m_dataAdapter.Update(ds, "ClassifySTD");
  571. System.Data.SQLite.SQLiteCommand tr = m_dbConnection.CreateCommand();
  572. cmm.CommandText = "delete from STDGroups";
  573. try
  574. {
  575. cmm.ExecuteNonQuery();
  576. }
  577. catch (Exception ex)
  578. {
  579. MessageBox.Show(ex.ToString());
  580. return false;
  581. }
  582. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter_grp = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups", m_dbConnection);
  583. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder_grp = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter_grp);
  584. DataSet ds_grp = new DataSet();
  585. m_dataAdapter_grp.Fill(ds_grp, "STDGroups");
  586. DataTable dt_grp = ds_grp.Tables["STDGroups"];
  587. dt_grp.Clear();
  588. foreach (STDGroups kv in GroupDictionary.Values)
  589. {
  590. DataRow newRow = dt_grp.NewRow();
  591. newRow["id"] = kv.id;
  592. newRow["name"] = kv.name;
  593. newRow["color"] = kv.color;
  594. newRow["iorder"] = 0;
  595. if (kv.name == "Default")
  596. {
  597. newRow["iorder"] = 10000;
  598. }
  599. dt_grp.Rows.Add(newRow);
  600. }
  601. m_dataAdapter_grp.Update(ds_grp, "STDGroups");
  602. m_dbConnection.Close();
  603. UpdateDBAddress(DBAddress);
  604. }
  605. catch (Exception ex)
  606. {
  607. MessageBox.Show(ex.ToString());
  608. return false;
  609. }
  610. return true;
  611. }
  612. public void UpdateSTDDictionaryInitial()
  613. {
  614. STDDictionaryInitial.Clear();
  615. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  616. }
  617. public void SetNonexistentGroupsToDefault()
  618. {
  619. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  620. {
  621. if (!GroupIdDictionaryFromId.Keys.Contains(kv.Value.GroupId))
  622. {
  623. kv.Value.GroupId = -1;
  624. }
  625. }
  626. }
  627. public void UpdateDBAddress(string DBAddress)
  628. {
  629. _DBAddress = DBAddress;
  630. }
  631. }
  632. }