Form_Main.cs 65 KB

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