Form_Main.cs 63 KB

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