From 749c56f1e5956a84ae82e5d08998ba2f77dfb68c Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@uliege.be>
Date: Wed, 29 Dec 2021 14:32:42 +0100
Subject: [PATCH] make all interactive api examples repond to -nopopup so they
 can be tested in batch mode

---
 examples/api/CMakeLists.txt            | 12 ++++++------
 examples/api/adapt_mesh.cpp            |  7 ++++++-
 examples/api/aneurysm.py               |  4 +++-
 examples/api/closest_point.py          |  3 ++-
 examples/api/copy_mesh.py              |  5 ++++-
 examples/api/edges.cpp                 |  4 +++-
 examples/api/faces.cpp                 |  4 +++-
 examples/api/fragment_surfaces.cpp     |  7 +++++--
 examples/api/gui.cpp                   |  4 ++++
 examples/api/gui.jl                    |  4 ++++
 examples/api/gui.py                    |  9 +++++----
 examples/api/mirror_mesh.py            |  4 +++-
 examples/api/naca_boundary_layer_2d.py |  3 ++-
 examples/api/naca_boundary_layer_3d.py |  7 ++++---
 examples/api/onelab_test.jl            |  4 +++-
 examples/api/onelab_test.py            |  1 -
 examples/api/partition.cpp             |  5 ++++-
 examples/api/raw_tetrahedralization.py |  3 ++-
 examples/api/raw_triangulation.py      |  3 ++-
 examples/api/spherical_surf.jl         |  6 +++++-
 examples/api/spline.cpp                |  6 +++++-
 examples/api/split_window.py           |  3 +++
 examples/api/square.cpp                |  9 +++++++--
 examples/api/surface_filling.py        |  7 ++++++-
 examples/api/view_renumbering.py       |  3 ++-
 25 files changed, 93 insertions(+), 34 deletions(-)

diff --git a/examples/api/CMakeLists.txt b/examples/api/CMakeLists.txt
index 35e4902415..6ec8855830 100644
--- a/examples/api/CMakeLists.txt
+++ b/examples/api/CMakeLists.txt
@@ -73,13 +73,13 @@ foreach(EXAMPLE ${EXAMPLES})
             </plist>")
       set_target_properties(${EXAMPLENAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST
                             ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLENAME}.plist)
-      add_test(${EXAMPLENAME} ${EXAMPLENAME}.app/Contents/MacOS/${EXAMPLENAME})
+      add_test(${EXAMPLENAME}_cpp ${EXAMPLENAME}.app/Contents/MacOS/${EXAMPLENAME} -nopopup)
     else()
-      add_test(${EXAMPLENAME} ${EXAMPLENAME})
+      add_test(${EXAMPLENAME}_cpp ${EXAMPLENAME} -nopopup)
     endif()
   else()
     add_executable(${EXAMPLENAME} ${EXAMPLE})
-    add_test(${EXAMPLENAME}_cpp ${EXAMPLENAME})
+    add_test(${EXAMPLENAME}_cpp ${EXAMPLENAME} -nopopup)
   endif()
   target_link_libraries(${EXAMPLENAME} ${GMSH_LIB} ${EXTRA_LIBS})
 endforeach()
@@ -89,10 +89,10 @@ foreach(EXAMPLE ${EXAMPLES})
   get_filename_component(EXAMPLENAME ${EXAMPLE} NAME_WE)
   add_executable(${EXAMPLENAME}c ${EXAMPLE})
   target_link_libraries(${EXAMPLENAME}c ${GMSH_LIB} ${EXTRA_LIBS})
-  add_test(${EXAMPLENAME}_c ${EXAMPLENAME}c)
+  add_test(${EXAMPLENAME}_c ${EXAMPLENAME}c -nopopup)
 endforeach()
 
-find_program(PYTHON python)
+find_program(PYTHON python3)
 if(PYTHON)
   file(GLOB EXAMPLES *.py)
   foreach(EXAMPLE ${EXAMPLES})
@@ -106,6 +106,6 @@ if(JULIA)
   file(GLOB EXAMPLES *.jl)
   foreach(EXAMPLE ${EXAMPLES})
     get_filename_component(EXAMPLENAME ${EXAMPLE} NAME_WE)
-    add_test(${EXAMPLENAME}_jl ${JULIA} ${EXAMPLE})
+    add_test(${EXAMPLENAME}_jl ${JULIA} ${EXAMPLE} -nopopup)
   endforeach()
 endif()
diff --git a/examples/api/adapt_mesh.cpp b/examples/api/adapt_mesh.cpp
index 2c1d18f141..2d337c99c4 100644
--- a/examples/api/adapt_mesh.cpp
+++ b/examples/api/adapt_mesh.cpp
@@ -1,6 +1,7 @@
 #include <gmsh.h>
 #include <cmath>
 #include <map>
+#include <set>
 #include <complex>
 
 class myVertex {
@@ -217,6 +218,10 @@ int main(int argc, char **argv)
   double lc = 0.02;
   int N = 10000;
   bool dumpfiles = false;
+
+  std::set<std::string> args(argv, argv + argc);
+  if(args.count("-nopopup")) argc--;
+
   if(argc > 1) lc = atof(argv[1]);
   if(argc > 2) N = atoi(argv[2]);
   if(argc > 3) dumpfiles = atoi(argv[3]);
@@ -287,7 +292,7 @@ int main(int argc, char **argv)
   if(dumpfiles) gmsh::view::write(err2_view, "err2.pos");
 
   // show everything in the gui
-  gmsh::fltk::run();
+  if(!args.count("-nopopup")) gmsh::fltk::run();
 
   gmsh::finalize();
   return 0;
diff --git a/examples/api/aneurysm.py b/examples/api/aneurysm.py
index 56de81ecf7..5ad2a9cd9f 100644
--- a/examples/api/aneurysm.py
+++ b/examples/api/aneurysm.py
@@ -1,12 +1,14 @@
 import gmsh
 import sys
+import os
 import math
 
 gmsh.initialize(sys.argv)
 
 # merge STL, create surface patches that are reparametrizable (so we can remesh
 # them) and compute the parametrizations
-gmsh.merge('aneurysm_data.stl')
+path = os.path.dirname(os.path.abspath(__file__))
+gmsh.merge(os.path.join(path, 'aneurysm_data.stl'))
 gmsh.model.mesh.classifySurfaces(math.pi, True, True)
 gmsh.model.mesh.createGeometry()
 
diff --git a/examples/api/closest_point.py b/examples/api/closest_point.py
index 94c3fe559a..acbcbcb298 100644
--- a/examples/api/closest_point.py
+++ b/examples/api/closest_point.py
@@ -18,6 +18,7 @@ gmsh.model.occ.fragment([(0, pp)], [(1, c)])
 
 gmsh.model.occ.synchronize()
 
-gmsh.fltk.run()
+if '-nopopup' not in sys.argv:
+    gmsh.fltk.run()
 
 gmsh.finalize()
diff --git a/examples/api/copy_mesh.py b/examples/api/copy_mesh.py
index aff7bef167..b3fde5d1f6 100644
--- a/examples/api/copy_mesh.py
+++ b/examples/api/copy_mesh.py
@@ -1,4 +1,5 @@
 import gmsh
+import sys
 
 gmsh.initialize()
 
@@ -52,5 +53,7 @@ gmsh.option.setNumber("Mesh.Algorithm3D", 10)
 gmsh.model.mesh.clear()
 gmsh.model.mesh.generate(3)
 
-gmsh.fltk.run()
+if '-nopopup' not in sys.argv:
+    gmsh.fltk.run()
+
 gmsh.finalize()
diff --git a/examples/api/edges.cpp b/examples/api/edges.cpp
index af29210e47..7b8accc3f6 100644
--- a/examples/api/edges.cpp
+++ b/examples/api/edges.cpp
@@ -1,5 +1,6 @@
 #include <cstdio>
 #include <gmsh.h>
+#include <set>
 
 int main(int argc, char **argv)
 {
@@ -97,7 +98,8 @@ int main(int argc, char **argv)
     gmsh::model::mesh::getJacobians(eleType1D, uvw, jac, det, pts, c);
   }
 
-  gmsh::fltk::run();
+  std::set<std::string> args(argv, argv + argc);
+  if(!args.count("-nopopup")) gmsh::fltk::run();
 
   gmsh::finalize();
   return 0;
diff --git a/examples/api/faces.cpp b/examples/api/faces.cpp
index ef21c8f6e1..15130031b2 100644
--- a/examples/api/faces.cpp
+++ b/examples/api/faces.cpp
@@ -1,5 +1,6 @@
 #include <cstdio>
 #include <gmsh.h>
+#include <set>
 
 int main(int argc, char **argv)
 {
@@ -97,7 +98,8 @@ int main(int argc, char **argv)
     gmsh::model::mesh::getJacobians(eleType2D, uvw, jac, det, pts, s);
   }
 
-  gmsh::fltk::run();
+  std::set<std::string> args(argv, argv + argc);
+  if(!args.count("-nopopup")) gmsh::fltk::run();
 
   gmsh::finalize();
   return 0;
diff --git a/examples/api/fragment_surfaces.cpp b/examples/api/fragment_surfaces.cpp
index af1dc83df6..bfb7ab0b56 100644
--- a/examples/api/fragment_surfaces.cpp
+++ b/examples/api/fragment_surfaces.cpp
@@ -1,6 +1,7 @@
 #include <gmsh.h>
+#include <set>
 
-int main()
+int main(int argc, char **argv)
 {
   gmsh::initialize();
   int s1 = gmsh::model::occ::addRectangle(0, 0, 0, 1, 1);
@@ -10,5 +11,7 @@ int main()
   std::vector<std::vector<std::pair<int, int> > > outmap;
   gmsh::model::occ::fragment({{2, s1}}, {{2, s2}}, out, outmap);
   gmsh::model::occ::synchronize();
-  gmsh::fltk::run();
+
+  std::set<std::string> args(argv, argv + argc);
+  if(!args.count("-nopopup")) gmsh::fltk::run();
 }
diff --git a/examples/api/gui.cpp b/examples/api/gui.cpp
index e842077925..8e450b052a 100644
--- a/examples/api/gui.cpp
+++ b/examples/api/gui.cpp
@@ -1,8 +1,12 @@
 #include <cstdio>
 #include <gmsh.h>
+#include <set>
 
 int main(int argc, char **argv)
 {
+  std::set<std::string> args(argv, argv + argc);
+  if(args.count("-nopopup")) exit(0);
+
   gmsh::initialize(argc, argv);
 
   // creates the FLTK user interface; this could also be called after the
diff --git a/examples/api/gui.jl b/examples/api/gui.jl
index 8cba16c0cb..a7b5b7aa1d 100644
--- a/examples/api/gui.jl
+++ b/examples/api/gui.jl
@@ -1,5 +1,9 @@
 import gmsh
 
+if "-nopopup" in ARGS
+    exit(0)
+end
+
 gmsh.initialize(append!(["gmsh"], ARGS))
 
 # creates the FLTK user interface; this could also be called after the geometry
diff --git a/examples/api/gui.py b/examples/api/gui.py
index e3fb0353b4..14d6d4e842 100644
--- a/examples/api/gui.py
+++ b/examples/api/gui.py
@@ -1,12 +1,14 @@
 import gmsh
 import sys
 
+if '-nopopup' in sys.argv:
+    exit(0)
+
 gmsh.initialize(sys.argv)
 
 # creates the FLTK user interface; this could also be called after the geometry
 # is created (or not at all - fltk.run() will do it automatically)
-if '-nopopup' not in sys.argv:
-    gmsh.fltk.initialize()
+gmsh.fltk.initialize()
 
 # Copied from boolean.py...
 gmsh.model.add("boolean")
@@ -39,7 +41,6 @@ gmsh.model.setColor(gmsh.model.getEntities(2), 249, 166, 2)
 #     gmsh.fltk.wait()
 #     print("just treated an event in the interface")
 
-if '-nopopup' not in sys.argv:
-    gmsh.fltk.run()
+gmsh.fltk.run()
 
 gmsh.finalize()
diff --git a/examples/api/mirror_mesh.py b/examples/api/mirror_mesh.py
index 1331472cca..16beaee621 100644
--- a/examples/api/mirror_mesh.py
+++ b/examples/api/mirror_mesh.py
@@ -3,6 +3,7 @@
 # overall conforming mesh
 
 import gmsh
+import sys
 
 gmsh.initialize()
 
@@ -50,4 +51,5 @@ transform(m, 3000, 3000000, 3000000, -1, -1, 1)
 # boundaries
 gmsh.model.mesh.removeDuplicateNodes()
 
-gmsh.fltk.run()
+if '-nopopup' not in sys.argv:
+    gmsh.fltk.run()
diff --git a/examples/api/naca_boundary_layer_2d.py b/examples/api/naca_boundary_layer_2d.py
index 0623f8092a..e1eeffd472 100644
--- a/examples/api/naca_boundary_layer_2d.py
+++ b/examples/api/naca_boundary_layer_2d.py
@@ -70,7 +70,8 @@ if by_extrusion:
     gmsh.model.occ.synchronize()
     # a boundary layer can be created through extrusion using the built-in CAD
     # kernel: this creates topological entities that will be filled with a
-    # discrete geometry (a mesh) during mesh generation
+    # discrete geometry (a mesh extruded along the boundary normals) during mesh
+    # generation
     n = np.linspace(1, 1, 7)
     d = np.logspace(-4, -2, 7)
     extbl = gmsh.model.geo.extrudeBoundaryLayer(gmsh.model.getEntities(1),
diff --git a/examples/api/naca_boundary_layer_3d.py b/examples/api/naca_boundary_layer_3d.py
index 52bca883f8..290bed2872 100644
--- a/examples/api/naca_boundary_layer_3d.py
+++ b/examples/api/naca_boundary_layer_3d.py
@@ -85,9 +85,10 @@ gmsh.model.occ.synchronize()
 
 # create a boundary layer for all the surfaces through extrusion using the
 # built-in CAD kernel: this creates topological entities that will be filled
-# with a discrete geometry (a mesh) during mesh generation; more general
-# boundary layer meshing constraints are currently only available in 2D, through
-# the BoundaryLayer Field - see 'naca_boundary_layer_2d.py'.
+# with a discrete geometry (a mesh extruded along the boundary normals) during
+# mesh generation; more general boundary layer meshing constraints are also
+# available in 2D through the BoundaryLayer Field - see
+# 'naca_boundary_layer_2d.py'.
 n = np.linspace(1, 1, 7)
 d = np.logspace(-4, -2, 7)
 extbl = gmsh.model.geo.extrudeBoundaryLayer(gmsh.model.getEntities(2),
diff --git a/examples/api/onelab_test.jl b/examples/api/onelab_test.jl
index 6803f698d6..7ba2baf672 100644
--- a/examples/api/onelab_test.jl
+++ b/examples/api/onelab_test.jl
@@ -41,6 +41,8 @@ gmsh.onelab.setString("string 1", ["goodbye"])
 # remove a parameter
 gmsh.onelab.clear("string 2")
 
-gmsh.fltk.run()
+if !("-nopopup" in ARGS)
+    gmsh.fltk.run()
+end
 
 gmsh.finalize()
diff --git a/examples/api/onelab_test.py b/examples/api/onelab_test.py
index b4f4be129a..3f394ec8df 100644
--- a/examples/api/onelab_test.py
+++ b/examples/api/onelab_test.py
@@ -2,7 +2,6 @@ import gmsh
 import sys
 import json
 import math
-import thread
 
 gmsh.initialize()
 
diff --git a/examples/api/partition.cpp b/examples/api/partition.cpp
index 39c7090f75..f4ef6a13fa 100644
--- a/examples/api/partition.cpp
+++ b/examples/api/partition.cpp
@@ -1,5 +1,6 @@
 #include <gmsh.h>
 #include <iostream>
+#include <set>
 
 int main(int argc, char **argv)
 {
@@ -68,7 +69,9 @@ int main(int argc, char **argv)
       std::cout << "\n";
     }
   }
-  gmsh::fltk::run();
+
+  std::set<std::string> args(argv, argv + argc);
+  if(!args.count("-nopopup")) gmsh::fltk::run();
 
   gmsh::finalize();
 
diff --git a/examples/api/raw_tetrahedralization.py b/examples/api/raw_tetrahedralization.py
index 913af7c29e..d9dcfd147d 100644
--- a/examples/api/raw_tetrahedralization.py
+++ b/examples/api/raw_tetrahedralization.py
@@ -1,11 +1,12 @@
 import numpy as np
 import gmsh
+import sys
 
 # number of points to tetrahedralize
 N = 100
 
 # visualize the mesh?
-visu = True
+visu = ("-nopopup" not in sys.argv)
 
 gmsh.initialize()
 
diff --git a/examples/api/raw_triangulation.py b/examples/api/raw_triangulation.py
index d5c8f0f54b..f3df634376 100644
--- a/examples/api/raw_triangulation.py
+++ b/examples/api/raw_triangulation.py
@@ -1,11 +1,12 @@
 import numpy as np
 import gmsh
+import sys
 
 # number of points to trianguate
 N = 100
 
 # visualize the mesh?
-visu = True
+visu = ("-nopopup" not in sys.argv)
 
 gmsh.initialize()
 
diff --git a/examples/api/spherical_surf.jl b/examples/api/spherical_surf.jl
index b80c7b65db..6125c70a43 100644
--- a/examples/api/spherical_surf.jl
+++ b/examples/api/spherical_surf.jl
@@ -13,5 +13,9 @@ gmsh.model.occ.synchronize()
 
 gmsh.model.removeEntities([(3,sph)])
 gmsh.model.removeEntities([(2,2), (2,4), (2,6)], true)
-gmsh.fltk.run()
+
+if !("-nopopup" in ARGS)
+    gmsh.fltk.run()
+end
+
 gmsh.finalize()
diff --git a/examples/api/spline.cpp b/examples/api/spline.cpp
index 0e37b3b93f..5d7b73bf60 100644
--- a/examples/api/spline.cpp
+++ b/examples/api/spline.cpp
@@ -1,4 +1,5 @@
 #include <gmsh.h>
+#include <set>
 
 int main(int argc, char **argv)
 {
@@ -35,7 +36,10 @@ int main(int argc, char **argv)
                                {3, 1, 3});
 
   gmsh::model::occ::synchronize();
-  gmsh::fltk::run();
+
+  std::set<std::string> args(argv, argv + argc);
+  if(!args.count("-nopopup")) gmsh::fltk::run();
+
   gmsh::finalize();
   return 0;
 }
diff --git a/examples/api/split_window.py b/examples/api/split_window.py
index 8137c964c4..a043c83ef4 100644
--- a/examples/api/split_window.py
+++ b/examples/api/split_window.py
@@ -1,6 +1,9 @@
 import gmsh
 import sys
 
+if '-nopopup' in sys.argv:
+    exit(0)
+
 gmsh.initialize(sys.argv)
 
 # create simple geometry
diff --git a/examples/api/square.cpp b/examples/api/square.cpp
index 18714c149a..c7e6fce6cf 100644
--- a/examples/api/square.cpp
+++ b/examples/api/square.cpp
@@ -1,5 +1,7 @@
 #include <gmsh.h>
-int main()
+#include <set>
+
+int main(int argc, char **argv)
 {
   gmsh::initialize();
   gmsh::model::occ::addRectangle(0, 0, 0, 1, 1, 1);
@@ -8,6 +10,9 @@ int main()
   gmsh::option::setNumber("Mesh.MeshSizeMin", 1);
   gmsh::option::setNumber("Mesh.MeshSizeMax", 1);
   gmsh::model::mesh::generate();
-  gmsh::fltk::run();
+
+  std::set<std::string> args(argv, argv + argc);
+  if(!args.count("-nopopup")) gmsh::fltk::run();
+
   gmsh::finalize();
 }
diff --git a/examples/api/surface_filling.py b/examples/api/surface_filling.py
index 1518baedee..d0f9d7f369 100644
--- a/examples/api/surface_filling.py
+++ b/examples/api/surface_filling.py
@@ -1,4 +1,5 @@
 import gmsh
+import sys
 
 gmsh.initialize()
 gmsh.option.setNumber('Mesh.MeshSizeMin', 0.02)
@@ -29,4 +30,8 @@ gmsh.model.occ.addPoint(-0.67, 0.1, -0.2, 8)
 gmsh.model.occ.addSurfaceFilling(1, 4, [7, 8])
 
 gmsh.model.occ.synchronize()
-gmsh.fltk.run()
+
+if '-nopopup' not in sys.argv:
+    gmsh.fltk.run()
+
+gmsh.finalize()
diff --git a/examples/api/view_renumbering.py b/examples/api/view_renumbering.py
index 7700833ad4..c1a26c6cf3 100644
--- a/examples/api/view_renumbering.py
+++ b/examples/api/view_renumbering.py
@@ -31,6 +31,7 @@ gmsh.view.addHomogeneousModelData(
 gmsh.model.mesh.renumberNodes()
 gmsh.model.mesh.renumberElements()
 
-gmsh.fltk.run()
+if '-nopopup' not in sys.argv:
+    gmsh.fltk.run()
 
 gmsh.finalize()
-- 
GitLab