/
How to check if a file contains a mesh
How to check if a file contains a mesh
At times it might be useful to check if a file contains a mesh. If it doesn’t, appropriate corrective action can be taken (such as fetching the mesh from the appropriate file before opening the mesh-less file).
The following code is an example of how this check can be done:
/*----------------------------------------------------------------------
open file if it contains a mesh
----------------------------------------------------------------------*/
//--------------------------------------------------------------------------------------------------
/// \brief open file if it contains a mesh
///
/// \param datafun Pointer to datafun object that has been initialized, but not open.
/// If the file contains a mesh, the datafun object will be opened.
/// \param filename File name.
/// \param libraryType VdmTools library type.
///
/// \return
/// The function returns a boolean that is true if the file contained a mesh.
/// error flag, \a errorflag.
///
/// If the file contains a mesh, the file will be opened in the vdm_DataFun object and
/// the function will return true. Otherwise, the function will return false. This
/// functions sets the error handler to the default error handler (vut_ErrorHandler).
/// This functions assumes that the file is correct and that the only potential reason
/// for an error is the absence of a mesh in the file.
//--------------------------------------------------------------------------------------------------
bool openFileIfContainsMesh(vdm_DataFun* datafun, Vchar* filename, Vint libraryType)
{
/*
Setting a silent error handler function in order to avoid sending
the potential SYS_ERROR_OPERATION to standard output.
*/
using ErrorHandler = void(*)(const char*, Vint, const char*);
ErrorHandler silentErrorHandler = [](const char*, Vint, const char*) { ; };
vut_ErrorSetHandler(silentErrorHandler);
/*
Trying to open the file.
*/
vdm_DataFunOpen(datafun, 0, filename, libraryType);
/*
Going back to using the default error handler.
*/
vut_ErrorSetHandler(vut_ErrorHandler);
/*
Checking if the file contained a mesh.
*/
const Vint errorCode = vdm_DataFunError(datafun);
bool containsMesh;
if (errorCode == SYS_ERROR_OPERATION) {
containsMesh = false;
return containsMesh;
}
containsMesh = true;
return containsMesh;
}
int main() {
Vint libraryType = SYS_NASTRAN_OUTPUT2;
char filename[] = "meshlessfile.op2";
vdm_DataFun* datafun = vdm_DataFunBegin();
vdm_NASLib* naslib = vdm_NASLibBegin();
vdm_NASLibDataFun(naslib, datafun);
vdm_DataFunSetStatus(datafun, VDM_STATUS_OLD);
const bool fileContainsMesh = openFileIfContainsMesh(datafun, filename, libraryType);
if (fileContainsMesh) {
/* Use your file */
}
else {
/* Handle file without mesh */
}
vdm_DataFunClose(datafun);
vdm_NASLibEnd(naslib);
vdm_DataFunEnd(datafun);
return 0;
}
, multiple selections available,