37{
38 TCLAP::CmdLine cmd(
39 "Takes a mesh with multiple MaterialIDs and writes elements of a given "
40 "ID into a new mesh. If no ID is specified, meshes for each existing "
41 "MaterialID are created.\n\n"
42 "OpenGeoSys-6 software, version " +
44 ".\n"
45 "Copyright (c) 2012-2026, OpenGeoSys Community "
46 "(http://www.opengeosys.org)",
48 TCLAP::ValueArg<std::size_t> arg_mat_id(
49 "m", "material-id",
50 "The MaterialID for which elements should be extracted into a new "
51 "mesh.",
52 false, 0, "MATERIAL_ID");
53 cmd.add(arg_mat_id);
54 TCLAP::ValueArg<std::string> output_arg(
55 "o", "output", "Output (.vtu). Name of the output mesh", true, "",
56 "OUTPUT_FILE");
57 cmd.add(output_arg);
58 TCLAP::ValueArg<std::string> input_arg(
59 "i", "input", "Input (.vtu). Name of the input mesh", true, "",
60 "INPUT_FILE");
61 cmd.add(input_arg);
63 cmd.add(log_level_arg);
64 cmd.parse(argc, argv);
65
68
69 std::string const input_name = input_arg.getValue();
70 std::string const output_name = output_arg.getValue();
73
74 std::unique_ptr<MeshLib::Mesh> const mesh(
76 if (mesh == nullptr)
77 {
78 ERR(
"Error reading input mesh. Aborting...");
79 return EXIT_FAILURE;
80 }
81
83 if (mat_ids == nullptr)
84 {
85 ERR(
"No material IDs found in mesh. Aborting...");
86 return EXIT_FAILURE;
87 }
88
89 auto const [min, max] = ranges::minmax(*mat_ids);
90 if (min == max)
91 {
92 ERR(
"Mesh only contains one material, no extraction required.");
93 return EXIT_FAILURE;
94 }
95 int min_id, max_id;
96 if (arg_mat_id.isSet())
97 {
98 min_id = static_cast<int>(arg_mat_id.getValue());
99 if (min_id < min || min_id > max)
100 {
101 ERR(
"Specified material ID does not exist.");
102 return EXIT_FAILURE;
103 }
104 max_id = min_id;
105 }
106 else
107 {
108 min_id = min;
109 max_id = max;
110 }
111
112 std::ofstream ostream;
113 if (min_id != max_id)
114 {
115 ostream.open(base_name + "_Layers.txt");
116 }
117
118 for (int i = min_id; i <= max_id; ++i)
119 {
120 INFO(
"Extracting material group {:d}...", i);
122 if (mat_group == nullptr)
123 {
124 WARN(
"No elements with material group {:d} found.", i);
125 continue;
126 }
128 std::string const file_name(base_name + "_Layer" + std::to_string(i) +
129 ext);
130 vtu.writeToFile(file_name);
131 if (ostream.is_open())
132 {
133 ostream << file_name << "\n";
134 }
135 }
136 if (ostream.is_open())
137 {
138 ostream.close();
139 }
140 return EXIT_SUCCESS;
141}
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....
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
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)