Loading [MathJax]/extensions/tex2jax.js
Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ReaderWriterSet.cpp
Go to the documentation of this file.
1 /** 2  * MOAB, a Mesh-Oriented datABase, is a software component for creating, 3  * storing and accessing finite element mesh data. 4  * 5  * Copyright 2004 Sandia Corporation. Under the terms of Contract 6  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 7  * retains certain rights in this software. 8  * 9  * This library is free software; you can redistribute it and/or 10  * modify it under the terms of the GNU Lesser General Public 11  * License as published by the Free Software Foundation; either 12  * version 2.1 of the License, or (at your option) any later version. 13  * 14  */ 15  16 #include "moab/Core.hpp" 17  18 #include "moab/ReaderWriterSet.hpp" 19 #include "moab/ReaderIface.hpp" 20 #include "moab/WriterIface.hpp" 21  22 #include "ReadVtk.hpp" 23 #include "ReadSTL.hpp" 24 #include "ReadGmsh.hpp" 25 #include "ReadIDEAS.hpp" 26 #include "ReadMCNP5.hpp" 27 #include "ReadOBJ.hpp" 28 #include "ReadNASTRAN.hpp" 29 #include "ReadRTT.hpp" 30 #include "ReadABAQUS.hpp" 31 #include "ReadSms.hpp" 32 #include "Tqdcfr.hpp" 33 #include "ReadTetGen.hpp" 34 #include "ReadSmf.hpp" 35 #include "ReadTemplate.hpp" 36  37 #include "WriteAns.hpp" 38 #include "WriteVtk.hpp" 39 #include "WriteGMV.hpp" 40 #include "WriteSTL.hpp" 41 #include "WriteGmsh.hpp" 42 #include "WriteSmf.hpp" 43 #include "WriteTemplate.hpp" 44  45 #ifdef MOAB_HAVE_NETCDF 46 #include "ReadNCDF.hpp" 47 #include "WriteNCDF.hpp" 48 #include "WriteNC.hpp" 49 #include "WriteSLAC.hpp" 50 #include "ReadNC.hpp" 51 #endif 52  53 // 2nd include of ReadNC in case we have pnetcdf and not netcdf 54 #if defined( MOAB_HAVE_PNETCDF ) && !defined( MOAB_HAVE_NETCDF ) 55 #include "ReadNC.hpp" 56 #endif 57  58 #ifdef MOAB_HAVE_CGNS 59 #include "ReadCGNS.hpp" 60 #include "WriteCGNS.hpp" 61 #endif 62  63 #ifdef MOAB_HAVE_CCMIO 64 #include "ReadCCMIO.hpp" 65 #include "WriteCCMIO.hpp" 66 #endif 67  68 #ifdef MOAB_HAVE_DAMSEL 69 #include "WriteDamsel.hpp" 70 #include "ReadDamsel.hpp" 71 #endif 72  73 #ifdef MOAB_HAVE_HDF5 74 #include "ReadHDF5.hpp" 75 #ifdef MOAB_HAVE_HDF5_PARALLEL 76 #include "WriteHDF5Parallel.hpp" 77 #else 78 #include "WriteHDF5.hpp" 79 #endif 80 #endif 81  82 #include <algorithm> 83  84 namespace moab 85 { 86  87 ReaderWriterSet::ReaderWriterSet( Core* mdb ) : mbCore( mdb ) 88 { 89 #ifdef MOAB_HAVE_HDF5 90  const char* hdf5_sufxs[] = { "h5m", "mhdf", NULL }; 91 #ifdef MOAB_HAVE_HDF5_PARALLEL 92  register_factory( ReadHDF5::factory, WriteHDF5Parallel::factory, "MOAB native (HDF5)", hdf5_sufxs, "MOAB" ); 93 #else 94  register_factory( ReadHDF5::factory, WriteHDF5::factory, "MOAB native (HDF5)", hdf5_sufxs, "MOAB" ); 95 #endif 96 #endif 97  98 #ifdef MOAB_HAVE_NETCDF 99  const char* exo_sufxs[] = { "exo", "exoII", "exo2", "g", "gen", NULL }; 100  register_factory( ReadNCDF::factory, WriteNCDF::factory, "Exodus II", exo_sufxs, "EXODUS" ); 101  register_factory( ReadNC::factory, WriteNC::factory, "Climate NC", "nc", "NC" ); 102 #endif 103  104 #ifdef MOAB_HAVE_CGNS 105  const char* cgns_sufxs[] = { "cgns", NULL }; 106  register_factory( ReadCGNS::factory, WriteCGNS::factory, "CGNS", cgns_sufxs, "CGNS" ); 107 #endif 108  109  register_factory( ReadIDEAS::factory, NULL, "IDEAS format", "unv", "UNV" ); 110  111  register_factory( ReadMCNP5::factory, NULL, "MCNP5 format", "meshtal", "MESHTAL" ); 112  113  const char* nastran_sufxs[] = { "nas", "bdf", NULL }; 114  register_factory( ReadNASTRAN::factory, NULL, "NASTRAN format", nastran_sufxs, "NAS" ); 115  116  register_factory( ReadABAQUS::factory, NULL, "ABAQUS INP mesh format", "abq", "Abaqus mesh" ); 117  118  register_factory( ReadRTT::factory, NULL, "RTT Mesh Format", "rtt", "Atilla RTT Mesh" ); 119  120  register_factory( ReadVtk::factory, WriteVtk::factory, "Kitware VTK", "vtk", "VTK" ); 121  122  register_factory( ReadOBJ::factory, NULL, "OBJ mesh format", "obj", "OBJ mesh" ); 123  124  register_factory( ReadSms::factory, NULL, "RPI SMS", "sms", "SMS" ); 125  126  register_factory( Tqdcfr::factory, NULL, "Cubit", "cub", "CUBIT" ); 127  128  register_factory( ReadSmf::factory, WriteSmf::factory, "QSlim format", "smf", "SMF" ); 129  130 #ifdef MOAB_HAVE_NETCDF 131  register_factory( NULL, WriteSLAC::factory, "SLAC", "slac", "SLAC" ); 132 #endif 133  134 #ifdef MOAB_HAVE_CCMIO 135  const char* ccmio_sufxs[] = { "ccm", "ccmg", NULL }; 136  register_factory( ReadCCMIO::factory, WriteCCMIO::factory, "CCMIO files", ccmio_sufxs, "CCMIO" ); 137 #endif 138  139 #ifdef MOAB_HAVE_DAMSEL 140  const char* damsel_sufxs[] = { "h5", NULL }; 141  register_factory( ReadDamsel::factory, WriteDamsel::factory, "Damsel files", damsel_sufxs, "DAMSEL" ); 142 #endif 143  144  register_factory( NULL, WriteGMV::factory, "GMV", "gmv", "GMV" ); 145  146  register_factory( NULL, WriteAns::factory, "Ansys", "ans", "ANSYS" ); 147  148  const char* gmsh_sufxs[] = { "msh", "gmsh", NULL }; 149  register_factory( ReadGmsh::factory, WriteGmsh::factory, "Gmsh mesh file", gmsh_sufxs, "GMSH" ); 150  151  register_factory( ReadSTL::factory, WriteSTL::factory, "Stereo Lithography File (STL)", "stl", "STL" ); 152  153  const char* tetgen_sufxs[] = { "node", "ele", "face", "edge", NULL }; 154  register_factory( ReadTetGen::factory, 0, "TetGen output files", tetgen_sufxs, "TETGEN" ); 155  156  const char* template_sufxs[] = { NULL }; 157  register_factory( ReadTemplate::factory, WriteTemplate::factory, "Template input files", template_sufxs, 158  "TEMPLATE" ); 159 } 160  161 ReaderWriterSet::~ReaderWriterSet() {} 162  163 ErrorCode ReaderWriterSet::register_factory( reader_factory_t reader, 164  writer_factory_t writer, 165  const char* description, 166  const char* const* extensions, 167  const char* name ) 168 { 169  if( !reader && !writer ) return MB_FAILURE; 170  171  // check for duplicate names 172  iterator h = handler_by_name( name ); 173  if( h != end() ) 174  { 175  MB_SET_ERR( MB_FAILURE, "Conflicting string name for file formats: \"" << name << "\"" ); 176  } 177  178  // count extensions and check for duplicates 179  const char* const* iter; 180  for( iter = extensions; *iter; ++iter ) 181  { 182  h = handler_from_extension( *iter ); 183  if( h != end() ) 184  { 185  if( NULL != reader && h->have_reader() ) 186  MB_SET_ERR( MB_FAILURE, "Conflicting readers for file extension \"" 187  << *iter << "\": \"" << h->description() << "\" and \"" << description 188  << "\"." ); 189  else if( NULL != writer && h->have_writer() ) 190  MB_SET_ERR( MB_FAILURE, "Conflicting writers for file extension \"" 191  << *iter << "\": \"" << h->description() << "\" and \"" << description 192  << "\"." ); 193  } 194  } 195  handlerList.push_back( Handler( reader, writer, name, description, extensions, iter - extensions ) ); 196  return MB_SUCCESS; 197 } 198  199 ErrorCode ReaderWriterSet::register_factory( reader_factory_t reader, 200  writer_factory_t writer, 201  const char* description, 202  const char* extension, 203  const char* name ) 204 { 205  const char* extensions[2] = { extension, NULL }; 206  return register_factory( reader, writer, description, extensions, name ); 207 } 208  209 ReaderIface* ReaderWriterSet::get_file_extension_reader( const std::string& filename ) const 210 { 211  std::string ext = extension_from_filename( filename ); 212  iterator handler = handler_from_extension( ext, true, false ); 213  return handler == end() ? NULL : handler->make_reader( mbCore ); 214 } 215  216 WriterIface* ReaderWriterSet::get_file_extension_writer( const std::string& filename ) const 217 { 218  std::string ext = extension_from_filename( filename ); 219  iterator handler = handler_from_extension( ext, false, true ); 220  return handler == end() ? NULL : handler->make_writer( mbCore ); 221 } 222  223 std::string ReaderWriterSet::extension_from_filename( const std::string& filename ) 224 { 225  std::string::size_type idx = filename.find_last_of( "." ); 226  std::string::size_type idirx = filename.find_last_of( "\\/" ); 227  228  if( idx == std::string::npos ) return std::string( "" ); 229  if( ( idirx != std::string::npos ) && ( idirx > idx ) ) return std::string( "" ); 230  return filename.substr( idx + 1 ); 231 } 232  233 ReaderWriterSet::Handler::Handler( reader_factory_t read_f, 234  writer_factory_t write_f, 235  const char* nm, 236  const char* desc, 237  const char* const* ext, 238  int num_ext ) 239  : mReader( read_f ), mWriter( write_f ), mName( nm ), mDescription( desc ), mExtensions( num_ext ) 240 { 241  for( int i = 0; i < num_ext; ++i ) 242  mExtensions[i] = ext[i]; 243 } 244  245 #ifdef WIN32 246 #define strcasecmp( A, B ) _stricmp( A, B ) 247 #endif 248  249 ReaderWriterSet::iterator ReaderWriterSet::handler_from_extension( const std::string& ext, 250  bool with_reader, 251  bool with_writer ) const 252 { 253  iterator iter; 254  std::vector< std::string >::const_iterator siter; 255  256  // try case-sensitive compare 257  for( iter = begin(); iter != end(); ++iter ) 258  { 259  if( ( with_reader && !iter->have_reader() ) || ( with_writer && !iter->have_writer() ) ) continue; 260  261  for( siter = iter->mExtensions.begin(); siter != iter->mExtensions.end(); ++siter ) 262  if( *siter == ext ) return iter; 263  } 264  265  // try case-insensitive compare 266  for( iter = begin(); iter != end(); ++iter ) 267  { 268  if( ( with_reader && !iter->have_reader() ) || ( with_writer && !iter->have_writer() ) ) continue; 269  270  for( siter = iter->mExtensions.begin(); siter != iter->mExtensions.end(); ++siter ) 271  if( 0 == strcasecmp( siter->c_str(), ext.c_str() ) ) return iter; 272  } 273  274  return end(); 275 } 276  277 bool ReaderWriterSet::Handler::reads_extension( const char* ext ) const 278 { 279  if( !have_reader() ) return false; 280  281  std::vector< std::string >::const_iterator siter; 282  for( siter = mExtensions.begin(); siter != mExtensions.end(); ++siter ) 283  if( !( *siter ).compare( ext ) ) 284  return true; 285  else if( 0 == strcasecmp( siter->c_str(), ext ) ) 286  return true; 287  288  return false; 289 } 290  291 bool ReaderWriterSet::Handler::writes_extension( const char* ext ) const 292 { 293  if( !have_writer() ) return false; 294  295  std::vector< std::string >::const_iterator siter; 296  for( siter = mExtensions.begin(); siter != mExtensions.end(); ++siter ) 297  if( !( *siter ).compare( ext ) ) 298  return true; 299  else if( 0 == strcasecmp( siter->c_str(), ext ) ) 300  return true; 301  302  return false; 303 } 304  305 ReaderWriterSet::iterator ReaderWriterSet::handler_by_name( const char* nm ) const 306 { 307  return std::find( begin(), end(), nm ); 308 } 309  310 bool ReaderWriterSet::Handler::operator==( const char* nm ) const 311 { 312  // do case-insensitive comparison 313  std::string::const_iterator siter = mName.begin(); 314  for( ; *nm; ++nm, ++siter ) 315  if( siter == mName.end() || tolower( *nm ) != tolower( *siter ) ) return false; 316  return *nm == '\0'; 317 } 318  319 } // namespace moab