Form_Main.cs 66 KB

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