OGS
writeMeshToFile.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "writeMeshToFile.h"
5
6#include <vector>
7
8#include "BaseLib/FileTools.h"
9#include "BaseLib/Logging.h"
10#include "BaseLib/StringTools.h"
14#include "MeshLib/Mesh.h"
15
16namespace MeshLib::IO
17{
19 std::filesystem::path const& file_path,
20 [[maybe_unused]] std::set<std::string>
21 output_variable_names,
22 bool const use_compression,
23 int const data_mode)
24{
25 if (file_path.extension().string() == ".msh")
26 {
28 meshIO.setMesh(&mesh);
30 return 0;
31 }
32 if (file_path.extension().string() == ".vtu")
33 {
34 if (output_variable_names.empty())
35 {
36 for (auto const& name :
38 {
39 output_variable_names.insert(name);
40 }
41 }
42 MeshLib::IO::VtuInterface writer(&mesh, output_variable_names,
43 data_mode, use_compression);
44 auto const result = writer.writeToFile(file_path);
45 if (!result)
46 {
47 ERR("writeMeshToFile(): Could not write mesh to '{:s}'.",
48 file_path.string());
49 return -1;
50 }
51 return 0;
52 }
53 if (file_path.extension().string() == ".xdmf")
54 {
55 std::vector<std::reference_wrapper<const MeshLib::Mesh>> meshes;
56 const std::reference_wrapper<const MeshLib::Mesh> mr = mesh;
57 meshes.push_back(mr);
58 MeshLib::IO::XdmfHdfWriter(std::move(meshes), file_path, 0, 0.0,
59 output_variable_names, use_compression, 1,
60 1048576);
61 return 0;
62 }
63 ERR("writeMeshToFile(): Unknown file extension '{:s}'. Can not write file "
64 "'{:s}'.",
65 file_path.extension().string(), file_path.string());
66 return -1;
67}
68} // namespace MeshLib::IO
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::string writeToString()
Writes the object to a string.
Definition Writer.cpp:20
Interface for handling mesh files from OGS-5 and below. (*.msh files)
Definition MeshIO.h:26
void setMesh(const MeshLib::Mesh *mesh)
Set mesh for writing.
Definition MeshIO.cpp:427
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
Properties & getProperties()
Definition Mesh.h:127
std::vector< std::string > getPropertyVectorNames() const
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:34
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > output_variable_names, bool const use_compression, int const data_mode)