From fc8e184b7ab8738a2b15b37eab601591676bd759 Mon Sep 17 00:00:00 2001
From: Jonathan Lambrechts <jonathan.lambrechts@uclouvain.be>
Date: Sun, 19 Nov 2017 19:00:46 +0100
Subject: [PATCH] script to generate the C++ api header

---
 api/GenApi.py | 209 +++++++++++++++++++++++++++++++++++++++
 api/gen.py    | 268 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 477 insertions(+)
 create mode 100644 api/GenApi.py
 create mode 100644 api/gen.py

diff --git a/api/GenApi.py b/api/GenApi.py
new file mode 100644
index 0000000000..aa66ce55e1
--- /dev/null
+++ b/api/GenApi.py
@@ -0,0 +1,209 @@
+class arg :
+
+    def __init__(self, name, value=None) :
+        self.name = name
+        self.value = value
+
+    @property
+    def cpp_value(self) :
+        if self.value is None :
+            return ""
+        return " = "+self.value
+
+    @property
+    def cpp(self) :
+        n = self.type_cpp 
+        if n[-1] != "&" :
+            n += " "
+        n += self.name
+        n += self.cpp_value
+        return n
+
+    @property
+    def c(self):
+        return ""
+
+class simplearg(arg) :
+    def __init__(self,cpptype,ctype,out,name,value):
+        self.type_cpp = cpptype
+        self.type_c = ctype
+        arg.__init__(self,name,value)
+
+    @property
+    def c(self):
+        return self.type_c + " " + self.name 
+
+class vectorarg(arg) :
+    def __init__(self,cpptype,ctype,out,name,value):
+        self.type_cpp = cpptype
+        self.type_c = ctype
+        vectorarg.__init__(self,name,value)
+
+class iint(simplearg):
+    def __init__(self,name,value=None):
+        simplearg.__init__(self,"const int","const int",False,name,value)
+
+class oint(simplearg):
+    def __init__(self,name,value=None) :
+        simplearg.__init__(self,"int &","int *",True,name,value)
+
+class istring(simplearg) :
+    def __init__(self,name,value=None) :
+        simplearg.__init__(self,"const std::string &", "const char*",False,name,value)
+    @property
+    def cpp_value(self) :
+        if self.value is None :
+            return ""
+        return " = \""+self.value+"\""
+
+class ostring(simplearg) :
+    def __init__(self,name,value=None) :
+        simplearg.__init__(self,"std::string &","char**",True,name,value)
+
+class idouble(simplearg) :
+    def __init__(self,name,value=None) :
+        simplearg.__init__(self,"const double","const double",False,name,value)
+
+class odouble(simplearg) :
+    def __init__(self,name,value=None) :
+        simplearg.__init__(self,"double &","double *",True,name,value)
+
+class ovectorpair(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "vector_pair &"
+
+class ivectorpair(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "const vector_pair &"
+
+class ibool(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "const bool"
+    
+class obool(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "bool &"
+
+class ivectorint(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "const std::vector<int> &"
+
+class ovectorint(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "std::vector<int> &"
+
+class ovectorvectorint(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "std::vector<std::vector<int> > &"
+
+class ovectorvectorpair(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "std::vector<vector_pair> &"
+
+class ivectordouble(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "const std::vector<double> &"
+
+class ovectordouble(arg) :
+    def __init__(self,name,value=None) :
+        arg.__init__(self,name,value)
+    type_cpp = "std::vector<double> &"
+
+
+class Module :
+
+    def __init__(self, name) :
+        self.name = name
+        self.fs = []
+
+    def add(self,name, *args) :
+        self.fs.append((name,args))
+
+cpp_header="""// Gmsh - Copyright (C) 1997-2017 C. Geuzaine, J.-F. Remacle
+//
+// See the LICENSE.txt file for license information. Please report all
+// bugs and problems to the public mailing list <gmsh@onelab.info>.
+
+#ifndef _GMSH_H_
+#define _GMSH_H_
+
+// This is the embryo of what will become the Gmsh API.
+//
+// Don't use it yet, it's not ready :-) We plan to release a first version in
+// Gmsh 3.1, and something more complete in Gmsh 4.0.
+//
+// Your input is welcome: please contribute your ideas on
+// https://gitlab.onelab.info/gmsh/gmsh/issues/188
+//
+// By design, the API is purely functional, and only uses elementary C++ types
+// from the standard library. This design should not and will not change.
+
+// All functions return 0 as the first entry of the returned vector on
+// successful completion. Additional integer results can be appends to this
+// returned value, depending on context.
+
+#include <cmath>
+#include <vector>
+#include <string>
+
+#if defined(WIN32)
+#define GMSH_API __declspec(dllexport) std::vector<int>
+#else
+#define GMSH_API std::vector<int>
+#endif
+
+typedef std::vector<std::pair<int, int> > vector_pair;
+"""
+
+cpp_footer=""" 
+#undef GMSH_API
+
+#endif
+"""
+
+c_header="""
+"""
+
+c_footer="""
+"""
+
+class API :
+
+    def __init__(self) :
+        self.modules = []
+
+    def add_module(self, name):
+        module = Module(name)
+        self.modules.append(module)
+        return module
+
+    def write_cpp(self, filename):
+        with open(filename+".h","w") as f :
+            f.write(cpp_header)
+            for module in self.modules :
+                f.write("\n")
+                f.write("// gmsh"+module.name+"\n")
+                for name, args in module.fs :
+                    f.write("GMSH_API gmsh" + module.name+name+"("+", ".join(
+                        list((a.cpp for a in args)))+");\n")
+            f.write(cpp_footer)
+
+    def write_c(self,filename) :
+        with open(filename+".h","w") as f :
+            f.write(c_header)
+            for module in self.modules :
+                f.write("\n")
+                f.write("// gmsh"+module.name+"\n")
+                for name, args in module.fs :
+                    f.write("GMSH_API gmsh" + module.name+name+"("+", ".join(
+                        list((a.c for a in args)))+");\n")
+            f.write(c_footer)
diff --git a/api/gen.py b/api/gen.py
new file mode 100644
index 0000000000..f22d280fd7
--- /dev/null
+++ b/api/gen.py
@@ -0,0 +1,268 @@
+from GenApi import *
+
+api = API()
+
+#add_function("","Initialize",[iintp("argc"),arg("char **","argv")])
+gmsh = api.add_module("")
+
+gmsh.add("Finalize")
+gmsh.add("Open",istring("fileName"))
+gmsh.add("Merge",istring("fileName"))
+gmsh.add("Export",istring("fileName"))
+gmsh.add("Clear")
+
+options = api.add_module("Option")
+options.add("SetNumber",istring("name"),idouble("value"))
+options.add("GetNumber",istring("name"),odouble("value"))
+options.add("SetString",istring("name"),istring("value"))
+options.add("GetString",istring("name"),ostring("value"))
+
+model = api.add_module("Model")
+model.add("Create",istring("name"))
+model.add("SetCurrent",istring("name"))
+model.add("Destroy")
+
+model.add("GetEntities",ovectorpair("dimTags"),iint("dim","-1"))
+model.add("GetPhysicalGroups",ovectorpair("dimTags"),iint("dim","-1"))
+model.add("AddPhysicalGroup",iint("dim"),iint("tag"),ivectorint("tags"))
+model.add("GetEntitiesForPhysicalGroup",iint("dim"),iint("tag"),ovectorint("tags"))
+model.add("SetPhysicalName",iint("dim"),iint("tag"),istring("name"))
+model.add("GetPhysicalName",iint("dim"),iint("tag"),ostring("name"))
+model.add("GetVertexCoordinates",iint("tag"),odouble("x"),odouble("y"),odouble("z"))
+model.add("GetBoundary",ivectorpair("inDimTags"),ovectorpair("outDimTags"),
+        ibool("combined","true"),ibool("oriented","true"),ibool("recursive","false"))
+model.add("GetEntitiesInBoundingBox",
+        idouble("x1"),idouble("y1"),idouble("z1"),
+        idouble("x2"),idouble("y2"),idouble("z2"),
+        ovectorpair("tags"),iint("dim","-1"))
+
+model.add("GetBoundingBox",iint("dim"),iint("tag"),odouble("x1"),odouble("y1"),
+        odouble("z1"),odouble("x2"),odouble("y2"),odouble("z2"))
+model.add("Remove",ivectorpair("dimTags"),ibool("recursive","false"))
+model.add("Mesh",iint("dim"))
+model.add("GetMeshVertices",iint("dim"),iint("tag"),
+        ovectorint("vertexTags"),
+        ovectordouble("coords"))
+model.add("GetMeshElements",iint("dim"),iint("tag"),
+        ovectorint("types"),
+        ovectorvectorint("elementTags"),
+        ovectorvectorint("vertexTags"))
+model.add("SetMeshSize",ivectorpair("dimTags"),idouble("size"))
+model.add("SetTransfiniteLine",iint("tag"),iint("nPoints"),
+        istring("type","Progression"),
+        idouble("coef","1."))
+model.add("SetTransfiniteSurface",iint("tag"),
+        istring("arrangement","Left"),
+        ivectorint("cornerTags","std::vector<int>()"))
+model.add("SetTransfiniteVolume",iint("tag"),
+        ivectorint("cornerTags","std::vector<int>()"))
+model.add("SetRecombine",iint("dim"),iint("tag"),idouble("angle","45."))
+model.add("SetSmoothing",iint("dim"),iint("tag"),iint("val"))
+model.add("SetReverseMesh",iint("dim"),iint("tag"),ibool("val","true"))
+model.add("Embed",iint("dim"),ivectorint("tags"),iint("inDim"),
+        iint("inTag"))
+
+model_geo = api.add_module("ModelGeo")
+model_geo.add("AddPoint",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("meshSize","0."))
+model_geo.add("AddLine",iint("tag"),iint("startTag"),iint("endTag"))
+model_geo.add("AddCircleArc",iint("tag"),iint("startTag"),
+        iint("centerTag"),iint("endTag"),
+        idouble("nx","0."),idouble("ny","0."),
+        idouble("nz","0."))
+model_geo.add("AddEllipseArc",iint("tag"),iint("startTag"),iint("centerTag"),
+        iint("majorTag"),iint("endTag"),
+        idouble("nx","0."),idouble("ny","0."),
+        idouble("nz","0."))
+model_geo.add("AddSpline",iint("tag"),ivectorint("vertexTags"))
+model_geo.add("AddBSpline",iint("tag"),ivectorint("vertexTags"))
+model_geo.add("AddBezier",iint("tag"),ivectorint("vertexTags"))
+model_geo.add("AddLineLoop",iint("tag"),ivectorint("edgeTags"))
+model_geo.add("AddPlaneSurface",iint("tag"),ivectorint("wireTags"))
+model_geo.add("AddSurfaceFilling",iint("tag"),ivectorint("wireTags"),
+        iint("sphereCenterTag","-1"))
+model_geo.add("AddSurfaceLoop",iint("tag"),ivectorint("faceTags"))
+model_geo.add("AddVolume",iint("tag"),ivectorint("shellTags"))
+model_geo.add("Extrude",ivectorpair("inDimTags"),
+        idouble("dx"),idouble("dy"),idouble("dz"),
+        ovectorpair("outDimTags"),
+        ivectorint("numElements","std::vector<int>()"),
+        ivectordouble("heights","std::vector<double>()"),
+        ibool("recombine","false"))
+model_geo.add("Revolve",ivectorpair("inDimTags"),
+        idouble("x"),idouble("y"),idouble("z"),
+        idouble("ax"),idouble("ay"),
+        idouble("az"),idouble("angle"),
+        ovectorpair("outDimTags"),
+        ivectorint("numElements","std::vector<int>()"),
+        ivectordouble("heights","std::vector<double>()"),
+        ibool("recombine","false"))
+model_geo.add("Twist",ivectorpair("inDimTags"),
+        idouble("x"),idouble("y"),idouble("z"),
+        idouble("dx"),idouble("dy"),idouble("dz"),
+        idouble("ax"),idouble("ay"),idouble("az"),
+        idouble("angle"),
+        ovectorpair("outDimTags"),
+        ivectorint("numElements","std::vector<int>()"),
+        ivectordouble("heights","std::vector<double>()"),
+        ibool("recombine","false"))
+model_geo.add("Translate",ivectorpair("dimTags"),
+        idouble("dx"),idouble("dy"),idouble("dz"))
+model_geo.add("Rotate",ivectorpair("dimTags"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("ax"),idouble("ay"),
+        idouble("az"),idouble("angle"))
+model_geo.add("Dilate",ivectorpair("dimTags"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("a"),idouble("b"),
+        idouble("c"))
+model_geo.add("Symmetry",ivectorpair("dimTags"),idouble("a"),idouble("b"),
+        idouble("c"),idouble("d"))
+model_geo.add("Copy",ivectorpair("inDimTags"),ovectorpair("outDimTags"))
+model_geo.add("Remove",ivectorpair("dimTags"),ibool("recursive","false"))
+model_geo.add("RemoveAllDuplicates")
+model_geo.add("SetMeshSize",ivectorpair("dimTags"),idouble("size"))
+model_geo.add("SetTransfiniteLine",iint("tag"),iint("nPoints"),
+        istring("type","Progression"),
+        idouble("coef","1."))
+model_geo.add("SetTransfiniteSurface",iint("tag"),
+        istring("arrangement","Left"),
+        ivectorint("cornerTags = std::vector<int>()"))
+model_geo.add("SetTransfiniteVolume",iint("tag"),
+        ivectorint("cornerTags = std::vector<int>()"))
+model_geo.add("SetRecombine",iint("dim"),iint("tag"),idouble("angle","45."))
+model_geo.add("SetSmoothing",iint("dim"),iint("tag"),iint("val"))
+model_geo.add("SetReverseMesh",iint("dim"),iint("tag"),ibool("val","true"))
+model_geo.add("Synchronize")
+
+model_occ = api.add_module("ModelOcc")
+model_occ.add("AddPoint",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("meshSize","0."))
+model_occ.add("AddLine",iint("tag"),iint("startTag"),iint("endTag"))
+model_occ.add("AddCircleArc",iint("tag"),iint("startTag"),iint("centerTag"),
+        iint("endTag"))
+model_occ.add("AddCircle",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("r"),
+        idouble("angle1","0."),idouble("angle2","2*M_PI"))
+model_occ.add("AddEllipseArc",iint("tag"),iint("startTag"),iint("centerTag"),
+        iint("endTag"))
+model_occ.add("AddEllipse",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("r1"),idouble("r2"),
+        idouble("angle1","0."),
+        idouble("angle2","2*M_PI"))
+model_occ.add("AddSpline",iint("tag"),ivectorint("vertexTags"))
+model_occ.add("AddBezier",iint("tag"),ivectorint("vertexTags"))
+model_occ.add("AddBSpline",iint("tag"),ivectorint("vertexTags"))
+model_occ.add("AddWire",iint("tag"),ivectorint("edgeTags"),
+        ibool("checkClosed","false"))
+model_occ.add("AddLineLoop",iint("tag"),ivectorint("edgeTags"))
+model_occ.add("AddRectangle",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("dx"),idouble("dy"),
+        idouble("roundedRadius","0."))
+model_occ.add("AddDisk",iint("tag"),idouble("xc"),idouble("yc"),
+        idouble("zc"),idouble("rx"),idouble("ry"))
+model_occ.add("AddPlaneSurface",iint("tag"),ivectorint("wireTags"))
+model_occ.add("AddSurfaceFilling",iint("tag"),iint("wireTag"))
+model_occ.add("AddSurfaceLoop",iint("tag"),ivectorint("faceTags"))
+model_occ.add("AddVolume",iint("tag"),ivectorint("shellTags"))
+model_occ.add("AddSphere",iint("tag"),idouble("xc"),idouble("yc"),
+        idouble("zc"),idouble("radius"),
+        idouble("angle1","-M_PI/2"),
+        idouble("angle2","M_PI/2"),
+        idouble("angle3","2*M_PI"))
+model_occ.add("AddBox",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("dx"),idouble("dy"),
+        idouble("dz"))
+model_occ.add("AddCylinder",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("dx"),idouble("dy"),
+        idouble("dz"),idouble("r"),
+        idouble("angle","2*M_PI"))
+model_occ.add("AddCone",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("dx"),idouble("dy"),
+        idouble("dz"),idouble("r1"),idouble("r2"),
+        idouble("angle","2*M_PI"))
+model_occ.add("AddWedge",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("dx"),idouble("dy"),
+        idouble("dz"),idouble("ltx","0."))
+model_occ.add("AddTorus",iint("tag"),idouble("x"),idouble("y"),
+        idouble("z"),idouble("r1"),idouble("r2"),
+        idouble("angle","2*M_PI"))
+model_occ.add("AddThruSections",iint("tag"),ivectorint("wireTags"),
+        ovectorpair("outDimTags"),ibool("makeSolid","true"),
+        ibool("makeRuled","false"))
+#GMSH_API addThickSolid"",iint("tag"),iint("solidTag"),
+#                       ivectorint("excludeFaceTags"),
+#                       idouble("offset"),ovectorpair("outDimTags"))
+model_occ.add("Extrude",ivectorpair("inDimTags"),idouble("dx"),idouble("dy"),
+        idouble("dz"),ovectorpair("outDimTags"),
+        ivectorint("numElements","std::vector<int>()"),
+        ivectordouble("heights","std::vector<double>()"),
+        ibool("recombine","false"))
+model_occ.add("Revolve",ivectorpair("inDimTags"),
+        idouble("x"),idouble("y"),idouble("z"),
+        idouble("ax"),idouble("ay"),idouble("az"),
+        idouble("angle"),ovectorpair("outDimTags"),
+        ivectorint("numElements","std::vector<int>()"),
+        ivectordouble("heights","std::vector<double>()"),
+        ibool("recombine","false"))
+model_occ.add("AddPipe",ivectorpair("inDimTags"),iint("wireTag"),
+        ovectorpair("outDimTags"))
+model_occ.add("Fillet",ivectorint("regionTags"),
+        ivectorint("edgeTags"),
+        idouble("radius"),ovectorpair("outDimTags"),
+        ibool("removeRegion","true"))
+model_occ.add("BooleanUnion",iint("tag"),ivectorpair("objectDimTags"),
+        ivectorpair("toolDimTags"),
+        ovectorpair("outDimTags"),
+        ovectorvectorpair("outDimTagsMap"),
+        ibool("removeObject","true"),
+        ibool("removeTool","true"))
+model_occ.add("BooleanIntersection",iint("tag"),ivectorpair("objectDimTags"),
+        ivectorpair("toolDimTags"),
+        ovectorpair("outDimTags"),
+        ovectorvectorpair("outDimTagsMap"),
+        ibool("removeObject","true"),
+        ibool("removeTool","true"))
+model_occ.add("BooleanDifference",iint("tag"),ivectorpair("objectDimTags"),
+        ivectorpair("toolDimTags"),
+        ovectorpair("outDimTags"),
+        ovectorvectorpair("outDimTagsMap"),
+        ibool("removeObject","true"),
+        ibool("removeTool","true"))
+model_occ.add("BooleanFragments",iint("tag"),ivectorpair("objectDimTags"),
+        ivectorpair("toolDimTags"),
+        ovectorpair("outDimTags"),
+        ovectorvectorpair("outDimTagsMap"),
+        ibool("removeObject","true"),
+        ibool("removeTool","true"))
+model_occ.add("Translate",ivectorpair("dimTags"),idouble("dx"),
+        idouble("dy"),idouble("dz"))
+model_occ.add("Rotate",ivectorpair("dimTags"),idouble("x"),
+        idouble("y"),idouble("z"),idouble("ax"),
+        idouble("ay"),idouble("az"),idouble("angle"))
+model_occ.add("Dilate",ivectorpair("dimTags"),idouble("x"),
+        idouble("y"),idouble("z"),idouble("a"),
+        idouble("b"),idouble("c"))
+model_occ.add("Symmetry",ivectorpair("dimTags"),idouble("a"),
+        idouble("b"),idouble("c"),idouble("d"))
+model_occ.add("Copy",ivectorpair("inDimTags"),ovectorpair("outDimTags"))
+model_occ.add("Remove",ivectorpair("dimTags"),ibool("recursive","false"))
+model_occ.add("RemoveAllDuplicates")
+model_occ.add("ImportShapes",istring("fileName"),ovectorpair("outDimTags"),
+        ibool("highestDimOnly","true"),
+        istring("format",""))
+model_occ.add("SetMeshSize",ivectorpair("dimTags"),idouble("size"))
+model_occ.add("Synchronize")
+
+model_field = api.add_module("ModelField")
+model_field.add("Create",iint("tag"),istring("type"))
+model_field.add("SetNumber",iint("tag"),istring("option"),idouble("value"))
+model_field.add("SetString",iint("tag"),istring("option"),istring("value"))
+model_field.add("SetNumbers",iint("tag"),istring("option"),ivectordouble("value"))
+model_field.add("SetAsBackground",iint("tag"))
+model_field.add("Delete",iint("tag"))
+
+api.add_module("View")
+api.add_module("Plugin")
+api.add_module("Graphics")
+
+api.write_cpp("gmsh")
+#api.write_c("gmshc")
-- 
GitLab