8 #include "GmshConfig.h"
22 #if defined(HAVE_MESH)
26 #if defined(HAVE_ONELAB)
31 const std::string &fileNameOrEmpty,
32 const std::string &lang)
35 if(!text.empty()) printf(
"%s: %s\n", lang.c_str(), text.c_str());
39 const std::string &fileName = fileNameOrEmpty;
40 if(fileName.empty()) {
46 Msg::Debug(
"Adding `%s' to file `%s'", text.c_str(), fileName.c_str());
48 std::string noExt = split[0] + split[1], ext = split[2];
50 static bool proceed =
false;
52 if(ext.size() && ext !=
".geo" && ext !=
".GEO") {
53 std::ostringstream sstream;
54 sstream <<
"A scripting command is going to be appended to a non-`.geo' "
55 "file. Are\nyou sure you want to proceed?\n\n"
56 "You probably want to create a new `.geo' file containing the "
58 << split[1] + split[2]
59 <<
"\";' and use that file instead.\n\n"
60 "(To disable this warning in the future, select `Enable "
61 "expert mode'\nin the option dialog.)";
63 "Proceed as is",
"Create new `.geo' file");
65 std::string newFileName = split[0] + split[1] +
".geo";
68 std::ostringstream sstream;
69 sstream <<
"File '" << newFileName
70 <<
"' already exists.\n\nDo you want to replace it?";
75 FILE *fp =
Fopen(newFileName.c_str(),
"w");
77 Msg::Error(
"Unable to open file '%s'", newFileName.c_str());
80 fprintf(fp,
"Merge \"%s\";\n//+\n%s\n", (split[1] + split[2]).c_str(),
93 #if defined(HAVE_PARSER)
94 std::string tmpFileName =
98 if(!(tmp_file =
Fopen(tmpFileName.c_str(),
"w"))) {
99 Msg::Error(
"Unable to open temporary file '%s'", tmpFileName.c_str());
102 fprintf(tmp_file,
"%s\n", text.c_str());
115 FILE *fp =
Fopen(fileName.c_str(),
"a");
117 Msg::Error(
"Unable to open file '%s'", fileName.c_str());
120 fprintf(fp,
"//+\n%s\n", text.c_str());
123 Msg::Error(
"GEO file creation not available without Gmsh parser");
127 if(text.find(
"Physical") != std::string::npos) {
132 else if(text.find(
"MeshSize") != std::string::npos) {
147 t.open(fileName.c_str(), std::ifstream::in);
148 std::stringstream buffer;
150 std::string s(buffer.str());
151 int found = (int)s.rfind(
"//+");
152 if(found != (
int)std::string::npos) { s.erase(found); }
154 Msg::Warning(
"Could not find last command in script `%s'",
158 FILE *fp =
Fopen(fileName.c_str(),
"w");
160 fprintf(fp,
"%s", s.c_str());
164 Msg::Error(
"Could not open file `%s'", fileName.c_str());
171 std::ostringstream sstream;
172 if(lang ==
"py" || lang ==
"jl")
174 else if(lang ==
"geo" || lang ==
"cpp")
176 for(
int i = 0; i <
List_Nbr(list); i++) {
179 if(i) sstream <<
", ";
182 if(lang ==
"py" || lang ==
"jl")
184 else if(lang ==
"geo" || lang ==
"cpp")
186 return sstream.str();
190 const std::string &lang)
192 std::ostringstream sstream;
193 if(lang ==
"py" || lang ==
"jl")
195 else if(lang ==
"geo" || lang ==
"cpp")
197 for(std::size_t i = 0; i < v.size(); i++) {
198 if(i) sstream <<
", ";
201 if(lang ==
"py" || lang ==
"jl")
203 else if(lang ==
"geo" || lang ==
"cpp")
205 return sstream.str();
209 const std::string &lang)
211 std::ostringstream sstream;
212 if(lang ==
"py" || lang ==
"jl")
214 else if(lang ==
"cpp")
217 for(std::size_t i = 0; i < l.size(); i++) {
220 case 0: sstream <<
"Point{" << l[i].second <<
"}; ";
break;
221 case 1: sstream <<
"Curve{" << l[i].second <<
"}; ";
break;
222 case 2: sstream <<
"Surface{" << l[i].second <<
"}; ";
break;
223 case 3: sstream <<
"Volume{" << l[i].second <<
"}; ";
break;
226 else if(lang ==
"py" || lang ==
"jl") {
227 if(i) sstream <<
", ";
228 sstream <<
"(" << l[i].first <<
", " << l[i].second <<
")";
230 else if(lang ==
"cpp") {
231 if(i) sstream <<
", ";
232 sstream <<
"{" << l[i].first <<
", " << l[i].second <<
"}";
235 Msg::Error(
"Unhandled language ('%s') in script generator", lang.c_str());
239 if(lang ==
"py" || lang ==
"jl")
241 else if(lang ==
"cpp")
243 return sstream.str();
248 static void checkOCC(std::ostringstream &sstream,
const std::string &lang)
251 #if defined(HAVE_PARSER)
253 sstream <<
"SetFactory(\"OpenCASCADE\");\n";
261 if(factory ==
"OpenCASCADE")
266 std::ostringstream sstream;
268 sstream <<
"SetFactory(\"" << factory <<
"\");";
274 static std::string
api(
const std::string &name,
const std::string &args,
275 const std::string &lang)
277 if(lang ==
"py" || lang ==
"jl")
279 else if(lang ==
"cpp")
282 Msg::Error(
"Unhandled language ('%s') in script generator", lang.c_str());
287 const std::string &lc)
290 std::ostringstream sstream;
292 sstream <<
"MeshSize " <<
vector2String(l, lang) <<
" = " << lc <<
";";
295 std::vector<std::pair<int, int> > dimTags;
296 for(
auto t : l) dimTags.push_back(std::make_pair(0, t));
297 sstream <<
api(
"gmsh/model/mesh/setSize",
305 const std::vector<int> &l)
308 std::ostringstream sstream;
310 sstream <<
"Recombine Surface " <<
vector2String(l, lang) <<
";";
315 sstream <<
api(
"gmsh/model/occ/synchronize",
"", lang) <<
"\n"
323 const std::string &type,
324 const std::string &typearg,
325 const std::string &pts)
328 std::ostringstream sstream;
330 sstream <<
"Transfinite Curve {";
331 for(std::size_t i = 0; i < l.size(); i++) {
332 if(i) sstream <<
", ";
335 sstream <<
"} = " << pts;
336 if(typearg.size()) sstream <<
" Using " << type <<
" " << typearg;
347 const std::string &fileName,
348 const std::string &dir)
351 std::ostringstream sstream;
353 sstream <<
"Transfinite Surface {" << l[0] <<
"}";
356 for(std::size_t i = 1; i < l.size(); i++) {
357 if(i > 1) sstream <<
", ";
362 if(dir !=
"Left") sstream <<
" " << dir;
373 const std::string &fileName)
376 std::ostringstream sstream;
378 sstream <<
"Transfinite Volume{" << l[0] <<
"}";
381 for(std::size_t i = 1; i < l.size(); i++) {
382 if(i > 1) sstream <<
", ";
396 void scriptEmbed(
const std::string &fileName,
const std::string &what,
397 std::vector<int> &l,
int dim,
int tag)
400 std::ostringstream sstream;
404 sstream <<
"Surface{";
406 sstream <<
"Volume{";
407 sstream << tag <<
"};";
417 const std::string &label,
const std::string &path,
418 const std::string &fileName)
421 std::ostringstream sstream;
423 sstream << par <<
" = DefineNumber[ " << value;
424 sstream <<
", Name \"";
425 if(path.size() && label.size())
426 sstream << path <<
"/" << label;
428 sstream << path <<
"/" << par;
429 else if(label.size())
443 const std::string &y,
const std::string &
z,
444 const std::string &lc)
447 std::ostringstream sstream;
450 <<
") = {" << x <<
", " << y <<
", " <<
z;
451 if(lc.size()) sstream <<
", " << lc;
455 std::string addPointStr =
"gmsh/model/" +
currentFactory +
"/addPoint";
456 std::ostringstream args;
457 args << x <<
", " << y <<
", " <<
z;
458 if(lc.size()) args <<
", " << lc;
459 sstream <<
api(addPointStr, args.str(), lang);
466 const std::string &option_value,
int option_type,
467 const std::string &fileName)
469 #if defined(HAVE_MESH)
471 std::ostringstream sstream;
473 sstream <<
"Field[" << field_id <<
"]." << option_name <<
" = "
474 << option_value <<
";";
477 std::ostringstream args;
478 switch(option_type) {
482 args << field_id <<
", \"" << option_name <<
"\", " << option_value;
483 sstream <<
api(
"gmsh/model/mesh/field/setNumber", args.str(), lang);
487 args << field_id <<
", \"" << option_name <<
"\", " << option_value;
488 sstream <<
api(
"gmsh/model/mesh/field/setString", args.str(), lang);
492 std::string list_val = option_value;
493 if(lang ==
"py" || lang ==
"jl") {
497 args << field_id <<
", \"" << option_name <<
"\", " << list_val;
498 sstream <<
api(
"gmsh/model/mesh/field/setNumbers", args.str(), lang);
508 const std::string &fileName)
511 std::ostringstream sstream;
513 sstream <<
"Field[" << field_id <<
"] = " << type_name <<
";";
516 std::ostringstream args;
517 args <<
"\"" << type_name <<
"\"" <<
", " << field_id;
518 sstream <<
api(
"gmsh/model/mesh/field/add", args.str(), lang);
527 std::ostringstream sstream;
528 if(lang ==
"geo") { sstream <<
"Delete Field [" << field_id <<
"];"; }
530 sstream <<
api(
"gmsh/model/mesh/field/remove",
531 std::to_string(field_id), lang);
540 std::ostringstream sstream;
541 if(lang ==
"geo") { sstream <<
"Background Field = " << field_id <<
";"; }
543 sstream <<
api(
"gmsh/model/mesh/field/setAsBackgroundMesh",
544 std::to_string(field_id), lang);
551 const std::string &fileName)
554 std::ostringstream sstream;
558 for(std::size_t i = 0; i < p.size(); i++) {
559 if(i) sstream <<
", ";
574 std::ostringstream sstream;
577 <<
") = {" << p1 <<
", " << p2 <<
", " << p3 <<
"};";
587 const std::string &fileName)
590 std::ostringstream sstream;
593 <<
") = {" << p1 <<
", " << p2 <<
", " << p3 <<
", " << p4
612 std::ostringstream sstream;
614 sstream <<
"Curve Loop(" << *numloop <<
") = " <<
list2String(list, lang)
625 const std::string &fileName)
628 std::ostringstream sstream;
650 std::ostringstream sstream;
652 sstream <<
"Surface Loop(" << *numloop
665 std::ostringstream sstream;
678 const std::string &what,
679 const std::vector<int> &l,
680 const std::string &name,
int forceTag,
681 bool append,
const std::string &mode)
684 std::ostringstream sstream;
686 sstream <<
"Physical " << what <<
"(";
688 sstream <<
"\"" << name <<
"\"";
689 if(forceTag) sstream <<
", " << forceTag;
712 const std::vector<int> &l)
715 std::ostringstream sstream;
717 sstream <<
"Compound " << type <<
" " <<
vector2String(l, lang) <<
";";
727 const std::string &y,
const std::string &
z,
728 const std::string &r,
const std::string &alpha1,
729 const std::string &alpha2)
732 std::ostringstream sstream;
736 <<
") = {" << x <<
", " << y <<
", " <<
z <<
", " << r;
737 if(alpha1.size()) sstream <<
", " << alpha1;
738 if(alpha1.size() && alpha2.size()) sstream <<
", " << alpha2;
749 const std::string &y,
const std::string &
z,
750 const std::string &rx,
const std::string &ry,
751 const std::string &alpha1,
const std::string &alpha2)
754 std::ostringstream sstream;
758 <<
") = {" << x <<
", " << y <<
", " <<
z <<
", " << rx <<
", "
760 if(alpha1.size()) sstream <<
", " << alpha1;
761 if(alpha1.size() && alpha2.size()) sstream <<
", " << alpha2;
772 const std::string &y,
const std::string &
z,
773 const std::string &rx,
const std::string &ry)
776 std::ostringstream sstream;
780 <<
") = {" << x <<
", " << y <<
", " <<
z <<
", " << rx <<
", "
791 const std::string &y,
const std::string &
z,
792 const std::string &dx,
const std::string &dy,
793 const std::string &roundedRadius)
796 std::ostringstream sstream;
799 sstream <<
"Rectangle("
801 << x <<
", " << y <<
", " <<
z <<
", " << dx <<
", " << dy;
802 if(roundedRadius.size()) sstream <<
", " << roundedRadius;
813 const std::string &y,
const std::string &
z,
814 const std::string &r,
const std::string &alpha1,
815 const std::string &alpha2,
const std::string &alpha3)
818 std::ostringstream sstream;
822 <<
") = {" << x <<
", " << y <<
", " <<
z <<
", " << r;
823 if(alpha1.size()) sstream <<
", " << alpha1;
824 if(alpha1.size() && alpha2.size()) sstream <<
", " << alpha2;
825 if(alpha1.size() && alpha2.size() && alpha3.size())
826 sstream <<
", " << alpha3;
837 const std::string &y,
const std::string &
z,
838 const std::string &dx,
const std::string &dy,
839 const std::string &dz,
const std::string &r,
840 const std::string &alpha)
843 std::ostringstream sstream;
847 <<
") = {" << x <<
", " << y <<
", " <<
z <<
", " << dx <<
", "
848 << dy <<
", " << dz <<
", " << r;
849 if(alpha.size()) sstream <<
", " << alpha;
860 const std::string &y,
const std::string &
z,
861 const std::string &dx,
const std::string &dy,
862 const std::string &dz)
865 std::ostringstream sstream;
869 <<
") = {" << x <<
", " << y <<
", " <<
z <<
", " << dx <<
", "
870 << dy <<
", " << dz <<
"};";
880 const std::string &y,
const std::string &
z,
881 const std::string &r1,
const std::string &r2,
882 const std::string &alpha)
885 std::ostringstream sstream;
889 <<
") = {" << x <<
", " << y <<
", " <<
z <<
", " << r1 <<
", "
891 if(alpha.size()) sstream <<
", " << alpha;
902 const std::string &y,
const std::string &
z,
903 const std::string &dx,
const std::string &dy,
904 const std::string &dz,
const std::string &r1,
905 const std::string &r2,
const std::string &alpha)
908 std::ostringstream sstream;
912 <<
") = {" << x <<
", " << y <<
", " <<
z <<
", " << dx <<
", "
913 << dy <<
", " << dz <<
", " << r1 <<
", " << r2;
914 if(alpha.size()) sstream <<
", " << alpha;
925 const std::string &y,
const std::string &
z,
926 const std::string &dx,
const std::string &dy,
927 const std::string &dz,
const std::string <x)
930 std::ostringstream sstream;
934 <<
") = {" << x <<
", " << y <<
", " <<
z <<
", " << dx <<
", "
935 << dy <<
", " << dz <<
", " << ltx <<
"};";
945 const std::vector<std::pair<int, int> > &l,
946 const std::string &tx,
const std::string &ty,
947 const std::string &tz,
bool duplicata)
950 std::ostringstream sstream;
952 sstream <<
"Translate {" << tx <<
", " << ty <<
", " << tz <<
"} {\n ";
953 if(duplicata) sstream <<
"Duplicata { ";
955 if(duplicata) sstream <<
"}";
966 const std::vector<std::pair<int, int> > &l,
967 const std::string &ax,
const std::string &ay,
968 const std::string &az,
const std::string &px,
969 const std::string &py,
const std::string &pz,
970 const std::string &
angle,
bool duplicata)
973 std::ostringstream sstream;
975 sstream <<
"Rotate {{" << ax <<
", " << ay <<
", " << az <<
"}, {" << px
976 <<
", " << py <<
", " << pz <<
"}, " <<
angle <<
"} {\n ";
977 if(duplicata) sstream <<
"Duplicata { ";
979 if(duplicata) sstream <<
"}";
990 const std::vector<std::pair<int, int> > &l,
991 const std::string &cx,
const std::string &cy,
992 const std::string &cz,
const std::string &sx,
993 const std::string &sy,
const std::string &sz,
bool duplicata)
996 std::ostringstream sstream;
998 sstream <<
"Dilate {{" << cx <<
", " << cy <<
", " << cz <<
"}, {" << sx
999 <<
", " << sy <<
", " << sz <<
"}} {\n ";
1000 if(duplicata) sstream <<
"Duplicata { ";
1002 if(duplicata) sstream <<
"}";
1013 const std::vector<std::pair<int, int> > &l,
1014 const std::string &sa,
const std::string &sb,
1015 const std::string &sc,
const std::string &sd,
bool duplicata)
1018 std::ostringstream sstream;
1020 sstream <<
"Symmetry {" << sa <<
", " << sb <<
", " << sc <<
", " << sd
1022 if(duplicata) sstream <<
"Duplicata { ";
1024 if(duplicata) sstream <<
"}";
1035 const std::vector<std::pair<int, int> > &l,
1036 const std::string &tx,
const std::string &ty,
1038 const std::string &layers,
bool recombineMesh)
1041 std::ostringstream sstream;
1043 sstream <<
"Extrude {" << tx <<
", " << ty <<
", " << tz <<
"} {\n "
1046 sstream <<
"Layers {" << layers <<
"}; ";
1047 if(recombineMesh) sstream <<
"Recombine;";
1059 const std::vector<std::pair<int, int> > &l,
1060 const std::string &ax,
const std::string &ay,
1061 const std::string &az,
const std::string &px,
1062 const std::string &py,
const std::string &pz,
1064 const std::string &layers,
bool recombineMesh)
1067 std::ostringstream sstream;
1069 sstream <<
"Extrude {{" << ax <<
", " << ay <<
", " << az <<
"}, {" << px
1070 <<
", " << py <<
", " << pz <<
"}, " <<
angle <<
"} {\n "
1073 sstream <<
"Layers{" << layers <<
"}; ";
1074 if(recombineMesh) sstream <<
"Recombine;";
1086 const std::vector<std::pair<int, int> > &l,
1087 const std::vector<int> &l2)
1090 std::ostringstream sstream;
1097 sstream <<
"Wire(" << wire <<
") = " <<
vector2String(l2, lang) <<
";\n";
1098 sstream <<
"Extrude { " <<
dimTags2String(l, lang) <<
"} Using Wire {"
1109 const std::string &fileName)
1112 std::ostringstream sstream;
1114 sstream <<
"Split Curve {" << edge_id <<
"} Point "
1125 const std::vector<std::pair<int, int> > &
object,
1126 const std::vector<std::pair<int, int> > &tool,
1127 int deleteObject,
int deleteTool)
1130 std::ostringstream sstream;
1134 if(deleteObject) sstream <<
"Delete; ";
1136 if(deleteTool) sstream <<
"Delete; ";
1146 void scriptFillet(
const std::string &fileName,
const std::vector<int> ®ions,
1147 const std::vector<int> &
edges,
const std::string &radius)
1150 std::ostringstream sstream;
1174 const std::vector<std::pair<int, int> > &l,
1178 std::ostringstream sstream;
1180 if(recursive) sstream <<
"Recursive ";
1206 const std::string &fileName)
1209 std::ostringstream sstream;