1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
 * MOAB, a Mesh-Oriented datABase, is a software component for creating,
 * storing and accessing finite element mesh data.
 *
 * Copyright 2004 Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
 * retains certain rights in this software.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 */

#include "moab/Core.hpp"

#include "moab/ReaderWriterSet.hpp"
#include "moab/ReaderIface.hpp"
#include "moab/WriterIface.hpp"

#include "ReadVtk.hpp"
#include "ReadSTL.hpp"
#include "ReadGmsh.hpp"
#include "ReadIDEAS.hpp"
#include "ReadMCNP5.hpp"
#include "ReadOBJ.hpp"
#include "ReadNASTRAN.hpp"
#include "ReadRTT.hpp"
#include "ReadABAQUS.hpp"
#include "ReadSms.hpp"
#include "Tqdcfr.hpp"
#include "ReadTetGen.hpp"
#include "ReadSmf.hpp"
#include "ReadTemplate.hpp"
#ifdef MOAB_HAVE_CGM
#include "ReadCGM.hpp"
#endif

#include "WriteAns.hpp"
#include "WriteVtk.hpp"
#include "WriteGMV.hpp"
#include "WriteSTL.hpp"
#include "WriteGmsh.hpp"
#include "WriteSmf.hpp"
#include "WriteTemplate.hpp"

#ifdef MOAB_HAVE_NETCDF
#include "ReadNCDF.hpp"
#include "WriteNCDF.hpp"
#include "WriteNC.hpp"
#include "WriteSLAC.hpp"
#include "ReadNC.hpp"
#endif

// 2nd include of ReadNC in case we have pnetcdf and not netcdf
#if defined( MOAB_HAVE_PNETCDF ) && !defined( MOAB_HAVE_NETCDF )
#include "ReadNC.hpp"
#endif

#ifdef MOAB_HAVE_CGNS
#include "ReadCGNS.hpp"
#include "WriteCGNS.hpp"
#endif

#ifdef MOAB_HAVE_CCMIO
#include "ReadCCMIO.hpp"
#include "WriteCCMIO.hpp"
#endif

#ifdef MOAB_HAVE_DAMSEL
#include "WriteDamsel.hpp"
#include "ReadDamsel.hpp"
#endif

#ifdef MOAB_HAVE_HDF5
#include "ReadHDF5.hpp"
#ifdef MOAB_HAVE_HDF5_PARALLEL
#include "WriteHDF5Parallel.hpp"
#else
#include "WriteHDF5.hpp"
#endif
#endif

#include <algorithm>

namespace moab
{

ReaderWriterSet::ReaderWriterSet( Core* mdb ) : mbCore( mdb )
{
#ifdef MOAB_HAVE_HDF5
    const char* hdf5_sufxs[] = { "h5m", "mhdf", NULL };
#ifdef MOAB_HAVE_HDF5_PARALLEL
    register_factory( ReadHDF5::factory, WriteHDF5Parallel::factory, "MOAB native (HDF5)", hdf5_sufxs, "MOAB" );
#else
    register_factory( ReadHDF5::factory, WriteHDF5::factory, "MOAB native (HDF5)", hdf5_sufxs, "MOAB" );
#endif
#endif

#ifdef MOAB_HAVE_NETCDF
    const char* exo_sufxs[] = { "exo", "exoII", "exo2", "g", "gen", NULL };
    register_factory( ReadNCDF::factory, WriteNCDF::factory, "Exodus II", exo_sufxs, "EXODUS" );
    register_factory( ReadNC::factory, WriteNC::factory, "Climate NC", "nc", "NC" );
#endif

#ifdef MOAB_HAVE_CGNS
    const char* cgns_sufxs[] = { "cgns", NULL };
    register_factory( ReadCGNS::factory, WriteCGNS::factory, "CGNS", cgns_sufxs, "CGNS" );
#endif

    register_factory( ReadIDEAS::factory, NULL, "IDEAS format", "unv", "UNV" );

    register_factory( ReadMCNP5::factory, NULL, "MCNP5 format", "meshtal", "MESHTAL" );

    const char* nastran_sufxs[] = { "nas", "bdf", NULL };
    register_factory( ReadNASTRAN::factory, NULL, "NASTRAN format", nastran_sufxs, "NAS" );

    register_factory( ReadABAQUS::factory, NULL, "ABAQUS INP mesh format", "abq", "Abaqus mesh" );

    register_factory( ReadRTT::factory, NULL, "RTT Mesh Format", "rtt", "Atilla RTT Mesh" );

    register_factory( ReadVtk::factory, WriteVtk::factory, "Kitware VTK", "vtk", "VTK" );

    register_factory( ReadOBJ::factory, NULL, "OBJ mesh format", "obj", "OBJ mesh" );

    register_factory( ReadSms::factory, NULL, "RPI SMS", "sms", "SMS" );

    register_factory( Tqdcfr::factory, NULL, "Cubit", "cub", "CUBIT" );

    register_factory( ReadSmf::factory, WriteSmf::factory, "QSlim format", "smf", "SMF" );
#ifdef MOAB_HAVE_CGM_FACET
    const char* facet_sufxs[] = { "facet", NULL };
    register_factory( ReadCGM::factory, NULL, "Facet Engine Solid Model", facet_sufxs, "facet" );
#endif
#ifdef MOAB_HAVE_CGM_OCC
    const char* occ_sufxs[]  = { "brep", "occ", NULL };
    const char* step_sufxs[] = { "step", "stp", NULL };
    const char* iges_sufxs[] = { "iges", "igs", NULL };
    register_factory( ReadCGM::factory, NULL, "OpenCascade solid model", occ_sufxs, "OCC" );
    register_factory( ReadCGM::factory, NULL, "STEP B-Rep exchange", step_sufxs, "STEP" );
    register_factory( ReadCGM::factory, NULL, "IGES B-Rep exchange", iges_sufxs, "IGES" );
#endif

#ifdef MOAB_HAVE_NETCDF
    register_factory( NULL, WriteSLAC::factory, "SLAC", "slac", "SLAC" );
#endif

#ifdef MOAB_HAVE_CCMIO
    const char* ccmio_sufxs[] = { "ccm", "ccmg", NULL };
    register_factory( ReadCCMIO::factory, WriteCCMIO::factory, "CCMIO files", ccmio_sufxs, "CCMIO" );
#endif

#ifdef MOAB_HAVE_DAMSEL
    const char* damsel_sufxs[] = { "h5", NULL };
    register_factory( ReadDamsel::factory, WriteDamsel::factory, "Damsel files", damsel_sufxs, "DAMSEL" );
#endif

    register_factory( NULL, WriteGMV::factory, "GMV", "gmv", "GMV" );

    register_factory( NULL, WriteAns::factory, "Ansys", "ans", "ANSYS" );

    const char* gmsh_sufxs[] = { "msh", "gmsh", NULL };
    register_factory( ReadGmsh::factory, WriteGmsh::factory, "Gmsh mesh file", gmsh_sufxs, "GMSH" );

    register_factory( ReadSTL::factory, WriteSTL::factory, "Stereo Lithography File (STL)", "stl", "STL" );

    const char* tetgen_sufxs[] = { "node", "ele", "face", "edge", NULL };
    register_factory( ReadTetGen::factory, 0, "TetGen output files", tetgen_sufxs, "TETGEN" );

    const char* template_sufxs[] = { NULL };
    register_factory( ReadTemplate::factory, WriteTemplate::factory, "Template input files", template_sufxs,
                      "TEMPLATE" );
}

ReaderWriterSet::~ReaderWriterSet() {}

ErrorCode ReaderWriterSet::register_factory( reader_factory_t reader,
                                             writer_factory_t writer,
                                             const char* description,
                                             const char* const* extensions,
                                             const char* name )
{
    if( !reader && !writer ) return MB_FAILURE;

    // check for duplicate names
    iterator h = handler_by_name( name );
    if( h != end() )
    {
        MB_SET_ERR( MB_FAILURE, "Conflicting string name for file formats: \"" << name << "\"" );
    }

    // count extensions and check for duplicates
    const char* const* iter;
    for( iter = extensions; *iter; ++iter )
    {
        h = handler_from_extension( *iter );
        if( h != end() )
        {
            if( NULL != reader && h->have_reader() )
                MB_SET_ERR( MB_FAILURE, "Conflicting readers for file extension \""
                                            << *iter << "\": \"" << h->description() << "\" and \"" << description
                                            << "\"." );
            else if( NULL != writer && h->have_writer() )
                MB_SET_ERR( MB_FAILURE, "Conflicting writers for file extension \""
                                            << *iter << "\": \"" << h->description() << "\" and \"" << description
                                            << "\"." );
        }
    }
    handlerList.push_back( Handler( reader, writer, name, description, extensions, iter - extensions ) );
    return MB_SUCCESS;
}

ErrorCode ReaderWriterSet::register_factory( reader_factory_t reader,
                                             writer_factory_t writer,
                                             const char* description,
                                             const char* extension,
                                             const char* name )
{
    const char* extensions[2] = { extension, NULL };
    return register_factory( reader, writer, description, extensions, name );
}

ReaderIface* ReaderWriterSet::get_file_extension_reader( const std::string& filename ) const
{
    std::string ext  = extension_from_filename( filename );
    iterator handler = handler_from_extension( ext, true, false );
    return handler == end() ? NULL : handler->make_reader( mbCore );
}

WriterIface* ReaderWriterSet::get_file_extension_writer( const std::string& filename ) const<--- The function 'get_file_extension_writer' is never used.
{
    std::string ext  = extension_from_filename( filename );
    iterator handler = handler_from_extension( ext, false, true );
    return handler == end() ? NULL : handler->make_writer( mbCore );
}

std::string ReaderWriterSet::extension_from_filename( const std::string& filename )
{
    std::string::size_type idx   = filename.find_last_of( "." );
    std::string::size_type idirx = filename.find_last_of( "\\/" );

    if( idx == std::string::npos ) return std::string( "" );
    if( ( idirx != std::string::npos ) && ( idirx > idx ) ) return std::string( "" );
    return filename.substr( idx + 1 );
}

ReaderWriterSet::Handler::Handler( reader_factory_t read_f,
                                   writer_factory_t write_f,
                                   const char* nm,
                                   const char* desc,
                                   const char* const* ext,
                                   int num_ext )
    : mReader( read_f ), mWriter( write_f ), mName( nm ), mDescription( desc ), mExtensions( num_ext )
{
    for( int i = 0; i < num_ext; ++i )
        mExtensions[i] = ext[i];
}

#ifdef WIN32
#define strcasecmp( A, B ) _stricmp( A, B )
#endif

ReaderWriterSet::iterator ReaderWriterSet::handler_from_extension( const std::string& ext,
                                                                   bool with_reader,
                                                                   bool with_writer ) const
{
    iterator iter;
    std::vector< std::string >::const_iterator siter;

    // try case-sensitive compare
    for( iter = begin(); iter != end(); ++iter )
    {
        if( ( with_reader && !iter->have_reader() ) || ( with_writer && !iter->have_writer() ) ) continue;

        for( siter = iter->mExtensions.begin(); siter != iter->mExtensions.end(); ++siter )
            if( *siter == ext ) return iter;
    }

    // try case-insensitive compare
    for( iter = begin(); iter != end(); ++iter )
    {
        if( ( with_reader && !iter->have_reader() ) || ( with_writer && !iter->have_writer() ) ) continue;

        for( siter = iter->mExtensions.begin(); siter != iter->mExtensions.end(); ++siter )
            if( 0 == strcasecmp( siter->c_str(), ext.c_str() ) ) return iter;
    }

    return end();
}

bool ReaderWriterSet::Handler::reads_extension( const char* ext ) const
{
    if( !have_reader() ) return false;

    std::vector< std::string >::const_iterator siter;
    for( siter = mExtensions.begin(); siter != mExtensions.end(); ++siter )
        if( !( *siter ).compare( ext ) )
            return true;
        else if( 0 == strcasecmp( siter->c_str(), ext ) )
            return true;

    return false;
}

bool ReaderWriterSet::Handler::writes_extension( const char* ext ) const
{
    if( !have_writer() ) return false;

    std::vector< std::string >::const_iterator siter;
    for( siter = mExtensions.begin(); siter != mExtensions.end(); ++siter )
        if( !( *siter ).compare( ext ) )
            return true;
        else if( 0 == strcasecmp( siter->c_str(), ext ) )
            return true;

    return false;
}

ReaderWriterSet::iterator ReaderWriterSet::handler_by_name( const char* nm ) const
{
    return std::find( begin(), end(), nm );
}

bool ReaderWriterSet::Handler::operator==( const char* nm ) const
{
    // do case-insensitive comparison
    std::string::const_iterator siter = mName.begin();
    for( ; *nm; ++nm, ++siter )
        if( siter == mName.end() || tolower( *nm ) != tolower( *siter ) ) return false;
    return *nm == '\0';
}

}  // namespace moab