OGS
OGSFileConverter.cpp
Go to the documentation of this file.
1
15#define BOOST_FILESYSTEM_VERSION 3
16
17#include "OGSFileConverter.h"
18
19#include <QFileInfo>
20
23#include "BaseLib/FileTools.h"
24#include "BaseLib/StringTools.h"
25#include "FileListDialog.h"
26#include "GeoLib/GEOObjects.h"
30#include "MeshLib/Mesh.h"
31
32OGSFileConverter::OGSFileConverter(std::string const& gmsh_path,
33 QWidget* parent)
34 : QDialog(parent), _gmsh_path(gmsh_path)
35{
36 setupUi(this);
37}
38
40
41void OGSFileConverter::convertGML2GLI(const QStringList& input,
42 const QString& output) const
43{
44 if (input.empty())
45 {
46 return;
47 }
48
49 GeoLib::GEOObjects geo_objects;
50 GeoLib::IO::XmlGmlInterface xml(geo_objects);
51
52 for (const auto& input_string : input)
53 {
54 const QFileInfo fi(input_string);
55 const std::string output_str =
56 QString(output + "/" + fi.completeBaseName() + ".gli")
57 .toStdString();
58
59 if (fileExists(output_str))
60 {
61 continue;
62 }
63
64 try
65 {
66 if (!xml.readFile(input_string))
67 {
68 OGSError::box("Error reading geometry " + fi.fileName());
69 continue;
70 }
71 }
72 catch (std::runtime_error const& err)
73 {
74 OGSError::box(err.what(),
75 "Failed to read file `" + input_string + "'");
76 continue;
77 }
78
79 auto const geo_name = geo_objects.getGeometryNames()[0];
80 FileIO::Legacy::writeGLIFileV4(output_str, geo_name, geo_objects);
81 geo_objects.removeSurfaceVec(geo_name);
82 geo_objects.removePolylineVec(geo_name);
83 geo_objects.removePointVec(geo_name);
84 }
85 OGSError::box("File conversion finished");
86}
87
88void OGSFileConverter::convertGLI2GML(const QStringList& input,
89 const QString& output) const
90{
91 if (input.empty())
92 {
93 return;
94 }
95
96 GeoLib::GEOObjects geo_objects;
97 GeoLib::IO::XmlGmlInterface xml(geo_objects);
98
99 for (const auto& input_string : input)
100 {
101 const QFileInfo fi(input_string);
102 const std::string output_str =
103 QString(output + "/" + fi.completeBaseName() + ".gml")
104 .toStdString();
105
106 if (fileExists(output_str))
107 {
108 continue;
109 }
110
111 std::string unique_name;
112 std::vector<std::string> errors;
113
114 FileIO::Legacy::readGLIFileV4(input_string.toStdString(), geo_objects,
115 unique_name, errors, _gmsh_path);
116 if (errors.empty() ||
117 (errors.size() == 1 &&
118 errors[0] == "[readSurface] polyline for surface not found!"))
119 {
120 std::string const geo_name =
121 BaseLib::extractBaseName(input_string.toStdString());
122 xml.export_name = geo_name;
124 geo_objects.removeSurfaceVec(geo_name);
125 geo_objects.removePolylineVec(geo_name);
126 geo_objects.removePointVec(geo_name);
127 }
128 else
129 {
130 for (auto& error : errors)
131 {
132 OGSError::box(QString::fromStdString(error));
133 }
134 }
135 }
136 OGSError::box("File conversion finished");
137}
138
139void OGSFileConverter::convertVTU2MSH(const QStringList& input,
140 const QString& output) const
141{
142 if (input.empty())
143 {
144 return;
145 }
146
147 for (const auto& input_string : input)
148 {
149 const QFileInfo fi(input_string);
150 const std::string output_str =
151 QString(output + "/" + fi.completeBaseName() + ".msh")
152 .toStdString();
153
154 if (fileExists(output_str))
155 {
156 continue;
157 }
158
159 MeshLib::Mesh const* const mesh(
160 MeshLib::IO::VtuInterface::readVTUFile(input_string.toStdString()));
161 if (mesh == nullptr)
162 {
163 OGSError::box("Error reading mesh " + fi.fileName());
164 continue;
165 }
167 meshIO.setMesh(mesh);
168 BaseLib::IO::writeStringToFile(meshIO.writeToString(), output_str);
169 delete mesh;
170 }
171 OGSError::box("File conversion finished");
172}
173
174void OGSFileConverter::convertMSH2VTU(const QStringList& input,
175 const QString& output) const
176{
177 if (input.empty())
178 {
179 return;
180 }
181
182 for (const auto& input_string : input)
183 {
184 const QFileInfo fi(input_string);
185 const std::string output_str =
186 QString(output + "/" + fi.completeBaseName() + ".vtu")
187 .toStdString();
188
189 if (fileExists(output_str))
190 {
191 continue;
192 }
193
195 MeshLib::Mesh const* const mesh(
196 meshIO.loadMeshFromFile(input_string.toStdString()));
197 if (mesh == nullptr)
198 {
199 OGSError::box("Error reading mesh " + fi.fileName());
200 continue;
201 }
203 vtu.writeToFile(output_str);
204 delete mesh;
205 }
206 OGSError::box("File conversion finished");
207}
208
210{
212 if (dlg.exec())
213 {
215 }
216}
217
219{
221 if (dlg.exec())
222 {
224 }
225}
226
228{
230 if (dlg.exec())
231 {
233 }
234}
235
237{
239 if (dlg.exec())
240 {
242 }
243}
244
246{
247 this->close();
248}
249
250bool OGSFileConverter::fileExists(const std::string& file_name) const
251{
252 std::ifstream const file(file_name.c_str());
253 if (file)
254 {
255 QString const name =
256 QString::fromStdString(BaseLib::extractBaseName(file_name));
257 return !OGSError::question(
258 "The file '" + name +
259 "' already exists.\n Do you want to overwrite it?",
260 "Warning");
261 }
262 return false;
263}
Definition of FileListDialog class.
Filename manipulation routines.
Definition of the GEOObjects class.
Definition of the MeshIO class.
Definition of the Mesh class.
Definition of the OGSError class.
Definition of OGSFileConverter class.
Definition of the OGSIOVer4 class.
Definition of string helper functions.
Implementation of the VtuInterface class.
Definition of the XmlGmlInterface class.
std::string writeToString()
Writes the object to a string.
Definition Writer.cpp:31
const QStringList getInputFileList() const
Returns list of all selected files.
const QString getOutputDir() const
Returns selected output directory.
Container class for geometric objects.
Definition GEOObjects.h:57
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
bool removePointVec(const std::string &name)
bool removeSurfaceVec(const std::string &name)
bool removePolylineVec(const std::string &name)
Reads and writes GeoObjects to and from XML files.
int readFile(const QString &fileName) override
Reads an xml-file containing geometric object definitions into the GEOObjects used in the constructor...
Interface for handling mesh files from OGS-5 and below. (*.msh files)
Definition MeshIO.h:37
MeshLib::Mesh * loadMeshFromFile(const std::string &file_name)
Read mesh from file.
Definition MeshIO.cpp:278
void setMesh(const MeshLib::Mesh *mesh)
Set mesh for writing.
Definition MeshIO.cpp:439
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
static MeshLib::Mesh * readVTUFile(std::string const &file_name, bool const compute_element_neighbors=false)
bool writeToFile(std::filesystem::path const &file_path)
static void box(const QString &e)
Definition OGSError.cpp:23
static bool question(const QString &e, const QString &t)
Definition OGSError.cpp:36
bool fileExists(const std::string &file_name) const
Checks if a given file already exists.
void on_vtu2mshButton_pressed() const
void convertGLI2GML(const QStringList &input, const QString &output) const
std::string const _gmsh_path
void on_msh2vtuButton_pressed() const
OGSFileConverter(std::string const &gmsh_path, QWidget *parent=nullptr)
Constructor.
void on_gml2gliButton_pressed() const
~OGSFileConverter() override
Destructor.
void convertVTU2MSH(const QStringList &input, const QString &output) const
void convertGML2GLI(const QStringList &input, const QString &output) const
Converts all files in the input list and writes the new files to the output directory with the same f...
void convertMSH2VTU(const QStringList &input, const QString &output) const
void on_gli2gmlButton_pressed() const
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:45
std::string extractBaseName(std::string const &pathname)
bool readGLIFileV4(const std::string &fname, GeoLib::GEOObjects &geo, std::string &unique_name, std::vector< std::string > &errors, std::string const &gmsh_path)
void writeGLIFileV4(const std::string &fname, const std::string &geo_name, const GeoLib::GEOObjects &geo)