46{
47 TCLAP::CmdLine cmd(
48 "Takes a mesh with multiple MaterialIDs and writes elements of a given "
49 "ID into a new mesh. If no ID is specified, meshes for each existing "
50 "MaterialID are created.\n\n"
51 "OpenGeoSys-6 software, version " +
53 ".\n"
54 "Copyright (c) 2012-2024, OpenGeoSys Community "
55 "(http://www.opengeosys.org)",
57 TCLAP::ValueArg<std::size_t> arg_mat_id(
58 "m", "material-id",
59 "The MaterialID for which elements should be extracted into a new "
60 "mesh.",
61 false, 0, "Number specifying the MaterialID");
62 cmd.add(arg_mat_id);
63 TCLAP::ValueArg<std::string> output_arg("o", "output",
64 "Name of the output mesh (*.vtu)",
65 true, "", "output file name");
66 cmd.add(output_arg);
67 TCLAP::ValueArg<std::string> input_arg("i", "input",
68 "Name of the input mesh (*.vtu)",
69 true, "", "input file name");
70 cmd.add(input_arg);
71 cmd.parse(argc, argv);
72
74
75 std::string const input_name = input_arg.getValue();
76 std::string const output_name = output_arg.getValue();
79
80 std::unique_ptr<MeshLib::Mesh> const mesh(
82 if (mesh == nullptr)
83 {
84 ERR(
"Error reading input mesh. Aborting...");
85 return EXIT_FAILURE;
86 }
87
89 if (mat_ids == nullptr)
90 {
91 ERR(
"No material IDs found in mesh. Aborting...");
92 return EXIT_FAILURE;
93 }
94
95 auto id_range = std::minmax_element(mat_ids->cbegin(), mat_ids->cend());
96 if (id_range.first == id_range.second)
97 {
98 ERR(
"Mesh only contains one material, no extraction required.");
99 return EXIT_FAILURE;
100 }
101 int min_id, max_id;
102 if (arg_mat_id.isSet())
103 {
104 min_id = static_cast<int>(arg_mat_id.getValue());
105 if (min_id < *id_range.first || min_id > *id_range.second)
106 {
107 ERR(
"Specified material ID does not exist.");
108 return EXIT_FAILURE;
109 }
110 max_id = min_id;
111 }
112 else
113 {
114 min_id = *id_range.first;
115 max_id = *id_range.second;
116 }
117
118 std::ofstream ostream;
119 if (min_id != max_id)
120 {
121 ostream.open(base_name + "_Layers.txt");
122 }
123
124 for (int i = min_id; i <= max_id; ++i)
125 {
126 INFO(
"Extracting material group {:d}...", i);
128 if (mat_group == nullptr)
129 {
130 WARN(
"No elements with material group {:d} found.", i);
131 continue;
132 }
134 std::string const file_name(base_name + "_Layer" + std::to_string(i) +
135 ext);
136 vtu.writeToFile(file_name);
137 if (ostream.is_open())
138 {
139 ostream << file_name << "\n";
140 }
141 }
142 if (ostream.is_open())
143 {
144 ostream.close();
145 }
146 return EXIT_SUCCESS;
147}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
std::string getFileExtension(const std::string &path)
std::string dropFileExtension(std::string const &filename)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
PropertyVector< int > const * materialIDs(Mesh const &mesh)