16 char str[256], key_bracket[256];
17 strcpy(key_bracket, key);
18 strcat(key_bracket,
"[");
19 while(fscanf(fp,
"%s", str)) {
20 if(!strcmp(str, key)) {
21 while(!feof(fp) && fgetc(fp) !=
'[') {}
24 if(!strcmp(str, key_bracket)) {
return 1; }
30 std::vector<MVertex *> &allVertexVector)
33 if(fscanf(fp,
"%lf %lf %lf", &x, &y, &
z) != 3)
return 0;
34 vertexVector.push_back(
new MVertex(x, y,
z));
35 while(fscanf(fp,
" , %lf %lf %lf", &x, &y, &
z) == 3)
36 vertexVector.push_back(
new MVertex(x, y,
z));
37 for(std::size_t i = 0; i < vertexVector.size(); i++)
38 allVertexVector.push_back(vertexVector[i]);
39 Msg::Info(
"%d nodes", vertexVector.size());
45 std::map<
int, std::vector<MElement *> > elements[3],
50 if(fscanf(fp,
"%d", &i) != 1)
return 0;
57 fgetpos(fp, &position);
58 if(!fgets(tmp,
sizeof(tmp), fp))
return 0;
59 fsetpos(fp, &position);
65 while(fscanf(fp, format, &i) == 1) {
66 if(i != -1) { idx.push_back(i); }
68 std::vector<MVertex *> vertices;
69 for(std::size_t j = 0; j < idx.size(); j++) {
70 if(idx[j] < 0 || idx[j] > (
int)(vertexVector.size() - 1)) {
75 vertices.push_back(vertexVector[idx[j]]);
78 if(vertices.size() < 2) {
79 Msg::Info(
"Skipping %d-node element", (
int)vertices.size());
81 else if(vertices.size() == 2) {
82 elements[0][region].push_back(
new MLine(vertices));
84 else if(vertices.size() == 3) {
85 elements[1][region].push_back(
new MTriangle(vertices));
87 else if(!strips && vertices.size() == 4) {
88 elements[2][region].push_back(
new MQuadrangle(vertices));
91 for(std::size_t j = 2; j < vertices.size(); j++) {
93 elements[1][region].push_back(
94 new MTriangle(vertices[j], vertices[j - 1], vertices[j - 2]));
96 elements[1][region].push_back(
97 new MTriangle(vertices[j - 2], vertices[j - 1], vertices[j]));
101 for(std::size_t j = 2; j < vertices.size(); j++) {
102 elements[1][region].push_back(
103 new MTriangle(vertices[0], vertices[j - 1], vertices[j]));
112 Msg::Info(
"%d elements", elements[0][region].size() +
113 elements[1][region].size() +
114 elements[2][region].size());
120 FILE *fp =
Fopen(name.c_str(),
"r");
122 Msg::Error(
"Unable to open file '%s'", name.c_str());
129 std::vector<MVertex *> vertexVector, allVertexVector;
130 std::map<int, std::vector<MElement *> > elements[3];
132 char buffer[256], str[256];
134 if(!fgets(buffer,
sizeof(buffer), fp))
break;
135 if(buffer[0] !=
'#') {
136 sscanf(buffer,
"%s", str);
137 if(!strcmp(str,
"Coordinate3")) {
138 vertexVector.clear();
142 else if(!strcmp(str,
"coord")) {
144 vertexVector.clear();
150 else if(!strcmp(str,
"IndexedTriangleStripSet")) {
152 vertexVector.clear();
158 else if(!strcmp(str,
"IndexedFaceSet") ||
159 !strcmp(str,
"IndexedLineSet")) {
164 else if(!strcmp(str,
"DEF")) {
165 char str1[256], str2[256];
166 if(!sscanf(buffer,
"%s %s %s", str1, str2, str))
break;
167 if(!strcmp(str,
"Coordinate")) {
168 vertexVector.clear();
172 else if(!strcmp(str,
"IndexedFaceSet") ||
173 !strcmp(str,
"IndexedLineSet")) {
182 for(
int i = 0; i < (int)(
sizeof(elements) /
sizeof(elements[0])); i++)
192 double scalingFactor)
194 FILE *fp =
Fopen(name.c_str(),
"w");
196 Msg::Error(
"Unable to open file '%s'", name.c_str());
204 fprintf(fp,
"#VRML V1.0 ascii\n");
205 fprintf(fp,
"#created by Gmsh\n");
206 fprintf(fp,
"Coordinate3 {\n");
207 fprintf(fp,
" point [\n");
210 for(std::size_t i = 0; i < (*it)->mesh_vertices.size(); i++)
211 (*it)->mesh_vertices[i]->writeVRML(fp, scalingFactor);
213 for(std::size_t i = 0; i < (*it)->mesh_vertices.size(); i++)
214 (*it)->mesh_vertices[i]->writeVRML(fp, scalingFactor);
216 for(std::size_t i = 0; i < (*it)->mesh_vertices.size(); i++)
217 (*it)->mesh_vertices[i]->writeVRML(fp, scalingFactor);
223 if(saveAll || (*it)->physicals.size()) {
224 fprintf(fp,
"DEF Curve%d IndexedLineSet {\n", (*it)->tag());
225 fprintf(fp,
" coordIndex [\n");
226 for(std::size_t i = 0; i < (*it)->lines.size(); i++)
227 (*it)->lines[i]->writeVRML(fp);
234 if(saveAll || (*it)->physicals.size()) {
235 fprintf(fp,
"DEF Surface%d IndexedFaceSet {\n", (*it)->tag());
236 fprintf(fp,
" coordIndex [\n");
237 for(std::size_t i = 0; i < (*it)->triangles.size(); i++)
238 (*it)->triangles[i]->writeVRML(fp);
239 for(std::size_t i = 0; i < (*it)->quadrangles.size(); i++)
240 (*it)->quadrangles[i]->writeVRML(fp);