Form_Main.cs 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  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 Form_Main()
  38. {
  39. InitializeComponent();
  40. m_STDRuleslist = new STDRuleslist(this);
  41. m_Attributes = new Attributes(this);
  42. m_SubMidWindow = new SubMidWindow(this);
  43. m_DockWindow = new OTSDockWindow(this);
  44. }
  45. public Form_Main(string[] ImportStandardLibraryArgs)
  46. {
  47. InitializeComponent();
  48. m_STDRuleslist = new STDRuleslist(this);
  49. m_Attributes = new Attributes(this);
  50. m_SubMidWindow = new SubMidWindow(this);
  51. m_DockWindow = new OTSDockWindow(this);
  52. foreach (var it in ImportStandardLibraryArgs)
  53. {
  54. ImportArgs.Add(it);
  55. }
  56. }
  57. private void Form_ConstantsEditor2_Load(object sender, EventArgs e)
  58. {
  59. lan = new Language(this);
  60. table = lan.GetNameTable("Form_ConstantsEditor2");
  61. if (STDDBAddress != "")
  62. {
  63. InitForms(STDDBAddress);
  64. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  65. IsModified = false;
  66. if (ImportArgs.Count > 1)
  67. {
  68. ImportStandardLibraryTypeSelection m_importStandardLibraryTypeSelection = new ImportStandardLibraryTypeSelection();
  69. var result = m_importStandardLibraryTypeSelection.ShowDialog();
  70. if (m_importStandardLibraryTypeSelection.SelectEvent == "xray")
  71. {
  72. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + ImportArgs[1] + "'");
  73. m_dbConnection.Open();
  74. string sqlp = @"select xraydata from xraydata where fieldid=" + ImportArgs[2] + " and xrayindex=" + ImportArgs[4];
  75. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter(sqlp, m_dbConnection);
  76. DataSet ds = new DataSet();
  77. m_dataAdapter.Fill(ds);
  78. DataTable dt = ds.Tables[0];
  79. if (dt.Rows.Count > 0)
  80. {
  81. int STDId = AddSTDDictionaryItem();
  82. if (dt != null)
  83. {
  84. if (dt.Rows.Count > 0)
  85. {
  86. STDEditor.STDXray tDXray = new STDEditor.STDXray();
  87. tDXray.StdID = STDId.ToString();
  88. tDXray.XrayData = (byte[])dt.Rows[0]["XrayData"];
  89. tDXray.InfoState = (int)STDEditor.STDXrayInfoState.Add;
  90. m_SubMidWindow.m_STDEditor.m_STDXrayList.Add(tDXray);
  91. m_STDRuleslist.AddNewRow(STDId, "NewClassify", Color.Gray);
  92. m_SubMidWindow.m_STDEditor.ShowTabXray();
  93. }
  94. }
  95. }
  96. m_dbConnection.Close();
  97. }
  98. else if (m_importStandardLibraryTypeSelection.SelectEvent == "expression")
  99. {
  100. int STDId = AddSTDDictionaryItem();
  101. m_STDRuleslist.AddNewRow(STDId, "NewClassify", Color.Gray);
  102. }
  103. }
  104. }
  105. }
  106. void LoadConstants(string STDDBAddress)
  107. {
  108. try
  109. {
  110. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  111. m_dbConnection.Open();
  112. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from Constants", m_dbConnection);
  113. DataSet ds = new DataSet();
  114. m_dataAdapter.Fill(ds);
  115. DataTable dt = ds.Tables[0];
  116. if (dt.Rows.Count > 0)
  117. {
  118. string ConstantsStr = dt.Rows[0][0].ToString();
  119. ConstantsStr = ConstantsStr.Replace(" ", "");
  120. string[] ConstantsStr2 = ConstantsStr.Split(',');
  121. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Clear();
  122. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.AddRange(ConstantsStr2);
  123. }
  124. m_dbConnection.Close();
  125. }
  126. catch (Exception ee)
  127. {
  128. MessageBox.Show(ee.ToString());
  129. }
  130. }
  131. bool LoadClassifyToDictionary(string DBAddress, ref Dictionary<int, STDdata> STDDictionary)
  132. {
  133. try
  134. {
  135. //STDDictionaryInitial.Clear();
  136. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  137. m_dbConnection.Open();
  138. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD order by ListNum", m_dbConnection);
  139. DataSet ds = new DataSet();
  140. m_dataAdapter.Fill(ds);
  141. DataTable dt = ds.Tables[0];
  142. if (dt != null)
  143. {
  144. if (dt.Rows.Count > 0)
  145. {
  146. foreach (DataRow item in dt.Rows)
  147. {
  148. STDdata new_STDdata = new STDdata();
  149. new_STDdata.Hardness = item["Hardness"].ToString();
  150. new_STDdata.Density = item["Density"].ToString();
  151. new_STDdata.Electrical_conductivity = item["Electrical_conductivity"].ToString();
  152. new_STDdata.BSE = item["BSE"].ToString();
  153. new_STDdata.Formula = item["Formula"].ToString();
  154. new_STDdata.Element = item["Element"].ToString();
  155. new_STDdata.StrName = item["StrName"].ToString();
  156. new_STDdata.Expression = item["Expression"].ToString();
  157. new_STDdata.Color = item["Color"].ToString();
  158. new_STDdata.KeyElementList = item["KeyElementList"].ToString();
  159. new_STDdata.SubElementList = item["SubElementList"].ToString();
  160. new_STDdata.GroupId = item["GroupId"].ToString();
  161. new_STDdata.ListNum= item["ListNum"].ToString();
  162. new_STDdata.GroupId = item["GroupId"].ToString();
  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_SubMidWindow.SelSTDXray(STDId, STDDictionary[STDId]);
  179. m_Attributes.SetDatatoGrid_Attributes(STDId);
  180. }
  181. private void rbBackup_Click(object sender, EventArgs e)
  182. {
  183. if (m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row >= 0)
  184. {
  185. //if (m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != "")
  186. //{
  187. if (CheckAttributes())
  188. {
  189. if (Checktextbox_STDEditor())
  190. {
  191. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Column);
  192. try
  193. {
  194. SaveFileDialog saveFile = new SaveFileDialog();
  195. saveFile.Title = "Please select the path which to save the file";
  196. saveFile.Filter = "Database File(*.db)|*.db";
  197. saveFile.InitialDirectory = Application.StartupPath + "\\Config\\SysData\\";
  198. saveFile.OverwritePrompt = true; //是否覆盖当前文件
  199. //saveFile.RestoreDirectory = true; //还原上次目录
  200. if (saveFile.ShowDialog() == DialogResult.OK)
  201. {
  202. System.IO.File.Copy(STDDBAddress, saveFile.FileName, true);
  203. bool result = SaveDictionaryToClassify(saveFile.FileName);
  204. STDDBAddress = saveFile.FileName;
  205. this.Text = this.Text.Split(' ')[0] + " " + saveFile.FileName;
  206. //if (result)
  207. //{
  208. // MessageBox.Show("Export file successful!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  209. //}
  210. }
  211. else
  212. {
  213. return;
  214. }
  215. }
  216. catch (Exception ex)
  217. {
  218. MessageBox.Show(ex.Message);
  219. }
  220. }
  221. //}
  222. }
  223. else
  224. {
  225. MessageBox.Show("The name of rulecannot be empty!", "Tip");
  226. }
  227. }
  228. else
  229. {
  230. try
  231. {
  232. SaveFileDialog saveFile = new SaveFileDialog();
  233. saveFile.Title = "Please select the path which to save the file";
  234. saveFile.Filter = "Database File(*.db)|*.db";
  235. saveFile.OverwritePrompt = true; //是否覆盖当前文件
  236. saveFile.RestoreDirectory = true; //还原上次目录
  237. if (saveFile.ShowDialog() == DialogResult.OK)
  238. {
  239. System.IO.File.Copy(STDDBAddress, saveFile.FileName, true);
  240. bool result = SaveDictionaryToClassify(saveFile.FileName);
  241. if (result)
  242. {
  243. MessageBox.Show("Export file successful!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  244. }
  245. }
  246. else
  247. {
  248. return;
  249. }
  250. }
  251. catch (Exception ex)
  252. {
  253. MessageBox.Show(ex.Message);
  254. }
  255. }
  256. }
  257. public bool ClearDb(string DBAddress, string DBTableName)
  258. {
  259. try
  260. {
  261. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  262. m_dbConnection.Open();
  263. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  264. cmm.CommandText = "delete from " + DBTableName;
  265. cmm.ExecuteNonQuery();
  266. m_dbConnection.Close();
  267. return true;
  268. }
  269. catch (Exception ex)
  270. {
  271. MessageBox.Show(ex.ToString());
  272. return false;
  273. }
  274. }
  275. private void rbRecover_Click(object sender, EventArgs e)
  276. {
  277. }
  278. object Clone(Dictionary<int, STDdata> STDDictionary)
  279. {
  280. Dictionary<int, STDdata> STDDictionaryInitial = new Dictionary<int, STDdata>();
  281. foreach (var key in STDDictionary.Keys)
  282. {
  283. STDdata tDdata = new STDdata();
  284. tDdata = tDdata.Clone(STDDictionary[key]) as STDdata;
  285. STDDictionaryInitial.Add(key, tDdata);
  286. }
  287. return STDDictionaryInitial;
  288. }
  289. private void rbSave_Click(object sender, EventArgs e)
  290. {
  291. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  292. {
  293. if (m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row > 0)
  294. {
  295. if (m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != "")
  296. {
  297. if (CheckAttributes())
  298. {
  299. if (Checktextbox_STDEditor())
  300. {
  301. FormForWaiting forWaiting = new FormForWaiting();
  302. forWaiting.Show();
  303. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  304. // SaveXmlTreeDataToXml(Address);
  305. bool result = SaveDictionaryToClassify(STDDBAddress);
  306. if (result)
  307. {
  308. m_SubMidWindow.m_STDEditor.SaveSTDXray();
  309. STDDictionaryInitial.Clear();
  310. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  311. forWaiting.Close();
  312. IsModified = false;
  313. //MessageBox.Show("Save successful!", "Tip");
  314. }
  315. else
  316. {
  317. forWaiting.Close();
  318. MessageBox.Show("Save failed!", "Tip");
  319. }
  320. }
  321. }
  322. }
  323. }
  324. else
  325. {
  326. Position pos = new Position(1, 0);
  327. m_STDRuleslist.Grid_Minerals[1, 0].Grid.Select();
  328. if (m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != "")
  329. {
  330. if (CheckAttributes())
  331. {
  332. if (Checktextbox_STDEditor())
  333. {
  334. FormForWaiting forWaiting = new FormForWaiting();
  335. forWaiting.Show();
  336. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  337. //SaveXmlTreeDataToXml(Address);
  338. bool result = SaveDictionaryToClassify(STDDBAddress);
  339. if (result)
  340. {
  341. m_SubMidWindow.m_STDEditor.SaveSTDXray();
  342. STDDictionaryInitial.Clear();
  343. STDDictionaryInitial = new Dictionary<int, STDdata>(STDDictionary);
  344. forWaiting.Close();
  345. IsModified = false;
  346. MessageBox.Show("Save successful!", "Tip");
  347. }
  348. else
  349. {
  350. forWaiting.Close();
  351. MessageBox.Show("Save failed!", "Tip");
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. /// <summary>
  360. /// [颜色:RGB转成16进制]
  361. /// </summary>
  362. /// <param name="R">红 int</param>
  363. /// <param name="G">绿 int</param>
  364. /// <param name="B">蓝 int</param>
  365. /// <returns></returns>
  366. public static string colorRGBtoHx16(int R, int G, int B)
  367. {
  368. return System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(R, G, B));
  369. }
  370. //表达式规则检查
  371. public bool Checktextbox_STDEditor()
  372. {
  373. string str_RemoveBlank = m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text.ToString().Replace(" ", "");
  374. if (str_RemoveBlank != "")
  375. {
  376. str_RemoveBlank = str_RemoveBlank.Replace("\r\n", "");
  377. //分割符号检验
  378. string[] Symbol = { "and", "or", "+", "-", "*", "/", "=", ">", "<" };
  379. for (int i = 0; i < Symbol.Length; i++)
  380. {
  381. for (int j = 0; j < Symbol.Length; j++)
  382. {
  383. if ((Symbol[i] + Symbol[j]) != ">=" && (Symbol[i] + Symbol[j]) != "<=")
  384. {
  385. if (str_RemoveBlank.Contains(Symbol[i] + Symbol[j]))
  386. {
  387. MessageBox.Show("Invalid string exists:" + Symbol[i] + Symbol[j], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  388. return false;
  389. }
  390. }
  391. }
  392. if (str_RemoveBlank.Contains(Symbol[i] + ")") || str_RemoveBlank.Contains("(" + Symbol[i]))
  393. {
  394. MessageBox.Show("Invalid string exists:" + Symbol[i] + ")", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  395. return false;
  396. }
  397. if (str_RemoveBlank.Contains("(" + Symbol[i]))
  398. {
  399. MessageBox.Show("Invalid string exists:" + "(" + Symbol[i], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  400. return false;
  401. }
  402. }
  403. if ((str_RemoveBlank != null) && (str_RemoveBlank != ""))
  404. {
  405. //左右括号匹配检验
  406. int BracketsNum = 0;
  407. for (int i = 0; i < str_RemoveBlank.Length; i++)
  408. {
  409. if (str_RemoveBlank[i] == '(') BracketsNum++;
  410. if (str_RemoveBlank[i] == ')') BracketsNum--;
  411. }
  412. if (BracketsNum != 0)
  413. {
  414. MessageBox.Show("Number of left and right parentheses does not match!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  415. return false;
  416. }
  417. //首字符检测
  418. 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] == '('))
  419. {
  420. MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  421. return false;
  422. }
  423. if (str_RemoveBlank.Length >= 3)
  424. {
  425. if ((str_RemoveBlank.Substring(0, 3) == "and") || (str_RemoveBlank.Substring(0, 3) == "end"))
  426. {
  427. MessageBox.Show("First character is error!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  428. return false;
  429. }
  430. }
  431. }
  432. //forth_elem干扰or分隔符,故先行去掉
  433. str_RemoveBlank = str_RemoveBlank.Replace("forth_elem", "");
  434. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  435. List<string> str_Removeandor = new List<string>();
  436. for (int i = 0; i < str_Removeand.Length; i++)
  437. {
  438. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  439. }
  440. List<string> list_all = new List<string>();
  441. for (int i = 0; i < str_Removeandor.Count; i++)
  442. {
  443. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  444. }
  445. //常量
  446. List<string> Constantslist = new List<string>();
  447. for (int j = 0; j < m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Count; j++)
  448. {
  449. Constantslist.Add(m_SubMidWindow.m_STDEditor.comboBox_Constants.Items[j].ToString().Split('=')[0]);
  450. }
  451. for (int i = 0; i < list_all.Count; i++)
  452. {
  453. //周期元素?
  454. if (this.m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  455. {
  456. continue;
  457. }
  458. //first_elem?
  459. if (this.m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  460. {
  461. continue;
  462. }
  463. //Element1?
  464. if (this.m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  465. {
  466. continue;
  467. }
  468. //其它元素?
  469. if (this.m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  470. {
  471. continue;
  472. }
  473. //常量?
  474. if (Constantslist.Contains(list_all[i]))
  475. {
  476. continue;
  477. }
  478. //数字?
  479. double DNum = 0;
  480. if (double.TryParse(list_all[i], out DNum))
  481. {
  482. continue;
  483. }
  484. //>=? <=?等其它情况
  485. if (list_all[i] == "")
  486. {
  487. continue;
  488. }
  489. //true false?
  490. if ((list_all[i] == "true") || (list_all[i] == "false"))
  491. {
  492. continue;
  493. }
  494. MessageBox.Show("Please check whether the input rules are correct, error characters:" + list_all[i], "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  495. return false;
  496. }
  497. //检测是否有<>= true,false
  498. 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")
  499. {
  500. MessageBox.Show("Expressions have no effect!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  501. return false;
  502. }
  503. }
  504. else
  505. {
  506. var XrayInfo = m_SubMidWindow.m_STDEditor.m_STDXrayList.Find(STDXray => STDXray.StdID == m_STDRuleslist.Grid_Minerals[m_STDRuleslist.PreRow, 0].Tag.ToString());
  507. if (XrayInfo==null||XrayInfo.XrayData==null)
  508. {
  509. MessageBox.Show("Expressions cannot be empty!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  510. return false;
  511. }
  512. }
  513. return true;
  514. }
  515. //属性检测
  516. public bool CheckAttributes()
  517. {
  518. //if (double.Parse(m_Attributes.Grid_Attributes[4, 1].Value.ToString()) <= 0)
  519. //{
  520. // Position pos = new Position(4, 1);
  521. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  522. // MessageBox.Show(table["message10"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  523. // return false;
  524. //}
  525. //if (double.Parse(m_Attributes.Grid_Attributes[5, 1].Value.ToString()) <= 0)
  526. //{
  527. // Position pos = new Position(5, 1);
  528. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  529. // MessageBox.Show(table["message11"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  530. // return false;
  531. //}
  532. //if (double.Parse(m_Attributes.Grid_Attributes[6, 1].Value.ToString()) <= 0)
  533. //{
  534. // Position pos = new Position(6, 1);
  535. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  536. // MessageBox.Show(table["message12"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  537. // return false;
  538. //}
  539. //if (int.Parse(m_Attributes.Grid_Attributes[7, 1].Value.ToString()) <= 0)
  540. //{
  541. // Position pos = new Position(7, 1);
  542. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  543. // MessageBox.Show(table["message9"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  544. // return false;
  545. //}
  546. if (m_Attributes.Grid_Attributes[10, 1].Value != null)
  547. {
  548. if (!GroupIdDictionaryFromName.ContainsKey(m_Attributes.Grid_Attributes[10, 1].Value.ToString()))
  549. {
  550. Position pos = new Position(10, 1);
  551. m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  552. MessageBox.Show("This group name is not included, please fill in again!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
  553. return false;
  554. }
  555. }
  556. #region 化学式和元素暂无限制
  557. //if (m_Attributes.Grid_Attributes[7, 1].Value.ToString())
  558. //{
  559. // Position pos = new Position(7, 1);
  560. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  561. // MessageBox.Show(table["message10"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  562. // return false;
  563. //}
  564. //if (m_Attributes.Grid_Attributes[8, 1].Value.ToString())
  565. //{
  566. // Position pos = new Position(8, 1);
  567. // m_Attributes.Grid_Attributes.Selection.Focus(pos, true);
  568. // MessageBox.Show(table["message10"].ToString(), table["message32"].ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
  569. // return false;
  570. //}
  571. #endregion
  572. return true;
  573. }
  574. public void SaveDataOfSelRule(int PreRow, int PreColumn)
  575. {
  576. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].StrName = m_Attributes.Grid_Attributes[2, 1].Value.ToString();
  577. 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);
  578. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Hardness = m_Attributes.Grid_Attributes[4, 1].Value.ToString();
  579. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Density = m_Attributes.Grid_Attributes[5, 1].Value.ToString();
  580. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Electrical_conductivity = m_Attributes.Grid_Attributes[6, 1].Value.ToString();
  581. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].BSE = m_Attributes.Grid_Attributes[7, 1].Value.ToString();
  582. if (m_Attributes.Grid_Attributes[8, 1].Value!= null)
  583. {
  584. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Formula = m_Attributes.Grid_Attributes[8, 1].Value.ToString();
  585. }
  586. else
  587. {
  588. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Formula = "";
  589. }
  590. if (m_Attributes.Grid_Attributes[9, 1].Value != null)
  591. {
  592. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Element = m_Attributes.Grid_Attributes[9, 1].Value.ToString();
  593. }
  594. else
  595. {
  596. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Element = "";
  597. }
  598. if (m_Attributes.Grid_Attributes[10, 1].Value != null)
  599. {
  600. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].GroupId = GroupIdDictionaryFromName[m_Attributes.Grid_Attributes[10, 1].Value.ToString()].ToString();
  601. }
  602. else
  603. {
  604. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].GroupId = "0";
  605. }
  606. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Expression = m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text.ToString();
  607. string sKeyElements = "";
  608. for (int i = 0; i < m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Columns.Count; i++)
  609. {
  610. sKeyElements += m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Rows[0].Cells[i].Value.ToString() + ",";
  611. }
  612. if (sKeyElements.Length > 0)
  613. {
  614. sKeyElements = sKeyElements.Substring(0, sKeyElements.Length - 1);
  615. }
  616. string sSubElements = "";
  617. for (int i = 0; i < m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Columns.Count; i++)
  618. {
  619. sSubElements += m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Rows[0].Cells[i].Value.ToString() + ",";
  620. }
  621. if (sSubElements.Length > 0)
  622. {
  623. sSubElements = sSubElements.Substring(0, sSubElements.Length - 1);
  624. }
  625. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].KeyElementList = sKeyElements;
  626. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].SubElementList = sSubElements;
  627. }
  628. /// <summary>
  629. /// 保存ClassifySTD库和MineralElements库
  630. /// </summary>
  631. /// <param name="DBAddress"></param>
  632. /// <returns></returns>
  633. bool SaveDictionaryToClassify(string DBAddress)
  634. {
  635. //保存列表顺序
  636. for(int i=1;i<m_STDRuleslist.Grid_Minerals.RowsCount;i++)
  637. {
  638. STDDictionary[(int)m_STDRuleslist.Grid_Minerals[i,0].Tag].ListNum = i.ToString();
  639. }
  640. try
  641. {
  642. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  643. m_dbConnection.Open();
  644. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  645. cmm.CommandText = "delete from ClassifySTD";
  646. //cmm.CommandText = "delete from ClassifySTD_Backup";
  647. try
  648. {
  649. cmm.ExecuteNonQuery();
  650. }
  651. catch (Exception ex)
  652. {
  653. MessageBox.Show(ex.ToString());
  654. return false;
  655. }
  656. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection);
  657. //System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD_Backup", m_dbConnection);
  658. System.Data.SQLite.SQLiteCommandBuilder qLiteCommandBuilder = new System.Data.SQLite.SQLiteCommandBuilder(m_dataAdapter);
  659. DataSet ds = new DataSet();
  660. m_dataAdapter.Fill(ds, "ClassifySTD");
  661. DataTable dt = ds.Tables["ClassifySTD"];
  662. //m_dataAdapter.Fill(ds, "ClassifySTD_Backup");
  663. //DataTable dt = ds.Tables["ClassifySTD_Backup"];
  664. dt.Clear();
  665. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  666. {
  667. string UsingElementList = "";
  668. string UsingImgPropertyList = "";
  669. string UsingOtherPropertyList = "";
  670. List<string> UsingElementL = new List<string>();
  671. List<string> UsingImgPropertyL = new List<string>();
  672. List<string> UsingOtherPropertyL = new List<string>();
  673. string str_RemoveBlank = kv.Value.Expression;
  674. //forth_elem干扰or分隔符,故先行去掉
  675. if (str_RemoveBlank.Contains("forth_elem"))
  676. {
  677. str_RemoveBlank = str_RemoveBlank.Replace("forth_elem", "");
  678. UsingOtherPropertyList = "forth_elem,";
  679. }
  680. str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  681. string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  682. List<string> str_Removeandor = new List<string>();
  683. for (int i = 0; i < str_Removeand.Length; i++)
  684. {
  685. str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  686. }
  687. List<string> list_all = new List<string>();
  688. for (int i = 0; i < str_Removeandor.Count; i++)
  689. {
  690. list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  691. }
  692. for (int i = 0; i < list_all.Count; i++)
  693. {
  694. //周期元素?
  695. if (m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  696. {
  697. if (!UsingElementL.Contains(list_all[i]))
  698. {
  699. UsingElementL.Add(list_all[i]);
  700. }
  701. }
  702. //first_elem?
  703. if (m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  704. {
  705. if (!UsingOtherPropertyL.Contains(list_all[i]))
  706. {
  707. UsingOtherPropertyL.Add(list_all[i]);
  708. }
  709. }
  710. //Element1?
  711. if (m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  712. {
  713. if (!UsingOtherPropertyL.Contains(list_all[i]))
  714. {
  715. UsingOtherPropertyL.Add(list_all[i]);
  716. }
  717. }
  718. //其它元素?
  719. if (m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  720. {
  721. if (!UsingImgPropertyL.Contains(list_all[i]))
  722. {
  723. UsingImgPropertyL.Add(list_all[i]);
  724. }
  725. }
  726. }
  727. if (UsingElementL.Count > 0)
  728. {
  729. for (int i = 0; i < UsingElementL.Count - 1; i++)
  730. {
  731. UsingElementList += UsingElementL[i] + ",";
  732. }
  733. UsingElementList += UsingElementL[UsingElementL.Count - 1];
  734. }
  735. if (UsingImgPropertyL.Count > 0)
  736. {
  737. for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  738. {
  739. UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  740. }
  741. UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  742. }
  743. if (UsingOtherPropertyL.Count > 0)
  744. {
  745. for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  746. {
  747. UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  748. }
  749. UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  750. }
  751. DataRow newRow = dt.NewRow();
  752. newRow["STDId"] = kv.Key;
  753. newRow["StrName"] = kv.Value.StrName;
  754. newRow["Color"] = kv.Value.Color;
  755. newRow["KeyElementList"] = kv.Value.KeyElementList;
  756. newRow["SubElementList"] = kv.Value.SubElementList;
  757. newRow["UsingImgPropertyList"] = UsingImgPropertyList;
  758. newRow["UsingOtherPropertyList"] = UsingOtherPropertyList;
  759. newRow["Expression"] = kv.Value.Expression;
  760. newRow["Hardness"] = kv.Value.Hardness;
  761. newRow["Density"] = kv.Value.Density;
  762. newRow["Electrical_conductivity"] = kv.Value.Electrical_conductivity;
  763. newRow["BSE"] = int.Parse(kv.Value.BSE);
  764. newRow["Formula"] = kv.Value.Formula;
  765. newRow["Element"] = kv.Value.Element;
  766. newRow["GroupId"] = int.Parse(kv.Value.GroupId);
  767. newRow["ListNum"] = kv.Value.ListNum;
  768. dt.Rows.Add(newRow);
  769. }
  770. m_dataAdapter.Update(ds,"ClassifySTD");
  771. m_dbConnection.Close();
  772. }
  773. catch (Exception ex)
  774. {
  775. MessageBox.Show(ex.ToString());
  776. return false;
  777. }
  778. return true;
  779. }
  780. public int AddSTDDictionaryItem()
  781. {
  782. STDdata new_STDdata = new STDdata();//定义一个TreeNode节点对象
  783. new_STDdata.Hardness = "1";
  784. new_STDdata.Density = "1";
  785. new_STDdata.Electrical_conductivity = "1";
  786. new_STDdata.BSE = "1";
  787. new_STDdata.Formula = "1";
  788. new_STDdata.Element = "1";
  789. new_STDdata.StrName = "NewRuleName";
  790. //new_STDdata.Expression = "NewRules";
  791. new_STDdata.Expression = "";
  792. new_STDdata.KeyElementList = "";
  793. new_STDdata.SubElementList = "";
  794. new_STDdata.Color = colorRGBtoHx16(Color.Gray.R, Color.Gray.G, Color.Gray.B);
  795. int STDId = 40000;
  796. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  797. {
  798. if (STDId < kv.Key)
  799. {
  800. STDId = kv.Key;
  801. }
  802. }
  803. STDDictionary.Add(STDId + 1, new_STDdata);
  804. return STDId + 1;
  805. }
  806. public void RemoveSTDDictionaryItem(int STDId)
  807. {
  808. STDDictionary.Remove(STDId);
  809. }
  810. public void ChangeStrName(int RowNum, string NewStrName)
  811. {
  812. STDDictionary[(int)m_STDRuleslist.Grid_Minerals[RowNum, 0].Tag].StrName = NewStrName;
  813. m_Attributes.Grid_Attributes[2, 1].Value = NewStrName;
  814. }
  815. public void ChangeSTDRulesLISTBackColor()
  816. {
  817. int stdid=m_STDRuleslist.ChangeSTDRulesLISTBackColor();
  818. if (stdid != -1)
  819. {
  820. m_SubMidWindow.m_STDEditor.SelSTDXray(stdid, STDDictionary[stdid]);
  821. }
  822. }
  823. public void ChangeSTDRuleName(String RuleName)
  824. {
  825. m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value = RuleName;
  826. }
  827. public void SetNull()
  828. {
  829. m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text = "";
  830. for (int i = 1; i<m_Attributes.Grid_Attributes.RowsCount-1; i++)
  831. {
  832. m_Attributes.Grid_Attributes[i, 1].Value = "";
  833. }
  834. SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell();
  835. view.BackColor = Color.White;
  836. m_Attributes.Grid_Attributes[2, 1].View= view;
  837. m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Rows.Clear();
  838. m_SubMidWindow.m_STDEditor.dataGridView_KeyElements.Columns.Clear();
  839. m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Rows.Clear();
  840. m_SubMidWindow.m_STDEditor.dataGridView_SubElements.Columns.Clear();
  841. }
  842. private void ribbon_ESDMaxRules_Click(object sender, EventArgs e)
  843. {
  844. Form_MaxEDSRules form_MaxEDSRules = new Form_MaxEDSRules(STDDBAddress);
  845. form_MaxEDSRules.Show();
  846. }
  847. private void ribbon_ZeroRules_Click(object sender, EventArgs e)
  848. {
  849. Form_ZeroElementRules form_ZeroElementRules = new Form_ZeroElementRules(STDDBAddress);
  850. form_ZeroElementRules.Show();
  851. }
  852. private void ribbon_UserConstants_Click(object sender, EventArgs e)
  853. {
  854. Form_UserConstants form_UserConstants = new Form_UserConstants(STDDBAddress);
  855. DialogResult dialogResult=form_UserConstants.ShowDialog();
  856. if (dialogResult == DialogResult.Yes)
  857. {
  858. try
  859. {
  860. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  861. m_dbConnection.Open();
  862. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select value from Constants", m_dbConnection);
  863. DataSet ds = new DataSet();
  864. m_dataAdapter.Fill(ds);
  865. DataTable dt = ds.Tables[0];
  866. string ConstantsStr = dt.Rows[0][0].ToString();
  867. ConstantsStr = ConstantsStr.Replace(" ", "");
  868. string[] ConstantsStr2 = ConstantsStr.Split(',');
  869. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Clear();
  870. m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.AddRange(ConstantsStr2);
  871. m_dbConnection.Close();
  872. }
  873. catch (Exception ee)
  874. {
  875. MessageBox.Show(ee.ToString());
  876. }
  877. }
  878. }
  879. public void AddSTDGroupsToAttribute()
  880. {
  881. try
  882. {
  883. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  884. m_dbConnection.Open();
  885. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select name,id from STDGroups order by iorder", m_dbConnection);
  886. DataSet ds = new DataSet();
  887. m_dataAdapter.Fill(ds);
  888. DataTable dt = ds.Tables[0];
  889. if (dt != null)
  890. {
  891. if (dt.Rows.Count >= 0)
  892. {
  893. GroupIdDictionaryFromId.Clear();
  894. GroupIdDictionaryFromName.Clear();
  895. if (dt.Select("id='0'").Length==0)
  896. {
  897. GroupIdDictionaryFromId.Add(0, "Default");
  898. GroupIdDictionaryFromName.Add("Default", 0);
  899. }
  900. List<string> ss = new List<string>();
  901. foreach (DataRow item in dt.Rows)
  902. {
  903. ss.Add(item["name"].ToString());
  904. GroupIdDictionaryFromName.Add(item["name"].ToString(), int.Parse(item["id"].ToString()));
  905. GroupIdDictionaryFromId.Add(int.Parse(item["id"].ToString()),item["name"].ToString());
  906. }
  907. SourceGrid.Cells.Editors.ComboBox GroupIdCBBox = new SourceGrid.Cells.Editors.ComboBox(typeof(string));
  908. GroupIdCBBox.StandardValues = ss;
  909. GroupIdCBBox.EditableMode = SourceGrid.EditableMode.SingleClick| SourceGrid.EditableMode.Focus;
  910. m_Attributes.Grid_Attributes[10, 1] = new SourceGrid.Cells.Cell("Default", GroupIdCBBox);
  911. }
  912. }
  913. //string ConstantsStr = dt.Rows[0][0].ToString();
  914. m_dbConnection.Close();
  915. }
  916. catch (Exception ee)
  917. {
  918. MessageBox.Show(ee.ToString());
  919. }
  920. }
  921. void SetNonexistentGroupsToDefault()
  922. {
  923. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  924. {
  925. if(!GroupIdDictionaryFromId.Keys.Contains(int.Parse(kv.Value.GroupId)))
  926. {
  927. kv.Value.GroupId = "0";
  928. }
  929. }
  930. m_Attributes.Grid_Attributes.Refresh();
  931. }
  932. private void ribbon_GroupNameMaintenance_Click(object sender, EventArgs e)
  933. {
  934. //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();
  935. int selGroupId = GroupIdDictionaryFromName[m_Attributes.Grid_Attributes[10, 1].Value.ToString()];
  936. Form_GroupId form_GroupId = new Form_GroupId(STDDBAddress,this);
  937. DialogResult dialogResult = form_GroupId.ShowDialog();
  938. if (dialogResult == DialogResult.Yes)
  939. {
  940. AddSTDGroupsToAttribute();
  941. //SetNonexistentGroupsToDefault();
  942. m_Attributes.Grid_Attributes[10, 1].Value = GroupIdDictionaryFromId[selGroupId];
  943. }
  944. }
  945. private void ribbon_Exit_Click(object sender, EventArgs e)
  946. {
  947. this.Close();
  948. }
  949. #region XML文档部分代码
  950. //string Address = Application.StartupPath + "\\Config\\SysData\\OTSParticleSTD.xml";
  951. //string Address_backup = Application.StartupPath + "\\Config\\SysData\\OTSParticleSTD_backup.xml";
  952. //private void Form_ConstantsEditor2_Load(object sender, EventArgs e)
  953. //{
  954. // //XmlDocument doc = new XmlDocument();
  955. // //doc.Load(Address);
  956. // //doc.Load(Address_backup);
  957. // //LoadXmlToDictionary(doc, ref STDDictionary);
  958. //}
  959. //private void rbRecover_Click(object sender, EventArgs e)
  960. //{
  961. // try
  962. // {
  963. // //System.IO.File.Copy(Address_backup, Address, true);
  964. // //XmlDocument doc = new XmlDocument();
  965. // //doc.Load(Address);
  966. // //LoadXmlToDictionary(doc, ref STDDictionary);
  967. // }
  968. //}
  969. //void LoadXmlToDictionary(XmlDocument xml, ref Dictionary<int, STDdata> STDDictionary)
  970. //{
  971. // XmlNode root = xml.SelectSingleNode("XMLData");
  972. // XmlNodeList root2 = root.SelectNodes("Collection");
  973. // for (int j = 0; j < root2.Count; j++)
  974. // {
  975. // if (root2[j].Attributes["RegName"].Value.ToString() == "STDList")
  976. // {
  977. // XmlNodeList childlist = root2[j].ChildNodes;
  978. // for (int i = 0; i < childlist.Count; i++)
  979. // {
  980. // //BSE、化学式、元素
  981. // string strBseValue = string.Empty;
  982. // string strFormula = string.Empty;
  983. // string strElement = string.Empty;
  984. // if (childlist[i].Attributes["BSE"] != null)
  985. // {
  986. // strBseValue = childlist[i].Attributes["BSE"].Value;
  987. // }
  988. // if (childlist[i].Attributes["Formula"] != null)
  989. // {
  990. // strFormula = childlist[i].Attributes["Formula"].Value;
  991. // }
  992. // if (childlist[i].Attributes["Element"] != null)
  993. // {
  994. // strElement = childlist[i].Attributes["Element"].Value;
  995. // }
  996. // STDdata new_STDdata = new STDdata();//定义一个TreeNode节点对象
  997. // new_STDdata.Hardness = childlist[i].Attributes["Hardness"].Value;
  998. // new_STDdata.Density = childlist[i].Attributes["Density"].Value;
  999. // new_STDdata.Electrical_conductivity = childlist[i].Attributes["Electrical_conductivity"].Value;
  1000. // new_STDdata.BSE = strBseValue;
  1001. // new_STDdata.Formula = strFormula;
  1002. // new_STDdata.Element = strElement;
  1003. // new_STDdata.StrName = childlist[i].Attributes["StrName"].Value;
  1004. // new_STDdata.Expression = childlist[i].Attributes["Expression"].Value;
  1005. // new_STDdata.STDId = childlist[i].Attributes["STDId"].Value;
  1006. // new_STDdata.Color = childlist[i].Attributes["Color"].Value;
  1007. // new_STDdata.KeyElementList = childlist[i].Attributes["KeyElementList"].Value;
  1008. // new_STDdata.SubElementList = childlist[i].Attributes["SubElementList"].Value;
  1009. // STDDictionary.Add(int.Parse(childlist[i].Attributes["STDId"].Value), new_STDdata);
  1010. // }
  1011. // }
  1012. // }
  1013. // XmlNode root3 = root.SelectSingleNode("Member");
  1014. // string ConstantsStr = root3.Attributes["value"].Value;
  1015. // ConstantsStr = ConstantsStr.Replace(" ", "");
  1016. // string[] ConstantsStr2 = ConstantsStr.Split(',');
  1017. // m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Clear();
  1018. // m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.AddRange(ConstantsStr2);
  1019. //}
  1020. ////保存xmltree内容到xml文件
  1021. //void SaveXmlTreeDataToXml(string Address)
  1022. //{
  1023. // #region text
  1024. // //XElement xele = XElement.Load(Address);
  1025. // //var item = (from ele1 in xele.Elements("Collection")
  1026. // // where ele1.Attribute("RegName").Value.Equals("STDList")
  1027. // // select ele1).FirstOrDefault();
  1028. // //if (item != null)
  1029. // //{
  1030. // // item.Remove();
  1031. // //}
  1032. // //xele.Save(Address);
  1033. // #endregion
  1034. // XDocument xdoc = XDocument.Load(Address);
  1035. // IEnumerable<XElement> elements = from ele in xdoc.Descendants("XMLData") select ele;
  1036. // var item = (from ele1 in elements.Elements("Collection")
  1037. // where ele1.Attribute("RegName").Value.Equals("STDList")
  1038. // select ele1).FirstOrDefault();
  1039. // if (item != null)
  1040. // {
  1041. // item.Remove();
  1042. // }
  1043. // XElement STD = new XElement("Collection");
  1044. // STD.SetAttributeValue("RegName", "STDList");
  1045. // elements.ElementAt(0).Add(STD);
  1046. // foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  1047. // {
  1048. // string UsingElementList = "";
  1049. // string UsingImgPropertyList = "";
  1050. // string UsingOtherPropertyList = "";
  1051. // List<string> UsingElementL = new List<string>();
  1052. // List<string> UsingImgPropertyL = new List<string>();
  1053. // List<string> UsingOtherPropertyL = new List<string>();
  1054. // string str_RemoveBlank = kv.Value.Expression;
  1055. // //forth_elem干扰or分隔符,故先行去掉
  1056. // if (str_RemoveBlank.Contains("forth_elem"))
  1057. // {
  1058. // str_RemoveBlank = str_RemoveBlank.Replace("forth_elem", "");
  1059. // UsingOtherPropertyList = "forth_elem,";
  1060. // }
  1061. // str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  1062. // string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  1063. // List<string> str_Removeandor = new List<string>();
  1064. // for (int i = 0; i < str_Removeand.Length; i++)
  1065. // {
  1066. // str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  1067. // }
  1068. // List<string> list_all = new List<string>();
  1069. // for (int i = 0; i < str_Removeandor.Count; i++)
  1070. // {
  1071. // list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  1072. // }
  1073. // for (int i = 0; i < list_all.Count; i++)
  1074. // {
  1075. // //周期元素?
  1076. // if (m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  1077. // {
  1078. // if (!UsingElementL.Contains(list_all[i]))
  1079. // {
  1080. // UsingElementL.Add(list_all[i]);
  1081. // }
  1082. // }
  1083. // //first_elem?
  1084. // if (m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  1085. // {
  1086. // if (!UsingOtherPropertyL.Contains(list_all[i]))
  1087. // {
  1088. // UsingOtherPropertyL.Add(list_all[i]);
  1089. // }
  1090. // }
  1091. // //Element1?
  1092. // if (m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  1093. // {
  1094. // if (!UsingOtherPropertyL.Contains(list_all[i]))
  1095. // {
  1096. // UsingOtherPropertyL.Add(list_all[i]);
  1097. // }
  1098. // }
  1099. // //其它元素?
  1100. // if (m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  1101. // {
  1102. // if (!UsingImgPropertyL.Contains(list_all[i]))
  1103. // {
  1104. // UsingImgPropertyL.Add(list_all[i]);
  1105. // }
  1106. // }
  1107. // }
  1108. // if (UsingElementL.Count > 0)
  1109. // {
  1110. // for (int i = 0; i < UsingElementL.Count - 1; i++)
  1111. // {
  1112. // UsingElementList += UsingElementL[i] + ",";
  1113. // }
  1114. // UsingElementList += UsingElementL[UsingElementL.Count - 1];
  1115. // }
  1116. // if (UsingImgPropertyL.Count > 0)
  1117. // {
  1118. // for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  1119. // {
  1120. // UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  1121. // }
  1122. // UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  1123. // }
  1124. // if (UsingOtherPropertyL.Count > 0)
  1125. // {
  1126. // for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  1127. // {
  1128. // UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  1129. // }
  1130. // UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  1131. // }
  1132. // XElement EleName = new XElement("Member");
  1133. // EleName.SetAttributeValue("STDId", kv.Key);
  1134. // EleName.SetAttributeValue("StrName", kv.Value.StrName);
  1135. // EleName.SetAttributeValue("Color", kv.Value.Color);
  1136. // //EleName.SetAttributeValue("UsingElementList", UsingElementList);
  1137. // EleName.SetAttributeValue("KeyElementList", kv.Value.KeyElementList);
  1138. // EleName.SetAttributeValue("SubElementList", kv.Value.SubElementList);
  1139. // EleName.SetAttributeValue("UsingImgPropertyList", UsingImgPropertyList);
  1140. // EleName.SetAttributeValue("UsingOtherPropertyList", UsingOtherPropertyList);
  1141. // EleName.SetAttributeValue("Expression", kv.Value.Expression);
  1142. // EleName.SetAttributeValue("Hardness", kv.Value.Hardness);
  1143. // EleName.SetAttributeValue("Density", kv.Value.Density);
  1144. // EleName.SetAttributeValue("Electrical_conductivity", kv.Value.Electrical_conductivity);
  1145. // EleName.SetAttributeValue("BSE", kv.Value.BSE);
  1146. // EleName.SetAttributeValue("Formula", kv.Value.Formula);
  1147. // EleName.SetAttributeValue("Element", kv.Value.Element);
  1148. // STD.Add(EleName);
  1149. // }
  1150. // xdoc.Save(Address);
  1151. //}
  1152. #endregion
  1153. private void rbClose_Click(object sender, EventArgs e)
  1154. {
  1155. if (!EqualsBetweenDictionary(STDDictionaryInitial, STDDictionary) || IsModified)
  1156. {
  1157. DialogResult dr = MessageBox.Show("Whether to save the current modification?", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1158. if (dr == DialogResult.Yes)
  1159. {
  1160. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1161. {
  1162. if ((m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != "") && CheckAttributes() && Checktextbox_STDEditor())
  1163. {
  1164. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  1165. }
  1166. else
  1167. {
  1168. return;
  1169. }
  1170. }
  1171. else
  1172. {
  1173. STDDictionaryInitial.Clear();
  1174. }
  1175. FormForWaiting forWaiting = new FormForWaiting();
  1176. forWaiting.Show();
  1177. bool result = SaveDictionaryToClassify(STDDBAddress);
  1178. if (result)
  1179. {
  1180. m_SubMidWindow.m_STDEditor.SaveSTDXray();
  1181. forWaiting.Close();
  1182. }
  1183. else
  1184. {
  1185. forWaiting.Close();
  1186. MessageBox.Show("Save failed!", "Tip");
  1187. }
  1188. }
  1189. }
  1190. m_Attributes.Close();
  1191. m_STDRuleslist.Close();
  1192. m_SubMidWindow.m_STDEditor.Close();
  1193. m_SubMidWindow.m_ComparativeLibrary.Close();
  1194. m_SubMidWindow.Close();
  1195. ButtonStatusClose();
  1196. if (this.Text.Split(' ').Count() != 0)
  1197. {
  1198. this.Text = this.Text.Split(' ')[0];
  1199. }
  1200. }
  1201. private void rbOpen_Click(object sender, EventArgs e)
  1202. {
  1203. OpenFileDialog openFileDialog = new OpenFileDialog();
  1204. openFileDialog.Filter = "(*.db)|*.db";
  1205. openFileDialog.RestoreDirectory = true;
  1206. openFileDialog.InitialDirectory = Application.StartupPath + "\\Config\\SysData\\";
  1207. openFileDialog.FilterIndex = 1;
  1208. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1209. {
  1210. try
  1211. {
  1212. STDDBAddress = openFileDialog.FileName;
  1213. InitForms(STDDBAddress);
  1214. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  1215. IsModified = false;
  1216. }
  1217. catch
  1218. {
  1219. MessageBox.Show("The selected standard library is formatted incorrectly, please open the correct standard library!", "Tip");
  1220. }
  1221. }
  1222. }
  1223. void ButtonStatusOpen()
  1224. {
  1225. rbOpen.Enabled = false;
  1226. rbClose.Enabled = true;
  1227. //rbRecover.Enabled = true;
  1228. rbBackup.Enabled = true;
  1229. rbSave.Enabled = true;
  1230. rbNew.Enabled = false;
  1231. ribbon_ZeroRules.Enabled = true;
  1232. ribbon_ESDMaxRules.Enabled = true;
  1233. ribbon_UserConstants.Enabled = true;
  1234. ribbon_GroupNameMaintenance.Enabled = true;
  1235. ribbon_BulkImport.Enabled = true;
  1236. }
  1237. void ButtonStatusClose()
  1238. {
  1239. rbOpen.Enabled = true;
  1240. rbClose.Enabled = false;
  1241. //rbRecover.Enabled = false;
  1242. rbBackup.Enabled = false;
  1243. rbSave.Enabled = false;
  1244. rbNew.Enabled = true;
  1245. ribbon_ZeroRules.Enabled = false;
  1246. ribbon_ESDMaxRules.Enabled = false;
  1247. ribbon_UserConstants.Enabled = false;
  1248. ribbon_GroupNameMaintenance.Enabled = false;
  1249. ribbon_BulkImport.Enabled = false;
  1250. }
  1251. void InitForms(string DBAddress)
  1252. {
  1253. STDDictionary.Clear();
  1254. bool ret = LoadClassifyToDictionary(DBAddress, ref STDDictionary);
  1255. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  1256. if (!ret)
  1257. {
  1258. return;
  1259. }
  1260. ButtonStatusOpen();
  1261. m_STDRuleslist = new STDRuleslist(this);
  1262. m_Attributes = new Attributes(this);
  1263. m_SubMidWindow = new SubMidWindow(this);
  1264. m_DockWindow = new OTSDockWindow(this);
  1265. m_DockWindow.CreateMainWindow();
  1266. if (m_SubMidWindow.m_STDEditor.m_sc != null)
  1267. {
  1268. m_SubMidWindow.m_STDEditor.m_sc.CloseDB();
  1269. }
  1270. m_SubMidWindow.m_STDEditor.m_sc = new SqlLiteClass(DBAddress);
  1271. LoadConstants(DBAddress);
  1272. AddSTDGroupsToAttribute();
  1273. m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text = "";
  1274. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1275. {
  1276. m_STDRuleslist.button_UpOrder.Enabled = false;
  1277. if (m_STDRuleslist.Grid_Minerals.RowsCount == 2) //就一条时向下按钮置灰
  1278. {
  1279. m_STDRuleslist.button_DownOrder.Enabled = false;
  1280. }
  1281. ChangeSTDEditorAndGrid_Attributes(int.Parse(m_STDRuleslist.Grid_Minerals[1, 0].Tag.ToString()));
  1282. Position pos = new Position(1, 0);
  1283. m_STDRuleslist.Grid_Minerals[1, 0].Grid.Select();
  1284. m_STDRuleslist.Grid_Minerals.Selection.Focus(pos, true);
  1285. }
  1286. }
  1287. private void ribbon_BulkImport_Click(object sender, EventArgs e)
  1288. {
  1289. OpenFileDialog openFileDialog = new OpenFileDialog();
  1290. openFileDialog.Filter = "(*.db)|*.db";
  1291. openFileDialog.RestoreDirectory = true;
  1292. openFileDialog.FilterIndex = 1;
  1293. openFileDialog.Title = "Please open a database!";
  1294. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1295. {
  1296. try
  1297. {
  1298. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + openFileDialog.FileName + "'");
  1299. m_dbConnection.Open();
  1300. if(STDDictionary.Count==0)
  1301. {
  1302. MessageBox.Show(table["message27"].ToString(), "Tip");
  1303. return;
  1304. }
  1305. string STDId = m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row,m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Column].Tag.ToString();
  1306. bool ret = false;//搜索结果
  1307. for(int i=0;i< STDDictionary.Count;i++)
  1308. {
  1309. STDEditor.Result result = m_SubMidWindow.m_STDEditor.ImportFromResult(STDDictionary.Keys.ElementAt(i).ToString(), m_dbConnection);
  1310. if (result == STDEditor.Result.Success)
  1311. {
  1312. ret = true;
  1313. }
  1314. else if(result == STDEditor.Result.Fail)
  1315. {
  1316. return;
  1317. }
  1318. }
  1319. if (!ret)
  1320. {
  1321. MessageBox.Show("No matching energy spectrum data was found", "Tip");
  1322. }
  1323. int color = Convert.ToInt32(m_SubMidWindow.m_STDEditor.ParseRGB(STDRuleslist.colorHx16toRGB(STDDictionary[int.Parse(STDId)].Color)).ToString());
  1324. for (int j = 0; j < m_SubMidWindow.m_STDEditor.m_STDXrayList.Count; j++)
  1325. {
  1326. if (m_SubMidWindow.m_STDEditor.m_STDXrayList[j].StdID == STDId)
  1327. {
  1328. m_SubMidWindow.m_STDEditor.ShowXrayAtlas(color, m_SubMidWindow.m_STDEditor.m_STDXrayList[j].XrayData);
  1329. break;
  1330. }
  1331. }
  1332. m_dbConnection.Close();
  1333. }
  1334. catch(Exception ex)
  1335. {
  1336. MessageBox.Show(ex.ToString());
  1337. }
  1338. }
  1339. }
  1340. bool EqualsBetweenDictionary(Dictionary<int, STDdata> STDDictionaryInitial, Dictionary<int, STDdata> STDDictionary)
  1341. {
  1342. if(STDDictionaryInitial.Count!= STDDictionary.Count)
  1343. {
  1344. return false;
  1345. }
  1346. foreach(var key in STDDictionaryInitial.Keys)
  1347. {
  1348. if (STDDictionary.Keys.Contains(key))
  1349. {
  1350. if(!STDDictionaryInitial[key].Equals(STDDictionary[key]))
  1351. {
  1352. return false;
  1353. }
  1354. }
  1355. else
  1356. {
  1357. return false;
  1358. }
  1359. }
  1360. return true;
  1361. }
  1362. private void Form_ConstantsEditor2_FormClosing(object sender, FormClosingEventArgs e)
  1363. {
  1364. if (m_STDRuleslist.Text != "")
  1365. {
  1366. if (!EqualsBetweenDictionary(STDDictionaryInitial, STDDictionary)|| IsModified)
  1367. {
  1368. DialogResult dr = MessageBox.Show("Whether to save the current modification", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1369. if (dr == DialogResult.Yes)
  1370. {
  1371. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1372. {
  1373. if ((m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != "") && CheckAttributes() && Checktextbox_STDEditor())
  1374. {
  1375. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  1376. }
  1377. else
  1378. {
  1379. e.Cancel = true;
  1380. return;
  1381. }
  1382. }
  1383. else
  1384. {
  1385. STDDictionaryInitial.Clear();
  1386. }
  1387. FormForWaiting forWaiting = new FormForWaiting();
  1388. forWaiting.Show();
  1389. bool result = SaveDictionaryToClassify(STDDBAddress);
  1390. if (result)
  1391. {
  1392. m_SubMidWindow.m_STDEditor.SaveSTDXray();
  1393. forWaiting.Close();
  1394. }
  1395. else
  1396. {
  1397. forWaiting.Close();
  1398. MessageBox.Show("Save failed!", "Tip");
  1399. }
  1400. }
  1401. else
  1402. {
  1403. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  1404. {
  1405. if (!GroupIdDictionaryFromId.Keys.Contains(int.Parse(kv.Value.GroupId)))
  1406. {
  1407. MessageBox.Show("There is non-existent grouping informations in the classification, please adjust!");
  1408. e.Cancel = true;
  1409. return;
  1410. }
  1411. }
  1412. }
  1413. }
  1414. }
  1415. }
  1416. private void ribbon_ConvertOreDatabaseToOtsDatabase_Click(object sender, EventArgs e)
  1417. {
  1418. DataTable OreDatabasedt = new DataTable();
  1419. OpenFileDialog openFileDialog = new OpenFileDialog();
  1420. openFileDialog.Title= "Please select the database you want to convert";
  1421. openFileDialog.Filter = "(*.db)|*.db";
  1422. openFileDialog.RestoreDirectory = true;
  1423. openFileDialog.FilterIndex = 1;
  1424. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1425. {
  1426. try
  1427. {
  1428. string OreDatabaseAddress = openFileDialog.FileName;
  1429. System.Data.SQLite.SQLiteConnection m_OreDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + OreDatabaseAddress + "'");
  1430. m_OreDatabaseConnection.Open();
  1431. System.Data.SQLite.SQLiteDataAdapter m_OreDatabasedataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from STDMinerals", m_OreDatabaseConnection);
  1432. DataSet OreDatabaseds = new DataSet();
  1433. m_OreDatabasedataAdapter.Fill(OreDatabaseds);
  1434. OreDatabasedt = OreDatabaseds.Tables[0];
  1435. int listnum = 1;
  1436. SaveFileDialog saveFile = new SaveFileDialog();
  1437. saveFile.Title = table["message23"].ToString();
  1438. saveFile.Filter = table["message36"].ToString();
  1439. saveFile.OverwritePrompt = true; //是否覆盖当前文件
  1440. saveFile.RestoreDirectory = true; //还原上次目录
  1441. if (saveFile.ShowDialog() == DialogResult.OK)
  1442. {
  1443. System.Data.SQLite.SQLiteConnection.CreateFile(saveFile.FileName);
  1444. System.Data.SQLite.SQLiteConnection m_OtsDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + saveFile.FileName + "'");
  1445. m_OtsDatabaseConnection.Open();
  1446. System.Data.SQLite.SQLiteCommand OtsDatabaseCommand = m_OtsDatabaseConnection.CreateCommand();
  1447. 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,ListNum INT,GroupId INT)";
  1448. OtsDatabaseCommand.ExecuteNonQuery();
  1449. OtsDatabaseCommand.CommandText = "CREATE TABLE Constants (value TEXT)";
  1450. OtsDatabaseCommand.ExecuteNonQuery();
  1451. OtsDatabaseCommand.CommandText = "CREATE TABLE MaxEDSRules (MaxEDSTime INT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1452. OtsDatabaseCommand.ExecuteNonQuery();
  1453. OtsDatabaseCommand.CommandText = "CREATE TABLE STDGroups (id INTEGER,name TEXT, color TEXT,iorder INTEGER)";
  1454. OtsDatabaseCommand.ExecuteNonQuery();
  1455. OtsDatabaseCommand.CommandText = "CREATE TABLE STDMinerals (id INTEGER,SPEC BLOB)";
  1456. OtsDatabaseCommand.ExecuteNonQuery();
  1457. OtsDatabaseCommand.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT, UsingImgPropertyList TEXT,Expression TEXT)";
  1458. OtsDatabaseCommand.ExecuteNonQuery();
  1459. System.Data.Common.DbTransaction trans = m_OtsDatabaseConnection.BeginTransaction();
  1460. foreach (DataRow item in OreDatabasedt.Rows)
  1461. {
  1462. int n = 0;
  1463. string STRcolor = "#0";
  1464. if(int.TryParse(item["color"].ToString(),out n))
  1465. {
  1466. STRcolor = "#"+n.ToString("X6");
  1467. }
  1468. int idaAdd10 = 0;
  1469. idaAdd10 = int.Parse(item["id"].ToString()) + 10;
  1470. OtsDatabaseCommand.CommandText = "insert into ClassifySTD (Density,BSE,Formula,StrName,Color,STDId,ListNum,GroupId,KeyElementList,SubElementList,UsingImgPropertyList,UsingOtherPropertyList,Expression,Hardness,Electrical_conductivity,Element) values ( '" + item["density"].ToString() + "','" + item["BSEValue"].ToString() + "','" + item["formula"].ToString() + "','" + item["name"].ToString() + "','" + STRcolor + "'," + idaAdd10.ToString() + "," + listnum.ToString() + @",0,' ',' ',' ',' ',' ',' ',' ',' ')";
  1471. OtsDatabaseCommand.ExecuteNonQuery();
  1472. listnum++;
  1473. OtsDatabaseCommand.CommandText = "insert into STDMinerals (id,SPEC) values (@id,@SPEC)";
  1474. OtsDatabaseCommand.Parameters.Add("id", DbType.Int32).Value = idaAdd10;
  1475. OtsDatabaseCommand.Parameters.Add("SPEC", DbType.Binary).Value = item["SPEC"];
  1476. OtsDatabaseCommand.ExecuteNonQuery();
  1477. }
  1478. trans.Commit();
  1479. }
  1480. else
  1481. {
  1482. return;
  1483. }
  1484. }
  1485. catch(Exception ex)
  1486. {
  1487. MessageBox.Show(ex.ToString());
  1488. }
  1489. }
  1490. else
  1491. {
  1492. return;
  1493. }
  1494. }
  1495. private void rbMatchescorrespondingelement_Click(object sender, EventArgs e)
  1496. {
  1497. DataTable OreDatabasedt = new DataTable();
  1498. OpenFileDialog openFileDialog = new OpenFileDialog();
  1499. openFileDialog.Title = "Please select the database you want to math:";
  1500. openFileDialog.Filter = "(*.db)|*.db";
  1501. openFileDialog.RestoreDirectory = true;
  1502. openFileDialog.FilterIndex = 1;
  1503. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1504. {
  1505. try
  1506. {
  1507. string OreDatabaseAddress = openFileDialog.FileName;
  1508. System.Data.SQLite.SQLiteConnection m_OreDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + OreDatabaseAddress + "'");
  1509. m_OreDatabaseConnection.Open();
  1510. System.Data.SQLite.SQLiteDataAdapter m_OreDatabasedataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from MineralElements", m_OreDatabaseConnection);
  1511. DataSet OreDatabaseds = new DataSet();
  1512. m_OreDatabasedataAdapter.Fill(OreDatabaseds);
  1513. OreDatabasedt = OreDatabaseds.Tables[0];
  1514. System.Data.SQLite.SQLiteConnection m_OtsDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  1515. m_OtsDatabaseConnection.Open();
  1516. System.Data.SQLite.SQLiteCommand OtsDatabaseCommand = m_OtsDatabaseConnection.CreateCommand();
  1517. OtsDatabaseCommand.CommandText = "CREATE TABLE MineralElements (mineralID INTEGER,elementID INTEGER,elementPercentage FLOAT)";
  1518. OtsDatabaseCommand.ExecuteNonQuery();
  1519. System.Data.Common.DbTransaction trans = m_OtsDatabaseConnection.BeginTransaction();
  1520. System.Data.SQLite.SQLiteDataAdapter m_OtsDatabasedataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select id from STDMinerals", m_OtsDatabaseConnection);
  1521. DataSet OtsDatabaseds = new DataSet();
  1522. m_OtsDatabasedataAdapter.Fill(OtsDatabaseds);
  1523. var OtsDatabasedt = OtsDatabaseds.Tables[0];
  1524. List<int> IdG = new List<int>();
  1525. foreach (DataRow item in OtsDatabasedt.Rows)
  1526. {
  1527. IdG.Add(int.Parse(item["id"].ToString()) - 10);
  1528. }
  1529. int n = 0;
  1530. foreach (DataRow item in OreDatabasedt.Rows)
  1531. {
  1532. if (!int.TryParse(item["mineralID"].ToString(), out n))
  1533. { continue; }
  1534. if (IdG.Contains(n))
  1535. {
  1536. OtsDatabaseCommand.CommandText = "insert into MineralElements (mineralID,elementID,elementPercentage) values (@mineralID,@elementID,@elementPercentage)";
  1537. OtsDatabaseCommand.Parameters.Add("mineralID", DbType.Int32).Value = (n+10);
  1538. OtsDatabaseCommand.Parameters.Add("elementID", DbType.Int32).Value = item["elementID"];
  1539. OtsDatabaseCommand.Parameters.Add("elementPercentage", DbType.Double).Value = item["elementPercentage"];
  1540. OtsDatabaseCommand.ExecuteNonQuery();
  1541. }
  1542. }
  1543. trans.Commit();
  1544. }
  1545. catch (Exception ex)
  1546. {
  1547. MessageBox.Show(ex.ToString());
  1548. }
  1549. }
  1550. else
  1551. {
  1552. return;
  1553. }
  1554. }
  1555. private void rbNew_Click(object sender, EventArgs e)
  1556. {
  1557. SaveFileDialog saveFile = new SaveFileDialog();
  1558. saveFile.Title = table["message23"].ToString();
  1559. saveFile.Filter = table["message36"].ToString();
  1560. saveFile.OverwritePrompt = true; //是否覆盖当前文件
  1561. saveFile.RestoreDirectory = true; //还原上次目录
  1562. if (saveFile.ShowDialog() == DialogResult.OK)
  1563. {
  1564. try
  1565. {
  1566. System.Data.SQLite.SQLiteConnection.CreateFile(saveFile.FileName);
  1567. System.Data.SQLite.SQLiteConnection m_OtsDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + saveFile.FileName + "'");
  1568. m_OtsDatabaseConnection.Open();
  1569. System.Data.SQLite.SQLiteCommand OtsDatabaseCommand = m_OtsDatabaseConnection.CreateCommand();
  1570. 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,ListNum INT,GroupId INT)";
  1571. OtsDatabaseCommand.ExecuteNonQuery();
  1572. OtsDatabaseCommand.CommandText = "CREATE TABLE Constants (value TEXT)";
  1573. OtsDatabaseCommand.ExecuteNonQuery();
  1574. OtsDatabaseCommand.CommandText = "CREATE TABLE MaxEDSRules (MaxEDSTime INT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1575. OtsDatabaseCommand.ExecuteNonQuery();
  1576. OtsDatabaseCommand.CommandText = "CREATE TABLE STDGroups (id INTEGER,name TEXT, color TEXT,iorder INTEGER)";
  1577. OtsDatabaseCommand.ExecuteNonQuery();
  1578. OtsDatabaseCommand.CommandText = "CREATE TABLE STDMinerals (id INTEGER,SPEC BLOB)";
  1579. OtsDatabaseCommand.ExecuteNonQuery();
  1580. OtsDatabaseCommand.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT, UsingImgPropertyList TEXT,Expression TEXT)";
  1581. OtsDatabaseCommand.ExecuteNonQuery();
  1582. System.Data.Common.DbTransaction trans = m_OtsDatabaseConnection.BeginTransaction();
  1583. trans.Commit();
  1584. }
  1585. catch(Exception ex)
  1586. {
  1587. MessageBox.Show(ex.ToString());
  1588. return;
  1589. }
  1590. STDDBAddress = saveFile.FileName;
  1591. InitForms(STDDBAddress);
  1592. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  1593. IsModified = false;
  1594. }
  1595. else
  1596. {
  1597. return;
  1598. }
  1599. }
  1600. }
  1601. }