Form_Main.cs 62 KB

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