Form_ConstantsEditor2.cs 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  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, 0].Tag.ToString())].StrName = m_Attributes.Grid_Attributes[2, 1].Value.ToString();
  574. 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);
  575. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Hardness = m_Attributes.Grid_Attributes[4, 1].Value.ToString();
  576. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Density = m_Attributes.Grid_Attributes[5, 1].Value.ToString();
  577. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Electrical_conductivity = m_Attributes.Grid_Attributes[6, 1].Value.ToString();
  578. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].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, 0].Tag.ToString())].Formula = m_Attributes.Grid_Attributes[8, 1].Value.ToString();
  582. }
  583. else
  584. {
  585. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Formula = "";
  586. }
  587. if (m_Attributes.Grid_Attributes[9, 1].Value != null)
  588. {
  589. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Element = m_Attributes.Grid_Attributes[9, 1].Value.ToString();
  590. }
  591. else
  592. {
  593. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].Tag.ToString())].Element = "";
  594. }
  595. if (m_Attributes.Grid_Attributes[10, 1].Value != null)
  596. {
  597. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].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, 0].Tag.ToString())].GroupId = "0";
  602. }
  603. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].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, 0].Tag.ToString())].KeyElementList = sKeyElements;
  623. STDDictionary[int.Parse(m_STDRuleslist.Grid_Minerals[PreRow, 0].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. int selGroupId = GroupIdDictionaryFromName[m_Attributes.Grid_Attributes[10, 1].Value.ToString()];
  933. Form_GroupId form_GroupId = new Form_GroupId(STDDBAddress,this);
  934. DialogResult dialogResult = form_GroupId.ShowDialog();
  935. if (dialogResult == DialogResult.Yes)
  936. {
  937. AddSTDGroupsToAttribute();
  938. //SetNonexistentGroupsToDefault();
  939. m_Attributes.Grid_Attributes[10, 1].Value = GroupIdDictionaryFromId[selGroupId];
  940. }
  941. }
  942. private void ribbon_Exit_Click(object sender, EventArgs e)
  943. {
  944. this.Close();
  945. }
  946. #region XML文档部分代码
  947. //string Address = Application.StartupPath + "\\Config\\SysData\\OTSParticleSTD.xml";
  948. //string Address_backup = Application.StartupPath + "\\Config\\SysData\\OTSParticleSTD_backup.xml";
  949. //private void Form_ConstantsEditor2_Load(object sender, EventArgs e)
  950. //{
  951. // //XmlDocument doc = new XmlDocument();
  952. // //doc.Load(Address);
  953. // //doc.Load(Address_backup);
  954. // //LoadXmlToDictionary(doc, ref STDDictionary);
  955. //}
  956. //private void rbRecover_Click(object sender, EventArgs e)
  957. //{
  958. // try
  959. // {
  960. // //System.IO.File.Copy(Address_backup, Address, true);
  961. // //XmlDocument doc = new XmlDocument();
  962. // //doc.Load(Address);
  963. // //LoadXmlToDictionary(doc, ref STDDictionary);
  964. // }
  965. //}
  966. //void LoadXmlToDictionary(XmlDocument xml, ref Dictionary<int, STDdata> STDDictionary)
  967. //{
  968. // XmlNode root = xml.SelectSingleNode("XMLData");
  969. // XmlNodeList root2 = root.SelectNodes("Collection");
  970. // for (int j = 0; j < root2.Count; j++)
  971. // {
  972. // if (root2[j].Attributes["RegName"].Value.ToString() == "STDList")
  973. // {
  974. // XmlNodeList childlist = root2[j].ChildNodes;
  975. // for (int i = 0; i < childlist.Count; i++)
  976. // {
  977. // //BSE、化学式、元素
  978. // string strBseValue = string.Empty;
  979. // string strFormula = string.Empty;
  980. // string strElement = string.Empty;
  981. // if (childlist[i].Attributes["BSE"] != null)
  982. // {
  983. // strBseValue = childlist[i].Attributes["BSE"].Value;
  984. // }
  985. // if (childlist[i].Attributes["Formula"] != null)
  986. // {
  987. // strFormula = childlist[i].Attributes["Formula"].Value;
  988. // }
  989. // if (childlist[i].Attributes["Element"] != null)
  990. // {
  991. // strElement = childlist[i].Attributes["Element"].Value;
  992. // }
  993. // STDdata new_STDdata = new STDdata();//定义一个TreeNode节点对象
  994. // new_STDdata.Hardness = childlist[i].Attributes["Hardness"].Value;
  995. // new_STDdata.Density = childlist[i].Attributes["Density"].Value;
  996. // new_STDdata.Electrical_conductivity = childlist[i].Attributes["Electrical_conductivity"].Value;
  997. // new_STDdata.BSE = strBseValue;
  998. // new_STDdata.Formula = strFormula;
  999. // new_STDdata.Element = strElement;
  1000. // new_STDdata.StrName = childlist[i].Attributes["StrName"].Value;
  1001. // new_STDdata.Expression = childlist[i].Attributes["Expression"].Value;
  1002. // new_STDdata.STDId = childlist[i].Attributes["STDId"].Value;
  1003. // new_STDdata.Color = childlist[i].Attributes["Color"].Value;
  1004. // new_STDdata.KeyElementList = childlist[i].Attributes["KeyElementList"].Value;
  1005. // new_STDdata.SubElementList = childlist[i].Attributes["SubElementList"].Value;
  1006. // STDDictionary.Add(int.Parse(childlist[i].Attributes["STDId"].Value), new_STDdata);
  1007. // }
  1008. // }
  1009. // }
  1010. // XmlNode root3 = root.SelectSingleNode("Member");
  1011. // string ConstantsStr = root3.Attributes["value"].Value;
  1012. // ConstantsStr = ConstantsStr.Replace(" ", "");
  1013. // string[] ConstantsStr2 = ConstantsStr.Split(',');
  1014. // m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.Clear();
  1015. // m_SubMidWindow.m_STDEditor.comboBox_Constants.Items.AddRange(ConstantsStr2);
  1016. //}
  1017. ////保存xmltree内容到xml文件
  1018. //void SaveXmlTreeDataToXml(string Address)
  1019. //{
  1020. // #region text
  1021. // //XElement xele = XElement.Load(Address);
  1022. // //var item = (from ele1 in xele.Elements("Collection")
  1023. // // where ele1.Attribute("RegName").Value.Equals("STDList")
  1024. // // select ele1).FirstOrDefault();
  1025. // //if (item != null)
  1026. // //{
  1027. // // item.Remove();
  1028. // //}
  1029. // //xele.Save(Address);
  1030. // #endregion
  1031. // XDocument xdoc = XDocument.Load(Address);
  1032. // IEnumerable<XElement> elements = from ele in xdoc.Descendants("XMLData") select ele;
  1033. // var item = (from ele1 in elements.Elements("Collection")
  1034. // where ele1.Attribute("RegName").Value.Equals("STDList")
  1035. // select ele1).FirstOrDefault();
  1036. // if (item != null)
  1037. // {
  1038. // item.Remove();
  1039. // }
  1040. // XElement STD = new XElement("Collection");
  1041. // STD.SetAttributeValue("RegName", "STDList");
  1042. // elements.ElementAt(0).Add(STD);
  1043. // foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  1044. // {
  1045. // string UsingElementList = "";
  1046. // string UsingImgPropertyList = "";
  1047. // string UsingOtherPropertyList = "";
  1048. // List<string> UsingElementL = new List<string>();
  1049. // List<string> UsingImgPropertyL = new List<string>();
  1050. // List<string> UsingOtherPropertyL = new List<string>();
  1051. // string str_RemoveBlank = kv.Value.Expression;
  1052. // //forth_elem干扰or分隔符,故先行去掉
  1053. // if (str_RemoveBlank.Contains("forth_elem"))
  1054. // {
  1055. // str_RemoveBlank = str_RemoveBlank.Replace("forth_elem", "");
  1056. // UsingOtherPropertyList = "forth_elem,";
  1057. // }
  1058. // str_RemoveBlank = str_RemoveBlank.Replace(" ", "");
  1059. // string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None);
  1060. // List<string> str_Removeandor = new List<string>();
  1061. // for (int i = 0; i < str_Removeand.Length; i++)
  1062. // {
  1063. // str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None));
  1064. // }
  1065. // List<string> list_all = new List<string>();
  1066. // for (int i = 0; i < str_Removeandor.Count; i++)
  1067. // {
  1068. // list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' }));
  1069. // }
  1070. // for (int i = 0; i < list_all.Count; i++)
  1071. // {
  1072. // //周期元素?
  1073. // if (m_SubMidWindow.m_STDEditor.comboBox_PeriodicTable.Items.Contains(list_all[i]))
  1074. // {
  1075. // if (!UsingElementL.Contains(list_all[i]))
  1076. // {
  1077. // UsingElementL.Add(list_all[i]);
  1078. // }
  1079. // }
  1080. // //first_elem?
  1081. // if (m_SubMidWindow.m_STDEditor.comboBox_Elem1.Items.Contains(list_all[i]))
  1082. // {
  1083. // if (!UsingOtherPropertyL.Contains(list_all[i]))
  1084. // {
  1085. // UsingOtherPropertyL.Add(list_all[i]);
  1086. // }
  1087. // }
  1088. // //Element1?
  1089. // if (m_SubMidWindow.m_STDEditor.comboBox_Elem.Items.Contains(list_all[i]))
  1090. // {
  1091. // if (!UsingOtherPropertyL.Contains(list_all[i]))
  1092. // {
  1093. // UsingOtherPropertyL.Add(list_all[i]);
  1094. // }
  1095. // }
  1096. // //其它元素?
  1097. // if (m_SubMidWindow.m_STDEditor.comboBox_ImgProperty.Items.Contains(list_all[i]))
  1098. // {
  1099. // if (!UsingImgPropertyL.Contains(list_all[i]))
  1100. // {
  1101. // UsingImgPropertyL.Add(list_all[i]);
  1102. // }
  1103. // }
  1104. // }
  1105. // if (UsingElementL.Count > 0)
  1106. // {
  1107. // for (int i = 0; i < UsingElementL.Count - 1; i++)
  1108. // {
  1109. // UsingElementList += UsingElementL[i] + ",";
  1110. // }
  1111. // UsingElementList += UsingElementL[UsingElementL.Count - 1];
  1112. // }
  1113. // if (UsingImgPropertyL.Count > 0)
  1114. // {
  1115. // for (int i = 0; i < UsingImgPropertyL.Count - 1; i++)
  1116. // {
  1117. // UsingImgPropertyList += UsingImgPropertyL[i] + ",";
  1118. // }
  1119. // UsingImgPropertyList += UsingImgPropertyL[UsingImgPropertyL.Count - 1];
  1120. // }
  1121. // if (UsingOtherPropertyL.Count > 0)
  1122. // {
  1123. // for (int i = 0; i < UsingOtherPropertyL.Count - 1; i++)
  1124. // {
  1125. // UsingOtherPropertyList += UsingOtherPropertyL[i] + ",";
  1126. // }
  1127. // UsingOtherPropertyList += UsingOtherPropertyL[UsingOtherPropertyL.Count - 1];
  1128. // }
  1129. // XElement EleName = new XElement("Member");
  1130. // EleName.SetAttributeValue("STDId", kv.Key);
  1131. // EleName.SetAttributeValue("StrName", kv.Value.StrName);
  1132. // EleName.SetAttributeValue("Color", kv.Value.Color);
  1133. // //EleName.SetAttributeValue("UsingElementList", UsingElementList);
  1134. // EleName.SetAttributeValue("KeyElementList", kv.Value.KeyElementList);
  1135. // EleName.SetAttributeValue("SubElementList", kv.Value.SubElementList);
  1136. // EleName.SetAttributeValue("UsingImgPropertyList", UsingImgPropertyList);
  1137. // EleName.SetAttributeValue("UsingOtherPropertyList", UsingOtherPropertyList);
  1138. // EleName.SetAttributeValue("Expression", kv.Value.Expression);
  1139. // EleName.SetAttributeValue("Hardness", kv.Value.Hardness);
  1140. // EleName.SetAttributeValue("Density", kv.Value.Density);
  1141. // EleName.SetAttributeValue("Electrical_conductivity", kv.Value.Electrical_conductivity);
  1142. // EleName.SetAttributeValue("BSE", kv.Value.BSE);
  1143. // EleName.SetAttributeValue("Formula", kv.Value.Formula);
  1144. // EleName.SetAttributeValue("Element", kv.Value.Element);
  1145. // STD.Add(EleName);
  1146. // }
  1147. // xdoc.Save(Address);
  1148. //}
  1149. #endregion
  1150. private void rbClose_Click(object sender, EventArgs e)
  1151. {
  1152. if (!EqualsBetweenDictionary(STDDictionaryInitial, STDDictionary) || IsModified)
  1153. {
  1154. DialogResult dr = MessageBox.Show("Whether to save the current modification?", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1155. if (dr == DialogResult.Yes)
  1156. {
  1157. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1158. {
  1159. if ((m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != "") && CheckAttributes() && Checktextbox_STDEditor())
  1160. {
  1161. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  1162. }
  1163. else
  1164. {
  1165. return;
  1166. }
  1167. }
  1168. else
  1169. {
  1170. STDDictionaryInitial.Clear();
  1171. }
  1172. FormForWaiting forWaiting = new FormForWaiting();
  1173. forWaiting.Show();
  1174. bool result = SaveDictionaryToClassify(STDDBAddress);
  1175. if (result)
  1176. {
  1177. m_SubMidWindow.m_STDEditor.SaveSTDXray();
  1178. forWaiting.Close();
  1179. }
  1180. else
  1181. {
  1182. forWaiting.Close();
  1183. MessageBox.Show("Save failed!", "Tip");
  1184. }
  1185. }
  1186. }
  1187. m_Attributes.Close();
  1188. m_STDRuleslist.Close();
  1189. m_SubMidWindow.m_STDEditor.Close();
  1190. m_SubMidWindow.m_ComparativeLibrary.Close();
  1191. m_SubMidWindow.Close();
  1192. ButtonStatusClose();
  1193. if (this.Text.Split(' ').Count() != 0)
  1194. {
  1195. this.Text = this.Text.Split(' ')[0];
  1196. }
  1197. }
  1198. private void rbOpen_Click(object sender, EventArgs e)
  1199. {
  1200. OpenFileDialog openFileDialog = new OpenFileDialog();
  1201. openFileDialog.Filter = "(*.db)|*.db";
  1202. openFileDialog.RestoreDirectory = true;
  1203. openFileDialog.FilterIndex = 1;
  1204. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1205. {
  1206. try
  1207. {
  1208. STDDBAddress = openFileDialog.FileName;
  1209. InitForms(STDDBAddress);
  1210. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  1211. IsModified = false;
  1212. }
  1213. catch
  1214. {
  1215. MessageBox.Show("The selected standard library is formatted incorrectly, please open the correct standard library!", "Tip");
  1216. }
  1217. }
  1218. }
  1219. void ButtonStatusOpen()
  1220. {
  1221. rbOpen.Enabled = false;
  1222. rbClose.Enabled = true;
  1223. //rbRecover.Enabled = true;
  1224. rbBackup.Enabled = true;
  1225. rbSave.Enabled = true;
  1226. rbNew.Enabled = false;
  1227. ribbon_ZeroRules.Enabled = true;
  1228. ribbon_ESDMaxRules.Enabled = true;
  1229. ribbon_UserConstants.Enabled = true;
  1230. ribbon_GroupNameMaintenance.Enabled = true;
  1231. ribbon_BulkImport.Enabled = true;
  1232. }
  1233. void ButtonStatusClose()
  1234. {
  1235. rbOpen.Enabled = true;
  1236. rbClose.Enabled = false;
  1237. //rbRecover.Enabled = false;
  1238. rbBackup.Enabled = false;
  1239. rbSave.Enabled = false;
  1240. rbNew.Enabled = true;
  1241. ribbon_ZeroRules.Enabled = false;
  1242. ribbon_ESDMaxRules.Enabled = false;
  1243. ribbon_UserConstants.Enabled = false;
  1244. ribbon_GroupNameMaintenance.Enabled = false;
  1245. ribbon_BulkImport.Enabled = false;
  1246. }
  1247. void InitForms(string DBAddress)
  1248. {
  1249. STDDictionary.Clear();
  1250. bool ret = LoadClassifyToDictionary(DBAddress, ref STDDictionary);
  1251. STDDictionaryInitial = Clone(STDDictionary) as Dictionary<int, STDdata>;
  1252. if (!ret)
  1253. {
  1254. return;
  1255. }
  1256. ButtonStatusOpen();
  1257. m_STDRuleslist = new STDRuleslist(this);
  1258. m_Attributes = new Attributes(this);
  1259. m_SubMidWindow = new SubMidWindow(this);
  1260. m_DockWindow = new OTSDockWindow(this);
  1261. m_DockWindow.CreateMainWindow();
  1262. if (m_SubMidWindow.m_STDEditor.m_sc != null)
  1263. {
  1264. m_SubMidWindow.m_STDEditor.m_sc.CloseDB();
  1265. }
  1266. m_SubMidWindow.m_STDEditor.m_sc = new SqlLiteClass(DBAddress);
  1267. LoadConstants(DBAddress);
  1268. AddSTDGroupsToAttribute();
  1269. m_SubMidWindow.m_STDEditor.textbox_STDEditor.Text = "";
  1270. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1271. {
  1272. m_STDRuleslist.button_UpOrder.Enabled = false;
  1273. if (m_STDRuleslist.Grid_Minerals.RowsCount == 2) //就一条时向下按钮置灰
  1274. {
  1275. m_STDRuleslist.button_DownOrder.Enabled = false;
  1276. }
  1277. ChangeSTDEditorAndGrid_Attributes(int.Parse(m_STDRuleslist.Grid_Minerals[1, 0].Tag.ToString()));
  1278. Position pos = new Position(1, 0);
  1279. m_STDRuleslist.Grid_Minerals[1, 0].Grid.Select();
  1280. m_STDRuleslist.Grid_Minerals.Selection.Focus(pos, true);
  1281. }
  1282. }
  1283. private void ribbon_BulkImport_Click(object sender, EventArgs e)
  1284. {
  1285. OpenFileDialog openFileDialog = new OpenFileDialog();
  1286. openFileDialog.Filter = "(*.db)|*.db";
  1287. openFileDialog.RestoreDirectory = true;
  1288. openFileDialog.FilterIndex = 1;
  1289. openFileDialog.Title = "Please open a database!";
  1290. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1291. {
  1292. try
  1293. {
  1294. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + openFileDialog.FileName + "'");
  1295. m_dbConnection.Open();
  1296. if(STDDictionary.Count==0)
  1297. {
  1298. MessageBox.Show(table["message27"].ToString(), "Tip");
  1299. return;
  1300. }
  1301. string STDId = m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row,m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Column].Tag.ToString();
  1302. bool ret = false;//搜索结果
  1303. for(int i=0;i< STDDictionary.Count;i++)
  1304. {
  1305. STDEditor.Result result = m_SubMidWindow.m_STDEditor.ImportFromResult(STDDictionary.Keys.ElementAt(i).ToString(), m_dbConnection);
  1306. if (result == STDEditor.Result.Success)
  1307. {
  1308. ret = true;
  1309. }
  1310. else if(result == STDEditor.Result.Fail)
  1311. {
  1312. return;
  1313. }
  1314. }
  1315. if (!ret)
  1316. {
  1317. MessageBox.Show("No matching energy spectrum data was found", "Tip");
  1318. }
  1319. int color = Convert.ToInt32(m_SubMidWindow.m_STDEditor.ParseRGB(STDRuleslist.colorHx16toRGB(STDDictionary[int.Parse(STDId)].Color)).ToString());
  1320. for (int j = 0; j < m_SubMidWindow.m_STDEditor.m_STDXrayList.Count; j++)
  1321. {
  1322. if (m_SubMidWindow.m_STDEditor.m_STDXrayList[j].StdID == STDId)
  1323. {
  1324. m_SubMidWindow.m_STDEditor.ShowXrayAtlas(color, m_SubMidWindow.m_STDEditor.m_STDXrayList[j].XrayData);
  1325. break;
  1326. }
  1327. }
  1328. m_dbConnection.Close();
  1329. }
  1330. catch(Exception ex)
  1331. {
  1332. MessageBox.Show(ex.ToString());
  1333. }
  1334. }
  1335. }
  1336. bool EqualsBetweenDictionary(Dictionary<int, STDdata> STDDictionaryInitial, Dictionary<int, STDdata> STDDictionary)
  1337. {
  1338. if(STDDictionaryInitial.Count!= STDDictionary.Count)
  1339. {
  1340. return false;
  1341. }
  1342. foreach(var key in STDDictionaryInitial.Keys)
  1343. {
  1344. if (STDDictionary.Keys.Contains(key))
  1345. {
  1346. if(!STDDictionaryInitial[key].Equals(STDDictionary[key]))
  1347. {
  1348. return false;
  1349. }
  1350. }
  1351. else
  1352. {
  1353. return false;
  1354. }
  1355. }
  1356. return true;
  1357. }
  1358. private void Form_ConstantsEditor2_FormClosing(object sender, FormClosingEventArgs e)
  1359. {
  1360. if (m_STDRuleslist.Text != "")
  1361. {
  1362. if (!EqualsBetweenDictionary(STDDictionaryInitial, STDDictionary)|| IsModified)
  1363. {
  1364. DialogResult dr = MessageBox.Show("Whether to save the current modification", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1365. if (dr == DialogResult.Yes)
  1366. {
  1367. if (m_STDRuleslist.Grid_Minerals.RowsCount > 1)
  1368. {
  1369. if ((m_STDRuleslist.Grid_Minerals[m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0].Value.ToString().Replace(" ", "").Trim() != "") && CheckAttributes() && Checktextbox_STDEditor())
  1370. {
  1371. SaveDataOfSelRule(m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, 0);
  1372. }
  1373. else
  1374. {
  1375. e.Cancel = true;
  1376. return;
  1377. }
  1378. }
  1379. else
  1380. {
  1381. STDDictionaryInitial.Clear();
  1382. }
  1383. FormForWaiting forWaiting = new FormForWaiting();
  1384. forWaiting.Show();
  1385. bool result = SaveDictionaryToClassify(STDDBAddress);
  1386. if (result)
  1387. {
  1388. m_SubMidWindow.m_STDEditor.SaveSTDXray();
  1389. forWaiting.Close();
  1390. }
  1391. else
  1392. {
  1393. forWaiting.Close();
  1394. MessageBox.Show("Save failed!", "Tip");
  1395. }
  1396. }
  1397. else
  1398. {
  1399. foreach (KeyValuePair<int, STDdata> kv in STDDictionary)
  1400. {
  1401. if (!GroupIdDictionaryFromId.Keys.Contains(int.Parse(kv.Value.GroupId)))
  1402. {
  1403. MessageBox.Show("There is non-existent grouping informations in the classification, please adjust!");
  1404. e.Cancel = true;
  1405. return;
  1406. }
  1407. }
  1408. }
  1409. }
  1410. }
  1411. }
  1412. private void ribbon_ConvertOreDatabaseToOtsDatabase_Click(object sender, EventArgs e)
  1413. {
  1414. DataTable OreDatabasedt = new DataTable();
  1415. OpenFileDialog openFileDialog = new OpenFileDialog();
  1416. openFileDialog.Title= "Please select the database you want to convert";
  1417. openFileDialog.Filter = "(*.db)|*.db";
  1418. openFileDialog.RestoreDirectory = true;
  1419. openFileDialog.FilterIndex = 1;
  1420. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1421. {
  1422. try
  1423. {
  1424. string OreDatabaseAddress = openFileDialog.FileName;
  1425. System.Data.SQLite.SQLiteConnection m_OreDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + OreDatabaseAddress + "'");
  1426. m_OreDatabaseConnection.Open();
  1427. System.Data.SQLite.SQLiteDataAdapter m_OreDatabasedataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from STDMinerals", m_OreDatabaseConnection);
  1428. DataSet OreDatabaseds = new DataSet();
  1429. m_OreDatabasedataAdapter.Fill(OreDatabaseds);
  1430. OreDatabasedt = OreDatabaseds.Tables[0];
  1431. int listnum = 1;
  1432. SaveFileDialog saveFile = new SaveFileDialog();
  1433. saveFile.Title = table["message23"].ToString();
  1434. saveFile.Filter = table["message36"].ToString();
  1435. saveFile.OverwritePrompt = true; //是否覆盖当前文件
  1436. saveFile.RestoreDirectory = true; //还原上次目录
  1437. if (saveFile.ShowDialog() == DialogResult.OK)
  1438. {
  1439. System.Data.SQLite.SQLiteConnection.CreateFile(saveFile.FileName);
  1440. System.Data.SQLite.SQLiteConnection m_OtsDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + saveFile.FileName + "'");
  1441. m_OtsDatabaseConnection.Open();
  1442. System.Data.SQLite.SQLiteCommand OtsDatabaseCommand = m_OtsDatabaseConnection.CreateCommand();
  1443. 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)";
  1444. OtsDatabaseCommand.ExecuteNonQuery();
  1445. OtsDatabaseCommand.CommandText = "CREATE TABLE Constants (value TEXT)";
  1446. OtsDatabaseCommand.ExecuteNonQuery();
  1447. OtsDatabaseCommand.CommandText = "CREATE TABLE MaxEDSRules (MaxEDSTime INT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1448. OtsDatabaseCommand.ExecuteNonQuery();
  1449. OtsDatabaseCommand.CommandText = "CREATE TABLE STDGroups (id INTEGER,name TEXT, color TEXT,iorder INTEGER)";
  1450. OtsDatabaseCommand.ExecuteNonQuery();
  1451. OtsDatabaseCommand.CommandText = "CREATE TABLE STDMinerals (id INTEGER,SPEC BLOB)";
  1452. OtsDatabaseCommand.ExecuteNonQuery();
  1453. OtsDatabaseCommand.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT, UsingImgPropertyList TEXT,Expression TEXT)";
  1454. OtsDatabaseCommand.ExecuteNonQuery();
  1455. System.Data.Common.DbTransaction trans = m_OtsDatabaseConnection.BeginTransaction();
  1456. foreach (DataRow item in OreDatabasedt.Rows)
  1457. {
  1458. int n = 0;
  1459. string STRcolor = "#0";
  1460. if(int.TryParse(item["color"].ToString(),out n))
  1461. {
  1462. STRcolor = "#"+n.ToString("X6");
  1463. }
  1464. int idaAdd10 = 0;
  1465. idaAdd10 = int.Parse(item["id"].ToString()) + 10;
  1466. 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,' ',' ',' ',' ',' ',' ',' ',' ')";
  1467. OtsDatabaseCommand.ExecuteNonQuery();
  1468. listnum++;
  1469. OtsDatabaseCommand.CommandText = "insert into STDMinerals (id,SPEC) values (@id,@SPEC)";
  1470. OtsDatabaseCommand.Parameters.Add("id", DbType.Int32).Value = idaAdd10;
  1471. OtsDatabaseCommand.Parameters.Add("SPEC", DbType.Binary).Value = item["SPEC"];
  1472. OtsDatabaseCommand.ExecuteNonQuery();
  1473. }
  1474. trans.Commit();
  1475. }
  1476. else
  1477. {
  1478. return;
  1479. }
  1480. }
  1481. catch(Exception ex)
  1482. {
  1483. MessageBox.Show(ex.ToString());
  1484. }
  1485. }
  1486. else
  1487. {
  1488. return;
  1489. }
  1490. }
  1491. private void rbMatchescorrespondingelement_Click(object sender, EventArgs e)
  1492. {
  1493. DataTable OreDatabasedt = new DataTable();
  1494. OpenFileDialog openFileDialog = new OpenFileDialog();
  1495. openFileDialog.Title = "Please select the database you want to math:";
  1496. openFileDialog.Filter = "(*.db)|*.db";
  1497. openFileDialog.RestoreDirectory = true;
  1498. openFileDialog.FilterIndex = 1;
  1499. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1500. {
  1501. try
  1502. {
  1503. string OreDatabaseAddress = openFileDialog.FileName;
  1504. System.Data.SQLite.SQLiteConnection m_OreDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + OreDatabaseAddress + "'");
  1505. m_OreDatabaseConnection.Open();
  1506. System.Data.SQLite.SQLiteDataAdapter m_OreDatabasedataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from MineralElements", m_OreDatabaseConnection);
  1507. DataSet OreDatabaseds = new DataSet();
  1508. m_OreDatabasedataAdapter.Fill(OreDatabaseds);
  1509. OreDatabasedt = OreDatabaseds.Tables[0];
  1510. System.Data.SQLite.SQLiteConnection m_OtsDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  1511. m_OtsDatabaseConnection.Open();
  1512. System.Data.SQLite.SQLiteCommand OtsDatabaseCommand = m_OtsDatabaseConnection.CreateCommand();
  1513. OtsDatabaseCommand.CommandText = "CREATE TABLE MineralElements (mineralID INTEGER,elementID INTEGER,elementPercentage FLOAT)";
  1514. OtsDatabaseCommand.ExecuteNonQuery();
  1515. System.Data.Common.DbTransaction trans = m_OtsDatabaseConnection.BeginTransaction();
  1516. System.Data.SQLite.SQLiteDataAdapter m_OtsDatabasedataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select id from STDMinerals", m_OtsDatabaseConnection);
  1517. DataSet OtsDatabaseds = new DataSet();
  1518. m_OtsDatabasedataAdapter.Fill(OtsDatabaseds);
  1519. var OtsDatabasedt = OtsDatabaseds.Tables[0];
  1520. List<int> IdG = new List<int>();
  1521. foreach (DataRow item in OtsDatabasedt.Rows)
  1522. {
  1523. IdG.Add(int.Parse(item["id"].ToString()) - 10);
  1524. }
  1525. int n = 0;
  1526. foreach (DataRow item in OreDatabasedt.Rows)
  1527. {
  1528. if (!int.TryParse(item["mineralID"].ToString(), out n))
  1529. { continue; }
  1530. if (IdG.Contains(n))
  1531. {
  1532. OtsDatabaseCommand.CommandText = "insert into MineralElements (mineralID,elementID,elementPercentage) values (@mineralID,@elementID,@elementPercentage)";
  1533. OtsDatabaseCommand.Parameters.Add("mineralID", DbType.Int32).Value = (n+10);
  1534. OtsDatabaseCommand.Parameters.Add("elementID", DbType.Int32).Value = item["elementID"];
  1535. OtsDatabaseCommand.Parameters.Add("elementPercentage", DbType.Double).Value = item["elementPercentage"];
  1536. OtsDatabaseCommand.ExecuteNonQuery();
  1537. }
  1538. }
  1539. trans.Commit();
  1540. }
  1541. catch (Exception ex)
  1542. {
  1543. MessageBox.Show(ex.ToString());
  1544. }
  1545. }
  1546. else
  1547. {
  1548. return;
  1549. }
  1550. }
  1551. private void rbNew_Click(object sender, EventArgs e)
  1552. {
  1553. SaveFileDialog saveFile = new SaveFileDialog();
  1554. saveFile.Title = table["message23"].ToString();
  1555. saveFile.Filter = table["message36"].ToString();
  1556. saveFile.OverwritePrompt = true; //是否覆盖当前文件
  1557. saveFile.RestoreDirectory = true; //还原上次目录
  1558. if (saveFile.ShowDialog() == DialogResult.OK)
  1559. {
  1560. try
  1561. {
  1562. System.Data.SQLite.SQLiteConnection.CreateFile(saveFile.FileName);
  1563. System.Data.SQLite.SQLiteConnection m_OtsDatabaseConnection = new System.Data.SQLite.SQLiteConnection("data source='" + saveFile.FileName + "'");
  1564. m_OtsDatabaseConnection.Open();
  1565. System.Data.SQLite.SQLiteCommand OtsDatabaseCommand = m_OtsDatabaseConnection.CreateCommand();
  1566. 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)";
  1567. OtsDatabaseCommand.ExecuteNonQuery();
  1568. OtsDatabaseCommand.CommandText = "CREATE TABLE Constants (value TEXT)";
  1569. OtsDatabaseCommand.ExecuteNonQuery();
  1570. OtsDatabaseCommand.CommandText = "CREATE TABLE MaxEDSRules (MaxEDSTime INT,UsingElementList TEXT, UsingImgPropertyList TEXT,UsingOtherPropertyList TEXT,Expression TEXT)";
  1571. OtsDatabaseCommand.ExecuteNonQuery();
  1572. OtsDatabaseCommand.CommandText = "CREATE TABLE STDGroups (id INTEGER,name TEXT, color TEXT,iorder INTEGER)";
  1573. OtsDatabaseCommand.ExecuteNonQuery();
  1574. OtsDatabaseCommand.CommandText = "CREATE TABLE STDMinerals (id INTEGER,SPEC BLOB)";
  1575. OtsDatabaseCommand.ExecuteNonQuery();
  1576. OtsDatabaseCommand.CommandText = "CREATE TABLE ZeroElementRules (ZeroElement TEXT,UsingElementList TEXT, UsingImgPropertyList TEXT,Expression TEXT)";
  1577. OtsDatabaseCommand.ExecuteNonQuery();
  1578. System.Data.Common.DbTransaction trans = m_OtsDatabaseConnection.BeginTransaction();
  1579. trans.Commit();
  1580. }
  1581. catch(Exception ex)
  1582. {
  1583. MessageBox.Show(ex.ToString());
  1584. return;
  1585. }
  1586. STDDBAddress = saveFile.FileName;
  1587. InitForms(STDDBAddress);
  1588. this.Text = this.Text.Split(' ')[0] + " " + STDDBAddress;
  1589. IsModified = false;
  1590. }
  1591. else
  1592. {
  1593. return;
  1594. }
  1595. }
  1596. }
  1597. }