From 9f9fa982a77f64ccbc47e511309b79f551c19d00 Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Mon, 8 Jun 2020 17:15:48 +0200
Subject: [PATCH] [MeL/ME/AddLayer] Switch to copy MaterialIDs.

---
 MeshLib/MeshEditing/AddLayerToMesh.cpp | 32 +++++++++++++++++++-------
 MeshLib/MeshEditing/AddLayerToMesh.h   | 12 +++++-----
 2 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/MeshLib/MeshEditing/AddLayerToMesh.cpp b/MeshLib/MeshEditing/AddLayerToMesh.cpp
index 68db0713295..d4de2e14ee2 100644
--- a/MeshLib/MeshEditing/AddLayerToMesh.cpp
+++ b/MeshLib/MeshEditing/AddLayerToMesh.cpp
@@ -84,14 +84,16 @@ MeshLib::Element* extrudeElement(std::vector<MeshLib::Node*> const& subsfc_nodes
 }
 
 MeshLib::Mesh* addTopLayerToMesh(MeshLib::Mesh const& mesh,
-    double thickness,
-    std::string const& name)
+                                 double thickness,
+                                 std::string const& name,
+                                 bool copy_material_ids)
 {
-    return addLayerToMesh(mesh, thickness, name, true);
+    return addLayerToMesh(mesh, thickness, name, true, copy_material_ids);
 }
 
 MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double thickness,
-                              std::string const& name, bool on_top)
+                              std::string const& name, bool on_top,
+                              bool copy_material_ids)
 {
     INFO("Extracting top surface of mesh '{:s}' ... ", mesh.getName());
     int const flag = (on_top) ? -1 : 1;
@@ -192,12 +194,26 @@ MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double thickness,
     }
 
     new_materials->reserve(subsfc_elements.size());
-    int new_layer_id(
-        *(std::max_element(materials->cbegin(), materials->cend())) + 1);
     std::copy(materials->cbegin(), materials->cend(),
               std::back_inserter(*new_materials));
-    auto const n_new_props(subsfc_elements.size() - mesh.getNumberOfElements());
-    std::fill_n(std::back_inserter(*new_materials), n_new_props, new_layer_id);
+
+    if (copy_material_ids &&
+        sfc_mesh->getProperties().existsPropertyVector<int>("MaterialIDs"))
+    {
+        auto const& sfc_material_ids =
+            *sfc_mesh->getProperties().getPropertyVector<int>("MaterialIDs");
+        std::copy(sfc_material_ids.cbegin(), sfc_material_ids.cend(),
+                  std::back_inserter(*new_materials));
+    }
+    else
+    {
+        int const new_layer_id(
+            *(std::max_element(materials->cbegin(), materials->cend())) + 1);
+        auto const n_new_props(subsfc_elements.size() -
+                               mesh.getNumberOfElements());
+        std::fill_n(std::back_inserter(*new_materials), n_new_props,
+                    new_layer_id);
+    }
 
     return new_mesh;
 }
diff --git a/MeshLib/MeshEditing/AddLayerToMesh.h b/MeshLib/MeshEditing/AddLayerToMesh.h
index e9a9f56acc3..fc94bf18690 100644
--- a/MeshLib/MeshEditing/AddLayerToMesh.h
+++ b/MeshLib/MeshEditing/AddLayerToMesh.h
@@ -26,14 +26,14 @@ class Element;
 
 /// Adds a layer on top of the mesh
 MeshLib::Mesh* addTopLayerToMesh(MeshLib::Mesh const& mesh,
-    double thickness,
-    std::string const& name);
+                                 double thickness,
+                                 std::string const& name,
+                                 bool copy_material_ids);
 
 /// Adds a layer to the mesh. If on_top is true, the layer is added on top,
 /// if it is false, the layer is added at the bottom.
-MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh,
-    double thickness,
-    std::string const& name,
-    bool on_top);
+MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double thickness,
+                              std::string const& name, bool on_top,
+                              bool copy_material_ids);
 
 } // end namespace MeshLib
-- 
GitLab