Mesh Oriented datABase  (version 5.5.0)
An array-based unstructured mesh library
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 #ifdef MOAB_HAVE_CGM
37 #include "ReadCGM.hpp"
38 #endif
39 
40 #include "WriteAns.hpp"
41 #include "WriteVtk.hpp"
42 #include "WriteGMV.hpp"
43 #include "WriteSTL.hpp"
44 #include "WriteGmsh.hpp"
45 #include "WriteSmf.hpp"
46 #include "WriteTemplate.hpp"
47 
48 #ifdef MOAB_HAVE_NETCDF
49 #include "ReadNCDF.hpp"
50 #include "WriteNCDF.hpp"
51 #include "WriteNC.hpp"
52 #include "WriteSLAC.hpp"
53 #include "ReadNC.hpp"
54 #endif
55 
56 // 2nd include of ReadNC in case we have pnetcdf and not netcdf
57 #if defined( MOAB_HAVE_PNETCDF ) && !defined( MOAB_HAVE_NETCDF )
58 #include "ReadNC.hpp"
59 #endif
60 
61 #ifdef MOAB_HAVE_CGNS
62 #include "ReadCGNS.hpp"
63 #include "WriteCGNS.hpp"
64 #endif
65 
66 #ifdef MOAB_HAVE_CCMIO
67 #include "ReadCCMIO.hpp"
68 #include "WriteCCMIO.hpp"
69 #endif
70 
71 #ifdef MOAB_HAVE_DAMSEL
72 #include "WriteDamsel.hpp"
73 #include "ReadDamsel.hpp"
74 #endif
75 
76 #ifdef MOAB_HAVE_HDF5
77 #include "ReadHDF5.hpp"
78 #ifdef MOAB_HAVE_HDF5_PARALLEL
79 #include "WriteHDF5Parallel.hpp"
80 #else
81 #include "WriteHDF5.hpp"
82 #endif
83 #endif
84 
85 #include <algorithm>
86 
87 namespace moab
88 {
89 
90 ReaderWriterSet::ReaderWriterSet( Core* mdb ) : mbCore( mdb )
91 {
92 #ifdef MOAB_HAVE_HDF5
93  const char* hdf5_sufxs[] = { "h5m", "mhdf", NULL };
94 #ifdef MOAB_HAVE_HDF5_PARALLEL
95  register_factory( ReadHDF5::factory, WriteHDF5Parallel::factory, "MOAB native (HDF5)", hdf5_sufxs, "MOAB" );
96 #else
97  register_factory( ReadHDF5::factory, WriteHDF5::factory, "MOAB native (HDF5)", hdf5_sufxs, "MOAB" );
98 #endif
99 #endif
100 
101 #ifdef MOAB_HAVE_NETCDF
102  const char* exo_sufxs[] = { "exo", "exoII", "exo2", "g", "gen", NULL };
103  register_factory( ReadNCDF::factory, WriteNCDF::factory, "Exodus II", exo_sufxs, "EXODUS" );
104  register_factory( ReadNC::factory, WriteNC::factory, "Climate NC", "nc", "NC" );
105 #endif
106 
107 #ifdef MOAB_HAVE_CGNS
108  const char* cgns_sufxs[] = { "cgns", NULL };
109  register_factory( ReadCGNS::factory, WriteCGNS::factory, "CGNS", cgns_sufxs, "CGNS" );
110 #endif
111 
112  register_factory( ReadIDEAS::factory, NULL, "IDEAS format", "unv", "UNV" );
113 
114  register_factory( ReadMCNP5::factory, NULL, "MCNP5 format", "meshtal", "MESHTAL" );
115 
116  const char* nastran_sufxs[] = { "nas", "bdf", NULL };
117  register_factory( ReadNASTRAN::factory, NULL, "NASTRAN format", nastran_sufxs, "NAS" );
118 
119  register_factory( ReadABAQUS::factory, NULL, "ABAQUS INP mesh format", "abq", "Abaqus mesh" );
120 
121  register_factory( ReadRTT::factory, NULL, "RTT Mesh Format", "rtt", "Atilla RTT Mesh" );
122 
123  register_factory( ReadVtk::factory, WriteVtk::factory, "Kitware VTK", "vtk", "VTK" );
124 
125  register_factory( ReadOBJ::factory, NULL, "OBJ mesh format", "obj", "OBJ mesh" );
126 
127  register_factory( ReadSms::factory, NULL, "RPI SMS", "sms", "SMS" );
128 
129  register_factory( Tqdcfr::factory, NULL, "Cubit", "cub", "CUBIT" );
130 
131  register_factory( ReadSmf::factory, WriteSmf::factory, "QSlim format", "smf", "SMF" );
132 #ifdef MOAB_HAVE_CGM_FACET
133  const char* facet_sufxs[] = { "facet", NULL };
134  register_factory( ReadCGM::factory, NULL, "Facet Engine Solid Model", facet_sufxs, "facet" );
135 #endif
136 #ifdef MOAB_HAVE_CGM_OCC
137  const char* occ_sufxs[] = { "brep", "occ", NULL };
138  const char* step_sufxs[] = { "step", "stp", NULL };
139  const char* iges_sufxs[] = { "iges", "igs", NULL };
140  register_factory( ReadCGM::factory, NULL, "OpenCascade solid model", occ_sufxs, "OCC" );
141  register_factory( ReadCGM::factory, NULL, "STEP B-Rep exchange", step_sufxs, "STEP" );
142  register_factory( ReadCGM::factory, NULL, "IGES B-Rep exchange", iges_sufxs, "IGES" );
143 #endif
144 
145 #ifdef MOAB_HAVE_NETCDF
146  register_factory( NULL, WriteSLAC::factory, "SLAC", "slac", "SLAC" );
147 #endif
148 
149 #ifdef MOAB_HAVE_CCMIO
150  const char* ccmio_sufxs[] = { "ccm", "ccmg", NULL };
151  register_factory( ReadCCMIO::factory, WriteCCMIO::factory, "CCMIO files", ccmio_sufxs, "CCMIO" );
152 #endif
153 
154 #ifdef MOAB_HAVE_DAMSEL
155  const char* damsel_sufxs[] = { "h5", NULL };
156  register_factory( ReadDamsel::factory, WriteDamsel::factory, "Damsel files", damsel_sufxs, "DAMSEL" );
157 #endif
158 
159  register_factory( NULL, WriteGMV::factory, "GMV", "gmv", "GMV" );
160 
161  register_factory( NULL, WriteAns::factory, "Ansys", "ans", "ANSYS" );
162 
163  const char* gmsh_sufxs[] = { "msh", "gmsh", NULL };
164  register_factory( ReadGmsh::factory, WriteGmsh::factory, "Gmsh mesh file", gmsh_sufxs, "GMSH" );
165 
166  register_factory( ReadSTL::factory, WriteSTL::factory, "Stereo Lithography File (STL)", "stl", "STL" );
167 
168  const char* tetgen_sufxs[] = { "node", "ele", "face", "edge", NULL };
169  register_factory( ReadTetGen::factory, 0, "TetGen output files", tetgen_sufxs, "TETGEN" );
170 
171  const char* template_sufxs[] = { NULL };
172  register_factory( ReadTemplate::factory, WriteTemplate::factory, "Template input files", template_sufxs,
173  "TEMPLATE" );
174 }
175 
177 
179  writer_factory_t writer,
180  const char* description,
181  const char* const* extensions,
182  const char* name )
183 {
184  if( !reader && !writer ) return MB_FAILURE;
185 
186  // check for duplicate names
187  iterator h = handler_by_name( name );
188  if( h != end() )
189  {
190  MB_SET_ERR( MB_FAILURE, "Conflicting string name for file formats: \"" << name << "\"" );
191  }
192 
193  // count extensions and check for duplicates
194  const char* const* iter;
195  for( iter = extensions; *iter; ++iter )
196  {
197  h = handler_from_extension( *iter );
198  if( h != end() )
199  {
200  if( NULL != reader && h->have_reader() )
201  MB_SET_ERR( MB_FAILURE, "Conflicting readers for file extension \""
202  << *iter << "\": \"" << h->description() << "\" and \"" << description
203  << "\"." );
204  else if( NULL != writer && h->have_writer() )
205  MB_SET_ERR( MB_FAILURE, "Conflicting writers for file extension \""
206  << *iter << "\": \"" << h->description() << "\" and \"" << description
207  << "\"." );
208  }
209  }
210  handlerList.push_back( Handler( reader, writer, name, description, extensions, iter - extensions ) );
211  return MB_SUCCESS;
212 }
213 
215  writer_factory_t writer,
216  const char* description,
217  const char* extension,
218  const char* name )
219 {
220  const char* extensions[2] = { extension, NULL };
221  return register_factory( reader, writer, description, extensions, name );
222 }
223 
224 ReaderIface* ReaderWriterSet::get_file_extension_reader( const std::string& filename ) const
225 {
226  std::string ext = extension_from_filename( filename );
227  iterator handler = handler_from_extension( ext, true, false );
228  return handler == end() ? NULL : handler->make_reader( mbCore );
229 }
230 
231 WriterIface* ReaderWriterSet::get_file_extension_writer( const std::string& filename ) const
232 {
233  std::string ext = extension_from_filename( filename );
234  iterator handler = handler_from_extension( ext, false, true );
235  return handler == end() ? NULL : handler->make_writer( mbCore );
236 }
237 
238 std::string ReaderWriterSet::extension_from_filename( const std::string& filename )
239 {
240  std::string::size_type idx = filename.find_last_of( "." );
241  std::string::size_type idirx = filename.find_last_of( "\\/" );
242 
243  if( idx == std::string::npos ) return std::string( "" );
244  if( ( idirx != std::string::npos ) && ( idirx > idx ) ) return std::string( "" );
245  return filename.substr( idx + 1 );
246 }
247 
248 ReaderWriterSet::Handler::Handler( reader_factory_t read_f,
249  writer_factory_t write_f,
250  const char* nm,
251  const char* desc,
252  const char* const* ext,
253  int num_ext )
254  : mReader( read_f ), mWriter( write_f ), mName( nm ), mDescription( desc ), mExtensions( num_ext )
255 {
256  for( int i = 0; i < num_ext; ++i )
257  mExtensions[i] = ext[i];
258 }
259 
260 #ifdef WIN32
261 #define strcasecmp( A, B ) _stricmp( A, B )
262 #endif
263 
265  bool with_reader,
266  bool with_writer ) const
267 {
268  iterator iter;
269  std::vector< std::string >::const_iterator siter;
270 
271  // try case-sensitive compare
272  for( iter = begin(); iter != end(); ++iter )
273  {
274  if( ( with_reader && !iter->have_reader() ) || ( with_writer && !iter->have_writer() ) ) continue;
275 
276  for( siter = iter->mExtensions.begin(); siter != iter->mExtensions.end(); ++siter )
277  if( *siter == ext ) return iter;
278  }
279 
280  // try case-insensitive compare
281  for( iter = begin(); iter != end(); ++iter )
282  {
283  if( ( with_reader && !iter->have_reader() ) || ( with_writer && !iter->have_writer() ) ) continue;
284 
285  for( siter = iter->mExtensions.begin(); siter != iter->mExtensions.end(); ++siter )
286  if( 0 == strcasecmp( siter->c_str(), ext.c_str() ) ) return iter;
287  }
288 
289  return end();
290 }
291 
292 bool ReaderWriterSet::Handler::reads_extension( const char* ext ) const
293 {
294  if( !have_reader() ) return false;
295 
296  std::vector< std::string >::const_iterator siter;
297  for( siter = mExtensions.begin(); siter != mExtensions.end(); ++siter )
298  if( !( *siter ).compare( ext ) )
299  return true;
300  else if( 0 == strcasecmp( siter->c_str(), ext ) )
301  return true;
302 
303  return false;
304 }
305 
306 bool ReaderWriterSet::Handler::writes_extension( const char* ext ) const
307 {
308  if( !have_writer() ) return false;
309 
310  std::vector< std::string >::const_iterator siter;
311  for( siter = mExtensions.begin(); siter != mExtensions.end(); ++siter )
312  if( !( *siter ).compare( ext ) )
313  return true;
314  else if( 0 == strcasecmp( siter->c_str(), ext ) )
315  return true;
316 
317  return false;
318 }
319 
321 {
322  return std::find( begin(), end(), nm );
323 }
324 
325 bool ReaderWriterSet::Handler::operator==( const char* nm ) const
326 {
327  // do case-insensitive comparison
328  std::string::const_iterator siter = mName.begin();
329  for( ; *nm; ++nm, ++siter )
330  if( siter == mName.end() || tolower( *nm ) != tolower( *siter ) ) return false;
331  return *nm == '\0';
332 }
333 
334 } // namespace moab