1
// $Id: GeoStringInterface.cpp,v 1.8 2007/02/07 13:06:49 geuzaine Exp $
3
// Copyright (C) 1997-2007 C. Geuzaine, J.-F. Remacle
5
// This program is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2 of the License, or
8
// (at your option) any later version.
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// Please report all bugs and problems to <gmsh@geuz.org>.
25
#include "GeoStringInterface.h"
32
extern GModel *GMODEL;
34
#define BUFFSIZE 128000
36
// Some old systems don't have snprintf... Just call sprintf instead.
38
#if defined(HAVE_NO_SNPRINTF)
39
int snprintf(char *str, size_t size, const char* fmt, ...){
42
int ret = vsprintf(str, fmt, args);
48
double evaluate_scalarfunction(char *var, double val, char *funct)
53
if(!(yyin = fopen(CTX.tmp_filename_fullpath, "w"))) {
54
Msg(GERROR, "Unable to open temporary file '%s'", CTX.tmp_filename_fullpath);
58
// pose "variable = function" and evaluate function
59
fprintf(yyin, "%s = %.16g ;\n", var, val);
60
fprintf(yyin, "ValeurTemporaire__ = %s ;\n", funct);
62
yyin = fopen(CTX.tmp_filename_fullpath, "r");
70
Symbol TheSymbol, *TheSymbol_P;
71
TheSymbol.Name = (char *)Malloc(100*sizeof(char));
72
strcpy(TheSymbol.Name, "ValeurTemporaire__");
73
if(!(TheSymbol_P = (Symbol*)Tree_PQuery(Symbol_T, &TheSymbol))) {
78
return *(double *)List_Pointer(TheSymbol_P->val, 0);
81
void add_infile(char *text, char *fich, bool deleted_something)
83
if(!(yyin = fopen(CTX.tmp_filename_fullpath, "w"))) {
84
Msg(GERROR, "Unable to open temporary file '%s'", CTX.tmp_filename_fullpath);
88
fprintf(yyin, "%s\n", text);
89
Msg(STATUS2, "%s", text);
91
yyin = fopen(CTX.tmp_filename_fullpath, "r");
97
if(deleted_something){
98
// we need to start from scratch since the command just parsed
99
// could have deleted some entities
102
GMODEL->importTHEM();
103
CTX.mesh.changed = ENT_ALL;
106
if(!(file = fopen(fich, "a"))) {
107
Msg(GERROR, "Unable to open file '%s'", fich);
111
if(!CTX.expert_mode) {
112
char base[256], ext[256];
113
SplitFileName(fich, base, ext);
114
if(strlen(ext) && strcmp(ext, ".geo") && strcmp(ext, ".GEO")){
117
"A scripting command is going to be appended to a non-`.geo' file.\n\n"
118
"Are you sure you want to proceed?\n\n"
119
"(You might want to create a new `.geo' file containing the command\n\n"
121
"and use that file instead. To disable this warning in the future, select\n"
122
"`Enable expert mode' in the option dialog.)", fich);
123
if(!GetBinaryAnswer(question, "Proceed", "Cancel", false)){
130
fprintf(file, "%s\n", text);
134
void coherence(char *fich)
136
add_infile("Coherence;", fich, true);
139
void strncat_list(char *text, List_T *list)
141
char text2[BUFFSIZE];
142
for(int i = 0; i < List_Nbr(list); i++){
144
List_Read(list, i, &num);
146
snprintf(text2, BUFFSIZE, "%d", num);
148
snprintf(text2, BUFFSIZE, ",%d", num);
149
strncat(text, text2, BUFFSIZE-strlen(text));
153
void delet(List_T *list, char *fich, char *what)
157
snprintf(text, BUFFSIZE, "Delete {\n %s{", what);
158
strncat_list(text, list);
159
strncat(text, "};\n}", BUFFSIZE-strlen(text));
160
add_infile(text, fich, true);
163
void add_trsfsurf(int N, int *l, char *fich, char *dir)
165
char text[BUFFSIZE], text2[BUFFSIZE];
167
snprintf(text, BUFFSIZE, "Transfinite Surface {%d} = {", l[0]);
168
for(int i = 1; i < N; i++) {
170
snprintf(text2, BUFFSIZE, "%d", l[i]);
172
snprintf(text2, BUFFSIZE, ",%d", l[i]);
173
strncat(text, text2, BUFFSIZE-strlen(text));
175
if (!strcmp(dir,"Left"))
176
snprintf(text2, BUFFSIZE, "};");
178
snprintf(text2, BUFFSIZE, "} %s;",dir);
180
strncat(text, text2, BUFFSIZE-strlen(text));
181
add_infile(text, fich);
184
void add_charlength(List_T *list, char *fich, char *lc)
188
snprintf(text, BUFFSIZE, "Characteristic Length {");
189
strncat_list(text, list);
190
strncat(text, "} = ", BUFFSIZE-strlen(text));
191
strncat(text, lc, BUFFSIZE-strlen(text));
192
strncat(text, ";", BUFFSIZE-strlen(text));
193
add_infile(text, fich);
196
void add_recosurf(List_T *list, char *fich)
200
snprintf(text, BUFFSIZE, "Recombine Surface {");
201
strncat_list(text, list);
202
strncat(text, "};", BUFFSIZE-strlen(text));
203
add_infile(text, fich);
206
void add_trsfline(int N, int *l, char *fich, char *type, char *typearg, char *pts)
208
char text[BUFFSIZE], text2[BUFFSIZE];
210
snprintf(text, BUFFSIZE, "Transfinite Line {");
211
for(int i = 0; i < N; i++) {
213
snprintf(text2, BUFFSIZE, "%d", l[i]);
215
snprintf(text2, BUFFSIZE, ",%d", l[i]);
216
strncat(text, text2, BUFFSIZE-strlen(text));
219
snprintf(text2, BUFFSIZE, "} = %s Using %s %s;", pts, type, typearg);
221
snprintf(text2, BUFFSIZE, "} = %s;", pts);
222
strncat(text, text2, BUFFSIZE-strlen(text));
223
add_infile(text, fich);
226
void add_param(char *par, char *value, char *fich)
229
snprintf(text, BUFFSIZE, "%s = %s;", par, value);
230
add_infile(text, fich);
233
void add_point(char *fich, char *x, char *y, char *z, char *lc)
237
snprintf(text, BUFFSIZE, "Point(%d) = {%s,%s,%s,%s};", ip, x, y, z, lc);
238
add_infile(text, fich);
241
void add_attractor(char *fich, int ip, int typ, char *ax, char *ay, char *ad)
245
snprintf(text, BUFFSIZE, "Attractor Point {%d} = {%s,%s,%s} = ;", ip, ax, ay, ad);
248
snprintf(text, BUFFSIZE, "Attractor Line {%d} = {%s,%s,%s};", ip, ax, ay, ad);
251
snprintf(text, BUFFSIZE, "Attractor Surface {%d} = {%s,%s,%s};", ip, ax, ay, ad);
253
add_infile(text, fich);
257
void add_line(int p1, int p2, char *fich)
261
List_T *list = List_Create(2, 2, sizeof(int));
264
if((recognize_seg(MSH_SEGM_LINE, list, &iseg))) {
270
snprintf(text, BUFFSIZE, "Line(%d) = {%d,%d};", NEWLINE(), p1, p2);
271
add_infile(text, fich);
274
void add_circ(int p1, int p2, int p3, char *fich)
278
snprintf(text, BUFFSIZE, "Circle(%d) = {%d,%d,%d};", NEWLINE(), p1, p2, p3);
279
add_infile(text, fich);
282
void add_ell(int p1, int p2, int p3, int p4, char *fich)
286
snprintf(text, BUFFSIZE, "Ellipse(%d) = {%d,%d,%d,%d};", NEWLINE(), p1, p2,
288
add_infile(text, fich);
291
void add_spline(int N, int *p, char *fich)
293
char text[BUFFSIZE], text2[BUFFSIZE];
295
snprintf(text, BUFFSIZE, "CatmullRom(%d) = {", NEWLINE());
296
for(int i = 0; i < N; i++) {
298
snprintf(text2, BUFFSIZE, "%d,", p[i]);
300
snprintf(text2, BUFFSIZE, "%d};", p[i]);
301
strncat(text, text2, BUFFSIZE-strlen(text));
303
add_infile(text, fich);
306
void add_bezier(int N, int *p, char *fich)
308
char text[BUFFSIZE], text2[BUFFSIZE];
310
snprintf(text, BUFFSIZE, "Bezier(%d) = {", NEWLINE());
311
for(int i = 0; i < N; i++) {
313
snprintf(text2, BUFFSIZE, "%d,", p[i]);
315
snprintf(text2, BUFFSIZE, "%d};", p[i]);
316
strncat(text, text2, BUFFSIZE-strlen(text));
318
add_infile(text, fich);
322
void add_bspline(int N, int *p, char *fich)
324
char text[BUFFSIZE], text2[BUFFSIZE];
326
snprintf(text, BUFFSIZE, "BSpline(%d) = {", NEWLINE());
327
for(int i = 0; i < N; i++) {
329
snprintf(text2, BUFFSIZE, "%d,", p[i]);
331
snprintf(text2, BUFFSIZE, "%d};", p[i]);
332
strncat(text, text2, BUFFSIZE-strlen(text));
334
add_infile(text, fich);
337
void add_multline(int N, int *p, char *fich)
339
char text[BUFFSIZE], text2[BUFFSIZE];
342
List_T *list = List_Create(N, 2, sizeof(int));
343
for(int i = 0; i < N; i++)
344
List_Add(list, &p[i]);
345
if((recognize_seg(MSH_SEGM_LINE, list, &iseg))) {
351
snprintf(text, BUFFSIZE, "Line(%d) = {", NEWLINE());
352
for(int i = 0; i < N; i++) {
354
snprintf(text2, BUFFSIZE, "%d,", p[i]);
356
snprintf(text2, BUFFSIZE, "%d};", p[i]);
357
strncat(text, text2, BUFFSIZE-strlen(text));
359
add_infile(text, fich);
362
void add_lineloop(List_T *list, char *fich, int *numloop)
366
if((recognize_loop(list, numloop)))
369
*numloop = NEWLINELOOP();
370
snprintf(text, BUFFSIZE, "Line Loop(%d) = {", *numloop);
371
strncat_list(text, list);
372
strncat(text, "};", BUFFSIZE-strlen(text));
373
add_infile(text, fich);
377
void add_surf(List_T *list, char *fich, int support, int typ)
382
snprintf(text, BUFFSIZE, "Ruled Surface(%d) = {", NEWSURFACE());
385
snprintf(text, BUFFSIZE, "Plane Surface(%d) = {", NEWSURFACE());
387
strncat_list(text, list);
388
strncat(text, "};", BUFFSIZE-strlen(text));
389
add_infile(text, fich);
392
void add_surfloop(List_T *list, char *fich, int *numvol)
396
if((recognize_surfloop(list, numvol)))
399
*numvol = NEWSURFACELOOP();
400
snprintf(text, BUFFSIZE, "Surface Loop(%d) = {", *numvol);
401
strncat_list(text, list);
402
strncat(text, "};", BUFFSIZE-strlen(text));
403
add_infile(text, fich);
406
void add_vol(List_T *list, char *fich)
410
snprintf(text, BUFFSIZE, "Volume(%d) = {", NEWVOLUME());
411
strncat_list(text, list);
412
strncat(text, "};", BUFFSIZE-strlen(text));
413
add_infile(text, fich);
416
void add_trsfvol(int N, int *l, char *fich)
418
char text[BUFFSIZE], text2[BUFFSIZE];
420
snprintf(text, BUFFSIZE, "Transfinite Volume{%d} = {", l[0]);
421
for(int i = 1; i < N; i++) {
423
snprintf(text2, BUFFSIZE, "%d", l[i]);
425
snprintf(text2, BUFFSIZE, ",%d", l[i]);
426
strncat(text, text2, BUFFSIZE-strlen(text));
428
snprintf(text2, BUFFSIZE, "};");
429
strncat(text, text2, BUFFSIZE-strlen(text));
430
add_infile(text, fich);
433
int add_physical(List_T *list, char *fich, int type)
436
int num = NEWPHYSICAL();
440
snprintf(text, BUFFSIZE, "Physical Point(%d) = {", num);
443
snprintf(text, BUFFSIZE, "Physical Line(%d) = {", num);
446
snprintf(text, BUFFSIZE, "Physical Surface(%d) = {", num);
449
snprintf(text, BUFFSIZE, "Physical Volume(%d) = {", num);
453
strncat_list(text, list);
454
strncat(text, "};", BUFFSIZE-strlen(text));
455
add_infile(text, fich);
460
void translate(int add, List_T *list, char *fich, char *what, char *tx, char *ty, char *tz)
465
snprintf(text, BUFFSIZE, "Translate {%s,%s,%s} {\n Duplicata { %s{", tx, ty, tz, what);
467
snprintf(text, BUFFSIZE, "Translate {%s,%s,%s} {\n %s{", tx, ty, tz, what);
469
strncat_list(text, list);
472
strncat(text, "}; }\n}", BUFFSIZE-strlen(text));
474
strncat(text, "};\n}", BUFFSIZE-strlen(text));
476
add_infile(text, fich);
479
void rotate(int add, List_T *list, char *fich, char *what, char *ax, char *ay, char *az,
480
char *px, char *py, char *pz, char *angle)
485
snprintf(text, BUFFSIZE, "Rotate {{%s,%s,%s}, {%s,%s,%s}, %s} {\n Duplicata { %s{",
486
ax, ay, az, px, py, pz, angle, what);
488
snprintf(text, BUFFSIZE, "Rotate {{%s,%s,%s}, {%s,%s,%s}, %s} {\n %s{",
489
ax, ay, az, px, py, pz, angle, what);
491
strncat_list(text, list);
494
strncat(text, "}; }\n}", BUFFSIZE-strlen(text));
496
strncat(text, "};\n}", BUFFSIZE-strlen(text));
498
add_infile(text, fich);
501
void dilate(int add, List_T *list, char *fich, char *what, char *dx, char *dy, char *dz, char *df)
506
snprintf(text, BUFFSIZE, "Dilate {{%s,%s,%s}, %s} {\n Duplicata { %s{",
507
dx, dy, dz, df, what);
509
snprintf(text, BUFFSIZE, "Dilate {{%s,%s,%s}, %s} {\n %s{",
510
dx, dy, dz, df, what);
512
strncat_list(text, list);
515
strncat(text, "}; }\n}", BUFFSIZE-strlen(text));
517
strncat(text, "};\n}", BUFFSIZE-strlen(text));
519
add_infile(text, fich);
522
void symmetry(int add, List_T *list, char *fich, char *what, char *sa, char *sb, char *sc, char *sd)
527
snprintf(text, BUFFSIZE, "Symmetry {%s,%s,%s,%s} {\n Duplicata { %s{",
528
sa, sb, sc, sd, what);
530
snprintf(text, BUFFSIZE, "Symmetry {%s,%s,%s,%s} {\n %s{",
531
sa, sb, sc, sd, what);
533
strncat_list(text, list);
536
strncat(text, "}; }\n}", BUFFSIZE-strlen(text));
538
strncat(text, "};\n}", BUFFSIZE-strlen(text));
540
add_infile(text, fich);
543
void extrude(List_T *list, char *fich, char *what, char *tx, char *ty, char *tz)
547
snprintf(text, BUFFSIZE, "Extrude {%s,%s,%s} {\n %s{", tx, ty, tz, what);
548
strncat_list(text, list);
549
strncat(text, "};\n}", BUFFSIZE-strlen(text));
550
add_infile(text, fich);
553
void protude(List_T *list, char *fich, char *what, char *ax, char *ay, char *az,
554
char *px, char *py, char *pz, char *angle)
558
snprintf(text, BUFFSIZE, "Extrude {{%s,%s,%s}, {%s,%s,%s}, %s} {\n %s{",
559
ax, ay, az, px, py, pz, angle, what);
560
strncat_list(text, list);
561
strncat(text, "};\n}", BUFFSIZE-strlen(text));
562
add_infile(text, fich);