OGS
OGSFileConverter Class Reference

Detailed Description

A conversion tool for ogs5 and ogs6 files

Definition at line 12 of file OGSFileConverter.h.

#include <OGSFileConverter.h>

Inheritance diagram for OGSFileConverter:
[legend]
Collaboration diagram for OGSFileConverter:
[legend]

Public Member Functions

 OGSFileConverter (std::string const &gmsh_path, QWidget *parent=nullptr)
 Constructor.
 ~OGSFileConverter () override
 Destructor.

Private Slots

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 file name + updated extension.
void convertGLI2GML (const QStringList &input, const QString &output) const
void convertVTU2MSH (const QStringList &input, const QString &output) const
void convertMSH2VTU (const QStringList &input, const QString &output) const
void on_gml2gliButton_pressed () const
void on_gli2gmlButton_pressed () const
void on_vtu2mshButton_pressed () const
void on_msh2vtuButton_pressed () const
void on_closeDialogButton_pressed ()

Private Member Functions

bool fileExists (const std::string &file_name) const
 Checks if a given file already exists.

Private Attributes

std::string const _gmsh_path

Constructor & Destructor Documentation

◆ OGSFileConverter()

OGSFileConverter::OGSFileConverter ( std::string const & gmsh_path,
QWidget * parent = nullptr )
explicit

Constructor.

Definition at line 21 of file OGSFileConverter.cpp.

23 : QDialog(parent), _gmsh_path(gmsh_path)
24{
25 setupUi(this);
26}
std::string const _gmsh_path

References _gmsh_path.

◆ ~OGSFileConverter()

OGSFileConverter::~OGSFileConverter ( )
overridedefault

Destructor.

Member Function Documentation

◆ convertGLI2GML

void OGSFileConverter::convertGLI2GML ( const QStringList & input,
const QString & output ) const
privateslot

Definition at line 77 of file OGSFileConverter.cpp.

79{
80 if (input.empty())
81 {
82 return;
83 }
84
85 GeoLib::GEOObjects geo_objects;
86 GeoLib::IO::XmlGmlInterface xml(geo_objects);
87
88 for (const auto& input_string : input)
89 {
90 const QFileInfo fi(input_string);
91 const std::string output_str =
92 QString(output + "/" + fi.completeBaseName() + ".gml")
93 .toStdString();
94
95 if (fileExists(output_str))
96 {
97 continue;
98 }
99
100 std::string unique_name;
101 std::vector<std::string> errors;
102
103 FileIO::Legacy::readGLIFileV4(input_string.toStdString(), geo_objects,
104 unique_name, errors, _gmsh_path);
105 if (errors.empty() ||
106 (errors.size() == 1 &&
107 errors[0] == "[readSurface] polyline for surface not found!"))
108 {
109 std::string const geo_name =
110 BaseLib::extractBaseName(input_string.toStdString());
111 xml.export_name = geo_name;
112 BaseLib::IO::writeStringToFile(xml.writeToString(), output_str);
113 geo_objects.removeSurfaceVec(geo_name);
114 geo_objects.removePolylineVec(geo_name);
115 geo_objects.removePointVec(geo_name);
116 }
117 else
118 {
119 for (auto& error : errors)
120 {
121 OGSError::box(QString::fromStdString(error));
122 }
123 }
124 }
125 OGSError::box("File conversion finished");
126}
bool removePointVec(const std::string &name)
bool removeSurfaceVec(const std::string &name)
bool removePolylineVec(const std::string &name)
static void box(const QString &e)
Definition OGSError.cpp:13
bool fileExists(const std::string &file_name) const
Checks if a given file already exists.
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:34
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)

References _gmsh_path, OGSError::box(), BaseLib::IO::XMLInterface::export_name, BaseLib::extractBaseName(), fileExists(), FileIO::Legacy::readGLIFileV4(), GeoLib::GEOObjects::removePointVec(), GeoLib::GEOObjects::removePolylineVec(), GeoLib::GEOObjects::removeSurfaceVec(), BaseLib::IO::writeStringToFile(), and BaseLib::IO::Writer::writeToString().

Referenced by on_gli2gmlButton_pressed().

◆ convertGML2GLI

void OGSFileConverter::convertGML2GLI ( const QStringList & input,
const QString & output ) const
privateslot

Converts all files in the input list and writes the new files to the output directory with the same file name + updated extension.

Definition at line 30 of file OGSFileConverter.cpp.

32{
33 if (input.empty())
34 {
35 return;
36 }
37
38 GeoLib::GEOObjects geo_objects;
39 GeoLib::IO::XmlGmlInterface xml(geo_objects);
40
41 for (const auto& input_string : input)
42 {
43 const QFileInfo fi(input_string);
44 const std::string output_str =
45 QString(output + "/" + fi.completeBaseName() + ".gli")
46 .toStdString();
47
48 if (fileExists(output_str))
49 {
50 continue;
51 }
52
53 try
54 {
55 if (!xml.readFile(input_string))
56 {
57 OGSError::box("Error reading geometry " + fi.fileName());
58 continue;
59 }
60 }
61 catch (std::runtime_error const& err)
62 {
63 OGSError::box(err.what(),
64 "Failed to read file `" + input_string + "'");
65 continue;
66 }
67
68 auto const geo_name = geo_objects.getGeometryNames()[0];
69 FileIO::Legacy::writeGLIFileV4(output_str, geo_name, geo_objects);
70 geo_objects.removeSurfaceVec(geo_name);
71 geo_objects.removePolylineVec(geo_name);
72 geo_objects.removePointVec(geo_name);
73 }
74 OGSError::box("File conversion finished");
75}
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
void writeGLIFileV4(const std::string &fname, const std::string &geo_name, const GeoLib::GEOObjects &geo)

References OGSError::box(), fileExists(), GeoLib::GEOObjects::getGeometryNames(), GeoLib::IO::XmlGmlInterface::readFile(), GeoLib::GEOObjects::removePointVec(), GeoLib::GEOObjects::removePolylineVec(), GeoLib::GEOObjects::removeSurfaceVec(), and FileIO::Legacy::writeGLIFileV4().

Referenced by on_gml2gliButton_pressed().

◆ convertMSH2VTU

void OGSFileConverter::convertMSH2VTU ( const QStringList & input,
const QString & output ) const
privateslot

Definition at line 163 of file OGSFileConverter.cpp.

165{
166 if (input.empty())
167 {
168 return;
169 }
170
171 for (const auto& input_string : input)
172 {
173 const QFileInfo fi(input_string);
174 const std::string output_str =
175 QString(output + "/" + fi.completeBaseName() + ".vtu")
176 .toStdString();
177
178 if (fileExists(output_str))
179 {
180 continue;
181 }
182
183 MeshLib::IO::Legacy::MeshIO meshIO;
184 MeshLib::Mesh const* const mesh(
185 meshIO.loadMeshFromFile(input_string.toStdString()));
186 if (mesh == nullptr)
187 {
188 OGSError::box("Error reading mesh " + fi.fileName());
189 continue;
190 }
191 MeshLib::IO::VtuInterface vtu(mesh);
192 vtu.writeToFile(output_str);
193 delete mesh;
194 }
195 OGSError::box("File conversion finished");
196}
MeshLib::Mesh * loadMeshFromFile(const std::string &file_name)
Read mesh from file.
Definition MeshIO.cpp:267

References OGSError::box(), fileExists(), MeshLib::IO::Legacy::MeshIO::loadMeshFromFile(), and MeshLib::IO::VtuInterface::writeToFile().

Referenced by on_msh2vtuButton_pressed().

◆ convertVTU2MSH

void OGSFileConverter::convertVTU2MSH ( const QStringList & input,
const QString & output ) const
privateslot

Definition at line 128 of file OGSFileConverter.cpp.

130{
131 if (input.empty())
132 {
133 return;
134 }
135
136 for (const auto& input_string : input)
137 {
138 const QFileInfo fi(input_string);
139 const std::string output_str =
140 QString(output + "/" + fi.completeBaseName() + ".msh")
141 .toStdString();
142
143 if (fileExists(output_str))
144 {
145 continue;
146 }
147
148 MeshLib::Mesh const* const mesh(
149 MeshLib::IO::VtuInterface::readVTUFile(input_string.toStdString()));
150 if (mesh == nullptr)
151 {
152 OGSError::box("Error reading mesh " + fi.fileName());
153 continue;
154 }
155 MeshLib::IO::Legacy::MeshIO meshIO;
156 meshIO.setMesh(mesh);
157 BaseLib::IO::writeStringToFile(meshIO.writeToString(), output_str);
158 delete mesh;
159 }
160 OGSError::box("File conversion finished");
161}
std::string writeToString()
Writes the object to a string.
Definition Writer.cpp:20
void setMesh(const MeshLib::Mesh *mesh)
Set mesh for writing.
Definition MeshIO.cpp:427
static MeshLib::Mesh * readVTUFile(std::string const &file_name, bool const compute_element_neighbors=false)

References OGSError::box(), fileExists(), MeshLib::IO::VtuInterface::readVTUFile(), MeshLib::IO::Legacy::MeshIO::setMesh(), BaseLib::IO::writeStringToFile(), and BaseLib::IO::Writer::writeToString().

Referenced by on_vtu2mshButton_pressed().

◆ fileExists()

bool OGSFileConverter::fileExists ( const std::string & file_name) const
private

Checks if a given file already exists.

Definition at line 239 of file OGSFileConverter.cpp.

240{
241 std::ifstream const file(file_name.c_str());
242 if (file)
243 {
244 QString const name =
245 QString::fromStdString(BaseLib::extractBaseName(file_name));
246 return !OGSError::question(
247 "The file '" + name +
248 "' already exists.\n Do you want to overwrite it?",
249 "Warning");
250 }
251 return false;
252}
static bool question(const QString &e, const QString &t)
Definition OGSError.cpp:26

References BaseLib::extractBaseName(), and OGSError::question().

Referenced by convertGLI2GML(), convertGML2GLI(), convertMSH2VTU(), and convertVTU2MSH().

◆ on_closeDialogButton_pressed

void OGSFileConverter::on_closeDialogButton_pressed ( )
privateslot

Definition at line 234 of file OGSFileConverter.cpp.

235{
236 this->close();
237}

◆ on_gli2gmlButton_pressed

void OGSFileConverter::on_gli2gmlButton_pressed ( ) const
privateslot

Definition at line 207 of file OGSFileConverter.cpp.

208{
209 FileListDialog dlg(FileType::GLI, FileType::GML);
210 if (dlg.exec())
211 {
212 convertGLI2GML(dlg.getInputFileList(), dlg.getOutputDir());
213 }
214}
void convertGLI2GML(const QStringList &input, const QString &output) const

References convertGLI2GML(), FileListDialog::getInputFileList(), FileListDialog::getOutputDir(), GLI, and GML.

◆ on_gml2gliButton_pressed

void OGSFileConverter::on_gml2gliButton_pressed ( ) const
privateslot

Definition at line 198 of file OGSFileConverter.cpp.

199{
200 FileListDialog dlg(FileType::GML, FileType::GLI);
201 if (dlg.exec())
202 {
203 convertGML2GLI(dlg.getInputFileList(), dlg.getOutputDir());
204 }
205}
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...

References convertGML2GLI(), FileListDialog::getInputFileList(), FileListDialog::getOutputDir(), GLI, and GML.

◆ on_msh2vtuButton_pressed

void OGSFileConverter::on_msh2vtuButton_pressed ( ) const
privateslot

Definition at line 225 of file OGSFileConverter.cpp.

226{
227 FileListDialog dlg(FileType::MSH, FileType::VTU);
228 if (dlg.exec())
229 {
230 convertMSH2VTU(dlg.getInputFileList(), dlg.getOutputDir());
231 }
232}
void convertMSH2VTU(const QStringList &input, const QString &output) const

References convertMSH2VTU(), FileListDialog::getInputFileList(), FileListDialog::getOutputDir(), MSH, and VTU.

◆ on_vtu2mshButton_pressed

void OGSFileConverter::on_vtu2mshButton_pressed ( ) const
privateslot

Definition at line 216 of file OGSFileConverter.cpp.

217{
218 FileListDialog dlg(FileType::VTU, FileType::MSH);
219 if (dlg.exec())
220 {
221 convertVTU2MSH(dlg.getInputFileList(), dlg.getOutputDir());
222 }
223}
void convertVTU2MSH(const QStringList &input, const QString &output) const

References convertVTU2MSH(), FileListDialog::getInputFileList(), FileListDialog::getOutputDir(), MSH, and VTU.

Member Data Documentation

◆ _gmsh_path

std::string const OGSFileConverter::_gmsh_path
private

Definition at line 26 of file OGSFileConverter.h.

Referenced by OGSFileConverter(), and convertGLI2GML().


The documentation for this class was generated from the following files: