public string Export(Model model) { string result; // MDL Standard bond length is 1.54 Angstoms (Å) // Already done in Ribbon Export Button code //model.ScaleToAverageBondLength(1.54); MemoryStream stream = new MemoryStream(); using (StreamWriter writer = new StreamWriter(stream)) { foreach (var mol in model.Molecules) { string message; CtabProcessor pct = new CtabProcessor(); pct.ExportToStream(mol, writer, out message); DataProcessor dp = new DataProcessor(_propertyTypes); dp.ExportToStream(mol, writer, out message); } writer.Flush(); } result = Encoding.ASCII.GetString(stream.ToArray()); return(result); }
public Model Import(object data) { Model model = null; if (data != null) { string dataAsString = (string)data; if (!dataAsString.Contains("v3000") && !dataAsString.Contains("V3000")) { model = new Model(); LineNumber = 0; // Convert incoming string to a stream MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write(dataAsString); writer.Flush(); stream.Position = 0; StreamReader sr = new StreamReader(stream); Molecule molecule = null; SdfState state = SdfState.Null; string message = null; while (!sr.EndOfStream) { switch (state) { case SdfState.Null: case SdfState.EndOfData: molecule = new Molecule(); CtabProcessor pct = new CtabProcessor(); state = pct.ImportFromStream(sr, molecule, out message); if (state == SdfState.Error) { model.GeneralErrors.Add(message); } //Ensure we add the molecule after it's populated model.Molecules.Add(molecule); if (model.Molecules.Count >= 16) { model.GeneralErrors.Add("This file has greater than 16 structures!"); sr.ReadToEnd(); } break; case SdfState.EndOfCtab: DataProcessor dp = new DataProcessor(_propertyTypes); state = dp.ImportFromStream(sr, molecule, out message); break; case SdfState.Error: // Swallow rest of stream sr.ReadToEnd(); break; case SdfState.Unsupported: // Swallow rest of stream sr.ReadToEnd(); break; } } // Can't use RebuildMolecules() as it trashes the formulae and labels //model.RebuildMolecules(); model.RefreshMolecules(); } } return(model); }