Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
ListSetsNTagsCXX.cpp
Go to the documentation of this file.
1 /** ListSetsNTags: list sets & tags from a mesh
2  *
3  * This program shows how to read and list sets and tags from a mesh
4  *
5  * Usage: SetsNTags <mesh_file_name>
6  *
7  */
8 
9 #include <iostream>
10 #include <cstdlib>
11 #include <cstring>
12 #include <vector>
13 #include "iMesh.h"
14 
15 #define ERRORR( a ) \
16  { \
17  if( iBase_SUCCESS != err ) \
18  { \
19  std::cout << ( a ) << std::endl; \
20  return err; \
21  } \
22  }
23 
24 int main( int argc, char* argv[] )
25 {
26  const char* filename;
27  bool read_par = false;
28  if( argc < 2 || !strcmp( argv[1], "-h" ) )
29  {
30  std::cout << "Usage: " << argv[0] << " <filename> [-p]" << std::endl;
31  return 0;
32  }
33  else
34  {
35  // Check command line arg
36  filename = argv[1];
37 
38  if( argc > 2 && !strcmp( argv[2], "-p" ) ) read_par = true;
39  }
40 
41  // create the Mesh instance
42  iMesh_Instance mesh;
43  int err;
44  iMesh_newMesh( NULL, &mesh, &err, 0 );ERRORR( "Error creating new mesh.\n" );
45 
46  iBase_EntitySetHandle root_set;
47  iMesh_getRootSet( mesh, &root_set, &err );ERRORR( "Couldn't get root set." );
48 
49  // load the mesh
50  if( read_par )
51  {
52  const char* opt = " moab:PARALLEL=READ_PART moab:PARTITION=PARALLEL_PARTITION moab:PARTITION_DISTRIBUTE "
53  "moab:PARALLEL_RESOLVE_SHARED_ENTS moab:DEBUG_PIO=2 moab:DEBUG_IO=2 moab:SETS=SETS ";
54  iMesh_load( mesh, root_set, filename, opt, &err, strlen( filename ), strlen( opt ) );
55  }
56  else
57  iMesh_load( mesh, root_set, filename, NULL, &err, strlen( filename ), 0 );ERRORR( "Couldn't load mesh." );
58 
59  // get all sets
60  iBase_EntitySetHandle* sets = NULL;
61  int sets_alloc = 0, sets_size;
62  iMesh_getEntSets( mesh, root_set, 1, &sets, &sets_alloc, &sets_size, &err );ERRORR( "Couldn't get all sets." );
63 
64  // iterate through them, checking whether they have tags
65  iBase_TagHandle* tags = NULL;
66  int tags_alloc = 0, tags_size;
67  int i, j;
68  for( i = 0; i < sets_size; i++ )
69  {
70  // get connectivity
71  iMesh_getAllEntSetTags( mesh, sets[i], &tags, &tags_alloc, &tags_size, &err );ERRORR( "Failed to get ent set tags." );
72 
73  if( 0 != tags_size )
74  {
75  std::cout << "Set " << sets[i] << "; Tags:" << std::endl;
76 
77  // list tag names on this set
78  for( j = 0; j < tags_size; j++ )
79  {
80  char tname[128];
81  std::vector< int > int_val;
82  std::vector< double > dbl_val;
83  iMesh_getTagName( mesh, tags[j], tname, &err, sizeof( tname ) );
84  tname[sizeof( tname ) - 1] = '\0';
85  int tag_type, tag_size;
86  iMesh_getTagType( mesh, tags[j], &tag_type, &err );ERRORR( "Failed to get tag type." );
87  iMesh_getTagType( mesh, tags[j], &tag_size, &err );ERRORR( "Failed to get tag size." );
88  if( iBase_INTEGER == tag_type )
89  {
90  int_val.resize( tag_size );
91  iMesh_getEntSetIntData( mesh, sets[i], tags[j], &int_val[0], &err );ERRORR( "Failed to get int data type." );
92  std::cout << tname << " = " << int_val[0];
93  if( tag_size > 1 )
94  for( int k = 1; k < tag_size; i++ )
95  std::cout << ", " << int_val[k];
96  std::cout << std::endl;
97  }
98  else if( iBase_DOUBLE == tag_type )
99  {
100  dbl_val.resize( tag_size );
101  iMesh_getEntSetDblData( mesh, sets[i], tags[j], &dbl_val[0], &err );
102  std::cout << tname << " = " << dbl_val[0];
103  if( tag_size > 1 )
104  for( int k = 1; k < tag_size; i++ )
105  std::cout << ", " << dbl_val[k];
106  std::cout << std::endl;
107  }
108  else
109  std::cout << tname << " (opaque) " << std::endl;
110  }
111  }
112  std::cout << std::endl;
113 
114  free( tags );
115  tags = NULL;
116  tags_alloc = 0;
117  }
118 
119  free( sets );
120 
121  iMesh_dtor( mesh, &err );ERRORR( "Failed to destruct interface." );
122 
123  return 0;
124 }