MOAB: Mesh Oriented datABase  (version 5.5.0)
read_cgm_group_test.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "moab/Interface.hpp"
3 #ifndef IS_BUILDING_MB
4 #define IS_BUILDING_MB
5 #endif
6 #include "TestUtil.hpp"
7 #include "Internals.hpp"
8 #include "moab/Core.hpp"
9 #include "MBTagConventions.hpp"
10 #include "InitCGMA.hpp"
11 #include "GeometryQueryTool.hpp"
12 
13 using namespace moab;
14 
15 #define CHKERR( A ) \
16  do \
17  { \
18  if( MB_SUCCESS != ( A ) ) \
19  { \
20  std::cerr << "Failure (error code " << ( A ) << ") at " __FILE__ ":" << __LINE__ << std::endl; \
21  return A; \
22  } \
23  } while( false )
24 
25 #ifdef HAVE_OCC_STEP
26 std::string input_cylcube = TestDir + "unittest/io/cylcube.stp";
27 #else
28 std::string input_cylcube = TestDir + "unittest/io/cylcube.sat";
29 #endif
30 
31 // Function used to load the test file
32 void read_file( Interface* moab, const char* input_file );
33 
34 // Function for getting entity ids
35 int geom_id_by_handle( Interface* moab, const EntityHandle set );
36 
37 // Function for checking retrieved group data
38 void check_group_data( std::vector< int >& group_ids,
39  std::vector< std::string >& group_names,
40 #ifdef HAVE_OCC_STEP
41  std::vector< int >& );
42 #else
43  std::vector< int >& group_ent_ids );
44 #endif
45 
46 // Function for loading all reference data
47 void load_group_references( std::vector< int >& ids, std::vector< std::string >& names, std::vector< int >& ent_ids );
48 
49 // List of tests in this file
51 
52 int main( int /* argc */, char** /* argv */ )
53 {
54  int result = 0;
55 
57 
58  return result;
59 }
60 
61 void read_file( Interface* moab, const char* input_file )
62 {
63  InitCGMA::initialize_cgma();
64  GeometryQueryTool::instance()->delete_geometry();
65 
66  ErrorCode rval = moab->load_file( input_file );CHECK_ERR( rval );
67 }
68 
69 // Checks that group information is being read correctly if MOAB is
70 // being build with CGM. If MOAB is built with OCC, it makes sure
71 // no erroneous group data is loaded as STEP files do not hold
72 // information about groups.
74 {
75 
76  ErrorCode rval;
77  // Open the test file
78  Core moab;
79  Interface* mb = &moab;
80  read_file( mb, input_cylcube.c_str() );
81 
82  // Get (or create) the name and category tags
84 
87 
90 
91  // Get the group entity handles
92  Range group_sets;
93  char query[CATEGORY_TAG_SIZE] = "Group\0";
94  // Has to be this way because of the way tags are created
95  void* val[] = { &query };
96  rval = mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &category_tag, val, 1, group_sets );CHECK_ERR( rval );
97  // Get group names and IDs
98  std::vector< int > g_ids;
99  std::vector< std::string > g_names;
100  std::vector< int > g_ent_ids;
101 
102  for( Range::iterator i = group_sets.begin(); i != group_sets.end(); ++i )
103  {
104  int group_id = geom_id_by_handle( mb, *i );
105  g_ids.push_back( group_id );
106  // Get the group name
107  char group_name[NAME_TAG_SIZE + 1];
108  rval = mb->tag_get_data( name_tag, &( *i ), 1, &group_name );CHECK_ERR( rval );
109  // Store group name
110  std::string temp( group_name );
111  g_names.push_back( temp );
112  // Get all entities in the group
113  Range group_ents;
114  rval = mb->get_entities_by_type( *i, MBENTITYSET, group_ents, false );CHECK_ERR( rval );
115  if( group_ents.size() != 1 ) CHECK( false );
116  int grp_ent_id = geom_id_by_handle( mb, group_ents[0] );
117  g_ent_ids.push_back( grp_ent_id );
118  }
119  check_group_data( g_ids, g_names, g_ent_ids );
120 }
121 
122 void check_group_data( std::vector< int >& group_ids,
123  std::vector< std::string >& group_names,
124 #ifdef HAVE_OCC_STEP
125  std::vector< int >& /*group_ent_ids*/ )
126 #else
127  std::vector< int >& group_ent_ids )
128 #endif
129 {
130  // Step files do not contain group data, MOAB shouldn't return errors when trying to access
131  // this data but there shouldn't be any found.
132 #ifdef HAVE_OCC_STEP
133  int num_g_ids = group_ids.size();
134  int num_g_names = group_names.size();
135 
136  CHECK_EQUAL( 0, num_g_ids );
137  CHECK_EQUAL( 0, num_g_names );
138 #else
139 
140  // Initialize reference data
141  std::vector< int > group_ref_ids;
142  std::vector< std::string > group_ref_names;
143  std::vector< int > group_ref_ent_ids;
144  load_group_references( group_ref_ids, group_ref_names, group_ref_ent_ids );
145 
146  // check that the correct number of entities were found
147  CHECK_EQUAL( group_ref_ids.size(), group_ids.size() );
148  CHECK_EQUAL( group_ref_names.size(), group_names.size() );
149  CHECK_EQUAL( group_ref_ent_ids.size(), group_ent_ids.size() );
150 
151  // now make sure that each group has a matching group
152  for( unsigned int i = 0; i < group_ids.size(); i++ )
153  {
154  for( unsigned int j = 0; j < group_ref_ids.size(); j++ )
155  {
156  if( group_ids[i] == group_ref_ids[j] && group_names[i] == group_ref_names[j] &&
157  group_ent_ids[i] == group_ref_ent_ids[j] )
158  {
159  group_ref_ids.erase( group_ref_ids.begin() + j );
160  group_ref_names.erase( group_ref_names.begin() + j );
161  group_ref_ent_ids.erase( group_ref_ent_ids.begin() + j );
162  continue;
163  }
164  }
165  }
166 
167  // Check sizes of reference vectors after matching
168  // (all should be zero)
169  int leftovers = group_ref_ids.size();
170  CHECK_EQUAL( 0, leftovers );
171  leftovers = group_ref_names.size();
172  CHECK_EQUAL( 0, leftovers );
173  leftovers = group_ref_ent_ids.size();
174  CHECK_EQUAL( 0, leftovers );
175 #endif
176 }
177 
178 void load_group_references( std::vector< int >& ids, std::vector< std::string >& names, std::vector< int >& ent_ids )
179 {
180  // First set of group info
181  names.push_back( "Group 3" );
182  ids.push_back( 3 );
183  ent_ids.push_back( 2 );
184 
185  // Second set of group info
186  names.push_back( "Group 2" );
187  ids.push_back( 2 );
188  ent_ids.push_back( 1 );
189 }
190 
192 {
193  ErrorCode rval;
194  // Get the id_tag handle
195  Tag id_tag = moab->globalId_tag();
196  // Load the ID for the EntHandle given to the function
197  int id;
198  rval = moab->tag_get_data( id_tag, &set, 1, &id );CHECK_ERR( rval );
199  return id;
200 }