Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
SetsNTags.cpp
Go to the documentation of this file.
1 /** @example SetsNTags.cpp
2  * Description: Get the sets representing materials and Dirichlet/Neumann boundary conditions and
3  * list their contents.\n This example shows how to get entity sets, and tags on those sets.
4  *
5  * To run: ./SetsNTags [meshfile]\n
6  * (default values can run if users don't specify a mesh file)
7  */
8 
9 #include "moab/Core.hpp"
10 #include "moab/Interface.hpp"
11 #include "moab/Range.hpp"
12 #include "MBTagConventions.hpp"
13 
14 #include <iostream>
15 
16 using namespace moab;
17 using namespace std;
18 
19 #ifndef MESH_DIR
20 #define MESH_DIR "."
21 #endif
22 
23 string test_file_name = string( MESH_DIR ) + string( "/hex01.vtk" );
24 
25 // Tag names for these conventional tags come from MBTagConventions.hpp
27 
28 int main( int argc, char** argv )
29 {
30  // Get the material set tag handle
31  Tag mtag;
32  ErrorCode rval;
33  Range sets, set_ents;
34 
35  // Get MOAB instance
36  Interface* mb = new( std::nothrow ) Core;
37  if( NULL == mb ) return 1;
38 
39  // Need option handling here for input filename
40  if( argc > 1 )
41  {
42  // User has input a mesh file
43  test_file_name = argv[1];
44  }
45 
46  // Load a file
47  rval = mb->load_file( test_file_name.c_str() );MB_CHK_ERR( rval );
48 
49  // Loop over set types
50  for( int i = 0; i < 3; i++ )
51  {
52  // Get the tag handle for this tag name; tag should already exist (it was created during
53  // file read)
54  rval = mb->tag_get_handle( tag_nms[i], 1, MB_TYPE_INTEGER, mtag );MB_CHK_ERR( rval );
55 
56  // Get all the sets having that tag (with any value for that tag)
57  sets.clear();
58  rval = mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &mtag, NULL, 1, sets );MB_CHK_ERR( rval );
59 
60  // Iterate over each set, getting the entities and printing them
61  Range::iterator set_it;
62  for( set_it = sets.begin(); set_it != sets.end(); ++set_it )
63  {
64  // Get the id for this set
65  int set_id;
66  rval = mb->tag_get_data( mtag, &( *set_it ), 1, &set_id );MB_CHK_ERR( rval );
67 
68  // Get the entities in the set, recursively
69  rval = mb->get_entities_by_handle( *set_it, set_ents, true );MB_CHK_ERR( rval );
70 
71  cout << tag_nms[i] << " " << set_id << " has " << set_ents.size() << " entities:" << endl;
72  set_ents.print( " " );
73  set_ents.clear();
74  }
75  }
76 
77  delete mb;
78 
79  return 0;
80 }