private XElement GetXElement(Atom atom) { var elementType = ""; if (atom.Element is Element element) { elementType = element.Symbol; } if (atom.Element is FunctionalGroup functionalGroup) { elementType = FunctionalGroups.GetKey(functionalGroup); } XElement result = new XElement(CMLNamespaces.cml + CMLConstants.TagAtom, new XAttribute(CMLConstants.AttributeId, atom.Id), new XAttribute(CMLConstants.AttributeElementType, elementType), new XAttribute(CMLConstants.AttributeX2, atom.Position.X.ToString("0.####", CultureInfo.InvariantCulture)), new XAttribute(CMLConstants.AttributeY2, atom.Position.Y.ToString("0.####", CultureInfo.InvariantCulture)) ); if (atom.FormalCharge != null && atom.FormalCharge.Value != 0) { result.Add(new XAttribute(CMLConstants.AttributeFormalCharge, atom.FormalCharge.Value)); } if (atom.IsotopeNumber != null && atom.IsotopeNumber.Value != 0) { result.Add(new XAttribute(CMLConstants.AttributeIsotopeNumber, atom.IsotopeNumber.Value)); } if (atom.Element is Element element2) { if (element2 == Globals.PeriodicTable.C && atom.ShowSymbol != null) { result.Add(new XAttribute(CMLNamespaces.c4w + CMLConstants.AttributeExplicit, atom.ShowSymbol)); } } return(result); }
private string FormatAtomSymbol(Atom atom) { var elementType = ""; if (atom.Element is Element element) { elementType = element.Symbol; } // Bit of a bodge, but it ensures that it can be re-loaded without exploding if (atom.Element is FunctionalGroup functionalGroup) { elementType = FunctionalGroups.GetKey(functionalGroup); if (elementType.Length > 3) { elementType = "*"; } } // Add three spaces the trim back to three characters return($"{elementType} ".Substring(0, 3)); }
private static MolJSON ExportMol(Molecule m1) { MolJSON mj = new MolJSON(); mj.a = new AtomJSON[m1.Atoms.Count]; Dictionary <Atom, int> indexLookup = new Dictionary <Atom, int>(); int iAtom = 0; foreach (Atom a in m1.Atoms.Values) { string elem = null; if (a.Element.Symbol != "C") { if (a.Element is Element element) { elem = element.Symbol; } if (a.Element is FunctionalGroup functionalGroup) { elem = FunctionalGroups.GetKey(functionalGroup); } } mj.a[iAtom] = new AtomJSON() { i = a.Id, x = a.Position.X, y = a.Position.Y, l = elem }; if (a.FormalCharge != null) { mj.a[iAtom].c = a.FormalCharge.Value; } indexLookup[a] = iAtom; iAtom++; } int iBond = 0; if (m1.Bonds.Any()) { mj.b = new BondJSON[m1.Bonds.Count]; foreach (Bond bond in m1.Bonds) { mj.b[iBond] = new BondJSON() { i = bond.Id, b = indexLookup[bond.StartAtom], e = indexLookup[bond.EndAtom] }; if (bond.Stereo == Globals.BondStereo.Wedge) { mj.b[iBond].s = Protruding; } else if (bond.Stereo == Globals.BondStereo.Hatch) { mj.b[iBond].s = Recessed; } else if (bond.Stereo == Globals.BondStereo.Indeterminate) { mj.b[iBond].s = Ambiguous; } if (bond.Order != Globals.OrderSingle) { mj.b[iBond].o = bond.OrderValue; } iBond++; } } return(mj); }