OGS
OGSFileConverter Class Reference

Detailed Description

A conversion tool for ogs5 and ogs6 files

Definition at line 23 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 32 of file OGSFileConverter.cpp.

34 : QDialog(parent), _gmsh_path(gmsh_path)
35{
36 setupUi(this);
37}
std::string const _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 88 of file OGSFileConverter.cpp.

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;
123 BaseLib::IO::writeStringToFile(xml.writeToString(), output_str);
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}
Container class for geometric objects.
Definition GEOObjects.h:57
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.
static void box(const QString &e)
Definition OGSError.cpp:23
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: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)

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 41 of file OGSFileConverter.cpp.

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}
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 174 of file OGSFileConverter.cpp.

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}
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
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....

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 139 of file OGSFileConverter.cpp.

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}
std::string writeToString()
Writes the object to a string.
Definition Writer.cpp:31
void setMesh(const MeshLib::Mesh *mesh)
Set mesh for writing.
Definition MeshIO.cpp:439
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 250 of file OGSFileConverter.cpp.

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}
static bool question(const QString &e, const QString &t)
Definition OGSError.cpp:36

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 245 of file OGSFileConverter.cpp.

246{
247 this->close();
248}

◆ on_gli2gmlButton_pressed

void OGSFileConverter::on_gli2gmlButton_pressed ( ) const
privateslot

Definition at line 218 of file OGSFileConverter.cpp.

219{
221 if (dlg.exec())
222 {
223 convertGLI2GML(dlg.getInputFileList(), dlg.getOutputDir());
224 }
225}
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 209 of file OGSFileConverter.cpp.

210{
212 if (dlg.exec())
213 {
214 convertGML2GLI(dlg.getInputFileList(), dlg.getOutputDir());
215 }
216}
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 236 of file OGSFileConverter.cpp.

237{
239 if (dlg.exec())
240 {
241 convertMSH2VTU(dlg.getInputFileList(), dlg.getOutputDir());
242 }
243}
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 227 of file OGSFileConverter.cpp.

228{
230 if (dlg.exec())
231 {
232 convertVTU2MSH(dlg.getInputFileList(), dlg.getOutputDir());
233 }
234}
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 37 of file OGSFileConverter.h.

Referenced by convertGLI2GML().


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