OGS
editMaterialID.cpp
Go to the documentation of this file.
1 
10 #include <tclap/CmdLine.h>
11 
12 #include <memory>
13 
14 #include "InfoLib/GitInfo.h"
18 #include "MeshLib/Mesh.h"
20 
21 int main(int argc, char* argv[])
22 {
23  TCLAP::CmdLine cmd(
24  "Edit material IDs of mesh elements.\n\n"
25  "OpenGeoSys-6 software, version " +
27  ".\n"
28  "Copyright (c) 2012-2021, OpenGeoSys Community "
29  "(http://www.opengeosys.org)",
31  TCLAP::SwitchArg replaceArg("r", "replace", "replace material IDs", false);
32  TCLAP::SwitchArg condenseArg("c", "condense", "condense material IDs",
33  false);
34  TCLAP::SwitchArg specifyArg(
35  "s", "specify", "specify material IDs by element types (-e)", false);
36  std::vector<TCLAP::Arg*> vec_xors;
37  vec_xors.push_back(&replaceArg);
38  vec_xors.push_back(&condenseArg);
39  vec_xors.push_back(&specifyArg);
40  cmd.xorAdd(vec_xors);
41  TCLAP::ValueArg<std::string> mesh_in(
42  "i", "mesh-input-file",
43  "the name of the file containing the input mesh", true, "",
44  "file name");
45  cmd.add(mesh_in);
46  TCLAP::ValueArg<std::string> mesh_out(
47  "o", "mesh-output-file",
48  "the name of the file the mesh will be written to", true, "",
49  "file name");
50  cmd.add(mesh_out);
51  TCLAP::MultiArg<unsigned> matIDArg("m", "current-material-id",
52  "current material id to be replaced",
53  false, "number");
54  cmd.add(matIDArg);
55  TCLAP::ValueArg<unsigned> newIDArg("n", "new-material-id",
56  "new material id", false, 0, "number");
57  cmd.add(newIDArg);
58  std::vector<std::string> eleList(MeshLib::getMeshElemTypeStringsShort());
59  TCLAP::ValuesConstraint<std::string> allowedVals(eleList);
60  TCLAP::ValueArg<std::string> eleTypeArg("e", "element-type", "element type",
61  false, "", &allowedVals);
62  cmd.add(eleTypeArg);
63 
64  cmd.parse(argc, argv);
65 
66  if (!replaceArg.isSet() && !condenseArg.isSet() && !specifyArg.isSet())
67  {
68  INFO("Please select editing mode: -r or -c or -s");
69  return 0;
70  }
71  if (replaceArg.isSet() && condenseArg.isSet())
72  {
73  INFO("Please select only one editing mode: -r or -c or -s");
74  return 0;
75  }
76  if (replaceArg.isSet())
77  {
78  if (!matIDArg.isSet() || !newIDArg.isSet())
79  {
80  INFO(
81  "current and new material IDs must be provided for "
82  "replacement");
83  return 0;
84  }
85  }
86  else if (specifyArg.isSet())
87  {
88  if (!eleTypeArg.isSet() || !newIDArg.isSet())
89  {
90  INFO(
91  "element type and new material IDs must be provided to specify "
92  "elements");
93  return 0;
94  }
95  }
96 
97  std::unique_ptr<MeshLib::Mesh> mesh(
98  MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
99  INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
100  mesh->getNumberOfElements());
101 
102  if (condenseArg.isSet())
103  {
104  INFO("Condensing material ID...");
106  }
107  else if (replaceArg.isSet())
108  {
109  INFO("Replacing material ID...");
110  const auto vecOldID = matIDArg.getValue();
111  const unsigned newID = newIDArg.getValue();
112  for (auto oldID : vecOldID)
113  {
114  INFO("{:d} -> {:d}", oldID, newID);
115  MeshLib::ElementValueModification::replace(*mesh, oldID, newID,
116  true);
117  }
118  }
119  else if (specifyArg.isSet())
120  {
121  INFO("Specifying material ID...");
122  const std::string eleTypeName(eleTypeArg.getValue());
123  const MeshLib::MeshElemType eleType =
124  MeshLib::String2MeshElemType(eleTypeName);
125  const unsigned newID = newIDArg.getValue();
127  *mesh, eleType, newID);
128  INFO("updated {:d} elements", cnt);
129  }
130 
131  MeshLib::IO::writeMeshToFile(*mesh, mesh_out.getValue());
132 
133  return EXIT_SUCCESS;
134 }
Definition of the ElementValueModification class.
Definition of the Element class.
Git information.
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
Definition of the Mesh class.
static bool replace(MeshLib::Mesh &mesh, int const old_value, int const new_value, bool replace_if_exists=false)
static std::size_t condense(MeshLib::Mesh &mesh)
static std::size_t setByElementType(MeshLib::Mesh &mesh, MeshElemType ele_type, int const new_value)
int main(int argc, char *argv[])
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, [[maybe_unused]] std::set< std::string > variable_output_names)
MeshElemType String2MeshElemType(const std::string &s)
Given a string of the shortened name of the element type, this returns the corresponding MeshElemType...
Definition: MeshEnums.cpp:95
std::vector< std::string > getMeshElemTypeStringsShort()
Returns a vector of strings of mesh element types.
Definition: MeshEnums.cpp:146
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition: MeshEnums.h:27
Definition of readMeshFromFile function.