From 3c130c538999608eb47509eef24d025da38e3134 Mon Sep 17 00:00:00 2001
From: "Dmitry Yu. Naumov" <github@naumov.de>
Date: Fri, 23 Nov 2018 16:00:10 +0100
Subject: [PATCH] [App/Utils] Use MeshLib::materialIDs().

---
 .../DataView/ElementTreeModel.cpp             |  7 +--
 .../generateMatPropsFromMatID.cpp             | 12 ++---
 Applications/Utils/MeshEdit/queryMesh.cpp     |  7 +--
 MeshGeoToolsLib/AppendLinesAlongPolyline.cpp  | 47 ++++++++++---------
 4 files changed, 29 insertions(+), 44 deletions(-)

diff --git a/Applications/DataExplorer/DataView/ElementTreeModel.cpp b/Applications/DataExplorer/DataView/ElementTreeModel.cpp
index bc407df5d31..d3cf487d6ff 100644
--- a/Applications/DataExplorer/DataView/ElementTreeModel.cpp
+++ b/Applications/DataExplorer/DataView/ElementTreeModel.cpp
@@ -65,12 +65,7 @@ void ElementTreeModel::setElement(vtkUnstructuredGridAlgorithm const*const grid,
     auto* typeItem = new TreeItem(typeData, elemItem);
     elemItem->appendChild(typeItem);
 
-    MeshLib::PropertyVector<int> const* const mat_ids =
-        mesh->getProperties().existsPropertyVector<int>(
-            "MaterialIDs", MeshLib::MeshItemType::Cell, 1)
-            ? mesh->getProperties().getPropertyVector<int>(
-                  "MaterialIDs", MeshLib::MeshItemType::Cell, 1)
-            : nullptr;
+    auto const mat_ids = materialIDs(*mesh);
     QString matIdString = !mat_ids ? QString("not defined") : QString::number((*mat_ids)[elem->getID()]);
     QList<QVariant> materialData;
     materialData << "MaterialID: " << matIdString;
diff --git a/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp b/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp
index 31b7c3162bd..24d0743225f 100644
--- a/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp
+++ b/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp
@@ -61,16 +61,10 @@ int main (int argc, char* argv[])
         return EXIT_FAILURE;
     }
 
-    MeshLib::PropertyVector<int>* materialIds = nullptr;
-    try
+    auto const materialIds = materialIDs(*mesh);
+    if (!materialIds)
     {
-        materialIds = mesh->getProperties().getPropertyVector<int>(
-            "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
-    }
-    catch (std::runtime_error const& e)
-    {
-        WARN("%s", e.what());
-        return EXIT_FAILURE;
+        OGS_FATAL("Mesh contains no int-property vector named 'MaterialIDs'.");
     }
 
     std::size_t const n_properties(materialIds->size());
diff --git a/Applications/Utils/MeshEdit/queryMesh.cpp b/Applications/Utils/MeshEdit/queryMesh.cpp
index 44bd0156e2a..ec0908e5fec 100644
--- a/Applications/Utils/MeshEdit/queryMesh.cpp
+++ b/Applications/Utils/MeshEdit/queryMesh.cpp
@@ -68,12 +68,7 @@ int main(int argc, char *argv[])
     }
     selected_node_ids.insert(selected_node_ids.end(), nodeId_arg.getValue().begin(), nodeId_arg.getValue().end());
 
-    MeshLib::PropertyVector<int> const* const materialIds =
-        mesh->getProperties().existsPropertyVector<int>(
-            "MaterialIDs", MeshLib::MeshItemType::Cell, 1)
-            ? mesh->getProperties().getPropertyVector<int>(
-                  "MaterialIDs", MeshLib::MeshItemType::Cell, 1)
-            : nullptr;
+    auto const materialIds = materialIDs(*mesh);
     for (auto ele_id : eleId_arg.getValue())
     {
         std::stringstream out;
diff --git a/MeshGeoToolsLib/AppendLinesAlongPolyline.cpp b/MeshGeoToolsLib/AppendLinesAlongPolyline.cpp
index c107c4d1089..6294d27cbec 100644
--- a/MeshGeoToolsLib/AppendLinesAlongPolyline.cpp
+++ b/MeshGeoToolsLib/AppendLinesAlongPolyline.cpp
@@ -1,4 +1,3 @@
-
 /**
  * @copyright
  * Copyright (c) 2012-2018, OpenGeoSys Community (http://www.opengeosys.org)
@@ -32,22 +31,13 @@ std::unique_ptr<MeshLib::Mesh> appendLinesAlongPolylines(
     std::vector<MeshLib::Node*> vec_new_nodes = MeshLib::copyNodeVector(mesh.getNodes());
     std::vector<MeshLib::Element*> vec_new_eles = MeshLib::copyElementVector(mesh.getElements(), vec_new_nodes);
 
-    std::vector<int> new_mat_ids;
-    try
-    {
-        auto ids = mesh.getProperties().getPropertyVector<int>(
-            "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
-        new_mat_ids.reserve(ids->size());
-        std::copy(ids->cbegin(), ids->cend(), std::back_inserter(new_mat_ids));
-    }
-    catch (std::runtime_error const& e)
-    {
-        WARN("%s", e.what());
-    }
-    int max_matID(0);
-    if (!new_mat_ids.empty())
-        max_matID = *(std::max_element(new_mat_ids.cbegin(), new_mat_ids.cend()));
+    auto const material_ids = materialIDs(mesh);
+    int const max_matID =
+        material_ids
+            ? *(std::max_element(begin(*material_ids), end(*material_ids)))
+            : 0;
 
+    std::vector<int> new_mat_ids;
     const std::size_t n_ply (ply_vec.size());
     // for each polyline
     for (std::size_t k(0); k < n_ply; k++)
@@ -81,14 +71,25 @@ std::unique_ptr<MeshLib::Mesh> appendLinesAlongPolylines(
     const std::string name = mesh.getName() + "_with_lines";
     auto new_mesh =
         std::make_unique<MeshLib::Mesh>(name, vec_new_nodes, vec_new_eles);
-    auto opt_mat_pv = new_mesh->getProperties().createNewPropertyVector<int>(
-        "MaterialIDs", MeshLib::MeshItemType::Cell);
-    if (opt_mat_pv) {
-        auto & mat_pv = *opt_mat_pv;
-        mat_pv.reserve(new_mat_ids.size());
-        std::copy(new_mat_ids.cbegin(), new_mat_ids.cend(),
-            std::back_inserter(mat_pv));
+    auto new_material_ids =
+        new_mesh->getProperties().createNewPropertyVector<int>(
+            "MaterialIDs", MeshLib::MeshItemType::Cell);
+    if (!new_material_ids)
+    {
+        OGS_FATAL("Could not create MaterialIDs cell vector in new mesh.");
+    }
+    new_material_ids->reserve(new_mesh->getNumberOfElements());
+    if (material_ids != nullptr)
+    {
+        std::copy(begin(*material_ids), end(*material_ids),
+                  std::back_inserter(*new_material_ids));
+    }
+    else
+    {
+        new_material_ids->resize(mesh.getNumberOfElements());
     }
+    std::copy(begin(new_mat_ids), end(new_mat_ids),
+              std::back_inserter(*new_material_ids));
     return new_mesh;
 }
 
-- 
GitLab