Mesh Oriented datABase  (version 5.5.0)
An array-based unstructured mesh library
moab::WriteSmf Class Reference

#include <WriteSmf.hpp>

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

Public Member Functions

 WriteSmf (Interface *impl)
 Constructor. More...
 
virtual ~WriteSmf ()
 Destructor. More...
 
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 More...
 
- Public Member Functions inherited from moab::WriterIface
virtual ~WriterIface ()
 

Static Public Member Functions

static WriterIfacefactory (Interface *)
 

Private Attributes

InterfacembImpl
 
WriteUtilIfacewriteTool
 

Detailed Description

Definition at line 29 of file WriteSmf.hpp.

Constructor & Destructor Documentation

◆ WriteSmf()

moab::WriteSmf::WriteSmf ( Interface impl)

Constructor.

Definition at line 54 of file WriteSmf.cpp.

54  : mbImpl( impl ), writeTool( 0 )
55 {
56  assert( impl != NULL );
57  impl->query_interface( writeTool );
58 }

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

Referenced by factory().

◆ ~WriteSmf()

moab::WriteSmf::~WriteSmf ( )
virtual

Destructor.

Definition at line 60 of file WriteSmf.cpp.

61 {
63 }

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

Member Function Documentation

◆ factory()

WriterIface * moab::WriteSmf::factory ( Interface iface)
static

Definition at line 49 of file WriteSmf.cpp.

50 {
51  return new WriteSmf( iface );
52 }

References iface, and WriteSmf().

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

◆ write_file()

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.

74 {
75  ErrorCode rval;
76 
77  // Get precision for node coordinates
78  int precision;
79  if( MB_SUCCESS != opts.get_int_option( "PRECISION", precision ) ) precision = DEFAULT_PRECISION;
80 
81  // Honor overwrite flag
82  if( !overwrite )
83  {
84  rval = writeTool->check_doesnt_exist( file_name );
85  if( MB_SUCCESS != rval ) return rval;
86  }
87 
88  // Create file
89  std::ofstream file( file_name );
90  if( !file )
91  {
92  MB_SET_ERR( MB_FILE_WRITE_ERROR, "Could not open file: " << file_name );
93  }
94  file.precision( precision );
95 
96  // Get entities to write
97  Range triangles;
98  if( !output_list || !num_sets )
99  {
100  rval = mbImpl->get_entities_by_type( 0, MBTRI, triangles, false );
101  if( MB_SUCCESS != rval ) return rval;
102 
103  // Somehow get all the nodes from this range, order them, uniquify, then use binary search
104  }
105  else
106  {
107  // Get all triangles from output sets
108  for( int i = 0; i < num_sets; i++ )
109  rval = mbImpl->get_entities_by_type( output_list[i], MBTRI, triangles, false );
110  }
111  // Use an array with all the connectivities in the triangles; it will be converted later to ints
112  int numTriangles = triangles.size();
113  int array_alloc = 3 * numTriangles; // Allocated size of 'array'
114  EntityHandle* array = new EntityHandle[array_alloc]; // ptr to working array of result handles
115  // Fill up array with node handles; reorder and uniquify
116  if( !array ) return MB_MEMORY_ALLOCATION_FAILED;
117  int fillA = 0;
118  for( Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e )
119  {
120  const EntityHandle* conn;
121  int conn_len;
122  rval = mbImpl->get_connectivity( *e, conn, conn_len );
123  if( MB_SUCCESS != rval )
124  {
125  delete[] array;
126  return rval;
127  }
128  if( 3 != conn_len )
129  {
130  delete[] array;
131  return MB_INVALID_SIZE;
132  }
133 
134  for( int i = 0; i < conn_len; ++i )
135  array[fillA++] = conn[i];
136  }
137  if( fillA != array_alloc )
138  {
139  delete[] array;
140  return MB_INVALID_SIZE;
141  }
142 
143  std::sort( array, array + array_alloc );
144  int numNodes = std::unique( array, array + array_alloc ) - array;
145 
146  file << "#$SMF 1.0\n";
147  file << "#$vertices " << numNodes << std::endl;
148  file << "#$faces " << numTriangles << std::endl;
149  file << "# \n";
150  file << "# output from MOAB \n";
151  file << "# \n";
152 
153  // Output first the nodes
154  // num nodes??
155  // Write the nodes
156  double coord[3];
157  for( int i = 0; i < numNodes; i++ )
158  {
159  EntityHandle node_handle = array[i];
160 
161  rval = mbImpl->get_coords( &node_handle, 1, coord );
162  if( rval != MB_SUCCESS )
163  {
164  delete[] array;
165  return rval;
166  }
167 
168  file << "v " << coord[0] << " " << coord[1] << " " << coord[2] << std::endl;
169  }
170  // Write faces now
171  // Leave a blank line for cosmetics
172  file << " \n";
173  for( Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e )
174  {
175  const EntityHandle* conn;
176  int conn_len;
177  rval = mbImpl->get_connectivity( *e, conn, conn_len );
178  if( MB_SUCCESS != rval )
179  {
180  delete[] array;
181  return rval;
182  }
183  if( 3 != conn_len )
184  {
185  delete[] array;
186  return MB_INVALID_SIZE;
187  }
188  file << "f ";
189  for( int i = 0; i < conn_len; ++i )
190  {
191  int indexInArray = std::lower_bound( array, array + numNodes, conn[i] ) - array;
192  file << indexInArray + 1 << " ";
193  }
194  file << std::endl;
195  }
196 
197  file.close();
198  delete[] array;
199  return MB_SUCCESS;
200 }

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.

Member Data Documentation

◆ mbImpl

Interface* moab::WriteSmf::mbImpl
private

Definition at line 53 of file WriteSmf.hpp.

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

◆ writeTool

WriteUtilIface* moab::WriteSmf::writeTool
private

Definition at line 54 of file WriteSmf.hpp.

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


The documentation for this class was generated from the following files: