MOAB: Mesh Oriented datABase  (version 5.4.1)
moab::WriteSmf Class Reference

#include <WriteSmf.hpp>

+ Inheritance diagram for moab::WriteSmf:
+ Collaboration diagram for moab::WriteSmf:

Public Member Functions

 WriteSmf (Interface *impl)
 Constructor.
virtual ~WriteSmf ()
 Destructor.
ErrorCode write_file (const char *file_name, const bool overwrite, const FileOptions &opts, const EntityHandle *output_list, const int num_sets, const std::vector< std::string > &qa_list, const Tag *tag_list=NULL, int num_tags=0, int export_dimension=3)
 writes out a file

Static Public Member Functions

static WriterIfacefactory (Interface *)

Private Attributes

InterfacembImpl
WriteUtilIfacewriteTool

Detailed Description

Definition at line 29 of file WriteSmf.hpp.


Constructor & Destructor Documentation

Constructor.

Definition at line 54 of file WriteSmf.cpp.

References moab::Interface::query_interface(), and writeTool.

Referenced by factory().

                                    : mbImpl( impl ), writeTool( 0 )
{
    assert( impl != NULL );
    impl->query_interface( writeTool );
}

Destructor.

Definition at line 60 of file WriteSmf.cpp.

References mbImpl, moab::Interface::release_interface(), and writeTool.


Member Function Documentation

Definition at line 49 of file WriteSmf.cpp.

References WriteSmf().

Referenced by moab::ReaderWriterSet::ReaderWriterSet().

{
    return new WriteSmf( iface );
}
ErrorCode moab::WriteSmf::write_file ( const char *  file_name,
const bool  overwrite,
const FileOptions opts,
const EntityHandle output_list,
const int  num_sets,
const std::vector< std::string > &  qa_list,
const Tag tag_list = NULL,
int  num_tags = 0,
int  export_dimension = 3 
) [virtual]

writes out a file

Implements moab::WriterIface.

Definition at line 65 of file WriteSmf.cpp.

References moab::Range::begin(), moab::WriteUtilIface::check_doesnt_exist(), moab::DEFAULT_PRECISION, moab::Range::end(), ErrorCode, moab::Interface::get_connectivity(), moab::Interface::get_coords(), moab::Interface::get_entities_by_type(), moab::FileOptions::get_int_option(), MB_FILE_WRITE_ERROR, MB_INVALID_SIZE, MB_MEMORY_ALLOCATION_FAILED, MB_SET_ERR, MB_SUCCESS, mbImpl, MBTRI, moab::Range::size(), and writeTool.

{
    ErrorCode rval;

    // Get precision for node coordinates
    int precision;
    if( MB_SUCCESS != opts.get_int_option( "PRECISION", precision ) ) precision = DEFAULT_PRECISION;

    // Honor overwrite flag
    if( !overwrite )
    {
        rval = writeTool->check_doesnt_exist( file_name );
        if( MB_SUCCESS != rval ) return rval;
    }

    // Create file
    std::ofstream file( file_name );
    if( !file )
    {
        MB_SET_ERR( MB_FILE_WRITE_ERROR, "Could not open file: " << file_name );
    }
    file.precision( precision );

    // Get entities to write
    Range triangles;
    if( !output_list || !num_sets )
    {
        rval = mbImpl->get_entities_by_type( 0, MBTRI, triangles, false );
        if( MB_SUCCESS != rval ) return rval;

        // Somehow get all the nodes from this range, order them, uniquify, then use binary search
    }
    else
    {
        // Get all triangles from output sets
        for( int i = 0; i < num_sets; i++ )
            rval = mbImpl->get_entities_by_type( output_list[i], MBTRI, triangles, false );
    }
    // Use an array with all the connectivities in the triangles; it will be converted later to ints
    int numTriangles    = triangles.size();
    int array_alloc     = 3 * numTriangles;               // Allocated size of 'array'
    EntityHandle* array = new EntityHandle[array_alloc];  // ptr to working array of result handles
    // Fill up array with node handles; reorder and uniquify
    if( !array ) return MB_MEMORY_ALLOCATION_FAILED;
    int fillA = 0;
    for( Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e )
    {
        const EntityHandle* conn;
        int conn_len;
        rval = mbImpl->get_connectivity( *e, conn, conn_len );
        if( MB_SUCCESS != rval )
        {
            delete[] array;
            return rval;
        }
        if( 3 != conn_len )
        {
            delete[] array;
            return MB_INVALID_SIZE;
        }

        for( int i = 0; i < conn_len; ++i )
            array[fillA++] = conn[i];
    }
    if( fillA != array_alloc )
    {
        delete[] array;
        return MB_INVALID_SIZE;
    }

    std::sort( array, array + array_alloc );
    int numNodes = std::unique( array, array + array_alloc ) - array;

    file << "#$SMF 1.0\n";
    file << "#$vertices " << numNodes << std::endl;
    file << "#$faces " << numTriangles << std::endl;
    file << "# \n";
    file << "# output from MOAB \n";
    file << "# \n";

    // Output first the nodes
    // num nodes??
    // Write the nodes
    double coord[3];
    for( int i = 0; i < numNodes; i++ )
    {
        EntityHandle node_handle = array[i];

        rval = mbImpl->get_coords( &node_handle, 1, coord );
        if( rval != MB_SUCCESS )
        {
            delete[] array;
            return rval;
        }

        file << "v " << coord[0] << " " << coord[1] << " " << coord[2] << std::endl;
    }
    // Write faces now
    // Leave a blank line for cosmetics
    file << " \n";
    for( Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e )
    {
        const EntityHandle* conn;
        int conn_len;
        rval = mbImpl->get_connectivity( *e, conn, conn_len );
        if( MB_SUCCESS != rval )
        {
            delete[] array;
            return rval;
        }
        if( 3 != conn_len )
        {
            delete[] array;
            return MB_INVALID_SIZE;
        }
        file << "f ";
        for( int i = 0; i < conn_len; ++i )
        {
            int indexInArray = std::lower_bound( array, array + numNodes, conn[i] ) - array;
            file << indexInArray + 1 << " ";
        }
        file << std::endl;
    }

    file.close();
    delete[] array;
    return MB_SUCCESS;
}

Member Data Documentation

Definition at line 53 of file WriteSmf.hpp.

Referenced by write_file(), and ~WriteSmf().

Definition at line 54 of file WriteSmf.hpp.

Referenced by write_file(), WriteSmf(), and ~WriteSmf().

List of all members.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines