Form_Main.cs 65 KB

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