18 std::vector<MVertex *> &vertices)
20 for(
int i = 0; i < num; i++) {
21 if(indices[i] < 0 || indices[i] > (
int)(vec.size() - 1)) {
26 vertices.push_back(vec[indices[i]]);
33 FILE *fp =
Fopen(name.c_str(),
"r");
35 Msg::Error(
"Unable to open file '%s'", name.c_str());
40 if(!fgets(buffer,
sizeof(buffer), fp)) {
47 sscanf(buffer,
"%s %d", str, &format);
49 Msg::Error(
"Medit mesh import only available for ASCII files");
54 std::vector<MVertex *> vertexVector;
55 std::map<int, std::vector<MElement *> > elements[5];
60 if(!fgets(buffer, 256, fp))
break;
61 if(buffer[0] !=
'#') {
63 sscanf(buffer,
"%s", str);
64 if(!strncmp(buffer,
"Dimension 3", 11)) {
67 else if(!strncmp(buffer,
"Dimension 2", 11)) {
70 else if(!strcmp(str,
"Dimension")) {
71 if(!fgets(buffer,
sizeof(buffer), fp))
break;
73 else if(!strcmp(str,
"Vertices")) {
74 if(!fgets(buffer,
sizeof(buffer), fp))
break;
76 sscanf(buffer,
"%d", &nbv);
78 vertexVector.resize(nbv);
79 for(
int i = 0; i < nbv; i++) {
80 if(!fgets(buffer,
sizeof(buffer), fp))
break;
84 sscanf(buffer,
"%lf %lf %lf %d", &x, &y, &
z, &dum);
86 sscanf(buffer,
"%lf %lf %d", &x, &y, &dum);
87 vertexVector[i] =
new MVertex(x, y,
z);
90 else if(!strcmp(str,
"Edges")) {
91 if(!fgets(buffer,
sizeof(buffer), fp))
break;
93 sscanf(buffer,
"%d", &nbe);
95 for(
int i = 0; i < nbe; i++) {
96 if(!fgets(buffer,
sizeof(buffer), fp))
break;
98 sscanf(buffer,
"%d %d %d", &n[0], &n[1], &cl);
99 for(
int j = 0; j < 2; j++) n[j]--;
108 else if(!strcmp(str,
"EdgesP2")) {
109 if(!fgets(buffer,
sizeof(buffer), fp))
break;
111 sscanf(buffer,
"%d", &nbe);
113 for(
int i = 0; i < nbe; i++) {
114 if(!fgets(buffer,
sizeof(buffer), fp))
break;
116 sscanf(buffer,
"%d %d %d %d", &n[0], &n[1], &n[2], &cl);
117 for(
int j = 0; j < 3; j++) n[j]--;
126 else if(!strcmp(str,
"Triangles")) {
127 if(!fgets(buffer,
sizeof(buffer), fp))
break;
129 sscanf(buffer,
"%d", &nbe);
131 for(
int i = 0; i < nbe; i++) {
132 if(!fgets(buffer,
sizeof(buffer), fp))
break;
134 sscanf(buffer,
"%d %d %d %d", &n[0], &n[1], &n[2], &cl);
135 for(
int j = 0; j < 3; j++) n[j]--;
144 else if(!strcmp(str,
"TrianglesP2")) {
145 if(!fgets(buffer,
sizeof(buffer), fp))
break;
147 sscanf(buffer,
"%d", &nbe);
149 for(
int i = 0; i < nbe; i++) {
150 if(!fgets(buffer,
sizeof(buffer), fp))
break;
152 sscanf(buffer,
"%d %d %d %d %d %d %d", &n[0], &n[1], &n[2], &n[3],
154 for(
int j = 0; j < 6; j++) n[j]--;
163 else if(!strcmp(str,
"Quadrilaterals")) {
164 if(!fgets(buffer,
sizeof(buffer), fp))
break;
166 sscanf(buffer,
"%d", &nbe);
168 for(
int i = 0; i < nbe; i++) {
169 if(!fgets(buffer,
sizeof(buffer), fp))
break;
171 sscanf(buffer,
"%d %d %d %d %d", &n[0], &n[1], &n[2], &n[3], &cl);
172 for(
int j = 0; j < 4; j++) n[j]--;
181 else if(!strcmp(str,
"Tetrahedra")) {
182 if(!fgets(buffer,
sizeof(buffer), fp))
break;
184 sscanf(buffer,
"%d", &nbe);
186 for(
int i = 0; i < nbe; i++) {
187 if(!fgets(buffer,
sizeof(buffer), fp))
break;
189 sscanf(buffer,
"%d %d %d %d %d", &n[0], &n[1], &n[2], &n[3], &cl);
190 for(
int j = 0; j < 4; j++) n[j]--;
199 else if(!strcmp(str,
"TetrahedraP2")) {
200 if(!fgets(buffer,
sizeof(buffer), fp))
break;
202 sscanf(buffer,
"%d", &nbe);
204 for(
int i = 0; i < nbe; i++) {
205 if(!fgets(buffer,
sizeof(buffer), fp))
break;
207 sscanf(buffer,
"%d %d %d %d %d %d %d %d %d %d %d", &n[0], &n[1],
208 &n[2], &n[3], &n[4], &n[5], &n[6], &n[7], &n[9], &n[8], &cl);
209 for(
int j = 0; j < 10; j++) n[j]--;
218 else if(!strcmp(str,
"Hexahedra")) {
219 if(!fgets(buffer,
sizeof(buffer), fp))
break;
221 sscanf(buffer,
"%d", &nbe);
223 for(
int i = 0; i < nbe; i++) {
224 if(!fgets(buffer,
sizeof(buffer), fp))
break;
226 sscanf(buffer,
"%d %d %d %d %d %d %d %d %d", &n[0], &n[1], &n[2],
227 &n[3], &n[4], &n[5], &n[6], &n[7], &cl);
228 for(
int j = 0; j < 8; j++) n[j]--;
240 for(
int i = 0; i < (int)(
sizeof(elements) /
sizeof(elements[0])); i++)
250 double scalingFactor)
252 FILE *fp =
Fopen(name.c_str(),
"w");
254 Msg::Error(
"Unable to open file '%s'", name.c_str());
262 fprintf(fp,
" MeshVersionFormatted 2\n");
263 fprintf(fp,
" Dimension\n");
266 fprintf(fp,
" Vertices\n");
267 fprintf(fp,
" %d\n", numVertices);
268 std::vector<GEntity *> entities;
270 for(std::size_t i = 0; i < entities.size(); i++)
271 for(std::size_t j = 0; j < entities[i]->mesh_vertices.size(); j++)
272 entities[i]->mesh_vertices[j]->
writeMESH(fp, scalingFactor);
274 int numEdges = 0, numTriangles = 0, numQuadrangles = 0;
275 int numTetrahedra = 0, numHexahedra = 0;
277 if(saveAll || (*it)->physicals.size()) { numEdges += (*it)->lines.size(); }
280 if(saveAll || (*it)->physicals.size()) {
281 numTriangles += (*it)->triangles.size();
282 numQuadrangles += (*it)->quadrangles.size();
286 if(saveAll || (*it)->physicals.size()) {
287 numTetrahedra += (*it)->tetrahedra.size();
288 numHexahedra += (*it)->hexahedra.size();
294 fprintf(fp,
" EdgesP2\n");
296 fprintf(fp,
" Edges\n");
297 fprintf(fp,
" %d\n", numEdges);
299 int numPhys = (*it)->physicals.size();
300 if(saveAll || numPhys) {
301 for(std::size_t i = 0; i < (*it)->lines.size(); i++)
302 (*it)->lines[i]->writeMESH(fp, elementTagType, (*it)->tag(),
303 numPhys ? (*it)->physicals[0] : 0);
309 fprintf(fp,
" TrianglesP2\n");
311 fprintf(fp,
" Triangles\n");
312 fprintf(fp,
" %d\n", numTriangles);
314 int numPhys = (*it)->physicals.size();
315 if(saveAll || numPhys) {
316 for(std::size_t i = 0; i < (*it)->triangles.size(); i++)
317 (*it)->triangles[i]->writeMESH(fp, elementTagType, (*it)->tag(),
318 numPhys ? (*it)->physicals[0] : 0);
323 fprintf(fp,
" Quadrilaterals\n");
324 fprintf(fp,
" %d\n", numQuadrangles);
326 int numPhys = (*it)->physicals.size();
327 if(saveAll || numPhys) {
328 for(std::size_t i = 0; i < (*it)->quadrangles.size(); i++)
329 (*it)->quadrangles[i]->writeMESH(fp, elementTagType, (*it)->tag(),
330 numPhys ? (*it)->physicals[0] : 0);
336 fprintf(fp,
" TetrahedraP2\n");
338 fprintf(fp,
" Tetrahedra\n");
339 fprintf(fp,
" %d\n", numTetrahedra);
341 int numPhys = (*it)->physicals.size();
342 if(saveAll || numPhys) {
343 for(std::size_t i = 0; i < (*it)->tetrahedra.size(); i++)
344 (*it)->tetrahedra[i]->writeMESH(fp, elementTagType, (*it)->tag(),
345 numPhys ? (*it)->physicals[0] : 0);
350 fprintf(fp,
" Hexahedra\n");
351 fprintf(fp,
" %d\n", numHexahedra);
353 int numPhys = (*it)->physicals.size();
354 if(saveAll || numPhys) {
355 for(std::size_t i = 0; i < (*it)->hexahedra.size(); i++)
356 (*it)->hexahedra[i]->writeMESH(fp, elementTagType, (*it)->tag(),
357 numPhys ? (*it)->physicals[0] : 0);
362 fprintf(fp,
" End\n");