Form_Main.cs 66 KB

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