OGS
GocadTSurfaceReader.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 <tclap/CmdLine.h>
5
7#include "BaseLib/Logging.h"
8#include "BaseLib/MPI.h"
10#include "InfoLib/GitInfo.h"
12#include "MeshLib/Mesh.h"
13
14std::string getDelim(std::string const& str)
15{
16 std::size_t const bslash = str.find_first_of('\\');
17 char const delim = (bslash == std::string::npos) ? '/' : '\\';
18 return (str.back() == delim) ? "" : std::string(1, delim);
19}
20
21int main(int argc, char* argv[])
22{
23 TCLAP::CmdLine cmd(
24 "Reads Gocad ascii files (*.ts, *.pl, *.mx) and writes TSurf- and PLine"
25 "data into one or more VTU unstructured grids.\n\n"
26 "OpenGeoSys-6 software, version " +
28 ".\n"
29 "Copyright (c) 2012-2026, OpenGeoSys Community "
30 "(http://www.opengeosys.org)",
32
33 TCLAP::SwitchArg write_binary_arg(
34 "b", "write-binary",
35 "if set, OGS-Meshes will be written in binary format");
36 cmd.add(write_binary_arg);
37
38 TCLAP::SwitchArg export_surfaces_arg(
39 "s", "surfaces-only",
40 "if set, only TSurf datasets will be parsed from the input file");
41 cmd.add(export_surfaces_arg);
42
43 TCLAP::SwitchArg export_lines_arg(
44 "", "lines-only",
45 "if set, only PLine datasets will be parsed from the input file");
46 cmd.add(export_lines_arg);
47
48 TCLAP::ValueArg<std::string> output_arg(
49 "o", "output-dir", "Output directory", true, "", "OUTPUT_PATH");
50 cmd.add(output_arg);
51
52 TCLAP::ValueArg<std::string> input_arg(
53 "i", "input-file",
54 "Input (.ts | .pl | .mx). Gocad triangular surfaces file"
55 "Provide a file with unix file "
56 "endings under unix. Use dos2unix to convert. ",
57 true, "", "INPUT_FILE");
58 auto log_level_arg = BaseLib::makeLogLevelArg();
59 cmd.add(log_level_arg);
60 cmd.add(input_arg);
61
62 cmd.parse(argc, argv);
63
64 BaseLib::MPI::Setup mpi_setup(argc, argv);
65 BaseLib::initOGSLogger(log_level_arg.getValue());
66
67 if (export_lines_arg.isSet() && export_surfaces_arg.isSet())
68 {
69 ERR("Both the 'lines-only'-flag and 'surfaces-only'-flag are set. Only "
70 "one is allowed at a time.");
71 return 2;
72 }
73
74 std::string const file_name(input_arg.getValue());
75
77 if (export_lines_arg.isSet())
78 {
80 }
81 if (export_surfaces_arg.isSet())
82 {
84 }
85 std::vector<std::unique_ptr<MeshLib::Mesh>> meshes;
86 if (!FileIO::Gocad::GocadAsciiReader::readFile(file_name, meshes, t))
87 {
88 ERR("Error reading file.");
89 return 1;
90 }
91 INFO("{:d} meshes found.", meshes.size());
92 std::string const dir = output_arg.getValue();
93 bool const write_binary = write_binary_arg.getValue();
94 std::string const delim = getDelim(dir);
95 for (auto& mesh : meshes)
96 {
97 if (mesh == nullptr)
98 {
99 continue;
100 }
101 INFO("Writing mesh \"{:s}\"", mesh->getName());
102 int data_mode = (write_binary) ? 2 : 0;
103 bool compressed = (write_binary);
104 MeshLib::IO::VtuInterface vtu(mesh.get(), data_mode, compressed);
105 vtu.writeToFile(dir + delim + mesh->getName() + ".vtu");
106 }
107 return 0;
108}
int main(int argc, char *argv[])
std::string getDelim(std::string const &str)
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
bool readFile(std::string const &file_name, std::vector< std::unique_ptr< MeshLib::Mesh > > &meshes, DataType const export_type)
Reads the specified file and writes data into internal mesh vector.
GITINFOLIB_EXPORT const std::string ogs_version