protected void OpenSpecieDB(string filename) { try { string db = File.ReadAllText(filename); if (!db.Contains("[Species]")) { Log.Message( "[Species] token not found in database file", MessageType.Error, new MessageSource(filename)); return; } int elementCount = 0, specieCount = 0; for (Match m = s_ElementRegex.Match(db); m.Success; m = m.NextMatch()) { Element elem = new Element(); elem.AtmoicNo = int.Parse(m.Groups["AtmNo"].Value, CultureInfo.InvariantCulture); elem.AtomicMass = double.Parse(m.Groups["AtmWt"].Value, CultureInfo.InvariantCulture); elem.Name = elem.Symbol = m.Groups["Sym"].Value; if (Element.AddElement(elem)) elementCount++; } List<int> groupIndices = new List<int>(); Dictionary<int, string> groupNames = new Dictionary<int, string>(); for (Match m = s_GroupRegex.Match(db); m.Success; m = m.NextMatch()) { int i = int.Parse(m.Groups["Index"].Value, CultureInfo.InvariantCulture); groupIndices.Add(i); groupNames.Add(i, m.Groups["Name"].Value); if (lstSpecies.Groups[m.Groups["Name"].Value] == null) lstSpecies.Groups.Add(m.Groups["Name"].Value, m.Groups["Name"].Value); } groupIndices.Sort(); for (Match m = s_CompoundRegex.Match(db); m.Success; m = m.NextMatch()) { Compound comp = new Compound(); comp.Name = m.Groups["Name"].Value; comp.Symbol = m.Groups["Sym"].Value; comp.Index = int.Parse(m.Groups["Index"].Value, CultureInfo.InvariantCulture); int HfOK, CpOK; int.TryParse(m.Groups["HfOK"].Value, out HfOK); int.TryParse(m.Groups["CpOK"].Value, out CpOK); comp.HeatOK = HfOK != 0 && CpOK != 0; int j = 0; while (j < groupIndices.Count && groupIndices[j] < comp.Index) j++; if (j != 0) j--; if (groupNames.Count > 0)//To stop the program from simply crashing. comp.Annotation = groupNames[groupIndices[j]]; if (m.Groups["Phase"].Value == "Solid") comp.Phase = Phases.Solid; else if (m.Groups["Phase"].Value == "Liquid") comp.Phase = Phases.Liquid; else if (m.Groups["Phase"].Value == "Vapour") comp.Phase = Phases.Gas; for (int i = 0; i < m.Groups["ElementSym"].Captures.Count; i++) { string sym = m.Groups["ElementSym"].Captures[i].Value; if (CreateElems && !Element.ElementList.ContainsKey(sym)) { Element.AddElement(new Element(sym , -1 , double.NaN)); Log.Message("Element '" + sym + "' not found in element database (Found in compound '" + comp.Name + "')", MessageType.Warning, new MessageSource(filename)); } comp.Elements.Add(Element.ElementList[sym], Fraction.ToFraction(m.Groups["ElemCount"].Captures[i].Value)); } if (Compound.AddCompound(comp)) { specieCount++; } } txtFilter_TextChanged(this, new EventArgs()); Log.Message(elementCount.ToString() + " Elements and " + specieCount.ToString() + " Species loaded.", MessageType.Note, new MessageSource(filename)); TreeNode newNode = new TreeNode(filename); //newNode.ContextMenuStrip = menuDatabaseFile; treeFiles.Nodes["SpecieDB"].Nodes.Add(newNode); treeFiles.Nodes["SpecieDB"].Expand(); regKey.SetValue("Last Database", filename); UpdateLastPath(Path.GetDirectoryName(filename)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Open database", MessageBoxButtons.OK, MessageBoxIcon.Error); Log.Message(ex.Message, MessageType.Error); } DoDatabaseChanged(); }
public static bool AddElement(Element e) { try { ElementList.Add(e.Symbol, e); return true; } catch (Exception ex) { if (SilentAddFail) return false; else throw ex; } }