20 for(std::size_t i = 0; i < strlen(buffer); i++) {
21 if(buffer[i] ==
',') {
22 if(buffer[keySize] ==
'*') {
32 if(buffer[keySize] ==
'*') {
41 int len = strlen(str);
43 for(
int i = 0; i < len; i++) {
44 if(str[i] ==
'E' || str[i] ==
'e') {
return atof(str); }
45 else if(str[i] ==
'D' || str[i] ==
'd') {
53 int j = 0, leading_minus = 1;
54 for(
int i = 0; i < len; i++) {
55 if(leading_minus && str[i] !=
' ' && str[i] !=
'-') leading_minus = 0;
56 if(!leading_minus && str[i] ==
'-') tmp[j++] =
'E';
57 if(str[i] ==
'+') tmp[j++] =
'E';
64 static void copyChar(
char *out,
const char *in,
int num)
66 for(
int i = 0; i < num; i++) out[i] = in[i];
70 double *x,
double *y,
double *
z)
78 for(
int i = 0; i < 5; i++) {
80 copyChar(tmp[i], &buffer[j + 1], 31);
81 for(
int k = 0; k < 31; k++) {
82 if(tmp[i][k] ==
',') tmp[i][k] =
'\0';
85 while(j < (
int)strlen(buffer) && buffer[j] !=
',') j++;
89 if(!fgets(buffer2,
sizeof(buffer2), fp))
return 0;
91 while(j < (
int)strlen(buffer2) && buffer2[j] !=
',') j++;
92 copyChar(tmp[4], &buffer2[j + 1], 31);
96 for(
int i = 0; i < 5; i++) tmp[i][8] =
'\0';
103 for(
int i = 0; i < 5; i++) tmp[i][16] =
'\0';
108 for(std::size_t i = 0; i <
sizeof(buffer2); i++) buffer2[i] =
'\0';
109 if(!fgets(buffer2,
sizeof(buffer2), fp))
return 0;
124 for(
int i = 0; i <
length; i++)
125 if(field[i] !=
'\0' && field[i] !=
' ' && field[i] !=
'\n' &&
131 static void readLineBDF(
char *buffer,
int format, std::vector<char *> &fields)
133 int cmax = (format == 2) ? 16 : 8;
134 int nmax = (format == 2) ? 4 : 8;
137 for(std::size_t i = 0; i < strlen(buffer); i++) {
138 if(buffer[i] ==
',') fields.push_back(&buffer[i + 1]);
142 for(
int i = 0; i < nmax + 1; i++) {
144 fields.push_back(&buffer[8 + cmax * i]);
150 int &num,
int ®ion,
152 std::map<int, MVertex *> &vertexMap)
154 char buffer2[256], buffer3[256];
155 std::vector<char *> fields;
158 for(std::size_t i = 0; i <
sizeof(buffer2); i++)
159 buffer2[i] = buffer3[i] =
'\0';
163 if(((
int)fields.size() - 2 < abs(numVertices)) ||
164 (numVertices < 0 && (fields.size() == 8 || fields.size() == 9))) {
165 if(fields.size() == 9) fields.pop_back();
168 if(!fgets(buffer2,
sizeof(buffer2), fp))
return 0;
169 if(buffer2[0] ==
'C' || buffer2[0] ==
'E' || buffer2[0] ==
'$') {
179 if(((
int)fields.size() - 2 < abs(numVertices)) ||
180 (numVertices < 0 && (fields.size() == 15 || fields.size() == 17))) {
181 if(fields.size() == 17) fields.pop_back();
182 if(!fgets(buffer3,
sizeof(buffer3), fp))
return 0;
187 if((
int)fields.size() - 2 < abs(numVertices)) {
188 Msg::Error(
"Wrong number of nodes %d for element", fields.size() - 2);
192 int n[30], cmax = (format == 2) ? 16 : 8;
199 for(std::size_t i = 2; i < fields.size(); i++) {
201 n[i - 2] = atoi(tmp);
205 int numCheck = (numVertices > 0) ? numVertices : fields.size() - 2;
207 for(
int i = 0; i < numCheck; i++) {
208 auto it = vertexMap.find(n[i]);
209 if(it == vertexMap.end()) {
220 FILE *fp =
Fopen(name.c_str(),
"r");
222 Msg::Error(
"Unable to open file '%s'", name.c_str());
227 std::map<int, MVertex *> vertexMap;
228 std::map<int, std::vector<MElement *> > elements[7];
233 for(std::size_t i = 0; i <
sizeof(buffer); i++) buffer[i] =
'\0';
234 if(!fgets(buffer,
sizeof(buffer), fp))
break;
235 if(buffer[0] !=
'$') {
236 if(!strncmp(buffer,
"GRID", 4)) {
240 vertexMap[num] =
new MVertex(x, y,
z,
nullptr, num);
248 for(std::size_t i = 0; i <
sizeof(buffer); i++) buffer[i] =
'\0';
249 if(!fgets(buffer,
sizeof(buffer), fp))
break;
250 if(buffer[0] !=
'$') {
253 if(!strncmp(buffer,
"CBAR", 4)) {
257 else if(!strncmp(buffer,
"CROD", 4)) {
261 else if(!strncmp(buffer,
"CBEAM", 5)) {
265 else if(!strncmp(buffer,
"CTRIA3", 6)) {
269 else if(!strncmp(buffer,
"CTRIA6", 6)) {
273 else if(!strncmp(buffer,
"CQUAD4", 6)) {
277 else if(!strncmp(buffer,
"CQUAD8", 6)) {
281 else if(!strncmp(buffer,
"CQUAD", 5)) {
292 else if(!strncmp(buffer,
"CTETRA", 6)) {
304 else if(!strncmp(buffer,
"CHEXA", 5)) {
318 else if(!strncmp(buffer,
"CPENTA", 6)) {
322 elements[5][region].push_back(
331 else if(!strncmp(buffer,
"CPYRAM", 6)) {
338 for(
int i = 0; i < (int)(
sizeof(elements) /
sizeof(elements[0])); i++)
348 bool saveAll,
double scalingFactor)
350 FILE *fp =
Fopen(name.c_str(),
"w");
352 Msg::Error(
"Unable to open file '%s'", name.c_str());
360 fprintf(fp,
"$ Created by Gmsh\n");
362 std::vector<GEntity *> entities;
366 for(std::size_t i = 0; i < entities.size(); i++)
367 for(std::size_t j = 0; j < entities[i]->mesh_vertices.size(); j++)
368 entities[i]->mesh_vertices[j]->
writeBDF(fp, format, scalingFactor);
371 for(std::size_t i = 0; i < entities.size(); i++)
372 for(std::size_t j = 0; j < entities[i]->getNumMeshElements(); j++) {
373 int numPhys = entities[i]->physicals.size();
374 if(saveAll || numPhys)
375 entities[i]->getMeshElement(j)->writeBDF(
376 fp, format, elementTagType, entities[i]->tag(),
377 numPhys ? entities[i]->physicals[0] : 0);
380 fprintf(fp,
"ENDDATA\n");