Form_ConstantsEditor2.cs 76 KB

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