Form_Main.cs 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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. //左右括号匹配检验
  351. int BracketsNum = 0;
  352. for (int i = 0; i < str_RemoveBlank.Length; i++)
  353. {
  354. if (str_RemoveBlank[i] == '(') BracketsNum++;
  355. if (str_RemoveBlank[i] == ')') BracketsNum--;
  356. }
  357. if (BracketsNum != 0)
  358. {
  359. MessageBox.Show("Number of left and right parentheses does not match!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  360. return false;
  361. }
  362. //首字符检测
  363. 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] == '('))
  364. {
  365. MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  366. return false;
  367. }
  368. if (str_RemoveBlank.Length >= 3)
  369. {
  370. if ((str_RemoveBlank.Substring(0, 3) == "and") || (str_RemoveBlank.Substring(0, 3) == "end"))
  371. {
  372. MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  373. return false;
  374. }
  375. }
  376. //forth_elem干扰or分隔符,故先行去掉
  377. //str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  378. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  379. List<string> str_Removeandor = new List<string>();
  380. for (int i = 0; i < str_Removeand.Length; i++)
  381. {
  382. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  383. }
  384. List<string> list_all = new List<string>();
  385. for (int i = 0; i < str_Removeandor.Count; i++)
  386. {
  387. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  388. }
  389. //常量
  390. List<string> Constantslist = new List<string>();
  391. for (int j = 0; j < m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Count; j++)
  392. {
  393. Constantslist.Add(m_SubMidWindow.m_STDEditor.comboBox_Constants.Items[j].ToString().Split('=')[0]);
  394. }
  395. for (int i = 0; i < list_all.Count; i++)
  396. {
  397. //周期元素?
  398. if (this.m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  399. {
  400. continue;
  401. }
  402. //first_elem?
  403. if (this.m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  404. {
  405. continue;
  406. }
  407. //Element1?
  408. if (this.m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  409. {
  410. continue;
  411. }
  412. //其它元素?
  413. if (this.m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  414. {
  415. continue;
  416. }
  417. //常量?
  418. if (Constantslist.Contains(list_all[i]))
  419. {
  420. continue;
  421. }
  422. //数字?
  423. double DNum = 0;
  424. if (double.TryParse(list_all[i], out DNum))
  425. {
  426. continue;
  427. }
  428. //>=? <=?等其它情况
  429. if (list_all[i] == "")
  430. {
  431. continue;
  432. }
  433. //true false?
  434. if ((list_all[i] == "true") || (list_all[i] == "false"))
  435. {
  436. continue;
  437. }
  438. MessageBox.Show("Please check whether the input rules are correct, error characters:" + list_all[i], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  439. return false;
  440. }
  441. //检测是否有<>= true,false
  442. if (!Expression.Contains("<") && !Expression.Contains(">") && !Expression.Contains("=") && Expression.ToLower() != "true" && Expression.ToLower() != "false")
  443. {
  444. MessageBox.Show("Expressions have no effect!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  445. return false;
  446. }
  447. }
  448. else
  449. {
  450. MessageBox.Show("The expresstion is mull!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Error);
  451. return false;
  452. }
  453. return true;
  454. }
  455. /// <summary>
  456. /// 属性检测
  457. /// </summary>
  458. /// <param name="ddata"></param>
  459. /// <returns></returns>
  460. public bool CheckAttributes(STDdata ddata)
  461. {
  462. if (string.IsNullOrEmpty(ddata.StrName))
  463. {
  464. MessageBox.Show("Rule name can not be null !", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  465. }
  466. //if (string.IsNullOrEmpty(ddata.g)
  467. //{
  468. // if (!GroupIdDictionaryFromName.ContainsKey(m_Attributes.Grid_Attributes[11, 1].Value.ToString()))
  469. // {
  470. // Position pos = new Position(11, 1);
  471. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  472. // MessageBox.Show("This group name is not included, please fill in again!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  473. // return false;
  474. // }
  475. //}
  476. #region 化学式和元素暂无限制
  477. //if (m_Attributes.Grid_Attributes[7, 1].Value.ToString())
  478. //{
  479. // Position pos = new Position(7, 1);
  480. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  481. // MessageBox.Show(table["message10"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  482. // return false;
  483. //}
  484. //if (m_Attributes.Grid_Attributes[8, 1].Value.ToString())
  485. //{
  486. // Position pos = new Position(8, 1);
  487. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  488. // MessageBox.Show(table["message10"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  489. // return false;
  490. //}
  491. #endregion
  492. return true;
  493. }
  494. public void SaveDataOfSelRule(int PreRow, int PreColumn)
  495. {
  496. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].StrName = Convert.ToString(m_Attributes.Grid_Attributes[2, 1].Value);
  497. 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);
  498. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].IfElementAnalysis = Convert.ToBoolean(m_Attributes.Grid_Attributes[4, 1].Value);
  499. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Hardness = Convert.ToString(m_Attributes.Grid_Attributes[5, 1].Value);
  500. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Density = Convert.ToString(m_Attributes.Grid_Attributes[6, 1].Value);
  501. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Electrical_conductivity = Convert.ToString(m_Attributes.Grid_Attributes[7, 1].Value);
  502. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].BSE = Convert.ToString(m_Attributes.Grid_Attributes[8, 1].Value);
  503. if (m_Attributes.Grid_Attributes[9, 1].Value != null)
  504. {
  505. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Formula = Convert.ToString(m_Attributes.Grid_Attributes[9, 1].Value);
  506. }
  507. else
  508. {
  509. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Formula = "";
  510. }
  511. if (m_Attributes.Grid_Attributes[10, 1].Value != null)
  512. {
  513. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Element = Convert.ToString(m_Attributes.Grid_Attributes[10, 1].Value);
  514. }
  515. else
  516. {
  517. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Element = "";
  518. }
  519. if (m_Attributes.Grid_Attributes[11, 1].Value != null)
  520. {
  521. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].GroupId = GroupIdDictionaryFromName[Convert.ToString(m_Attributes.Grid_Attributes[11, 1].Value)].ToString();
  522. }
  523. else
  524. {
  525. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].GroupId = "0";
  526. }
  527. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Expression = m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text.ToString();
  528. string sKeyElements = "";
  529. for (int i = 0; i < m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Columns.Count; i++)
  530. {
  531. sKeyElements += m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Rows[0].Cells[i].Value.ToString() + ",";
  532. }
  533. if (sKeyElements.Length > 0)
  534. {
  535. sKeyElements = sKeyElements.Substring(0, sKeyElements.Length - 1);
  536. }
  537. string sSubElements = "";
  538. for (int i = 0; i < m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Columns.Count; i++)
  539. {
  540. sSubElements += m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Rows[0].Cells[i].Value.ToString() + ",";
  541. }
  542. if (sSubElements.Length > 0)
  543. {
  544. sSubElements = sSubElements.Substring(0, sSubElements.Length - 1);
  545. }
  546. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].KeyElementList = sKeyElements;
  547. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].SubElementList = sSubElements;
  548. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].ListNum = PreRow.ToString();
  549. }
  550. /// <summary>
  551. /// 保存ClassifySTD库和MineralElements库
  552. /// </summary>
  553. /// <param name="DBAddress"></param>
  554. /// <returns></returns>
  555. bool SaveDictionaryToClassify(string DBAddress)
  556. {
  557. //保存列表顺序
  558. for (int i = 1; i < m_STDRuleslist.Grid_Minerals.RowsCount; i++)
  559. {
  560. STDDictionary[(int)m_STDRuleslist.Grid_Minerals[i, 0].Tag].ListNum = i.ToString();
  561. }
  562. try
  563. {
  564. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  565. m_dbConnection.Open();
  566. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  567. cmm.CommandText = "delete from ClassifySTD";
  568. //cmm.CommandText = "delete from ClassifySTD_Backup";
  569. try
  570. {
  571. cmm.ExecuteNonQuery();
  572. }
  573. catch (Exception ex)
  574. {
  575. MessageBox.Show(ex.ToString());
  576. return false;
  577. }
  578. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection);
  579. //System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD_Backup", m_dbConnection);
  580. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter);
  581. DataSet ds = new DataSet();
  582. m_dataAdapter.Fill(ds, "ClassifySTD");
  583. DataTable dt = ds.Tables["ClassifySTD"];
  584. //m_dataAdapter.Fill(ds, "ClassifySTD_Backup");
  585. //DataTable dt = ds.Tables["ClassifySTD_Backup"];
  586. dt.Clear();
  587. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  588. {
  589. string UsingElementList = "";
  590. string UsingImgPropertyList = "";
  591. string UsingOtherPropertyList = "";
  592. List<string> UsingElementL = new List<string>();
  593. List<string> UsingImgPropertyL = new List<string>();
  594. List<string> UsingOtherPropertyL = new List<string>();
  595. string str_RemoveBlank = kv.Value.Expression;
  596. ////forth_elem干扰or分隔符,故先行去掉
  597. //if (str_RemoveBlank.Contains("fourth_elem"))
  598. //{
  599. // str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  600. // UsingOtherPropertyList = "fourth_elem";
  601. //}
  602. str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  603. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  604. List<string> str_Removeandor = new List<string>();
  605. for (int i = 0; i < str_Removeand.Length; i++)
  606. {
  607. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  608. }
  609. List<string> list_all = new List<string>();
  610. for (int i = 0; i < str_Removeandor.Count; i++)
  611. {
  612. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  613. }
  614. for (int i = 0; i < list_all.Count; i++)
  615. {
  616. //周期元素?
  617. if (m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  618. {
  619. if (!UsingElementL.Contains(list_all[i]))
  620. {
  621. UsingElementL.Add(list_all[i]);
  622. }
  623. }
  624. //first_elem?
  625. if (m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  626. {
  627. if (!UsingOtherPropertyL.Contains(list_all[i]))
  628. {
  629. UsingOtherPropertyL.Add(list_all[i]);
  630. }
  631. }
  632. //Element1?
  633. if (m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  634. {
  635. if (!UsingOtherPropertyL.Contains(list_all[i]))
  636. {
  637. UsingOtherPropertyL.Add(list_all[i]);
  638. }
  639. }
  640. //其它元素?
  641. if (m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  642. {
  643. if (!UsingImgPropertyL.Contains(list_all[i]))
  644. {
  645. UsingImgPropertyL.Add(list_all[i]);
  646. }
  647. }
  648. }
  649. if (UsingElementL.Count > 0)
  650. {
  651. for (int i = 0; i < UsingElementL.Count - 1; i++)
  652. {
  653. UsingElementList += UsingElementL[i] + ",";
  654. }
  655. UsingElementList += UsingElementL[UsingElementL.Count - 1];
  656. }
  657. if (UsingImgPropertyL.Count > 0)
  658. {
  659. for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  660. {
  661. UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  662. }
  663. UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  664. }
  665. if (UsingOtherPropertyL.Count > 0)
  666. {
  667. for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  668. {
  669. UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  670. }
  671. UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  672. }
  673. DataRow newRow = dt.NewRow();
  674. newRow["STDId"] = kv.Key;
  675. newRow["StrName"] = kv.Value.StrName;
  676. newRow["Color"] = kv.Value.Color;
  677. newRow["KeyElementList"] = kv.Value.KeyElementList;
  678. newRow["SubElementList"] = kv.Value.SubElementList;
  679. newRow["UsingImgPropertyList"] = UsingImgPropertyList;
  680. newRow["UsingOtherPropertyList"] = UsingOtherPropertyList;
  681. newRow["Expression"] = kv.Value.Expression;
  682. newRow["Hardness"] = kv.Value.Hardness;
  683. newRow["Density"] = kv.Value.Density;
  684. newRow["Electrical_conductivity"] = kv.Value.Electrical_conductivity;
  685. newRow["BSE"] = int.Parse(kv.Value.BSE);
  686. newRow["Formula"] = kv.Value.Formula;
  687. newRow["Element"] = kv.Value.Element;
  688. newRow["IfElementAnalysis"] = kv.Value.IfElementAnalysis;
  689. newRow["GroupId"] = int.Parse(kv.Value.GroupId);
  690. newRow["ListNum"] = kv.Value.ListNum;
  691. dt.Rows.Add(newRow);
  692. }
  693. m_dataAdapter.Update(ds, "ClassifySTD");
  694. m_dbConnection.Close();
  695. }
  696. catch (Exception ex)
  697. {
  698. MessageBox.Show(ex.ToString());
  699. return false;
  700. }
  701. return true;
  702. }
  703. bool SaveAsDictionaryToClassify(string DBAddress)
  704. {
  705. ////保存列表顺序
  706. //for (int i = 1; i < m_STDRuleslist.Grid_Minerals.RowsCount; i++)
  707. //{
  708. // STDDictionary[(int)m_STDRuleslist.Grid_Minerals[i, 0].Tag].ListNum = i.ToString();
  709. //}
  710. try
  711. {
  712. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  713. m_dbConnection.Open();
  714. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  715. cmm.CommandText = "drop table ClassifySTD";
  716. cmm.ExecuteNonQuery();
  717. 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)";
  718. cmm.ExecuteNonQuery();
  719. System.Data.Common.DbTransaction trans = m_dbConnection.BeginTransaction();
  720. trans.Commit();
  721. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection);
  722. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter);
  723. DataSet ds = new DataSet();
  724. m_dataAdapter.Fill(ds, "ClassifySTD");
  725. DataTable dt = ds.Tables["ClassifySTD"];
  726. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  727. {
  728. string UsingElementList = "";
  729. string UsingImgPropertyList = "";
  730. string UsingOtherPropertyList = "";
  731. List<string> UsingElementL = new List<string>();
  732. List<string> UsingImgPropertyL = new List<string>();
  733. List<string> UsingOtherPropertyL = new List<string>();
  734. string str_RemoveBlank = kv.Value.Expression;
  735. ////forth_elem干扰or分隔符,故先行去掉
  736. //if (str_RemoveBlank.Contains("fourth_elem"))
  737. //{
  738. // str_RemoveBlank = str_RemoveBlank.Replace("fourth_elem", "");
  739. // UsingOtherPropertyList = "fourth_elem,";
  740. //}
  741. str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  742. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  743. List<string> str_Removeandor = new List<string>();
  744. for (int i = 0; i < str_Removeand.Length; i++)
  745. {
  746. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  747. }
  748. List<string> list_all = new List<string>();
  749. for (int i = 0; i < str_Removeandor.Count; i++)
  750. {
  751. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  752. }
  753. for (int i = 0; i < list_all.Count; i++)
  754. {
  755. //周期元素?
  756. if (m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  757. {
  758. if (!UsingElementL.Contains(list_all[i]))
  759. {
  760. UsingElementL.Add(list_all[i]);
  761. }
  762. }
  763. //first_elem?
  764. if (m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  765. {
  766. if (!UsingOtherPropertyL.Contains(list_all[i]))
  767. {
  768. UsingOtherPropertyL.Add(list_all[i]);
  769. }
  770. }
  771. //Element1?
  772. if (m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  773. {
  774. if (!UsingOtherPropertyL.Contains(list_all[i]))
  775. {
  776. UsingOtherPropertyL.Add(list_all[i]);
  777. }
  778. }
  779. //其它元素?
  780. if (m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  781. {
  782. if (!UsingImgPropertyL.Contains(list_all[i]))
  783. {
  784. UsingImgPropertyL.Add(list_all[i]);
  785. }
  786. }
  787. }
  788. if (UsingElementL.Count > 0)
  789. {
  790. for (int i = 0; i < UsingElementL.Count - 1; i++)
  791. {
  792. UsingElementList += UsingElementL[i] + ",";
  793. }
  794. UsingElementList += UsingElementL[UsingElementL.Count - 1];
  795. }
  796. if (UsingImgPropertyL.Count > 0)
  797. {
  798. for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  799. {
  800. UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  801. }
  802. UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  803. }
  804. if (UsingOtherPropertyL.Count > 0)
  805. {
  806. for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  807. {
  808. UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  809. }
  810. UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  811. }
  812. DataRow newRow = dt.NewRow();
  813. newRow["STDId"] = kv.Key;
  814. newRow["StrName"] = kv.Value.StrName;
  815. newRow["Color"] = kv.Value.Color;
  816. newRow["KeyElementList"] = kv.Value.KeyElementList;
  817. newRow["SubElementList"] = kv.Value.SubElementList;
  818. newRow["UsingImgPropertyList"] = UsingImgPropertyList;
  819. newRow["UsingOtherPropertyList"] = UsingOtherPropertyList;
  820. newRow["Expression"] = kv.Value.Expression;
  821. newRow["Hardness"] = kv.Value.Hardness;
  822. newRow["Density"] = kv.Value.Density;
  823. newRow["Electrical_conductivity"] = kv.Value.Electrical_conductivity;
  824. newRow["BSE"] = int.Parse(kv.Value.BSE);
  825. newRow["Formula"] = kv.Value.Formula;
  826. newRow["Element"] = kv.Value.Element;
  827. newRow["IfElementAnalysis"] = kv.Value.IfElementAnalysis;
  828. newRow["GroupId"] = int.Parse(kv.Value.GroupId);
  829. newRow["ListNum"] = kv.Value.ListNum;
  830. dt.Rows.Add(newRow);
  831. }
  832. m_dataAdapter.Update(ds, "ClassifySTD");
  833. m_dbConnection.Close();
  834. }
  835. catch (Exception ex)
  836. {
  837. MessageBox.Show(ex.ToString());
  838. return false;
  839. }
  840. return true;
  841. }
  842. public int AddSTDDictionaryItem()
  843. {
  844. STDdata new_STDdata = new STDdata();//定义一个TreeNode节点对象
  845. new_STDdata.Hardness = "1";
  846. new_STDdata.Density = "1";
  847. new_STDdata.Electrical_conductivity = "1";
  848. new_STDdata.BSE = "1";
  849. new_STDdata.Formula = "1";
  850. new_STDdata.Element = "1";
  851. new_STDdata.StrName = "NewRuleName";
  852. //new_STDdata.Expression = "NewRules";
  853. new_STDdata.Expression = "false";
  854. new_STDdata.KeyElementList = "";
  855. new_STDdata.SubElementList = "";
  856. Random random = new Random();
  857. new_STDdata.Color = ColorGroup[random.Next(ColorGroup.Length)];
  858. int STDId = 40000;
  859. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  860. {
  861. if (STDId < kv.Key)
  862. {
  863. STDId = kv.Key;
  864. }
  865. }
  866. STDDictionary.Add(STDId + 1, new_STDdata);
  867. return STDId + 1;
  868. }
  869. public void RemoveSTDDictionaryItem(int STDId)
  870. {
  871. STDDictionary.Remove(STDId);
  872. }
  873. public void ChangeStrName(int RowNum, string NewStrName)
  874. {
  875. STDDictionary[(int)m_STDRuleslist.Grid_Minerals[RowNum, 0].Tag].StrName = NewStrName;
  876. m_Attributes.Grid_Attributes[2, 1].Value = NewStrName;
  877. }
  878. public void ChangeSTDRulesLISTBackColor()
  879. {
  880. int stdid = m_STDRuleslist.ChangeSTDRulesLISTBackColor();
  881. }
  882. public void ChangeSTDRuleName(String RuleName)
  883. {
  884. m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value = RuleName;
  885. }
  886. public void SetNull()
  887. {
  888. m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text = "";
  889. for (int i = 1; i < m_Attributes.Grid_Attributes.RowsCount - 1; i++)
  890. {
  891. if (m_Attributes.Grid_Attributes[i, 1].Value != null)
  892. {
  893. if (m_Attributes.Grid_Attributes[i, 1].View is SourceGrid.Cells.Views.CheckBox)
  894. {
  895. m_Attributes.Grid_Attributes[i, 1].Value = true;
  896. }
  897. else
  898. {
  899. m_Attributes.Grid_Attributes[i, 1].Value = "";
  900. }
  901. }
  902. }
  903. SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell();
  904. view.BackColor = Color.White;
  905. m_Attributes.Grid_Attributes[2, 1].View = view;
  906. m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Rows.Clear();
  907. m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Columns.Clear();
  908. m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Rows.Clear();
  909. m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Columns.Clear();
  910. }
  911. private void ribbon_ESDMaxRules_Click(object sender, EventArgs e)
  912. {
  913. Form_MaxEDSRules form_MaxEDSRules = new Form_MaxEDSRules(STDDBAddress);
  914. form_MaxEDSRules.Show();
  915. }
  916. private void ribbon_ZeroRules_Click(object sender, EventArgs e)
  917. {
  918. Form_ZeroElementRules form_ZeroElementRules = new Form_ZeroElementRules(STDDBAddress);
  919. form_ZeroElementRules.Show();
  920. }
  921. private void ribbon_UserConstants_Click(object sender, EventArgs e)
  922. {
  923. Form_UserConstants form_UserConstants = new Form_UserConstants(STDDBAddress);
  924. DialogResult dialogResult = form_UserConstants.ShowDialog();
  925. if (dialogResult == DialogResult.Yes)
  926. {
  927. try
  928. {
  929. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  930. m_dbConnection.Open();
  931. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select value from Constants", m_dbConnection);
  932. DataSet ds = new DataSet();
  933. m_dataAdapter.Fill(ds);
  934. DataTable dt = ds.Tables[0];
  935. string ConstantsStr = dt.Rows[0][0].ToString();
  936. ConstantsStr = ConstantsStr.Replace(" ", "");
  937. string[] ConstantsStr2 = ConstantsStr.Split(',');
  938. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Clear();
  939. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.AddRange(ConstantsStr2);
  940. m_dbConnection.Close();
  941. }
  942. catch (Exception ee)
  943. {
  944. MessageBox.Show(ee.ToString());
  945. }
  946. }
  947. }
  948. public void AddSTDGroupsToAttribute()
  949. {
  950. try
  951. {
  952. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  953. m_dbConnection.Open();
  954. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select name,id from STDGroups order by iorder", m_dbConnection);
  955. DataSet ds = new DataSet();
  956. m_dataAdapter.Fill(ds);
  957. DataTable dt = ds.Tables[0];
  958. if (dt != null)
  959. {
  960. if (dt.Rows.Count >= 0)
  961. {
  962. GroupIdDictionaryFromId.Clear();
  963. GroupIdDictionaryFromName.Clear();
  964. List<string> ss = new List<string>();
  965. if (dt.Select("id='0'").Length == 0)
  966. {
  967. GroupIdDictionaryFromId.Add(0, "Default");
  968. GroupIdDictionaryFromName.Add("Default", 0);
  969. ss.Add("Default");
  970. }
  971. foreach (DataRow item in dt.Rows)
  972. {
  973. ss.Add(item["name"].ToString());
  974. GroupIdDictionaryFromName.Add(item["name"].ToString(), int.Parse(item["id"].ToString()));
  975. GroupIdDictionaryFromId.Add(int.Parse(item["id"].ToString()), item["name"].ToString());
  976. }
  977. SourceGrid.Cells.Editors.ComboBox GroupIdCBBox = new SourceGrid.Cells.Editors.ComboBox(typeof(string));
  978. GroupIdCBBox.StandardValues = ss;
  979. GroupIdCBBox.EditableMode = SourceGrid.EditableMode.SingleClick | SourceGrid.EditableMode.Focus;
  980. m_Attributes.Grid_Attributes[11, 1] = new SourceGrid.Cells.Cell("Default", GroupIdCBBox);
  981. GroupIdCBBox.Control.DropDownStyle = ComboBoxStyle.DropDownList; //设置下拉框为不可以编辑的状态
  982. }
  983. }
  984. //string ConstantsStr = dt.Rows[0][0].ToString();
  985. m_dbConnection.Close();
  986. }
  987. catch (Exception ee)
  988. {
  989. MessageBox.Show(ee.ToString());
  990. }
  991. }
  992. void SetNonexistentGroupsToDefault()
  993. {
  994. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  995. {
  996. if (!GroupIdDictionaryFromId.Keys.Contains(int.Parse(kv.Value.GroupId)))
  997. {
  998. kv.Value.GroupId = "0";
  999. }
  1000. }
  1001. m_Attributes.Grid_Attributes.Refresh();
  1002. }
  1003. private void ribbon_GroupNameMaintenance_Click(object sender, EventArgs e)
  1004. {
  1005. //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();
  1006. int selGroupId = GroupIdDictionaryFromName[m_Attributes.Grid_Attributes[11, 1].Value.ToString()];
  1007. Form_GroupId form_GroupId = new Form_GroupId(STDDBAddress, this);
  1008. DialogResult dialogResult = form_GroupId.ShowDialog();
  1009. if (dialogResult == DialogResult.Yes)
  1010. {
  1011. AddSTDGroupsToAttribute();
  1012. //SetNonexistentGroupsToDefault();
  1013. m_Attributes.Grid_Attributes[11, 1].Value = GroupIdDictionaryFromId[selGroupId];
  1014. }
  1015. }
  1016. private void ribbon_Exit_Click(object sender, EventArgs e)
  1017. {
  1018. this.Close();
  1019. }
  1020. private void rbClose_Click(object sender, EventArgs e)
  1021. {
  1022. if (!Isoldversion)
  1023. {
  1024. if (!EqualsBetweenDictionary(STDDictionaryInitial, STDDictionary) || IsModified)
  1025. {
  1026. DialogResult dr = MessageBox.Show("Whether to save the current modification?", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1027. if (dr == DialogResult.Yes)
  1028. {
  1029. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1030. {
  1031. if ((m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != ""))
  1032. {
  1033. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  1034. }
  1035. else
  1036. {
  1037. return;
  1038. }
  1039. }
  1040. else
  1041. {
  1042. STDDictionaryInitial.Clear();
  1043. }
  1044. FormForWaiting forWaiting = new FormForWaiting();
  1045. forWaiting.Show();
  1046. bool result = SaveDictionaryToClassify(STDDBAddress);
  1047. if (result)
  1048. {
  1049. forWaiting.Close();
  1050. }
  1051. else
  1052. {
  1053. forWaiting.Close();
  1054. MessageBox.Show("Save failed!", "Tip");
  1055. }
  1056. }
  1057. }
  1058. }
  1059. m_Attributes.Close();
  1060. m_STDRuleslist.Close();
  1061. m_SubMidWindow.m_STDEditor.Close();
  1062. m_SubMidWindow.m_ComparativeLibrary.Close();
  1063. m_SubMidWindow.Close();
  1064. ButtonStatusClose();
  1065. if (this.Text.Split(' ').Count() != 0)
  1066. {
  1067. this.Text = this.Text.Split(' ')[0];
  1068. }
  1069. }
  1070. private void rbOpen_Click(object sender, EventArgs e)
  1071. {
  1072. OpenFileDialog openFileDialog = new OpenFileDialog();
  1073. openFileDialog.Filter = "(*.db)|*.db";
  1074. openFileDialog.RestoreDirectory = true;
  1075. openFileDialog.InitialDirectory = Application.StartupPath + "\\Config\\SysData\\";
  1076. openFileDialog.FilterIndex = 1;
  1077. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1078. {
  1079. try
  1080. {
  1081. STDDBAddress = openFileDialog.FileName;
  1082. InitForms(STDDBAddress);
  1083. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  1084. IsModified = false;
  1085. CheckAccuracyofZeroRules();
  1086. }
  1087. catch(Exception ex)
  1088. {
  1089. MessageBox.Show(ex.ToString(), "Tip");
  1090. }
  1091. }
  1092. }
  1093. void ButtonStatusOpen()
  1094. {
  1095. rbOpen.Enabled = false;
  1096. rbClose.Enabled = true;
  1097. //rbRecover.Enabled = true;
  1098. rbBackup.Enabled = true;
  1099. rbSave.Enabled = true;
  1100. rbNew.Enabled = false;
  1101. ribbon_ZeroRules.Enabled = true;
  1102. ribbon_ESDMaxRules.Enabled = true;
  1103. ribbon_UserConstants.Enabled = true;
  1104. ribbon_GroupNameMaintenance.Enabled = true;
  1105. }
  1106. void ButtonStatusClose()
  1107. {
  1108. rbOpen.Enabled = true;
  1109. rbClose.Enabled = false;
  1110. //rbRecover.Enabled = false;
  1111. rbBackup.Enabled = false;
  1112. rbSave.Enabled = false;
  1113. rbNew.Enabled = true;
  1114. ribbon_ZeroRules.Enabled = false;
  1115. ribbon_ESDMaxRules.Enabled = false;
  1116. ribbon_UserConstants.Enabled = false;
  1117. ribbon_GroupNameMaintenance.Enabled = false;
  1118. }
  1119. void InitForms(string DBAddress)
  1120. {
  1121. STDDictionary.Clear();
  1122. ButtonStatusOpen();
  1123. bool ret = LoadClassifyToDictionary(DBAddress, ref STDDictionary);
  1124. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  1125. if (!ret)
  1126. {
  1127. return;
  1128. }
  1129. m_STDRuleslist = new STDRuleslist(this);
  1130. m_Attributes = new Attributes(this);
  1131. m_SubMidWindow = new SubMidWindow(this);
  1132. m_STDRuleslist.Show(dockPanel1, DockState.DockLeft);
  1133. m_Attributes.Show(dockPanel1, DockState.DockRight);
  1134. m_SubMidWindow.Show(dockPanel1, DockState.Document);
  1135. dockPanel1.DockLeftPortion = 360;
  1136. dockPanel1.DockRightPortion = 381;
  1137. if (m_SubMidWindow.m_STDEditor.m_sc != null)
  1138. {
  1139. m_SubMidWindow.m_STDEditor.m_sc.CloseDB();
  1140. }
  1141. m_SubMidWindow.m_STDEditor.m_sc = new SqlLiteClass(DBAddress);
  1142. LoadConstants(DBAddress);
  1143. AddSTDGroupsToAttribute();
  1144. m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text = "";
  1145. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1146. {
  1147. m_STDRuleslist.button_UpOrder.Enabled = false;
  1148. if (m_STDRuleslist.Grid_Minerals.RowsCount == 2) //就一条时向下按钮置灰
  1149. {
  1150. m_STDRuleslist.button_DownOrder.Enabled = false;
  1151. }
  1152. ChangeSTDEditorAndGrid_Attributes(int.Parse(m_STDRuleslist.Grid_Minerals[1, 0].Tag.ToString()));
  1153. Position pos = new Position(1, 0);
  1154. m_STDRuleslist.Grid_Minerals[1, 0].Grid.Select();
  1155. m_STDRuleslist.Grid_Minerals.Selection.Focus(pos, true);
  1156. }
  1157. }
  1158. bool EqualsBetweenDictionary(Dictionary<int, STDdata> STDDictionaryInitial, Dictionary<int, STDdata> STDDictionary)
  1159. {
  1160. if (STDDictionaryInitial.Count != STDDictionary.Count)
  1161. {
  1162. return false;
  1163. }
  1164. foreach (var key in STDDictionaryInitial.Keys)
  1165. {
  1166. if (STDDictionary.Keys.Contains(key))
  1167. {
  1168. if (!STDDictionaryInitial[key].Equals(STDDictionary[key]))
  1169. {
  1170. return false;
  1171. }
  1172. }
  1173. else
  1174. {
  1175. return false;
  1176. }
  1177. }
  1178. return true;
  1179. }
  1180. private void Form_ConstantsEditor2_FormClosing(object sender, FormClosingEventArgs e)
  1181. {
  1182. if (Isoldversion)
  1183. {
  1184. return;
  1185. }
  1186. if (m_STDRuleslist.Text != "")
  1187. {
  1188. if (!EqualsBetweenDictionary(STDDictionaryInitial, STDDictionary) || IsModified)
  1189. {
  1190. DialogResult dr = MessageBox.Show("Whether to save the current modification", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1191. if (dr == DialogResult.Yes)
  1192. {
  1193. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1194. {
  1195. if (m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != "")
  1196. {
  1197. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  1198. }
  1199. else
  1200. {
  1201. e.Cancel = true;
  1202. return;
  1203. }
  1204. }
  1205. else
  1206. {
  1207. STDDictionaryInitial.Clear();
  1208. }
  1209. FormForWaiting forWaiting = new FormForWaiting();
  1210. forWaiting.Show();
  1211. bool result = SaveDictionaryToClassify(STDDBAddress);
  1212. if (result)
  1213. {
  1214. forWaiting.Close();
  1215. }
  1216. else
  1217. {
  1218. forWaiting.Close();
  1219. MessageBox.Show("Save failed!", "Tip");
  1220. }
  1221. }
  1222. else
  1223. {
  1224. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  1225. {
  1226. if (!GroupIdDictionaryFromId.Keys.Contains(int.Parse(kv.Value.GroupId)))
  1227. {
  1228. MessageBox.Show("There is non-existent grouping informations in the classification, please adjust!");
  1229. e.Cancel = true;
  1230. return;
  1231. }
  1232. }
  1233. }
  1234. }
  1235. }
  1236. }
  1237. private void rbNew_Click(object sender, EventArgs e)
  1238. {
  1239. SaveFileDialog saveFile = new SaveFileDialog();
  1240. saveFile.Title = table["message23"].ToString();
  1241. saveFile.Filter = table["message36"].ToString();
  1242. saveFile.OverwritePrompt = true; //是否覆盖当前文件
  1243. saveFile.RestoreDirectory = true; //还原上次目录
  1244. if (saveFile.ShowDialog() == DialogResult.OK)
  1245. {
  1246. try
  1247. {
  1248. System.Data.SQLite.SQLiteConnection.CreateFile(saveFile.FileName);
  1249. System.Data.SQLite.SQLiteConnection m_OtsDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + saveFile.FileName + "'");
  1250. m_OtsDatabaseConnection.Open();
  1251. System.Data.SQLite.SQLiteCommand OtsDatabaseCommand = m_OtsDatabaseConnection.CreateCommand();
  1252. 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)";
  1253. OtsDatabaseCommand.ExecuteNonQuery();
  1254. OtsDatabaseCommand.CommandText = "CREATE TABLE Constants (value TEXT)";
  1255. OtsDatabaseCommand.ExecuteNonQuery();
  1256. OtsDatabaseCommand.CommandText = "CREATE TABLE MaxEDSRules (MaxEDSTime INT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1257. OtsDatabaseCommand.ExecuteNonQuery();
  1258. OtsDatabaseCommand.CommandText = "CREATE TABLE STDGroups (id INTEGER,name TEXT, color TEXT,iorder INTEGER)";
  1259. OtsDatabaseCommand.ExecuteNonQuery();
  1260. OtsDatabaseCommand.CommandText = "CREATE TABLE STDMinerals (id INTEGER,SPEC BLOB)";
  1261. OtsDatabaseCommand.ExecuteNonQuery();
  1262. OtsDatabaseCommand.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1263. OtsDatabaseCommand.ExecuteNonQuery();
  1264. System.Data.Common.DbTransaction trans = m_OtsDatabaseConnection.BeginTransaction();
  1265. trans.Commit();
  1266. }
  1267. catch (Exception ex)
  1268. {
  1269. MessageBox.Show(ex.ToString());
  1270. return;
  1271. }
  1272. STDDBAddress = saveFile.FileName;
  1273. InitForms(STDDBAddress);
  1274. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  1275. IsModified = false;
  1276. }
  1277. else
  1278. {
  1279. return;
  1280. }
  1281. }
  1282. void CheckAccuracyofZeroRules()
  1283. {
  1284. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  1285. m_dbConnection.Open();
  1286. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ZeroElementRules", m_dbConnection);
  1287. DataSet ds = new DataSet();
  1288. m_dataAdapter.Fill(ds);
  1289. DataTable dt = ds.Tables[0];
  1290. DataTable dt2 = dt.Copy();
  1291. if (dt != null)
  1292. {
  1293. if (dt.Columns[dt.Columns.Count - 1].ColumnName != "Expression")
  1294. {
  1295. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  1296. cmm.CommandText = "CREATE TABLE sqlitestudio_temp_table AS SELECT * FROM ZeroElementRules";
  1297. cmm.ExecuteNonQuery();
  1298. cmm.CommandText = "DROP TABLE ZeroElementRules";
  1299. cmm.ExecuteNonQuery();
  1300. cmm.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT,UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1301. cmm.ExecuteNonQuery();
  1302. cmm.CommandText = @"INSERT INTO ZeroElementRules (
  1303. ZeroElement,
  1304. UsingElementList,
  1305. UsingImgPropertyList,
  1306. UsingOtherPropertyList,
  1307. Expression
  1308. )
  1309. SELECT ZeroElement,
  1310. UsingElementList,
  1311. UsingImgPropertyList,
  1312. UsingOtherPropertyList,
  1313. Expression
  1314. FROM sqlitestudio_temp_table; ";
  1315. cmm.ExecuteNonQuery();
  1316. cmm.CommandText = "DROP TABLE sqlitestudio_temp_table";
  1317. cmm.ExecuteNonQuery();
  1318. m_dbConnection.Close();
  1319. MessageBox.Show("检测到该数据库零元素规则格式错误,已进行修正!");
  1320. }
  1321. }
  1322. }
  1323. }
  1324. }