From bb028c798998015bfebec284b0580b0823024b87 Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 17 May 2022 12:20:22 +0200 Subject: [PATCH] Mesh: modernize MeshIO classes --- src/Mod/Mesh/App/Core/MeshIO.cpp | 89 ++++++++++++++------------------ src/Mod/Mesh/App/Core/MeshIO.h | 4 +- 2 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/Mod/Mesh/App/Core/MeshIO.cpp b/src/Mod/Mesh/App/Core/MeshIO.cpp index a2e8a4b1dc..c4b6d23cdd 100644 --- a/src/Mod/Mesh/App/Core/MeshIO.cpp +++ b/src/Mod/Mesh/App/Core/MeshIO.cpp @@ -57,39 +57,7 @@ using namespace MeshCore; -char *upper(char * string) -{ - int i; - int l; - - if (string != nullptr) { - l = std::strlen(string); - for (i=0; i MeshInput::supportedMeshFormats() return fmt; } +MeshIO::Format MeshInput::getFormat(const char* FileName) +{ + Base::FileInfo fi(FileName); + if (fi.hasExtension("bms")) { + return MeshIO::Format::BMS; + } + else if (fi.hasExtension("ply")) { + return MeshIO::Format::PLY; + } + else if (fi.hasExtension("stl")) { + return MeshIO::Format::STL; + } + else if (fi.hasExtension("ast")) { + return MeshIO::Format::ASTL; + } + else if (fi.hasExtension("obj")) { + return MeshIO::Format::OBJ; + } + else if (fi.hasExtension("off")) { + return MeshIO::Format::OFF; + } + else if (fi.hasExtension("smf")) { + return MeshIO::Format::SMF; + } + else { + throw Base::FileException("File extension not supported",FileName); + } +} + bool MeshInput::LoadAny(const char* FileName) { // ask for read permission @@ -219,6 +214,8 @@ bool MeshInput::LoadFormat(std::istream &str, MeshIO::Format fmt) return LoadAsciiSTL(str); case MeshIO::BSTL: return LoadBinarySTL(str); + case MeshIO::STL: + return LoadSTL(str); case MeshIO::OBJ: return LoadOBJ(str); case MeshIO::SMF: @@ -262,7 +259,7 @@ bool MeshInput::LoadSTL (std::istream &rstrIn) if (!rstrIn.read(szBuf, ulBytes)) return (ulCt==0); szBuf[ulBytes] = 0; - upper(szBuf); + boost::algorithm::to_upper(szBuf); try { if ((strstr(szBuf, "SOLID") == nullptr) && (strstr(szBuf, "FACET") == nullptr) && (strstr(szBuf, "NORMAL") == nullptr) && @@ -1343,8 +1340,7 @@ bool MeshInput::LoadAsciiSTL (std::istream &rstrIn) // count facets while (std::getline(rstrIn, line)) { - for (std::string::iterator it = line.begin(); it != line.end(); ++it) - *it = toupper(*it); + boost::algorithm::to_upper(line); if (line.find("ENDFACET") != std::string::npos) ulFacetCt++; // prevent from reading EOF (as I don't know how to reread the file then) @@ -1366,8 +1362,7 @@ bool MeshInput::LoadAsciiSTL (std::istream &rstrIn) ulVertexCt = 0; while (std::getline(rstrIn, line)) { - for (std::string::iterator it = line.begin(); it != line.end(); ++it) - *it = toupper(*it); + boost::algorithm::to_upper(line); if (boost::regex_match(line.c_str(), what, rx_f)) { fX = (float)std::atof(what[1].first); fY = (float)std::atof(what[4].first); @@ -1527,8 +1522,7 @@ bool MeshInput::LoadInventor (std::istream &rstrIn) bool points = false; bool facets = false; while (std::getline(rstrIn, line) && !facets) { - for (std::string::iterator it = line.begin(); it != line.end(); ++it) - *it = toupper(*it); + boost::algorithm::to_upper(line); // read the normals if they are defined if (!normals && line.find("NORMAL {") != std::string::npos) { @@ -1540,8 +1534,7 @@ bool MeshInput::LoadInventor (std::istream &rstrIn) // This is a special case to support also file formats directly written by // Inventor 2.1 classes. std::getline(rstrIn, line); - for (std::string::iterator it = line.begin(); it != line.end(); ++it) - *it = toupper(*it); + boost::algorithm::to_upper(line); std::string::size_type pos = line.find("VECTOR ["); if (pos != std::string::npos) line = line.substr(pos+8); // 8 = length of 'VECTOR [' @@ -1567,8 +1560,7 @@ bool MeshInput::LoadInventor (std::istream &rstrIn) // This is a special case to support also file formats directly written by // Inventor 2.1 classes. std::getline(rstrIn, line); - for (std::string::iterator it = line.begin(); it != line.end(); ++it) - *it = toupper(*it); + boost::algorithm::to_upper(line); std::string::size_type pos = line.find("POINT ["); if (pos != std::string::npos) line = line.substr(pos+7); // 7 = length of 'POINT [' @@ -1595,8 +1587,7 @@ bool MeshInput::LoadInventor (std::istream &rstrIn) // Furthermore we must check whether more than one triple is given per line, which // is handled in the while-loop. std::getline(rstrIn, line); - for (std::string::iterator it = line.begin(); it != line.end(); ++it) - *it = toupper(*it); + boost::algorithm::to_upper(line); std::string::size_type pos = line.find("COORDINDEX ["); if (pos != std::string::npos) line = line.substr(pos+12); // 12 = length of 'COORDINDEX [' @@ -1660,7 +1651,7 @@ bool MeshInput::LoadNastran (std::istream &rstrIn) int badElementCounter = 0; while (std::getline(rstrIn, line)) { - upper(ltrim(line)); + boost::algorithm::to_upper(ltrim(line)); if (line.empty()) { // Skip all the following tests } diff --git a/src/Mod/Mesh/App/Core/MeshIO.h b/src/Mod/Mesh/App/Core/MeshIO.h index d7ff5c42e1..acd08f446b 100644 --- a/src/Mod/Mesh/App/Core/MeshIO.h +++ b/src/Mod/Mesh/App/Core/MeshIO.h @@ -44,6 +44,7 @@ namespace MeshIO { BMS, ASTL, BSTL, + STL, OBJ, OFF, IDTF, @@ -74,7 +75,7 @@ struct MeshExport Material { Material() : binding(MeshIO::OVERALL) {} MeshIO::Binding binding; - std::string library; + mutable std::string library; std::vector diffuseColor; }; @@ -134,6 +135,7 @@ public: bool LoadCadmouldFE (std::ifstream &rstrIn); static std::vector supportedMeshFormats(); + static MeshIO::Format getFormat(const char* FileName); protected: MeshKernel &_rclMesh; /**< reference to mesh data structure */