Go to the documentation of this file. 1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 #include <string>
5 #include <sstream>
6
7 #include "moab/Core.hpp"
8 #include "moab/ReadUtilIface.hpp"
9
10 using namespace std;
11 using namespace moab;
12
13 int comment( string& line )
14 {
15
16
17 size_t found = line.find_first_not_of( " \t" );
18 if( found == string::npos ) return 1;
19 if( '#' == line[found] ) return 1;
20 return 0;
21 }
22 ErrorCode ReadTriangleOutput( Interface* mb, string fileBase )
23 {
24
25
26
27 ReadUtilIface* iface;
28 ErrorCode rval = mb->query_interface( iface );
29
30 if( MB_SUCCESS != rval )
31 {
32 cout << "Can't get interface.\n";
33 return MB_FAILURE;
34 }
35
36 string nodeFileName = fileBase + ".node";
37 ifstream nodeFile( nodeFileName.c_str() );
38 if( !nodeFile.is_open() )
39 {
40 cout << "can't open node file .\n";
41 return MB_FILE_DOES_NOT_EXIST;
42 }
43 cout << "reading nodes from file " << nodeFileName.c_str() << endl;
44
45 string eleFileName = fileBase + ".ele";
46 ifstream eleFile( eleFileName.c_str() );
47 if( !eleFile.is_open() )
48 {
49 cout << "can't open element file .\n";
50 return MB_FILE_DOES_NOT_EXIST;
51 }
52 cout << "reading elements from file " << eleFileName.c_str() << endl;
53
54 string line;
55
56
57
58 int num_nodes = 0, num_triangles = 0;
59 while( num_nodes == 0 )
60 {
61 getline( nodeFile, line );
62 if( comment( line ) ) continue;
63 stringstream tks( line );
64 tks >> num_nodes;
65
66 cout << "num nodes:" << num_nodes << endl;
67 }
68
69
70
71
72
73 vector< double* > arrays;
74 EntityHandle startv;
75 rval = iface->get_node_coords( 2, num_nodes, 0, startv, arrays );
76 for( int i = 0; i < num_nodes; i++ )
77 {
78 getline( nodeFile, line );
79 if( comment( line ) )
80 {
81 i--;
82 continue;
83 }
84 stringstream tokens( line );
85 int nodeId;
86 tokens >> nodeId >> arrays[0][i] >> arrays[1][i];
87 }
88
89
90
91
92 while( num_triangles == 0 )
93 {
94 getline( eleFile, line );
95 if( comment( line ) ) continue;
96 stringstream tks( line );
97 tks >> num_triangles;
98 cout << "num triangles:" << num_triangles << endl;
99 }
100
101 EntityHandle starte;
102 EntityHandle* starth;
103
104
105 rval = iface->get_element_connect( num_triangles, 3, MBTRI, 0, starte, starth );
106
107 for( int j = 0; j < num_triangles; j++ )
108 {
109 getline( eleFile, line );
110 if( comment( line ) )
111 {
112 j--;
113 continue;
114 }
115 stringstream tokens( line );
116 int eleId;
117 unsigned int node;
118 tokens >> eleId;
119 for( int k = 0; k < 3; k++ )
120 {
121 tokens >> node;
122
123 starth[3 * j + k] = startv + node - 1;
124 }
125 }
126
127 mb->release_interface( iface );
128
129 return MB_SUCCESS;
130 }
131
132
133
134
135
136
137 int main( int argc, char** argv )
138 {
139 if( 3 != argc )
140 {
141 cout << "Usage: " << argv[0] << " <filename> <outFile> " << endl;
142 cout << " <filename> is the base file name; *.ele and *.node file are read; outFile "
143 "is a file with an extension recognized by MOAB "
144 << endl;
145 return 0;
146 }
147
148 string filename = argv[1];
149 char* outfile = argv[2];
150
151
152 Core* mb = new Core();
153
154 ErrorCode rval = ReadTriangleOutput( mb, filename );
155
156 if( rval == MB_SUCCESS )
157 {
158 cout << "Writing output file " << outfile << endl;
159 mb->write_file( outfile );
160 }
161
162 delete mb;
163
164 return 0;
165 }