From 24b2594a3efc770392d6452d42de42e3a98bdfb4 Mon Sep 17 00:00:00 2001
From: Bastien Gorissen <bastien.gorissen@cenaero.be>
Date: Mon, 8 Nov 2010 10:00:55 +0000
Subject: [PATCH] Adds an option to switch elementary and physical tags in
 loaded meshes, and fixes a bug with geo-mesh matching.

---
 Common/CommandLine.cpp  |  4 ++++
 Common/Context.h        |  3 ++-
 Common/DefaultOptions.h |  2 ++
 Common/OpenFile.cpp     |  5 +++--
 Common/Options.cpp      |  7 +++++++
 Common/Options.h        |  1 +
 Geo/GModelIO_Mesh.cpp   | 13 ++++++++++---
 7 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/Common/CommandLine.cpp b/Common/CommandLine.cpp
index 7fb7b2c816..a6be728be2 100644
--- a/Common/CommandLine.cpp
+++ b/Common/CommandLine.cpp
@@ -220,6 +220,10 @@ void GetOptions(int argc, char *argv[])
         CTX::instance()->mesh.saveAll = 1;
         i++;
       }
+      else if(!strcmp(argv[i] + 1, "switch_tags")) {
+        CTX::instance()->mesh.switchElementTags = 1;
+        i++;
+      }
       else if(!strcmp(argv[i] + 1, "optimize")) {
         CTX::instance()->mesh.optimize = 1;
         i++;
diff --git a/Common/Context.h b/Common/Context.h
index 2f2fc2b26f..a1ee4abc42 100644
--- a/Common/Context.h
+++ b/Common/Context.h
@@ -36,7 +36,8 @@ struct contextMeshOptions {
   int smoothInternalEdges, minCircPoints, minCurvPoints;
   int saveAll, saveGroupsOfNodes, binary, bdfFieldFormat, saveParametric;
   int smoothNormals, reverseAllNormals, zoneDefinition, clip;
-  int saveElementTagType;  
+  int saveElementTagType;
+  int switchElementTags;
 };
 
 struct contextGeometryOptions {
diff --git a/Common/DefaultOptions.h b/Common/DefaultOptions.h
index 435c93a034..6cf01b4b24 100644
--- a/Common/DefaultOptions.h
+++ b/Common/DefaultOptions.h
@@ -1271,6 +1271,8 @@ StringXNumber MeshOptions_Number[] = {
     "Display faces of surface mesh?" },
   { F|O, "SurfaceNumbers" , opt_mesh_surfaces_num , 0. , 
     "Display surface mesh element numbers?" },
+  { F|O, "SwitchElementTags", opt_mesh_switch_elem_tags, 0. ,
+    "Invert elementary and physical tags when reading the mesh"},
 
   { F|O, "Tangents" , opt_mesh_tangents , 0.0 , 
     "Display size of tangent vectors (in pixels)" }, 
diff --git a/Common/OpenFile.cpp b/Common/OpenFile.cpp
index b84169170a..555497aa74 100644
--- a/Common/OpenFile.cpp
+++ b/Common/OpenFile.cpp
@@ -350,10 +350,11 @@ int MergeFile(std::string fileName, bool warnIfMissing)
        !strncmp(header, "$MeshFormat", 11) || !strncmp(header, "$Comments", 9)) {
       // mesh matcher
       if(CTX::instance()->geom.matchGeomAndMesh && !GModel::current()->empty()){
+	GModel* tmp2 = GModel::current();
         GModel* tmp = new GModel();
         tmp->readMSH(fileName);
-        if(GeomMeshMatcher::instance()->match(GModel::current(), tmp))
-          fileName = "out.msh";
+        if(GeomMeshMatcher::instance()->match(tmp2, tmp))
+	  fileName = "out.msh";
         delete tmp;
       }
       status = GModel::current()->readMSH(fileName);
diff --git a/Common/Options.cpp b/Common/Options.cpp
index 7530791511..c07dbe1e28 100644
--- a/Common/Options.cpp
+++ b/Common/Options.cpp
@@ -5881,6 +5881,13 @@ double opt_mesh_color_carousel(OPT_ARGS_NUM)
   return CTX::instance()->mesh.colorCarousel;
 }
 
+double opt_mesh_switch_elem_tags(OPT_ARGS_NUM)
+{
+  if(action & GMSH_SET)
+    CTX::instance()->mesh.switchElementTags = val ? 1 : 0;
+  return CTX::instance()->mesh.switchElementTags;
+}
+
 double opt_mesh_zone_definition(OPT_ARGS_NUM)
 {
   if(action & GMSH_SET){
diff --git a/Common/Options.h b/Common/Options.h
index 5d59107db4..913a271e22 100644
--- a/Common/Options.h
+++ b/Common/Options.h
@@ -549,6 +549,7 @@ double opt_mesh_save_element_tag_type(OPT_ARGS_NUM);
 double opt_mesh_save_parametric(OPT_ARGS_NUM);
 double opt_mesh_save_groups_of_nodes(OPT_ARGS_NUM);
 double opt_mesh_color_carousel(OPT_ARGS_NUM);
+double opt_mesh_switch_elem_tags(OPT_ARGS_NUM);
 double opt_mesh_zone_definition(OPT_ARGS_NUM);
 double opt_mesh_nb_nodes(OPT_ARGS_NUM);
 double opt_mesh_nb_triangles(OPT_ARGS_NUM);
diff --git a/Geo/GModelIO_Mesh.cpp b/Geo/GModelIO_Mesh.cpp
index 88dcc72821..dbbd9e0c03 100644
--- a/Geo/GModelIO_Mesh.cpp
+++ b/Geo/GModelIO_Mesh.cpp
@@ -29,6 +29,7 @@
 #include "discreteFace.h"
 #include "discreteRegion.h"
 #include "MVertexPositionSet.h"
+#include "Context.h"
 #include "OS.h"
 
 #if defined(HAVE_POST)
@@ -394,9 +395,15 @@ int GModel::readMSH(const std::string &name)
           int dom1 = 0, dom2 = 0, numVertices;
           std::vector<short> ghosts;
           if(version <= 1.0){
-            if(fscanf(fp, "%d %d %d %d %d", &num, &type, &physical, &elementary, 
-                      &numVertices) != 5)
-              return 0;
+	    if(CTX::instance()->mesh.switchElementTags) {
+	      if(fscanf(fp, "%d %d %d %d %d", &num, &type, &elementary, &physical, 
+			&numVertices) != 5)
+		return 0;
+	    } else {
+	      if(fscanf(fp, "%d %d %d %d %d", &num, &type, &physical, &elementary, 
+			&numVertices) != 5)
+		return 0;
+	    }
             if(numVertices != MElement::getInfoMSH(type)) return 0;
           }
           else{
-- 
GitLab