35 #define IS_BUILDING_MB
41 #define CALL( A, B ) \
44 ErrorCode _r = iface->A B; \
45 if( MB_SUCCESS != _r ) \
47 std::cerr << #A << #B << " failed at " << __FILE__ << ":" << __LINE__ << std::endl; \
57 std::ostream& s =
error ? std::cerr : std::cout;
59 s <<
"Usage: " <<
exe_name <<
" <options> <input_file> <output_file>" << std::endl
60 <<
" " <<
exe_name <<
" -h" << std::endl
61 <<
"Options: " << std::endl
62 <<
" -t <ident_tag>[=<value>] " << std::endl
63 <<
" -d <data_tag>[=<default>] " << std::endl
64 <<
" -c <data_tag=type:size>[=defult] " << std::endl
65 <<
" -w <write_tag> " << std::endl
66 <<
" -n|-e|-E " << std::endl
70 s <<
"Try '-h' for verbose help." << std::endl;
74 s <<
"This utility will write tag data to a subset of the mesh entities " << std::endl
75 <<
"contained in a file. The behavior is controlled by three main " << std::endl
76 <<
"properties:" << std::endl
77 <<
" 1) The ident_tag is used to identify sets of entities for which " << std::endl
78 <<
" data will be stored on each contained element or node. The -n " << std::endl
79 <<
" or -e flags can be used to restrict operation to only nodes or " << std::endl
80 <<
" elements, respectively." << std::endl
81 <<
" 2) The data_tag is used to identify which value to write on to " << std::endl
82 <<
" each entity. This is a tag on the set containing the entities." << std::endl
83 <<
" 3) The write_tag is the name of the tag that the data is stored in " << std::endl
84 <<
" on each mesh entity." << std::endl
86 <<
" -t : Specify an ident_tag. If a value is specified, only those " << std::endl
87 <<
" sets with the specified value are processed. At least one " << std::endl
88 <<
" ident_tag must be specified. Multiple ident_tags may be " << std::endl
89 <<
" specified, in which case any set that matches any of the " << std::endl
90 <<
" specified ident_tags will be processed (logical OR)." << std::endl
92 <<
" -d : Specify the data_tag. If multiple ident_tags are specified " << std::endl
93 <<
" then the data_tag must be specified. If only one ident_tag " << std::endl
94 <<
" is specified then the data_tag specification is optional." << std::endl
95 <<
" If no data_tag is specified, the value of the ident_tag " << std::endl
96 <<
" will be used. If a value is specified for the data_tag, " << std::endl
97 <<
" then the specified value will be used for any set that " << std::endl
98 <<
" doesn't have a value for the data_tag." << std::endl
100 <<
" -c : Similar to -d, except that the tag is created if it doesn't" << std::endl
101 <<
" already exist. If the tag is created, then all entities" << std::endl
102 <<
" receive the specified default value for the tag. In this " << std::endl
103 <<
" case it is an error if no default value is specified." << std::endl
105 <<
" -w : Specify the tag to create and store values in on mesh " << std::endl
106 <<
" entities. If no write_tag is specified, the data_tag " << std::endl
107 <<
" will be used." << std::endl
109 <<
" -n : Write tag data only on nodes (vertices)." << std::endl
110 <<
" -e : Write tag data only on elements." << std::endl
111 <<
" -E : Tag value on each node is that of one of its adjacent elements." << std::endl
113 <<
"The syntax for specifying tag values is as follows: " << std::endl
122 std::ostream& s =
error ? std::cerr : std::cout;
123 s <<
"A utility to propogate tag values from the entity sets "
124 "containing mesh entities to the entities contained in "
134 if( val ) std::cerr <<
": " << val;
135 std::cerr << std::endl;
136 std::cerr <<
"Try '" <<
exe_name <<
" -h' for help" << std::endl;
140 int main(
int argc,
char* argv[] )
146 if( argc == 1 )
about();
151 const char* input_name = 0;
152 const char* output_name = 0;
153 for(
int i = 1; i < argc; ++i )
155 if( argv[i][0] ==
'-' )
176 else if( !input_name )
177 input_name = argv[i];
178 else if( !output_name )
179 output_name = argv[i];
184 if( !input_name )
parse_error(
"No input file specified." );
185 if( !output_name )
parse_error(
"No output file specified." );
190 std::cerr <<
"Failed to read file: " << input_name << std::endl;
196 bool nodes_spec =
false;
197 bool elems_spec =
false;
198 bool node_from_elem_spec =
false;
199 bool have_data_tag =
false;
200 const char* write_tag_name = 0;
203 typedef std::vector< TagSpec > TagVect;
207 for(
int i = 1; i < argc; ++i )
209 if( argv[i] == input_name || argv[i] == output_name )
211 else if( !strcmp( argv[i],
"-n" ) )
213 else if( !strcmp( argv[i],
"-e" ) )
215 else if( !strcmp( argv[i],
"-E" ) )
217 node_from_elem_spec =
true;
221 else if( !argv[i][0] )
225 char flag = argv[i][1];
226 if( ( flag !=
't' && flag !=
'd' && flag !=
'w' && flag !=
'c' ) || argv[i][2] )
230 if( i == argc )
parse_error(
"Expected tag spec following option", argv[i - 1] );
234 if( write_tag_name )
parse_error(
"Invalid argument", argv[i] );
235 write_tag_name = argv[i];
237 else if( flag ==
'c' )
242 if( have_data_tag )
parse_error(
"Invalid argument", argv[i] );
245 have_data_tag =
true;
254 if( have_data_tag )
parse_error(
"Invalid argument", argv[i] );
257 have_data_tag =
true;
261 ident_tags.push_back( spec );
268 if( !nodes_spec && !elems_spec ) nodes_spec = elems_spec =
true;
271 if( ident_tags.empty() )
parse_error(
"At least one identifying tag must be specified." );
276 if( ident_tags.size() > 1 )
parse_error(
"No data tag specified." );
278 data_tag.
handle = ident_tags[0].handle;
280 CALL( tag_get_bytes, ( data_tag.
handle, data_size ) );
283 if( !write_tag_name )
285 write_tag = data_tag.
handle;
293 CALL( tag_get_data_type, ( data_tag.
handle, data_type ) );
295 CALL( tag_get_handle,
303 for( TagVect::iterator i = ident_tags.begin(); i != ident_tags.end(); ++i )
305 const void* value[] = { i->value };
306 CALL( get_entities_by_type_and_tag, ( 0,
MBENTITYSET, &i->handle, i->value ? value : 0, 1, temp ) );
311 std::vector< unsigned char > tag_data( data_size );
318 if( !data_tag.
value )
320 std::cerr <<
"Data tag not set for entityset " <<
iface->
id_from_handle( *i ) << std::endl;
323 memcpy( &tag_data[0], data_tag.
value, data_size );
327 CALL( tag_get_data, ( data_tag.
handle, &*i, 1, &tag_data[0] ) );
337 CALL( tag_set_data, ( write_tag, &*j, 1, &tag_data[0] ) );
340 CALL( tag_set_data, ( write_tag, &*j, 1, &tag_data[0] ) );
341 if( node_from_elem_spec )
348 CALL( tag_set_data, ( write_tag, &*j, 1, &tag_data[0] ) );
355 std::cerr <<
"Failed to write file: " << output_name << std::endl;