My Regen program handles combustion and atmospheric chemistry mechanisms consisting of a large
number of elementary (usually radical) reactions. It imported molecules from MDL mol files, but
with a small amount of new code it can now import from any file type for which a OBFormat class
has been written. I'm not interested in using OBMol here, since the program uses its own
representation of molecules, so it is well insulated from it.
This code fragment is for MFC on Visual C++ 6. It is compiled with obconv.lib and needs
obconv.dll, obdll.dll and obformats.obf (and/or other *.obf format files) at runtime. Updates
will be achieved by changing only these runtime files
#include "obconversion.h" // obmol.h not needed
...
{
...
CString Filter = OBFilterString("MOL");
if(Filter.IsEmpty())
Filter="Mol(*.mol)|*.mol|All Files(*.*)|*.*||";
CFileDialog dlg(true,NULL,
NULL,
OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,
Filter );
dlg.m_ofn.lpstrTitle="Import Species";
try
{
if(dlg.DoModal()!=IDOK)
return;
std::ifstream ifs(dlg.m_ofn.lpstrFile);
if(!ifs)
throw "Trouble opening file";
std::istream* pIn = &ifs;
std::stringstream newstream;
if(stricmp(strrchr(dlg.m_ofn.lpstrFile,'.'),".MOL")) //do not convert mol files
{
//Use OpenBabel to convert other file formats to mol
OpenBabel::OBConversion conv;
OpenBabel::OBFormat* inFormat = conv.FormatFromExt(dlg.m_ofn.lpstrFile);
OpenBabel::OBFormat* outFormat = conv.FindFormat("MOL");
if(inFormat && outFormat)
{
conv.SetInAndOutFormats(inFormat,outFormat);
conv.Convert(pIn,&newstream);
pIn=&newstream;
}
else
{
AfxMessageBox("File format not recognized");
return;
}
}
AddSpFromMolFile(*pIn);
}
catch(...)
{
...
}
}
CString OBFilterString(const char* BaseFormatID, bool bOutput)
{
//Returns the filter string to be used with CFileDialog with three sections:
//Base format, All Chemical Formats, All Files
//If BaseFormatID==NULL the first section is omitted
//If the Base Format is not recognised returns empty CString
unsigned int Select = bOutput ? NOTWRITABLE : NOTREADABLE;
CString Filter;
OpenBabel::OBConversion conv; //ensure there is at least one instance to populate list of formats
OpenBabel::OBFormat* pFormat;
if(BaseFormatID)
{
pFormat = OpenBabel::OBConversion::FindFormat(BaseFormatID);
if(!pFormat) return Filter;
CString txt = pFormat->Description();
Filter = txt;
int eol = txt.Find('\n');
Filter = Filter.Left(eol);
Filter.TrimLeft(" :-\t");
Filter = Filter + " (*." + BaseFormatID + ")|*." + BaseFormatID + "|";
}
Filter += "All Chemical Formats|*.";
char* str=NULL;
OpenBabel::Formatpos pos;
while(OpenBabel::OBConversion::GetNextFormat(pos,str,pFormat))
{
if(!str || !pFormat) break; //no formats available
CString txt=str;
if(!(pFormat->Flags() & Select))
{
Filter += txt.Left(txt.Find(" "));
Filter += ";*.";
}
}
Filter = Filter.Left(Filter.GetLength()-3); //remove unneeded ;*.
Filter += "|AllFiles(*.*)|*.*||";
return Filter;
}