diff --git a/applications/test/surfaceMeshConvert/Test-surfaceMeshConvert.C b/applications/test/surfaceMeshConvert/Test-surfaceMeshConvert.C index 9b00d144cd..4f2b14b7db 100644 --- a/applications/test/surfaceMeshConvert/Test-surfaceMeshConvert.C +++ b/applications/test/surfaceMeshConvert/Test-surfaceMeshConvert.C @@ -148,8 +148,8 @@ int main(int argc, char *argv[]) if (importName == exportName) { - FatalErrorInFunction - << "Output file " << exportName << " would overwrite input file." + FatalError + << "Output file would overwrite input file." << exit(FatalError); } diff --git a/applications/utilities/mesh/conversion/ccm/ccmToFoam/ccmToFoam.C b/applications/utilities/mesh/conversion/ccm/ccmToFoam/ccmToFoam.C index dede3f2a6a..6fda2d0150 100644 --- a/applications/utilities/mesh/conversion/ccm/ccmToFoam/ccmToFoam.C +++ b/applications/utilities/mesh/conversion/ccm/ccmToFoam/ccmToFoam.C @@ -171,7 +171,7 @@ int main(int argc, char *argv[]) fileName exportName; if (args.readIfPresent("name", exportName)) { - const word ext = exportName.ext(); + const word ext(exportName.ext()); // strip erroneous extension (.ccm, .ccmg, .ccmp) if (ext == "ccm" || ext == "ccmg" || ext == "ccmp") { diff --git a/applications/utilities/mesh/conversion/ccm/foamToCcm/foamToCcm.C b/applications/utilities/mesh/conversion/ccm/foamToCcm/foamToCcm.C index 0e45adf708..0d2c7cfbba 100644 --- a/applications/utilities/mesh/conversion/ccm/foamToCcm/foamToCcm.C +++ b/applications/utilities/mesh/conversion/ccm/foamToCcm/foamToCcm.C @@ -133,7 +133,7 @@ int main(int argc, char *argv[]) fileName exportName = ccm::writer::defaultMeshName; if (args.readIfPresent("name", exportName)) { - const word ext = exportName.ext(); + const word ext(exportName.ext()); // strip erroneous extension (.ccm, .ccmg, .ccmp) if (ext == "ccm" || ext == "ccmg" || ext == "ccmp") { diff --git a/applications/utilities/surface/surfaceConvert/surfaceConvert.C b/applications/utilities/surface/surfaceConvert/surfaceConvert.C index a09414d2a8..c1707c4a87 100644 --- a/applications/utilities/surface/surfaceConvert/surfaceConvert.C +++ b/applications/utilities/surface/surfaceConvert/surfaceConvert.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -39,6 +40,12 @@ Usage - \par -clean Perform some surface checking/cleanup on the input surface + - \par -read-format \ + Specify input file format + + - \par -write-format \ + Specify output file format + - \par -scale \ Specify a scaling factor for writing the files @@ -59,6 +66,36 @@ Note using namespace Foam; +static word getExtension(const fileName& name) +{ + word ext(name.ext()); + if (ext == "gz") + { + ext = name.lessExt().ext(); + } + + return ext; +} + + +// Non-short-circuiting check to get all warnings +static bool hasReadWriteTypes(const word& readType, const word& writeType) +{ + volatile bool good = true; + + if (!triSurface::canReadType(readType, true)) + { + good = false; + } + + if (!triSurface::canWriteType(writeType, true)) + { + good = false; + } + + return good; +} + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -84,6 +121,18 @@ int main(int argc, char *argv[]) "Reorder faces into groups; one per region" ); argList::addOption + ( + "read-format", + "type", + "The input format (default: use file extension)" + ); + argList::addOption + ( + "write-format", + "type", + "The output format (default: use file extension)" + ); + argList::addOption ( "scale", "factor", @@ -93,7 +142,7 @@ int main(int argc, char *argv[]) ( "precision", "int", - "Write to output with the specified precision" + "The output precision" ); argList::addOptionCompat("precision", {"writePrecision", 1812}); @@ -110,30 +159,47 @@ int main(int argc, char *argv[]) } } - const fileName importName = args[1]; - const fileName exportName = args[2]; + const fileName importName(args[1]); + const fileName exportName(args[2]); if (importName == exportName) { - FatalErrorInFunction - << "Output file " << exportName << " would overwrite input file." + FatalError + << "Output file would overwrite input file." << nl << exit(FatalError); } - // Check that reading/writing is supported - if + const word readFileType ( - !triSurface::canRead(importName, true) - || !triSurface::canWriteType(exportName.ext(), true) - ) + args.getOrDefault("read-format", getExtension(importName)) + ); + + const word writeFileType + ( + args.getOrDefault("write-format", getExtension(exportName)) + ); + + + // Check that reading/writing is supported + if (!hasReadWriteTypes(readFileType, writeFileType)) { - return 1; + FatalError + << "Unsupported file format(s)" << nl + << exit(FatalError); } - const scalar scaleFactor = args.get("scale", -1); + + scalar scaleFactor(0); Info<< "Reading : " << importName << endl; - triSurface surf(importName, scaleFactor); + triSurface surf(importName, readFileType, scaleFactor); + + if (args.readIfPresent("scale", scaleFactor) && scaleFactor > 0) + { + Info<< "scale input " << scaleFactor << nl; + surf.scalePoints(scaleFactor); + } + Info<< "Read surface:" << endl; surf.writeStats(Info); @@ -159,10 +225,9 @@ int main(int argc, char *argv[]) Info<< "Maintaining face ordering" << endl; } - Info<< "writing " << exportName; - Info<< endl; + Info<< "writing " << exportName << endl; - surf.write(exportName, sortByRegion); + surf.write(exportName, writeFileType, sortByRegion); Info<< "\nEnd\n" << endl; diff --git a/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C b/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C index 93a93911e7..087b0eed27 100644 --- a/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C +++ b/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2015 OpenCFD Ltd. + Copyright (C) 2015-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -42,8 +42,38 @@ Description using namespace Foam; -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +static word getExtension(const fileName& name) +{ + word ext(name.ext()); + if (ext == "gz") + { + ext = name.lessExt().ext(); + } + return ext; +} + + +// Non-short-circuiting check to get all warnings +static bool hasReadWriteTypes(const word& readType, const word& writeType) +{ + volatile bool good = true; + + if (!edgeMesh::canReadType(readType, true)) + { + good = false; + } + + if (!edgeMesh::canWriteType(writeType, true)) + { + good = false; + } + + return good; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // int main(int argc, char *argv[]) { @@ -55,44 +85,64 @@ int main(int argc, char *argv[]) argList::addArgument("input", "The input edge file"); argList::addArgument("output", "The output edge file"); argList::addOption + ( + "read-format", + "type", + "The input format (default: use file extension)" + ); + argList::addOption + ( + "write-format", + "type", + "The output format (default: use file extension)" + ); + argList::addOption ( "scale", "factor", - "Geometry scaling factor - default is 1" + "Input geometry scaling factor" ); argList args(argc, argv); Time runTime(args.rootPath(), args.caseName()); - const fileName importName = args[1]; - const fileName exportName = args[2]; + const fileName importName(args[1]); + const fileName exportName(args[2]); // Disable inplace editing if (importName == exportName) { - FatalErrorInFunction - << "Output file " << exportName << " would overwrite input file." + FatalError + << "Output file would overwrite input file." << exit(FatalError); } - // Check that reading/writing is supported - if + const word readFileType ( - !edgeMesh::canReadType(importName.ext(), true) - || !edgeMesh::canWriteType(exportName.ext(), true) - ) + args.getOrDefault("read-format", getExtension(importName)) + ); + + const word writeFileType + ( + args.getOrDefault("write-format", getExtension(exportName)) + ); + + // Check that reading/writing is supported + if (!hasReadWriteTypes(readFileType, writeFileType)) { - return 1; + FatalError + << "Unsupported file format(s)" << nl + << exit(FatalError); } - edgeMesh mesh(importName); + edgeMesh mesh(importName, readFileType); Info<< "\nRead edgeMesh " << importName << nl; mesh.writeStats(Info); Info<< nl << "\nwriting " << exportName; - scalar scaleFactor = 0; + scalar scaleFactor(0); if (args.readIfPresent("scale", scaleFactor) && scaleFactor > 0) { Info<< " with scaling " << scaleFactor << endl; @@ -103,7 +153,7 @@ int main(int argc, char *argv[]) Info<< " without scaling" << endl; } - mesh.write(exportName); + mesh.write(exportName, writeFileType); mesh.writeStats(Info); Info<< "\nEnd\n" << endl; diff --git a/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C b/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C index b2e7582bb8..007f2a2940 100644 --- a/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C +++ b/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C @@ -41,26 +41,32 @@ Usage - \par -clean Perform some surface checking/cleanup on the input surface. - - \par -scaleIn \ - Specify a scaling factor when reading files. + - \par -read-format \ + The input file format (default: use file extension) - - \par -scaleOut \ - Specify a scaling factor when writing files. + - \par -write-format \ + The output file format (default: use file extension) + + - \par -read-scale \ + Input geometry scaling factor. + + - \par -write-scale \ + Output geometry scaling factor. - \par -dict \ - Specify an alternative dictionary for constant/coordinateSystems. + Alternative dictionary for constant/coordinateSystems. - \par -from \ - Specify a coordinate System when reading files. + Apply specified coordinate system after reading file. - \par -to \ - Specify a coordinate System when writing files. + Apply specified coordinate system before writing file. - \par -tri Triangulate surface. Note - The filename extensions are used to determine the file format type. + The filename extensions are used to determine the default file formats. \*---------------------------------------------------------------------------*/ @@ -73,6 +79,36 @@ Note using namespace Foam; +static word getExtension(const fileName& name) +{ + word ext(name.ext()); + if (ext == "gz") + { + ext = name.lessExt().ext(); + } + + return ext; +} + +// Non-short-circuiting check to get all warnings +static bool hasReadWriteTypes(const word& readType, const word& writeType) +{ + volatile bool good = true; + + if (!meshedSurface::canReadType(readType, true)) + { + good = false; + } + + if (!meshedSurface::canWriteType(writeType, true)) + { + good = false; + } + + return good; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // int main(int argc, char *argv[]) @@ -93,30 +129,46 @@ int main(int argc, char *argv[]) ); argList::addOption ( - "scaleIn", - "factor", - "Geometry scaling factor on input" + "read-format", + "type", + "Input format (default: use file extension)" ); argList::addOption ( - "scaleOut", - "factor", - "Geometry scaling factor on output" + "write-format", + "type", + "Output format (default: use file extension)" ); - argList::addOption("dict", "file", "Use alternative coordinateSystems"); + argList::addOption + ( + "read-scale", + "factor", + "Input geometry scaling factor" + ); + argList::addOption + ( + "write-scale", + "factor", + "Output geometry scaling factor" + ); + + argList::addOptionCompat("read-scale", {"scaleIn", 1912}); + argList::addOptionCompat("write-scale", {"scaleOut", 1912}); + + argList::addOption("dict", "file", "Alternative coordinateSystems"); argList::addOption ( "from", "system", - "Specify the source coordinate system, applied after '-scaleIn'", + "The source coordinate system, applied after '-read-scale'", true // advanced ); argList::addOption ( "to", "system", - "Specify the target coordinate system, applied before '-scaleOut'", + "The target coordinate system, applied before '-write-scale'", true // advanced ); argList::addBoolOption @@ -129,28 +181,38 @@ int main(int argc, char *argv[]) argList args(argc, argv); Time runTime(args.rootPath(), args.caseName()); - const fileName importName = args[1]; - const fileName exportName = args[2]; + const fileName importName(args[1]); + const fileName exportName(args[2]); - // disable inplace editing if (importName == exportName) { - FatalErrorInFunction - << "Output file " << exportName << " would overwrite input file." + FatalError + << "Output file would overwrite input file." << exit(FatalError); } - // Check that reading/writing is supported - if + const word readFileType ( - !MeshedSurface::canRead(importName, true) - || !MeshedSurface::canWriteType(exportName.ext(), true) - ) + args.getOrDefault("read-format", getExtension(importName)) + ); + + const word writeFileType + ( + args.getOrDefault("write-format", getExtension(exportName)) + ); + + + // Check that reading/writing is supported + if (!hasReadWriteTypes(readFileType, writeFileType)) { - return 1; + FatalError + << "Unsupported file format(s)" << nl + << exit(FatalError); } + scalar scaleFactor(0); + // The coordinate transformations (must be cartesian) autoPtr fromCsys; autoPtr toCsys; @@ -173,7 +235,7 @@ int main(int argc, char *argv[]) if (!ioCsys.typeHeaderOk(false)) { - FatalErrorInFunction + FatalError << "Cannot open coordinateSystems file\n " << ioCsys.objectPath() << nl << exit(FatalError); @@ -188,7 +250,7 @@ int main(int argc, char *argv[]) if (!csPtr) { - FatalErrorInFunction + FatalError << "Cannot find -from " << csName << nl << "available coordinateSystems: " << flatOutput(globalCoords.names()) << nl @@ -205,7 +267,7 @@ int main(int argc, char *argv[]) if (!csPtr) { - FatalErrorInFunction + FatalError << "Cannot find -to " << csName << nl << "available coordinateSystems: " << flatOutput(globalCoords.names()) << nl @@ -218,7 +280,7 @@ int main(int argc, char *argv[]) // Maybe fix this later if (fromCsys && toCsys) { - FatalErrorInFunction + FatalError << "Only allowed '-from' or '-to' option at the moment." << exit(FatalError); } @@ -226,24 +288,23 @@ int main(int argc, char *argv[]) { - MeshedSurface surf(importName); + meshedSurface surf(importName, readFileType); + + if (args.readIfPresent("read-scale", scaleFactor) && scaleFactor > 0) + { + Info<< "scale input " << scaleFactor << nl; + surf.scalePoints(scaleFactor); + } if (args.found("clean")) { surf.cleanup(true); } - scalar scaleIn = 0; - if (args.readIfPresent("scaleIn", scaleIn) && scaleIn > 0) - { - Info<< "scale input " << scaleIn << endl; - surf.scalePoints(scaleIn); - } - if (fromCsys) { Info<< "move points from coordinate system: " - << fromCsys->name() << endl; + << fromCsys->name() << nl; tmp tpf = fromCsys->localPosition(surf.points()); surf.movePoints(tpf()); } @@ -251,26 +312,25 @@ int main(int argc, char *argv[]) if (toCsys) { Info<< "move points to coordinate system: " - << toCsys->name() << endl; + << toCsys->name() << nl; tmp tpf = toCsys->globalPosition(surf.points()); surf.movePoints(tpf()); } - scalar scaleOut = 0; - if (args.readIfPresent("scaleOut", scaleOut) && scaleOut > 0) + if (args.readIfPresent("write-scale", scaleFactor) && scaleFactor > 0) { - Info<< "scale output " << scaleOut << endl; - surf.scalePoints(scaleOut); + Info<< "scale output " << scaleFactor << nl; + surf.scalePoints(scaleFactor); } if (args.found("tri")) { - Info<< "triangulate" << endl; + Info<< "triangulate" << nl; surf.triangulate(); } Info<< "writing " << exportName; - surf.write(exportName); + surf.write(exportName, writeFileType); } Info<< "\nEnd\n" << endl; diff --git a/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C b/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C index 53dc1406ee..d1d8728dc6 100644 --- a/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C +++ b/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C @@ -45,11 +45,14 @@ Usage - \par -name \ Specify an alternative surface name when writing. - - \par -scaleIn \ - Specify a scaling factor when reading files. + - \par -write-format \ + Specify output file format - - \par -scaleOut \ - Specify a scaling factor when writing files. + - \par -read-scale \ + Scale factor when reading files. + + - \par -write-scale \ + Scale factor when writing files. - \par -dict \ Specify an alternative dictionary for constant/coordinateSystems. @@ -74,6 +77,18 @@ Note using namespace Foam; +static word getExtension(const fileName& name) +{ + word ext(name.ext()); + if (ext == "gz") + { + ext = name.lessExt().ext(); + } + + return ext; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // int main(int argc, char *argv[]) @@ -100,46 +115,65 @@ int main(int argc, char *argv[]) ); argList::addOption ( - "scaleIn", - "factor", - "Geometry scaling factor on input - default is 1" + "write-format", + "type", + "Output format (default: use file extension)" ); argList::addOption ( - "scaleOut", + "read-scale", "factor", - "Geometry scaling factor on output - default is 1" + "Input geometry scaling factor" ); - argList::addOption("dict", "file", "Use alternative coordinateSystems"); + argList::addOption + ( + "write-scale", + "factor", + "Output geometry scaling factor" + ); + + argList::addOptionCompat("read-scale", {"scaleIn", 1912}); + argList::addOptionCompat("write-scale", {"scaleOut", 1912}); + + argList::addOption("dict", "file", "Alternative coordinateSystems"); argList::addOption ( "from", - "coordinateSystem", - "Specify the source coordinate system, applied after '-scaleIn'", + "system", + "The source coordinate system, applied after '-read-scale'", true // advanced ); argList::addOption ( "to", - "coordinateSystem", - "Specify the target coordinate system, applied before '-scaleOut'", + "system", + "The target coordinate system, applied before '-write-scale'", true // advanced ); argList args(argc, argv); Time runTime(args.rootPath(), args.caseName()); - const fileName exportName = args[1]; - const word importName = args.get("name", "default"); + const fileName exportName(args[1]); + const word importName(args.get("name", "default")); - // check that writing is supported - if (!MeshedSurface::canWriteType(exportName.ext(), true)) + const word writeFileType + ( + args.getOrDefault("write-format", getExtension(exportName)) + ); + + // Check read/write support for formats + if (!meshedSurface::canWriteType(writeFileType, true)) { - return 1; + FatalError + << "Unsupported file format(s)" << nl + << exit(FatalError); } + scalar scaleFactor(0); + // The coordinate transformations (must be cartesian) autoPtr fromCsys; autoPtr toCsys; @@ -162,7 +196,7 @@ int main(int argc, char *argv[]) if (!ioCsys.typeHeaderOk(false)) { - FatalErrorInFunction + FatalError << ioCsys.objectPath() << nl << exit(FatalError); } @@ -176,7 +210,7 @@ int main(int argc, char *argv[]) if (!csPtr) { - FatalErrorInFunction + FatalError << "Cannot find -from " << csName << nl << "available coordinateSystems: " << flatOutput(globalCoords.names()) << nl @@ -193,7 +227,7 @@ int main(int argc, char *argv[]) if (!csPtr) { - FatalErrorInFunction + FatalError << "Cannot find -to " << csName << nl << "available coordinateSystems: " << flatOutput(globalCoords.names()) << nl @@ -206,7 +240,7 @@ int main(int argc, char *argv[]) // Maybe fix this later if (fromCsys && toCsys) { - FatalErrorInFunction + FatalError << "Only allowed '-from' or '-to' option at the moment." << exit(FatalError); } @@ -228,22 +262,21 @@ int main(int argc, char *argv[]) Info<< "read surfMesh:\n " << smesh.objectPath() << endl; - // Simply copy for now, but really should have a separate write method + // Simply copy for now, but really could have a separate write method - MeshedSurface surf(smesh); + meshedSurface surf(smesh); + + if (args.readIfPresent("read-scale", scaleFactor) && scaleFactor > 0) + { + Info<< "scale input " << scaleFactor << nl; + surf.scalePoints(scaleFactor); + } if (args.found("clean")) { surf.cleanup(true); } - scalar scaleIn = 0; - if (args.readIfPresent("scaleIn", scaleIn) && scaleIn > 0) - { - Info<< "scale input " << scaleIn << endl; - surf.scalePoints(scaleIn); - } - if (fromCsys.valid()) { Info<< "move points from coordinate system: " @@ -260,19 +293,17 @@ int main(int argc, char *argv[]) surf.movePoints(tpf()); } - scalar scaleOut = 0; - if (args.readIfPresent("scaleOut", scaleOut) && scaleOut > 0) + if (args.readIfPresent("write-scale", scaleFactor) && scaleFactor > 0) { - Info<< "scale output " << scaleOut << endl; - surf.scalePoints(scaleOut); + Info<< "scale output " << scaleFactor << endl; + surf.scalePoints(scaleFactor); } - surf.writeStats(Info); Info<< endl; - Info<< "writing " << exportName << endl; - surf.write(exportName); + Info<< "writing " << exportName << nl; + surf.write(exportName, writeFileType); Info<< "\nEnd\n" << endl; diff --git a/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C b/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C index 112a42a6b0..339224b75a 100644 --- a/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C +++ b/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C @@ -45,11 +45,14 @@ Usage - \par -name \ Specify an alternative surface name when writing. - - \par -scaleIn \ - Specify a scaling factor when reading files. + - \par -read-format \ + Specify input file format - - \par -scaleOut \ - Specify a scaling factor when writing files. + - \par -read-scale \ + Scale factor when reading files. + + - \par -write-scale \ + Scale factor when writing files. - \par -dict \ Use alternative dictionary for constant/coordinateSystems. @@ -74,6 +77,18 @@ Note using namespace Foam; +static word getExtension(const fileName& name) +{ + word ext(name.ext()); + if (ext == "gz") + { + ext = name.lessExt().ext(); + } + + return ext; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // int main(int argc, char *argv[]) @@ -95,35 +110,44 @@ int main(int argc, char *argv[]) ( "name", "name", - "Specify an alternative surface name when writing - " - "default is 'default'" + "The surface name when writing (default is 'default')" ); argList::addOption ( - "scaleIn", - "factor", - "Geometry scaling factor on input - default is 1" + "read-format", + "type", + "Input format (default: use file extension)" ); argList::addOption ( - "scaleOut", + "read-scale", "factor", - "Geometry scaling factor on output - default is 1" + "Input geometry scaling factor" ); - argList::addOption("dict", "file", "Use alternative coordinateSystems"); + argList::addOption + ( + "write-scale", + "factor", + "Output geometry scaling factor" + ); + + argList::addOptionCompat("read-scale", {"scaleIn", 1912}); + argList::addOptionCompat("write-scale", {"scaleOut", 1912}); + + argList::addOption("dict", "file", "Alternative coordinateSystems"); argList::addOption ( "from", - "coordinateSystem", - "Specify a local coordinate system when reading files.", + "system", + "The source coordinate system, applied after '-read-scale'", true // advanced ); argList::addOption ( "to", - "coordinateSystem", - "Specify a local coordinate system when writing files.", + "system", + "The target coordinate system, applied before '-write-scale'", true // advanced ); @@ -143,16 +167,25 @@ int main(int argc, char *argv[]) } - const fileName importName = args[1]; - const word exportName = args.get("name", "default"); + const fileName importName(args[1]); + const word exportName(args.get("name", "default")); - // check that reading is supported - if (!MeshedSurface::canRead(importName, true)) + const word readFileType + ( + args.getOrDefault("read-format", getExtension(importName)) + ); + + // Check that reading is supported + if (!meshedSurface::canRead(readFileType, true)) { - return 1; + FatalError + << "Unsupported file format(s)" << nl + << exit(FatalError); } + scalar scaleFactor(0); + // The coordinate transformations (must be cartesian) autoPtr fromCsys; autoPtr toCsys; @@ -175,7 +208,7 @@ int main(int argc, char *argv[]) if (!ioCsys.typeHeaderOk(false)) { - FatalErrorInFunction + FatalError << ioCsys.objectPath() << nl << exit(FatalError); } @@ -189,7 +222,7 @@ int main(int argc, char *argv[]) if (!csPtr) { - FatalErrorInFunction + FatalError << "Cannot find -from " << csName << nl << "available coordinateSystems: " << flatOutput(globalCoords.names()) << nl @@ -206,7 +239,7 @@ int main(int argc, char *argv[]) if (!csPtr) { - FatalErrorInFunction + FatalError << "Cannot find -to " << csName << nl << "available coordinateSystems: " << flatOutput(globalCoords.names()) << nl @@ -219,28 +252,26 @@ int main(int argc, char *argv[]) // Maybe fix this later if (fromCsys && toCsys) { - FatalErrorInFunction + FatalError << "Only allowed '-from' or '-to' option at the moment." << exit(FatalError); } } - MeshedSurface surf(importName); + meshedSurface surf(importName, readFileType); + + if (args.readIfPresent("read-scale", scaleFactor) && scaleFactor > 0) + { + Info<< "scale input " << scaleFactor << nl; + surf.scalePoints(scaleFactor); + } if (args.found("clean")) { surf.cleanup(true); } - - scalar scaleIn = 0; - if (args.readIfPresent("scaleIn", scaleIn) && scaleIn > 0) - { - Info<< "scale input " << scaleIn << endl; - surf.scalePoints(scaleIn); - } - if (fromCsys) { Info<< "move points from coordinate system: " @@ -257,11 +288,10 @@ int main(int argc, char *argv[]) surf.movePoints(tpf()); } - scalar scaleOut = 0; - if (args.readIfPresent("scaleOut", scaleOut) && scaleOut > 0) + if (args.readIfPresent("write-scale", scaleFactor) && scaleFactor > 0) { - Info<< "scale output " << scaleOut << endl; - surf.scalePoints(scaleOut); + Info<< "scale output " << scaleFactor << nl; + surf.scalePoints(scaleFactor); } surfMesh smesh diff --git a/applications/utilities/surface/surfaceTransformPoints/surfaceTransformPoints.C b/applications/utilities/surface/surfaceTransformPoints/surfaceTransformPoints.C index f4d0ccc0d8..a3ba6687bc 100644 --- a/applications/utilities/surface/surfaceTransformPoints/surfaceTransformPoints.C +++ b/applications/utilities/surface/surfaceTransformPoints/surfaceTransformPoints.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -62,6 +62,36 @@ Description using namespace Foam; using namespace Foam::coordinateRotations; +static word getExtension(const fileName& name) +{ + word ext(name.ext()); + if (ext == "gz") + { + ext = name.lessExt().ext(); + } + + return ext; +} + + +// Non-short-circuiting check to get all warnings +static bool hasReadWriteTypes(const word& readType, const word& writeType) +{ + volatile bool good = true; + + if (!meshedSurface::canReadType(readType, true)) + { + good = false; + } + + if (!meshedSurface::canWriteType(writeType, true)) + { + good = false; + } + + return good; +} + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -119,6 +149,19 @@ int main(int argc, char *argv[]) "Scale by the specified amount - Eg, for uniform [mm] to [m] scaling " "use either '(0.001 0.001 0.001)' or simply '0.001'" ); + argList::addOption + ( + "read-format", + "type", + "Input format (default: use file extension)" + ); + argList::addOption + ( + "write-format", + "type", + "Output format (default: use file extension)" + ); + argList args(argc, argv); // Verify that an operation has been specified @@ -151,13 +194,34 @@ int main(int argc, char *argv[]) } } - const fileName surfFileName = args[1]; - const fileName outFileName = args[2]; + const fileName importName(args[1]); + const fileName exportName(args[2]); - Info<< "Reading surf from " << surfFileName << " ..." << nl - << "Writing surf to " << outFileName << " ..." << endl; + const word readFileType + ( + args.getOrDefault("read-format", getExtension(importName)) + ); - meshedSurface surf1(surfFileName); + const word writeFileType + ( + args.getOrDefault("write-format", getExtension(exportName)) + ); + + + // Check that reading/writing is supported + if (!hasReadWriteTypes(readFileType, writeFileType)) + { + FatalError + << "Unsupported file format(s)" << nl + << exit(FatalError); + } + + + Info<< "Reading surf from " << importName << " ..." << nl + << "Writing surf to " << exportName << " ..." << endl; + + + meshedSurface surf1(importName, readFileType); pointField points(surf1.points()); @@ -169,7 +233,7 @@ int main(int argc, char *argv[]) points += v; } - vector origin; + vector origin(Zero); const bool useOrigin = args.readIfPresent("origin", origin); if (useOrigin) { @@ -240,7 +304,16 @@ int main(int argc, char *argv[]) { // readListIfPresent handles single or multiple values - if (scaling.size() == 1) + if + ( + scaling.size() == 1 + || + ( + scaling.size() == 3 + && equal(scaling[0], scaling[1]) + && equal(scaling[0], scaling[2]) + ) + ) { Info<< "Scaling points uniformly by " << scaling[0] << nl; points *= scaling[0]; @@ -248,9 +321,9 @@ int main(int argc, char *argv[]) else if (scaling.size() == 3) { Info<< "Scaling points by (" - << scaling[0] << " " - << scaling[1] << " " - << scaling[2] << ")" << nl; + << scaling[0] << ' ' + << scaling[1] << ' ' + << scaling[2] << ')' << nl; points.replace(vector::X, scaling[0]*points.component(vector::X)); points.replace(vector::Y, scaling[1]*points.component(vector::Y)); @@ -272,7 +345,7 @@ int main(int argc, char *argv[]) } surf1.movePoints(points); - surf1.write(outFileName); + surf1.write(exportName, writeFileType); Info<< "End\n" << endl; diff --git a/src/conversion/fire/FIREMeshReader.C b/src/conversion/fire/FIREMeshReader.C index 6fa51242d2..941e1d3e53 100644 --- a/src/conversion/fire/FIREMeshReader.C +++ b/src/conversion/fire/FIREMeshReader.C @@ -378,8 +378,10 @@ bool Foam::fileFormats::FIREMeshReader::readGeometry(const scalar scaleFactor) { IOstream::streamFormat fmt = IOstream::ASCII; - const word ext = geometryFile_.ext(); + const word ext(geometryFile_.ext()); + bool supported = FIRECore::file3dExtensions.found(ext); + if (supported) { FIRECore::fileExt3d fireFileType = FIRECore::file3dExtensions[ext]; diff --git a/src/conversion/fire/FIREMeshWriter.C b/src/conversion/fire/FIREMeshWriter.C index 1c22a88710..b600d13052 100644 --- a/src/conversion/fire/FIREMeshWriter.C +++ b/src/conversion/fire/FIREMeshWriter.C @@ -272,7 +272,7 @@ bool Foam::fileFormats::FIREMeshWriter::write(const fileName& meshName) const } else { - const word ext = baseName.ext(); + const word ext(baseName.ext()); if (FIRECore::file3dExtensions.found(ext)) { diff --git a/src/meshTools/edgeMesh/edgeMesh.C b/src/meshTools/edgeMesh/edgeMesh.C index 6c5cc22874..0736b0ef3a 100644 --- a/src/meshTools/edgeMesh/edgeMesh.C +++ b/src/meshTools/edgeMesh/edgeMesh.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2015-2017 OpenCFD Ltd. + Copyright (C) 2015-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -44,6 +44,8 @@ namespace Foam } +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + Foam::wordHashSet Foam::edgeMesh::readTypes() { return wordHashSet(*fileExtensionConstructorTablePtr_); @@ -56,26 +58,24 @@ Foam::wordHashSet Foam::edgeMesh::writeTypes() } -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -bool Foam::edgeMesh::canReadType(const word& ext, bool verbose) +bool Foam::edgeMesh::canReadType(const word& fileType, bool verbose) { return checkSupport ( readTypes(), - ext, + fileType, verbose, "reading" ); } -bool Foam::edgeMesh::canWriteType(const word& ext, bool verbose) +bool Foam::edgeMesh::canWriteType(const word& fileType, bool verbose) { return checkSupport ( writeTypes(), - ext, + fileType, verbose, "writing" ); @@ -84,7 +84,7 @@ bool Foam::edgeMesh::canWriteType(const word& ext, bool verbose) bool Foam::edgeMesh::canRead(const fileName& name, bool verbose) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { ext = name.lessExt().ext(); @@ -97,14 +97,15 @@ bool Foam::edgeMesh::canRead(const fileName& name, bool verbose) void Foam::edgeMesh::calcPointEdges() const { - if (pointEdgesPtr_.valid()) + if (pointEdgesPtr_) { FatalErrorInFunction - << "pointEdges already calculated." << abort(FatalError); + << "pointEdges already calculated." + << abort(FatalError); } pointEdgesPtr_.reset(new labelListList(points_.size())); - labelListList& pointEdges = pointEdgesPtr_(); + auto& pointEdges = *pointEdgesPtr_; invertManyToMany(pointEdges.size(), edges_, pointEdges); } @@ -116,12 +117,17 @@ void Foam::edgeMesh::clear() { points_.clear(); edges_.clear(); - pointEdgesPtr_.clear(); + pointEdgesPtr_.reset(nullptr); } void Foam::edgeMesh::transfer(edgeMesh& mesh) { + if (&mesh == this) + { + return; // Self-transfer is a no-op + } + points_.transfer(mesh.points_); edges_.transfer(mesh.edges_); pointEdgesPtr_ = std::move(mesh.pointEdgesPtr_); @@ -189,6 +195,7 @@ Foam::label Foam::edgeMesh::regions(labelList& edgeRegion) const currentRegion++; } + return currentRegion; } @@ -220,7 +227,7 @@ void Foam::edgeMesh::mergePoints(const scalar mergeDist) if (hasMerged) { - pointEdgesPtr_.clear(); // connectivity change + pointEdgesPtr_.reset(nullptr); // connectivity change points_.transfer(newPoints); @@ -281,13 +288,13 @@ void Foam::edgeMesh::mergeEdges() if (nUniqEdges < edges_.size()) { - pointEdgesPtr_.clear(); // connectivity change + pointEdgesPtr_.reset(nullptr); // connectivity change edges_.setSize(nUniqEdges); // truncate } if (nUniqPoints < points_.size()) { - pointEdgesPtr_.clear(); // connectivity change + pointEdgesPtr_.reset(nullptr); // connectivity change // build a oldToNew point-map and rewrite the points. // We can do this simultaneously since the point order is unchanged @@ -320,7 +327,6 @@ void Foam::edgeMesh::mergeEdges() e[1] = pointMap[e[1]]; } } - } diff --git a/src/meshTools/edgeMesh/edgeMesh.H b/src/meshTools/edgeMesh/edgeMesh.H index 1fd498c8db..513f233b95 100644 --- a/src/meshTools/edgeMesh/edgeMesh.H +++ b/src/meshTools/edgeMesh/edgeMesh.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2014 OpenFOAM Foundation - Copyright (C) 2017-2018 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -56,16 +56,12 @@ SourceFiles namespace Foam { -// Forward declarations - +// Forward Declarations class edgeMesh; -class Istream; -class Ostream; Istream& operator>>(Istream& is, edgeMesh& em); Ostream& operator<<(Ostream& os, const edgeMesh& em); - /*---------------------------------------------------------------------------*\ Class edgeMesh Declaration \*---------------------------------------------------------------------------*/ @@ -74,7 +70,7 @@ class edgeMesh : public fileFormats::edgeMeshFormatsCore { - // Private data + // Private Data //- Vertices of the edges pointField points_; @@ -83,7 +79,7 @@ class edgeMesh edgeList edges_; //- From point to edges - mutable autoPtr pointEdgesPtr_; + mutable unique_ptr pointEdgesPtr_; // Private Member Functions @@ -105,28 +101,31 @@ protected: public: - //- Runtime type information - TypeName("edgeMesh"); + //- Runtime type information + TypeName("edgeMesh"); - // Static + // Static Member Functions + + //- Summary of supported read file types. + static wordHashSet readTypes(); + + //- Summary of supported write file types. + static wordHashSet writeTypes(); + + //- Can we read this file format? + static bool canReadType(const word& fileType, bool verbose=false); + + //- Can we write this file format type? + static bool canWriteType(const word& fileType, bool verbose=false); //- Can we read this file format? static bool canRead(const fileName& name, bool verbose=false); - //- Can we read this file format? - static bool canReadType(const word& ext, bool verbose=false); - - //- Can we write this file format type? - static bool canWriteType(const word& ext, bool verbose=false); - - static wordHashSet readTypes(); - static wordHashSet writeTypes(); - // Constructors - //- Construct null + //- Default construct inline edgeMesh(); //- Copy construct @@ -144,8 +143,8 @@ public: //- Construct from file name (uses extension to determine type) edgeMesh(const fileName& name); - //- Construct from file name (uses extension to determine type) - edgeMesh(const fileName& name, const word& ext); + //- Construct from file name with specified type + edgeMesh(const fileName& name, const word& fileType); // Declare run-time constructor selection table @@ -164,11 +163,11 @@ public: // Selectors - //- Select constructed from filename (explicit extension) + //- Read construct from filename with given format static autoPtr New ( const fileName& name, - const word& ext + const word& fileType ); //- Select constructed from filename (implicit extension) @@ -194,8 +193,20 @@ public: (name, mesh) ); - //- Write to file - static void write(const fileName& name, const edgeMesh& mesh); + //- Write to file (format implicit in the extension) + static void write + ( + const fileName& name, + const edgeMesh& mesh + ); + + //- Write to file, with given format + static void write + ( + const fileName& name, + const word& fileType, + const edgeMesh& mesh + ); // Member Functions @@ -207,7 +218,7 @@ public: // Read //- Read from file. Chooses reader based on explicit extension - bool read(const fileName& name, const word& ext); + bool read(const fileName& name, const word& fileType); //- Read from file. Chooses reader based on detected extension virtual bool read(const fileName& name); @@ -255,6 +266,12 @@ public: write(name, *this); } + //- Generic write routine. Chooses writer based on extension. + virtual void write(const fileName& name, const word& fileType) const + { + write(name, fileType, *this); + } + // Member Operators @@ -269,7 +286,6 @@ public: friend Ostream& operator<<(Ostream& os, const edgeMesh& em); friend Istream& operator>>(Istream& is, edgeMesh& em); - }; diff --git a/src/meshTools/edgeMesh/edgeMeshFormats/edgeMeshFormatsCore.C b/src/meshTools/edgeMesh/edgeMeshFormats/edgeMeshFormatsCore.C index f6d7e125c3..834430f912 100644 --- a/src/meshTools/edgeMesh/edgeMeshFormats/edgeMeshFormatsCore.C +++ b/src/meshTools/edgeMesh/edgeMeshFormats/edgeMeshFormatsCore.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,9 +27,8 @@ License \*---------------------------------------------------------------------------*/ #include "edgeMeshFormatsCore.H" - #include "Time.H" -#include "Fstream.H" +#include "ListOps.H" #include "edgeMesh.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -44,7 +44,7 @@ Foam::string Foam::fileFormats::edgeMeshFormatsCore::getLineNoComment const char comment ) { - Foam::string line; + string line; do { is.getLine(line); @@ -158,28 +158,27 @@ Foam::fileName Foam::fileFormats::edgeMeshFormatsCore::findMeshFile bool Foam::fileFormats::edgeMeshFormatsCore::checkSupport ( const wordHashSet& available, - const word& ext, + const word& fileType, const bool verbose, - const word& functionName + const char* functionName ) { - if (available.found(ext)) + if (available.found(fileType)) { return true; } else if (verbose) { - wordList known = available.sortedToc(); + Info<< "Unknown file type"; - Info<<"Unknown file extension for " << functionName - << " : " << ext << nl - <<"Valid types: ("; - // compact output: - forAll(known, i) + if (functionName) { - Info<<" " << known[i]; + Info<< " for " << functionName; } - Info<<" )" << endl; + + Info<< " : " << fileType << nl + << "Valid types: " << flatOutput(available.sortedToc()) << nl + << nl; } return false; diff --git a/src/meshTools/edgeMesh/edgeMeshFormats/edgeMeshFormatsCore.H b/src/meshTools/edgeMesh/edgeMeshFormats/edgeMeshFormatsCore.H index 366b5154d0..d0431c7d25 100644 --- a/src/meshTools/edgeMesh/edgeMeshFormats/edgeMeshFormatsCore.H +++ b/src/meshTools/edgeMesh/edgeMeshFormats/edgeMeshFormatsCore.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -46,8 +47,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes - +// Forward Declarations class ISstream; class Time; @@ -75,14 +75,16 @@ public: // Normally "eMesh" (edge-mesh) static word nativeExt; + // Static Member Functions + //- Verbose checking of fileType in the list of available types static bool checkSupport ( const wordHashSet& available, - const word& ext, - const bool verbose, - const word& functionName + const word& fileType, + const bool verbose = false, + const char* functionName = nullptr ); // //- Return the local file name (within time directory) @@ -102,15 +104,13 @@ public: // static fileName findMeshFile(const Time&, const word& edgeName=""); - // Constructors + // Generated Methods - //- Construct null + //- Default construct edgeMeshFormatsCore() = default; - - //- Destructor - virtual ~edgeMeshFormatsCore() = default; - + //- Destructor + virtual ~edgeMeshFormatsCore() = default; }; diff --git a/src/meshTools/edgeMesh/edgeMeshFormats/nastran/NASedgeFormatRunTime.C b/src/meshTools/edgeMesh/edgeMeshFormats/nastran/NASedgeFormatRunTime.C index 0c08072395..f6b24c6e16 100644 --- a/src/meshTools/edgeMesh/edgeMeshFormats/nastran/NASedgeFormatRunTime.C +++ b/src/meshTools/edgeMesh/edgeMeshFormats/nastran/NASedgeFormatRunTime.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,7 +27,6 @@ License \*---------------------------------------------------------------------------*/ #include "NASedgeFormat.H" - #include "addToRunTimeSelectionTable.H" #include "addToMemberFunctionSelectionTable.H" @@ -37,7 +37,16 @@ namespace Foam namespace fileFormats { -// read edgeMesh - .bdf (Bulk Data Format) +// Read +addNamedToRunTimeSelectionTable +( + edgeMesh, + NASedgeFormat, + fileExtension, + nastran +); + +// Read - .bdf (Bulk Data Format) addNamedToRunTimeSelectionTable ( edgeMesh, @@ -46,7 +55,7 @@ addNamedToRunTimeSelectionTable bdf ); -// read edgeMesh - .nas (Nastran Data Format) +// Read .nas (Nastran Data Format) addNamedToRunTimeSelectionTable ( edgeMesh, diff --git a/src/meshTools/edgeMesh/edgeMeshFormats/starcd/STARCDedgeFormat.H b/src/meshTools/edgeMesh/edgeMeshFormats/starcd/STARCDedgeFormat.H index 641f13dbb9..555e2a5980 100644 --- a/src/meshTools/edgeMesh/edgeMeshFormats/starcd/STARCDedgeFormat.H +++ b/src/meshTools/edgeMesh/edgeMeshFormats/starcd/STARCDedgeFormat.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -84,8 +84,8 @@ protected: static void writeCase ( - Ostream&, - const pointField&, + Ostream& os, + const pointField& pointLst, const label nEdges ); @@ -95,7 +95,7 @@ public: // Constructors //- Construct from file name - STARCDedgeFormat(const fileName&); + explicit STARCDedgeFormat(const fileName& filename); // Selectors @@ -114,10 +114,10 @@ public: // Member Functions //- Write edge mesh - static void write(const fileName&, const edgeMesh&); + static void write(const fileName& name, const edgeMesh& mesh); //- Read from file - virtual bool read(const fileName&); + virtual bool read(const fileName& name); //- Write object virtual void write(const fileName& name) const diff --git a/src/meshTools/edgeMesh/edgeMeshFormats/starcd/STARCDedgeFormatRunTime.C b/src/meshTools/edgeMesh/edgeMeshFormats/starcd/STARCDedgeFormatRunTime.C index 941a215c8e..e9645fc7f2 100644 --- a/src/meshTools/edgeMesh/edgeMeshFormats/starcd/STARCDedgeFormatRunTime.C +++ b/src/meshTools/edgeMesh/edgeMeshFormats/starcd/STARCDedgeFormatRunTime.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,7 +27,6 @@ License \*---------------------------------------------------------------------------*/ #include "STARCDedgeFormat.H" - #include "addToRunTimeSelectionTable.H" #include "addToMemberFunctionSelectionTable.H" @@ -37,23 +37,23 @@ namespace Foam namespace fileFormats { -// read edgeMesh +// Read addNamedToRunTimeSelectionTable ( edgeMesh, STARCDedgeFormat, fileExtension, - inp + starcd ); -// write edgeMesh +// Write addNamedToMemberFunctionSelectionTable ( edgeMesh, STARCDedgeFormat, write, fileExtension, - inp + starcd ); } diff --git a/src/meshTools/edgeMesh/edgeMeshI.H b/src/meshTools/edgeMesh/edgeMeshI.H index 4fcba57e55..260ee01cbe 100644 --- a/src/meshTools/edgeMesh/edgeMeshI.H +++ b/src/meshTools/edgeMesh/edgeMeshI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -110,7 +110,7 @@ inline const Foam::edgeList& Foam::edgeMesh::edges() const inline const Foam::labelListList& Foam::edgeMesh::pointEdges() const { - if (pointEdgesPtr_.empty()) + if (!pointEdgesPtr_) { calcPointEdges(); } @@ -124,7 +124,7 @@ inline void Foam::edgeMesh::operator=(const edgeMesh& rhs) { points_ = rhs.points_; edges_ = rhs.edges_; - pointEdgesPtr_.clear(); + pointEdgesPtr_.reset(nullptr); } diff --git a/src/meshTools/edgeMesh/edgeMeshIO.C b/src/meshTools/edgeMesh/edgeMeshIO.C index 1f946beec9..0fae87848b 100644 --- a/src/meshTools/edgeMesh/edgeMeshIO.C +++ b/src/meshTools/edgeMesh/edgeMeshIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -35,21 +35,21 @@ License Foam::edgeMesh::edgeMesh ( const fileName& name, - const word& ext + const word& fileType ) : - points_(0), - edges_(0), + points_(), + edges_(), pointEdgesPtr_(nullptr) { - read(name, ext); + read(name, fileType); } Foam::edgeMesh::edgeMesh(const fileName& name) : - points_(0), - edges_(0), + points_(), + edges_(), pointEdgesPtr_(nullptr) { read(name); @@ -60,7 +60,7 @@ Foam::edgeMesh::edgeMesh(const fileName& name) bool Foam::edgeMesh::read(const fileName& name) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { fileName unzipName = name.lessExt(); @@ -71,42 +71,50 @@ bool Foam::edgeMesh::read(const fileName& name) } -// Read from file in given format bool Foam::edgeMesh::read ( const fileName& name, - const word& ext + const word& fileType ) { - // read via selector mechanism - transfer(New(name, ext)()); + // Read via selector mechanism + transfer(*New(name, fileType)); return true; } +void Foam::edgeMesh::write +( + const fileName& name, + const word& fileType, + const edgeMesh& mesh +) +{ + DebugInFunction << "Writing to " << name << endl; + + auto mfIter = writefileExtensionMemberFunctionTablePtr_->cfind(fileType); + + if (!mfIter.found()) + { + FatalErrorInLookup + ( + "extension", + fileType, + *writefileExtensionMemberFunctionTablePtr_ + ) << exit(FatalError); + } + + mfIter()(name, mesh); +} + + void Foam::edgeMesh::write ( const fileName& name, const edgeMesh& mesh ) { - DebugInFunction << "Writing to " << name << endl; - - const word ext = name.ext(); - - auto mfIter = writefileExtensionMemberFunctionTablePtr_->cfind(ext); - - if (!mfIter.found()) - { - FatalErrorInLookup - ( - "extension", - ext, - *writefileExtensionMemberFunctionTablePtr_ - ) << exit(FatalError); - } - - mfIter()(name, mesh); + write(name, name.ext(), mesh); } @@ -133,7 +141,7 @@ Foam::Istream& Foam::operator>>(Istream& is, edgeMesh& em) { fileFormats::edgeMeshFormat::read(is, em.points_, em.edges_); - em.pointEdgesPtr_.clear(); + em.pointEdgesPtr_.reset(nullptr); is.check(FUNCTION_NAME); return is; diff --git a/src/meshTools/edgeMesh/edgeMeshNew.C b/src/meshTools/edgeMesh/edgeMeshNew.C index 7edfe34838..bd791e52e3 100644 --- a/src/meshTools/edgeMesh/edgeMeshNew.C +++ b/src/meshTools/edgeMesh/edgeMeshNew.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,18 +33,18 @@ License Foam::autoPtr Foam::edgeMesh::New ( const fileName& name, - const word& ext + const word& fileType ) { - auto cstrIter = fileExtensionConstructorTablePtr_->cfind(ext); + auto cstrIter = fileExtensionConstructorTablePtr_->cfind(fileType); if (!cstrIter.found()) { FatalErrorInFunction - << "Unknown file extension " << ext + << "Unknown edge format " << fileType << " for file " << name << nl << nl - << "Valid extensions :" << nl - << fileExtensionConstructorTablePtr_->sortedToc() + << "Valid types:" << nl + << flatOutput(fileExtensionConstructorTablePtr_->sortedToc()) << exit(FatalError); } @@ -53,7 +54,7 @@ Foam::autoPtr Foam::edgeMesh::New Foam::autoPtr Foam::edgeMesh::New(const fileName& name) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { ext = name.lessExt().ext(); diff --git a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMesh.C b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMesh.C index de7a4a8f24..6cbf278807 100644 --- a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMesh.C +++ b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMesh.C @@ -95,10 +95,8 @@ Foam::label Foam::extendedEdgeMesh::convexStart_ = 0; Foam::label Foam::extendedEdgeMesh::externalStart_ = 0; -Foam::label Foam::extendedEdgeMesh::nPointTypes = 4; - -Foam::label Foam::extendedEdgeMesh::nEdgeTypes = 5; +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // Foam::wordHashSet Foam::extendedEdgeMesh::readTypes() { @@ -112,26 +110,24 @@ Foam::wordHashSet Foam::extendedEdgeMesh::writeTypes() } -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -bool Foam::extendedEdgeMesh::canReadType(const word& ext, bool verbose) +bool Foam::extendedEdgeMesh::canReadType(const word& fileType, bool verbose) { return edgeMeshFormatsCore::checkSupport ( readTypes(), - ext, + fileType, verbose, "reading" ); } -bool Foam::extendedEdgeMesh::canWriteType(const word& ext, bool verbose) +bool Foam::extendedEdgeMesh::canWriteType(const word& fileType, bool verbose) { return edgeMeshFormatsCore::checkSupport ( writeTypes(), - ext, + fileType, verbose, "writing" ); @@ -140,7 +136,7 @@ bool Foam::extendedEdgeMesh::canWriteType(const word& ext, bool verbose) bool Foam::extendedEdgeMesh::canRead(const fileName& name, bool verbose) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { ext = name.lessExt().ext(); @@ -380,7 +376,7 @@ void Foam::extendedEdgeMesh::select Foam::extendedEdgeMesh::extendedEdgeMesh() : - edgeMesh(pointField(), edgeList()), + edgeMesh(), concaveStart_(0), mixedStart_(0), nonFeatureStart_(0), @@ -396,15 +392,15 @@ Foam::extendedEdgeMesh::extendedEdgeMesh() featurePointNormals_(0), featurePointEdges_(0), regionEdges_(0), - pointTree_(), - edgeTree_(), + pointTree_(nullptr), + edgeTree_(nullptr), edgeTreesByType_() {} Foam::extendedEdgeMesh::extendedEdgeMesh(class one::minus) : - edgeMesh(pointField(), edgeList()), + edgeMesh(), concaveStart_(-1), mixedStart_(-1), nonFeatureStart_(-1), @@ -420,8 +416,8 @@ Foam::extendedEdgeMesh::extendedEdgeMesh(class one::minus) featurePointNormals_(0), featurePointEdges_(0), regionEdges_(0), - pointTree_(), - edgeTree_(), + pointTree_(nullptr), + edgeTree_(nullptr), edgeTreesByType_() {} @@ -444,8 +440,8 @@ Foam::extendedEdgeMesh::extendedEdgeMesh(const extendedEdgeMesh& fem) featurePointNormals_(fem.featurePointNormals()), featurePointEdges_(fem.featurePointEdges()), regionEdges_(fem.regionEdges()), - pointTree_(), - edgeTree_(), + pointTree_(nullptr), + edgeTree_(nullptr), edgeTreesByType_() {} @@ -600,8 +596,8 @@ Foam::extendedEdgeMesh::extendedEdgeMesh featurePointNormals_(featurePointNormals), featurePointEdges_(featurePointEdges), regionEdges_(regionEdges), - pointTree_(), - edgeTree_(), + pointTree_(nullptr), + edgeTree_(nullptr), edgeTreesByType_() {} @@ -609,12 +605,12 @@ Foam::extendedEdgeMesh::extendedEdgeMesh Foam::extendedEdgeMesh::extendedEdgeMesh ( const fileName& name, - const word& ext + const word& fileType ) : extendedEdgeMesh() { - read(name, ext); + read(name, fileType); } @@ -626,17 +622,11 @@ Foam::extendedEdgeMesh::extendedEdgeMesh(const fileName& name) } -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -Foam::extendedEdgeMesh::~extendedEdgeMesh() -{} - - // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // bool Foam::extendedEdgeMesh::read(const fileName& name) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { fileName unzipName = name.lessExt(); @@ -647,15 +637,14 @@ bool Foam::extendedEdgeMesh::read(const fileName& name) } -// Read from file in given format bool Foam::extendedEdgeMesh::read ( const fileName& name, - const word& ext + const word& fileType ) { - // read via selector mechanism - transfer(New(name, ext)()); + // Read via selector mechanism + transfer(*New(name, fileType)); return true; } @@ -837,7 +826,7 @@ void Foam::extendedEdgeMesh::allNearestFeatureEdges const Foam::indexedOctree& Foam::extendedEdgeMesh::pointTree() const { - if (pointTree_.empty()) + if (!pointTree_) { Random rndGen(17301893); @@ -877,7 +866,7 @@ Foam::extendedEdgeMesh::pointTree() const const Foam::indexedOctree& Foam::extendedEdgeMesh::edgeTree() const { - if (edgeTree_.empty()) + if (!edgeTree_) { Random rndGen(17301893); @@ -919,10 +908,8 @@ Foam::extendedEdgeMesh::edgeTree() const const Foam::PtrList>& Foam::extendedEdgeMesh::edgeTreesByType() const { - if (edgeTreesByType_.size() == 0) + if (edgeTreesByType_.empty()) { - edgeTreesByType_.setSize(nEdgeTypes); - Random rndGen(872141); // Slightly extended bb. Slightly off-centred just so on symmetric @@ -954,6 +941,9 @@ Foam::extendedEdgeMesh::edgeTreesByType() const sliceEdges[4] = identity((edges().size() - multipleStart_), multipleStart_); + + edgeTreesByType_.resize(nEdgeTypes); + forAll(edgeTreesByType_, i) { edgeTreesByType_.set @@ -983,6 +973,11 @@ Foam::extendedEdgeMesh::edgeTreesByType() const void Foam::extendedEdgeMesh::transfer(extendedEdgeMesh& mesh) { + if (&mesh == this) + { + return; // Self-transfer is a no-op + } + edgeMesh::transfer(mesh); concaveStart_ = mesh.concaveStart_; @@ -1026,8 +1021,8 @@ void Foam::extendedEdgeMesh::clear() featurePointNormals_.clear(); featurePointEdges_.clear(); regionEdges_.clear(); - pointTree_.clear(); - edgeTree_.clear(); + pointTree_.reset(nullptr); + edgeTree_.reset(nullptr); edgeTreesByType_.clear(); } @@ -1296,8 +1291,8 @@ void Foam::extendedEdgeMesh::add(const extendedEdgeMesh& fem) regionEdges_.transfer(newRegionEdges); - pointTree_.clear(); - edgeTree_.clear(); + pointTree_.reset(nullptr); + edgeTree_.reset(nullptr); edgeTreesByType_.clear(); } @@ -1407,8 +1402,8 @@ void Foam::extendedEdgeMesh::flipNormals() featurePointNormals_.transfer(newFeaturePointNormals); regionEdges_.transfer(newRegionEdges); - pointTree_.clear(); - edgeTree_.clear(); + pointTree_.reset(nullptr); + edgeTree_.reset(nullptr); edgeTreesByType_.clear(); } diff --git a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMesh.H b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMesh.H index ddae82e4f8..3ae1747a3b 100644 --- a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMesh.H +++ b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMesh.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2015-2018 OpenCFD Ltd. + Copyright (C) 2015-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -70,8 +70,7 @@ SourceFiles namespace Foam { -// Forward declarations - +// Forward Declarations class surfaceFeatures; class searchableSurface; class extendedEdgeMesh; @@ -132,7 +131,7 @@ public: protected: - // Static data + // Static Data //- Index of the start of the convex feature points - static as 0 static label convexStart_; @@ -141,7 +140,7 @@ protected: static label externalStart_; - // Protected data + // Protected Data //- Index of the start of the concave feature points label concaveStart_; @@ -194,10 +193,10 @@ protected: labelList regionEdges_; //- Search tree for all feature points - mutable autoPtr> pointTree_; + mutable unique_ptr> pointTree_; //- Search tree for all edges - mutable autoPtr> edgeTree_; + mutable unique_ptr> edgeTree_; //- Individual search trees for each type of edge mutable PtrList> edgeTreesByType_; @@ -247,43 +246,49 @@ protected: public: - // Static data + // Static Data //- Number of possible point types (i.e. number of slices) - static label nPointTypes; + static constexpr label nPointTypes = 4; //- Number of possible feature edge types (i.e. number of slices) - static label nEdgeTypes; + static constexpr label nEdgeTypes = 5; + + + // Static Member Functions + + //- Summary of supported read file types. + static wordHashSet readTypes(); + + //- Summary of supported write file types. + static wordHashSet writeTypes(); + + //- Can we read this file format? + static bool canReadType(const word& fileType, bool verbose=false); + + //- Can we write this file format type? + static bool canWriteType(const word& fileType, bool verbose=false); //- Can we read this file format? static bool canRead(const fileName& name, bool verbose=false); - //- Can we read this file format? - static bool canReadType(const word& ext, bool verbose=false); - - //- Can we write this file format type? - static bool canWriteType(const word& ext, bool verbose=false); - - static wordHashSet readTypes(); - static wordHashSet writeTypes(); - // Constructors - //- Construct null + //- Default construct extendedEdgeMesh(); - //- Construct as copy + //- Copy construct explicit extendedEdgeMesh(const extendedEdgeMesh& fem); //- Construct from file name (uses extension to determine type) - extendedEdgeMesh(const fileName&); + explicit extendedEdgeMesh(const fileName& name); - //- Construct from file name (uses extension to determine type) - extendedEdgeMesh(const fileName&, const word& ext); + //- Construct from file name with given format type + extendedEdgeMesh(const fileName& name, const word& fileType); //- Construct from Istream - extendedEdgeMesh(Istream& is); + explicit extendedEdgeMesh(Istream& is); //- Copy construct from components extendedEdgeMesh(const pointField& points, const edgeList& edges); @@ -348,11 +353,11 @@ public: // Selectors - //- Select constructed from filename (explicit extension) + //- Select constructed from filename with given file format static autoPtr New ( const fileName& name, - const word& ext + const word& fileType ); //- Select constructed from filename (implicit extension) @@ -360,7 +365,7 @@ public: //- Destructor - ~extendedEdgeMesh(); + ~extendedEdgeMesh() = default; // Member Functions diff --git a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMeshNew.C b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMeshNew.C index dcceaabd56..6888b0dff0 100644 --- a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMeshNew.C +++ b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedEdgeMeshNew.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2013-2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,7 +28,6 @@ License #include "extendedEdgeMesh.H" - // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam @@ -41,18 +41,18 @@ namespace Foam Foam::autoPtr Foam::extendedEdgeMesh::New ( const fileName& name, - const word& ext + const word& fileType ) { - auto cstrIter = fileExtensionConstructorTablePtr_->cfind(ext); + auto cstrIter = fileExtensionConstructorTablePtr_->cfind(fileType); if (!cstrIter.found()) { FatalErrorInFunction - << "Unknown file extension " << ext - << " for file " << name << nl << nl - << "Valid extensions :" << nl - << fileExtensionConstructorTablePtr_->sortedToc() + << "Unknown edge format " << fileType + << " for file " << name << nl + << "Valid types:" << nl + << flatOutput(fileExtensionConstructorTablePtr_->sortedToc()) << exit(FatalError); } @@ -65,7 +65,7 @@ Foam::autoPtr Foam::extendedEdgeMesh::New const fileName& name ) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { ext = name.lessExt().ext(); diff --git a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedFeatureEdgeMesh/extendedFeatureEdgeMesh.C b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedFeatureEdgeMesh/extendedFeatureEdgeMesh.C index 9bb8ae836e..6e8a4a069d 100644 --- a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedFeatureEdgeMesh/extendedFeatureEdgeMesh.C +++ b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedFeatureEdgeMesh/extendedFeatureEdgeMesh.C @@ -179,12 +179,6 @@ Foam::extendedFeatureEdgeMesh::extendedFeatureEdgeMesh {} -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -Foam::extendedFeatureEdgeMesh::~extendedFeatureEdgeMesh() -{} - - // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // bool Foam::extendedFeatureEdgeMesh::readData(Istream& is) diff --git a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedFeatureEdgeMesh/extendedFeatureEdgeMesh.H b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedFeatureEdgeMesh/extendedFeatureEdgeMesh.H index 9b86856ffa..940dd2e87c 100644 --- a/src/meshTools/edgeMesh/extendedEdgeMesh/extendedFeatureEdgeMesh/extendedFeatureEdgeMesh.H +++ b/src/meshTools/edgeMesh/extendedEdgeMesh/extendedFeatureEdgeMesh/extendedFeatureEdgeMesh.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2014 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -46,6 +47,7 @@ SourceFiles namespace Foam { +// Forward Declarations class objectRegistry; /*---------------------------------------------------------------------------*\ @@ -67,12 +69,12 @@ public: // Constructors //- Construct (read) given an IOobject - extendedFeatureEdgeMesh(const IOobject&); + explicit extendedFeatureEdgeMesh(const IOobject& io); - //- Construct as copy + //- Copy construct with IOobject extendedFeatureEdgeMesh ( - const IOobject&, + const IOobject& io, const extendedEdgeMesh& ); @@ -91,7 +93,7 @@ public: //- Construct from PrimitivePatch extendedFeatureEdgeMesh ( - const IOobject&, + const IOobject& io, const PrimitivePatch& surf, const labelUList& featureEdges, const labelUList& regionFeatureEdges, @@ -123,7 +125,7 @@ public: //- Destructor - virtual ~extendedFeatureEdgeMesh(); + virtual ~extendedFeatureEdgeMesh() = default; // IO @@ -144,7 +146,7 @@ public: } //- Return complete path + object name if the file exists - // either in the case/processor or case otherwise null + //- either in the case/processor or case otherwise null virtual fileName filePath() const { return globalFilePath(type()); diff --git a/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.C b/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.C index ba6ffcaf2f..57417a45e1 100644 --- a/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.C +++ b/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.C @@ -256,7 +256,7 @@ Foam::triSurfaceMesh::triSurfaceMesh surfaceClosed_(-1), outsideVolType_(volumeType::UNKNOWN) { - // Adjust to use supplied file name instead of objectPath/filePath + // Read with supplied file name instead of objectPath/filePath if (dict.readIfPresent("file", fName_, keyType::LITERAL)) { fName_ = triSurface::relativeFilePath @@ -414,6 +414,9 @@ Foam::triSurfaceMesh::triSurfaceMesh { if (io.readOpt() != IOobject::NO_READ) { + // Surface type (optional) + const word surfType(dict.getOrDefault("fileType", word::null)); + // Scale factor (optional) const scalar scaleFactor(dict.getOrDefault("scale", 0)); @@ -446,6 +449,7 @@ Foam::triSurfaceMesh::triSurfaceMesh << " from:" << actualFile << endl; } + if (searchGlobal && Pstream::parRun()) { // Check where surface was found @@ -457,7 +461,7 @@ Foam::triSurfaceMesh::triSurfaceMesh // surface. Load on master only if (Pstream::master()) { - triSurface s2(actualFile, scaleFactor); + triSurface s2(actualFile, surfType, scaleFactor); triSurface::transfer(s2); } Pstream::scatter(triSurface::patches()); @@ -470,7 +474,7 @@ Foam::triSurfaceMesh::triSurfaceMesh else { // Read on all processors - triSurface s2(actualFile, scaleFactor); + triSurface s2(actualFile, surfType, scaleFactor); triSurface::transfer(s2); if (debug) { @@ -482,7 +486,7 @@ Foam::triSurfaceMesh::triSurfaceMesh else { // Read on all processors - triSurface s2(actualFile, scaleFactor); + triSurface s2(actualFile, surfType, scaleFactor); triSurface::transfer(s2); if (debug) { diff --git a/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.H b/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.H index c3e62014d1..e5fe8825ad 100644 --- a/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.H +++ b/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.H @@ -41,6 +41,7 @@ Description Property | Description | Required | Default type | triSurfaceMesh | selector | file | File name to locate the surface | no | + fileType | The surface format (Eg, nastran) | no | scale | Scaling factor | no | 0 minQuality | Quality criterion | no | -1 \endtable diff --git a/src/meshTools/sets/cellSources/surfaceToCell/surfaceToCell.C b/src/meshTools/sets/cellSources/surfaceToCell/surfaceToCell.C index b2a79c265a..5f5ac8939e 100644 --- a/src/meshTools/sets/cellSources/surfaceToCell/surfaceToCell.C +++ b/src/meshTools/sets/cellSources/surfaceToCell/surfaceToCell.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -450,6 +450,7 @@ Foam::surfaceToCell::surfaceToCell new triSurface ( surfName_, + dict.getOrDefault("fileType", word::null), dict.lookupOrDefault("scale", -1) ) ), diff --git a/src/meshTools/sets/cellSources/surfaceToCell/surfaceToCell.H b/src/meshTools/sets/cellSources/surfaceToCell/surfaceToCell.H index 49dcd411f4..460cd74c1f 100644 --- a/src/meshTools/sets/cellSources/surfaceToCell/surfaceToCell.H +++ b/src/meshTools/sets/cellSources/surfaceToCell/surfaceToCell.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -43,7 +43,8 @@ Description \table Property | Description | Required | Default file | The surface "filename" | yes | - scale | surface scaling factor | no | -1 + fileType | The surface format | no | + scale | Surface scaling factor | no | -1 nearDistance | Near distance to the surface | yes | curvature | surface curvature selection | yes | outsidePoints| Points outside of surface | yes | @@ -68,8 +69,10 @@ SourceFiles namespace Foam { -class triSurfaceSearch; + +// Forward Declarations class triSurface; +class triSurfaceSearch; /*---------------------------------------------------------------------------*\ Class surfaceToCell Declaration @@ -79,8 +82,7 @@ class surfaceToCell : public topoSetCellSource { - - // Private data + // Private Data //- Add usage string static addToUsageTable usage_; @@ -217,7 +219,6 @@ public: const topoSetSource::setAction action, topoSet& ) const; - }; diff --git a/src/meshTools/sets/pointSources/surfaceToPoint/surfaceToPoint.C b/src/meshTools/sets/pointSources/surfaceToPoint/surfaceToPoint.C index 94095c57be..418d2ea96d 100644 --- a/src/meshTools/sets/pointSources/surfaceToPoint/surfaceToPoint.C +++ b/src/meshTools/sets/pointSources/surfaceToPoint/surfaceToPoint.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -64,7 +64,7 @@ void Foam::surfaceToPoint::combine(topoSet& set, const bool add) const { cpuTime timer; - triSurface surf(surfName_, scale_); + triSurface surf(surfName_, surfType_, scale_); if (verbose_) { @@ -137,7 +137,8 @@ Foam::surfaceToPoint::surfaceToPoint : topoSetPointSource(mesh), surfName_(surfName), - scale_(1.0), + surfType_(), + scale_(-1), nearDist_(nearDist), includeInside_(includeInside), includeOutside_(includeOutside) @@ -154,6 +155,7 @@ Foam::surfaceToPoint::surfaceToPoint : topoSetPointSource(mesh), surfName_(dict.get("file").expand()), + surfType_(dict.getOrDefault("fileType", word::null)), scale_(dict.lookupOrDefault("scale", -1)), nearDist_(dict.get("nearDistance")), includeInside_(dict.get("includeInside")), @@ -171,7 +173,8 @@ Foam::surfaceToPoint::surfaceToPoint : topoSetPointSource(mesh), surfName_(checkIs(is)), - scale_(1.0), + surfType_(), + scale_(-1), nearDist_(readScalar(checkIs(is))), includeInside_(readBool(checkIs(is))), includeOutside_(readBool(checkIs(is))) diff --git a/src/meshTools/sets/pointSources/surfaceToPoint/surfaceToPoint.H b/src/meshTools/sets/pointSources/surfaceToPoint/surfaceToPoint.H index cf0ac2afbf..4e7c034232 100644 --- a/src/meshTools/sets/pointSources/surfaceToPoint/surfaceToPoint.H +++ b/src/meshTools/sets/pointSources/surfaceToPoint/surfaceToPoint.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -40,7 +40,8 @@ Description \table Property | Description | Required | Default file | The surface "filename" | yes | - scale | surface scaling factor | no | -1 + fileType | The surface format | no | + scale | Surface scaling factor | no | -1 nearDistance | Near distance to the surface | yes | includeInside | Include inside cells (bool) | yes | includeOutside | Include outside cells (bool) | yes | @@ -61,9 +62,6 @@ SourceFiles namespace Foam { -// Forward declarations -class triSurfaceSearch; - /*---------------------------------------------------------------------------*\ Class surfaceToPoint Declaration \*---------------------------------------------------------------------------*/ @@ -72,8 +70,7 @@ class surfaceToPoint : public topoSetPointSource { - - // Private data + // Private Data //- Add usage string static addToUsageTable usage_; @@ -81,7 +78,10 @@ class surfaceToPoint //- Name of surface file const fileName surfName_; - //- Optional scaling for surface + //- Surface file type (optional) + const word surfType_; + + //- Surface scaling factor (optional) const scalar scale_; //- If > 0 : include points with distance to surface less than nearDist. diff --git a/src/sampling/sampledSurface/sampledMeshedSurface/sampledMeshedSurface.H b/src/sampling/sampledSurface/sampledMeshedSurface/sampledMeshedSurface.H index 05da864184..db0c18b316 100644 --- a/src/sampling/sampledSurface/sampledMeshedSurface/sampledMeshedSurface.H +++ b/src/sampling/sampledSurface/sampledMeshedSurface/sampledMeshedSurface.H @@ -81,12 +81,15 @@ Usage Where the sub-entries comprise: \table - Property | Description | Required | Default - type | meshedSurface | yes | - surface | surface name in triSurface/ | yes | + Property | Description | Required | Default + type | meshedSurface | yes | + surface | surface name in triSurface/ | yes | patches | Limit to named surface regions (wordRes) | no | - source | cells/insideCells/boundaryFaces | yes | - keepIds | pass through id numbering | no | false + source | cells/insideCells/boundaryFaces | yes | + keepIds | pass through id numbering | no | false + file | Alternative file name | no | + fileType | The surface format | no | (extension) + scale | Surface scaling factor | no | 0 \endtable SourceFiles diff --git a/src/surfMesh/MeshedSurface/MeshedSurface.C b/src/surfMesh/MeshedSurface/MeshedSurface.C index 14bfd978a8..11194b8144 100644 --- a/src/surfMesh/MeshedSurface/MeshedSurface.C +++ b/src/surfMesh/MeshedSurface/MeshedSurface.C @@ -39,7 +39,7 @@ License #include "faceTraits.H" #include "addToRunTimeSelectionTable.H" -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // template Foam::wordHashSet Foam::MeshedSurface::readTypes() @@ -47,6 +47,7 @@ Foam::wordHashSet Foam::MeshedSurface::readTypes() return wordHashSet(*fileExtensionConstructorTablePtr_); } + template Foam::wordHashSet Foam::MeshedSurface::writeTypes() { @@ -54,36 +55,34 @@ Foam::wordHashSet Foam::MeshedSurface::writeTypes() } -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - template bool Foam::MeshedSurface::canReadType ( - const word& ext, + const word& fileType, bool verbose ) { return fileFormats::surfaceFormatsCore::checkSupport ( readTypes() | FriendType::readTypes(), - ext, + fileType, verbose, "reading" - ); + ); } template bool Foam::MeshedSurface::canWriteType ( - const word& ext, + const word& fileType, bool verbose ) { return fileFormats::surfaceFormatsCore::checkSupport ( writeTypes() | ProxyType::writeTypes(), - ext, + fileType, verbose, "writing" ); @@ -97,7 +96,7 @@ bool Foam::MeshedSurface::canRead bool verbose ) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { ext = name.lessExt().ext(); @@ -123,36 +122,53 @@ template void Foam::MeshedSurface::write ( const fileName& name, - const word& ext, + const word& fileType, const MeshedSurface& surf, IOstreamOption streamOpt, const dictionary& options ) { - if (debug) + if (fileType.empty()) { - InfoInFunction << "Writing to " << name << endl; + // Handle empty/missing type + + const word ext(name.ext()); + + if (ext.empty()) + { + FatalErrorInFunction + << "Cannot determine format from filename" << nl + << " " << name << nl + << exit(FatalError); + } + + write(name, ext, surf, streamOpt, options); + return; } - auto mfIter = writefileExtensionMemberFunctionTablePtr_->cfind(ext); + + DebugInFunction << "Writing to " << name << nl; + + auto mfIter = writefileExtensionMemberFunctionTablePtr_->cfind(fileType); if (!mfIter.found()) { - // No direct writer, delegate to proxy if possible - const wordHashSet& delegate = ProxyType::writeTypes(); + // Delegate to proxy if possible + const wordHashSet delegate(ProxyType::writeTypes()); - if (delegate.found(ext)) - { - MeshedSurfaceProxy(surf).write(name, ext, streamOpt, options); - } - else + if (!delegate.found(fileType)) { FatalErrorInFunction - << "Unknown file extension " << ext << nl << nl + << "Unknown write format " << fileType << nl << nl << "Valid types:" << nl << flatOutput((delegate | writeTypes()).sortedToc()) << nl << exit(FatalError); } + + MeshedSurfaceProxy(surf).write + ( + name, fileType, streamOpt, options + ); } else { @@ -396,12 +412,12 @@ template Foam::MeshedSurface::MeshedSurface ( const fileName& name, - const word& ext + const word& fileType ) : MeshedSurface() { - read(name, ext); + read(name, fileType); } @@ -481,11 +497,7 @@ Foam::MeshedSurface::MeshedSurface fileFormats::surfaceFormatsCore::checkFile(io, dict, isGlobal) ); - // TBD: - // word fExt(dict.getOrDefault("surfaceType", fName.ext())); - // read(fName, fExt); - - this->read(fName, fName.ext()); + this->read(fName, dict.getOrDefault("fileType", word::null)); this->scalePoints(dict.getOrDefault("scale", 0)); } @@ -1331,18 +1343,34 @@ bool Foam::MeshedSurface::read(const fileName& name) } -// Read from file in given format template bool Foam::MeshedSurface::read ( const fileName& name, - const word& ext + const word& fileType ) { + if (fileType.empty()) + { + // Handle empty/missing type + + const word ext(name.ext()); + + if (ext.empty()) + { + FatalErrorInFunction + << "Cannot determine format from filename" << nl + << " " << name << nl + << exit(FatalError); + } + + return read(name, ext); + } + clear(); - // read via selector mechanism - transfer(New(name, ext)()); + // Read via selector mechanism + transfer(*(New(name, fileType))); return true; } diff --git a/src/surfMesh/MeshedSurface/MeshedSurface.H b/src/surfMesh/MeshedSurface/MeshedSurface.H index e89015b56d..68e2d0a9c9 100644 --- a/src/surfMesh/MeshedSurface/MeshedSurface.H +++ b/src/surfMesh/MeshedSurface/MeshedSurface.H @@ -199,21 +199,21 @@ public: // Static Functions - //- Can we read this file format? - static bool canRead(const fileName& name, bool verbose=false); - - //- Can we read this file format? - static bool canReadType(const word& ext, bool verbose=false); - - //- Can we write this file format? - static bool canWriteType(const word& ext, bool verbose=false); - - //- Known readable file-types + //- Known readable file-types, without friends or proxies static wordHashSet readTypes(); - //- Known writable file-types + //- Known writable file-types, without friends or proxies static wordHashSet writeTypes(); + //- Can we read this file format? Also checks friend types. + static bool canReadType(const word& fileType, bool verbose=false); + + //- Can we write this file format? Also checks proxy types. + static bool canWriteType(const word& fileType, bool verbose=false); + + //- Can we read this file format? + static bool canRead(const fileName& name, bool verbose=false); + // Constructors @@ -282,8 +282,9 @@ public: //- Construct from file name (uses extension to determine type) explicit MeshedSurface(const fileName& name); - //- Construct from file name (uses extension to determine type) - explicit MeshedSurface(const fileName& name, const word& ext); + //- Construct from file name and given file type + // If the format type is "", uses the file extension. + explicit MeshedSurface(const fileName& name, const word& fileType); //- Construct from Istream explicit MeshedSurface(Istream& is); @@ -295,8 +296,11 @@ public: MeshedSurface(const Time& runTime, const word& surfName); //- Read construct using IO to find the file location. - // Dictionary may contain optional 'file' entry, and an - // optional 'scale' entry (eg, 0.001: mm -> m) + // Dictionary may contain the following entries: + // - \c file = alternative file name (default is dictionary name) + // - \c fileType = file format (default is from file extension) + // - \c scale (eg, 0.001: mm to m) + // . MeshedSurface ( const IOobject& io, @@ -321,14 +325,15 @@ public: // Selectors - //- Select constructed from filename (explicit extension) + //- Read construct from filename with given file type static autoPtr New ( const fileName& name, - const word& ext + const word& fileType, + bool mandatory = true ); - //- Select constructed from filename (implicit extension) + //- Read construct from filename (file type implicit from extension) static autoPtr New(const fileName& name); @@ -366,7 +371,7 @@ public: static void write ( const fileName& name, - const word& ext, + const word& fileType, const MeshedSurface& surf, IOstreamOption streamOpt = IOstreamOption(), const dictionary& options = dictionary::null @@ -574,7 +579,7 @@ public: // Read //- Read from file. Chooses reader based on explicit extension - bool read(const fileName& name, const word& ext); + bool read(const fileName& name, const word& fileType); //- Read from file. Chooses reader based on detected extension virtual bool read(const fileName& name); @@ -595,10 +600,24 @@ public: write(name, *this, streamOpt, options); } + //- Generic write routine for given format type. + // If the format type is "", uses the file extension. + virtual void write + ( + const fileName& name, + const word& fileType, + IOstreamOption streamOpt = IOstreamOption(), + const dictionary& options = dictionary::null + ) const + { + write(name, fileType, *this, streamOpt, options); + } + + //- Write to database void write ( - const Time& t, + const Time& runTime, const word& surfName = word::null ) const; diff --git a/src/surfMesh/MeshedSurface/MeshedSurfaceNew.C b/src/surfMesh/MeshedSurface/MeshedSurfaceNew.C index 9e8535b339..5712c526a3 100644 --- a/src/surfMesh/MeshedSurface/MeshedSurfaceNew.C +++ b/src/surfMesh/MeshedSurface/MeshedSurfaceNew.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -34,39 +34,46 @@ License template Foam::autoPtr> -Foam::MeshedSurface::New(const fileName& name, const word& ext) +Foam::MeshedSurface::New +( + const fileName& name, + const word& fileType, + bool mandatory +) { - if (debug) + DebugInFunction + << "Construct MeshedSurface (" << fileType << ")\n"; + + auto cstrIter = fileExtensionConstructorTablePtr_->cfind(fileType); + + if (cstrIter.found()) { - InfoInFunction << "Constructing MeshedSurface" << endl; + return autoPtr>(cstrIter()(name)); } - auto cstrIter = fileExtensionConstructorTablePtr_->cfind(ext); - if (!cstrIter.found()) + // Delegate to friend if possible + const wordHashSet delegate(FriendType::readTypes()); + + if (delegate.found(fileType)) { - // No direct reader, delegate to friend if possible - const wordHashSet& delegate = FriendType::readTypes(); + // OK, can create indirectly + auto surf = autoPtr>::New(); + surf->transfer(*FriendType::New(name, fileType)); - if (delegate.found(ext)) - { - // Create indirectly - auto surf = autoPtr>::New(); - surf().transfer(*(FriendType::New(name, ext))); - - return surf; - } - else - { - FatalErrorInFunction - << "Unknown file extension " << ext << nl << nl - << "Valid types:" << nl - << flatOutput((delegate | readTypes()).sortedToc()) << nl - << exit(FatalError); - } + return surf; + } + else if (mandatory) + { + FatalErrorInFunction + << "Unknown surface format " << fileType << nl << nl + << "Valid types:" << nl + << flatOutput((delegate | readTypes()).sortedToc()) << nl + << exit(FatalError); } - return autoPtr>(cstrIter()(name)); + // Failed, but was optional + return nullptr; } @@ -74,7 +81,7 @@ template Foam::autoPtr> Foam::MeshedSurface::New(const fileName& name) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { ext = name.lessExt().ext(); diff --git a/src/surfMesh/MeshedSurfaceProxy/MeshedSurfaceProxy.C b/src/surfMesh/MeshedSurfaceProxy/MeshedSurfaceProxy.C index c205812563..9ec84f1520 100644 --- a/src/surfMesh/MeshedSurfaceProxy/MeshedSurfaceProxy.C +++ b/src/surfMesh/MeshedSurfaceProxy/MeshedSurfaceProxy.C @@ -27,7 +27,6 @@ License \*---------------------------------------------------------------------------*/ #include "MeshedSurfaceProxy.H" - #include "Time.H" #include "ListOps.H" #include "surfMesh.H" @@ -46,13 +45,16 @@ Foam::wordHashSet Foam::MeshedSurfaceProxy::writeTypes() template bool Foam::MeshedSurfaceProxy::canWriteType ( - const word& ext, - const bool verbose + const word& fileType, + bool verbose ) { return fileFormats::surfaceFormatsCore::checkSupport ( - writeTypes(), ext, verbose, "writing" + writeTypes(), + fileType, + verbose, + "writing" ); } @@ -74,23 +76,39 @@ template void Foam::MeshedSurfaceProxy::write ( const fileName& name, - const word& ext, + const word& fileType, const MeshedSurfaceProxy& surf, IOstreamOption streamOpt, const dictionary& options ) { - if (debug) + if (fileType.empty()) { - InfoInFunction << "Writing to " << name << endl; + // Handle empty/missing type + + const word ext(name.ext()); + + if (ext.empty()) + { + FatalErrorInFunction + << "Cannot determine format from filename" << nl + << " " << name << nl + << exit(FatalError); + } + + write(name, ext, surf, streamOpt, options); + return; } - auto mfIter = writefileExtensionMemberFunctionTablePtr_->cfind(ext); + + DebugInFunction << "Writing to " << name << nl; + + auto mfIter = writefileExtensionMemberFunctionTablePtr_->cfind(fileType); if (!mfIter.found()) { FatalErrorInFunction - << "Unknown file extension " << ext << nl << nl + << "Unknown file type " << fileType << nl << nl << "Valid types:" << nl << flatOutput(writeTypes().sortedToc()) << nl << exit(FatalError); @@ -110,10 +128,7 @@ void Foam::MeshedSurfaceProxy::write // the surface name to be used const word name(surfName.size() ? surfName : surfaceRegistry::defaultName); - if (debug) - { - InfoInFunction << "Writing to " << name << endl; - } + DebugInFunction << "Writing to " << name << endl; // The local location diff --git a/src/surfMesh/MeshedSurfaceProxy/MeshedSurfaceProxy.H b/src/surfMesh/MeshedSurfaceProxy/MeshedSurfaceProxy.H index 66afff1cde..660d947561 100644 --- a/src/surfMesh/MeshedSurfaceProxy/MeshedSurfaceProxy.H +++ b/src/surfMesh/MeshedSurfaceProxy/MeshedSurfaceProxy.H @@ -94,7 +94,7 @@ public: static wordHashSet writeTypes(); //- Can this file format type be written via MeshedSurfaceProxy? - static bool canWriteType(const word& ext, const bool verbose=false); + static bool canWriteType(const word& fileType, bool verbose=false); // Constructors @@ -139,11 +139,12 @@ public: const dictionary& options = dictionary::null ); - //- Write to file, selected based on given extension + //- Write to file with given format type. + // If the format type is "", uses the file extension. static void write ( const fileName& name, - const word& ext, + const word& fileType, const MeshedSurfaceProxy& surf, IOstreamOption streamOpt = IOstreamOption(), const dictionary& options = dictionary::null @@ -196,37 +197,38 @@ public: inline label nTriangles() const; - // Write + // Write - //- Generic write routine. Chooses writer based on its extension. - virtual void write - ( - const fileName& name, - IOstreamOption streamOpt = IOstreamOption(), - const dictionary& options = dictionary::null - ) const - { - write(name, *this, streamOpt, options); - } + //- Write to file, choosing writer based on the file extension. + virtual void write + ( + const fileName& name, + IOstreamOption streamOpt = IOstreamOption(), + const dictionary& options = dictionary::null + ) const + { + write(name, *this, streamOpt, options); + } - //- Generic write routine. Chooses writer based on extension. - virtual void write - ( - const fileName& name, - const word& ext, - IOstreamOption streamOpt = IOstreamOption(), - const dictionary& options = dictionary::null - ) const - { - write(name, ext, *this, streamOpt, options); - } + //- Write to file with given format type. + // If the format type is "", uses the file extension. + virtual void write + ( + const fileName& name, + const word& fileType, + IOstreamOption streamOpt = IOstreamOption(), + const dictionary& options = dictionary::null + ) const + { + write(name, fileType, *this, streamOpt, options); + } - //- Write to database - virtual void write - ( - const Time& t, - const word& surfName = word::null - ) const; + //- Write to database + virtual void write + ( + const Time& t, + const word& surfName = word::null + ) const; }; diff --git a/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.C b/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.C index 0d43dfe835..42c8cc908b 100644 --- a/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.C +++ b/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.C @@ -54,14 +54,14 @@ Foam::wordHashSet Foam::UnsortedMeshedSurface::writeTypes() template bool Foam::UnsortedMeshedSurface::canReadType ( - const word& ext, + const word& fileType, bool verbose ) { return fileFormats::surfaceFormatsCore::checkSupport ( readTypes() | MeshReference::readTypes(), - ext, + fileType, verbose, "reading" ); @@ -71,14 +71,14 @@ bool Foam::UnsortedMeshedSurface::canReadType template bool Foam::UnsortedMeshedSurface::canWriteType ( - const word& ext, + const word& fileType, bool verbose ) { return fileFormats::surfaceFormatsCore::checkSupport ( writeTypes(), - ext, + fileType, verbose, "writing" ); @@ -92,7 +92,7 @@ bool Foam::UnsortedMeshedSurface::canRead bool verbose ) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { ext = name.lessExt().ext(); @@ -118,36 +118,53 @@ template void Foam::UnsortedMeshedSurface::write ( const fileName& name, - const word& ext, + const word& fileType, const UnsortedMeshedSurface& surf, IOstreamOption streamOpt, const dictionary& options ) { - if (debug) + if (fileType.empty()) { - InfoInFunction << "Writing to " << name << endl; + // Handle empty/missing type + + const word ext(name.ext()); + + if (ext.empty()) + { + FatalErrorInFunction + << "Cannot determine format from filename" << nl + << " " << name << nl + << exit(FatalError); + } + + write(name, ext, surf, streamOpt, options); + return; } - auto mfIter = writefileExtensionMemberFunctionTablePtr_->cfind(ext); + + DebugInFunction << "Writing to " << name << nl; + + auto mfIter = writefileExtensionMemberFunctionTablePtr_->cfind(fileType); if (!mfIter.found()) { - // No direct writer, delegate to proxy if possible - const wordHashSet& delegate = ProxyType::writeTypes(); + // Delegate to proxy if possible + const wordHashSet delegate(ProxyType::writeTypes()); - if (delegate.found(ext)) - { - MeshedSurfaceProxy(surf).write(name, ext, streamOpt, options); - } - else + if (!delegate.found(fileType)) { FatalErrorInFunction - << "Unknown file extension " << ext << nl << nl + << "Unknown write format " << fileType << nl << nl << "Valid types:" << nl << flatOutput((delegate | writeTypes()).sortedToc()) << nl << exit(FatalError); } + + MeshedSurfaceProxy(surf).write + ( + name, fileType, streamOpt, options + ); } else { @@ -309,11 +326,7 @@ Foam::UnsortedMeshedSurface::UnsortedMeshedSurface fileFormats::surfaceFormatsCore::checkFile(io, dict, isGlobal) ); - // TBD: - // word fExt(dict.getOrDefault("surfaceType", fName.ext())); - // read(fName, fExt); - - this->read(fName, fName.ext()); + this->read(fName, dict.getOrDefault("fileType", word::null)); this->scalePoints(dict.getOrDefault("scale", 0)); } @@ -452,26 +465,25 @@ void Foam::UnsortedMeshedSurface::remapFaces // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -Foam::Istream& Foam::UnsortedMeshedSurface::read(Istream& is) +bool Foam::UnsortedMeshedSurface::readIstream(Istream& is) { is >> this->storedZoneIds() >> this->storedPoints() >> this->storedFaces(); is.check(FUNCTION_NAME); - return is; + return true; } template -Foam::Ostream& Foam::UnsortedMeshedSurface::write(Ostream& os) const +void Foam::UnsortedMeshedSurface::writeOstream(Ostream& os) const { os << this->zoneIds() << this->points() << this->surfFaces(); os.check(FUNCTION_NAME); - return os; } @@ -734,7 +746,6 @@ Foam::UnsortedMeshedSurface::releaseZoneIds() } -// Read from file, determine format from extension template bool Foam::UnsortedMeshedSurface::read(const fileName& name) { @@ -749,18 +760,34 @@ bool Foam::UnsortedMeshedSurface::read(const fileName& name) } -// Read from file in given format template bool Foam::UnsortedMeshedSurface::read ( const fileName& name, - const word& ext + const word& fileType ) { + if (fileType.empty()) + { + // Handle empty/missing type + + const word ext(name.ext()); + + if (ext.empty()) + { + FatalErrorInFunction + << "Cannot determine format from filename" << nl + << " " << name << nl + << exit(FatalError); + } + + return read(name, ext); + } + clear(); - // read via use selector mechanism - transfer(New(name, ext)()); + // Read via selector mechanism + transfer(*New(name, fileType)); return true; } @@ -784,6 +811,11 @@ void Foam::UnsortedMeshedSurface::operator= const UnsortedMeshedSurface& surf ) { + if (&surf == this) + { + return; // Self-assignment is a no-op + } + clear(); this->storedPoints() = surf.points(); @@ -829,7 +861,8 @@ Foam::Istream& Foam::operator>> UnsortedMeshedSurface& surf ) { - return surf.read(is); + surf.readIstream(is); + return is; } @@ -840,7 +873,8 @@ Foam::Ostream& Foam::operator<< const UnsortedMeshedSurface& surf ) { - return surf.write(os); + surf.writeOstream(os); + return os; } diff --git a/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.H b/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.H index ba535a9eff..5637f835fd 100644 --- a/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.H +++ b/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.H @@ -120,10 +120,10 @@ private: void setSize(const label, const Face&) = delete; //- Read/construct from Istream - Istream& read(Istream&); + bool readIstream(Istream& is); //- Write to Ostream - Ostream& write(Ostream&) const; + void writeOstream(Ostream& os) const; //- Return a new surface using specified pointMap and faceMap @@ -174,21 +174,21 @@ public: // Static Functions - //- Can we read this file format type? - static bool canReadType(const word& ext, bool verbose=false); + //- Known readable file-types, without friends or proxies + static wordHashSet readTypes(); + + //- Known writable file-types, without friends or proxies + static wordHashSet writeTypes(); + + //- Can we read this file format? Also checks friend types. + static bool canReadType(const word& fileType, bool verbose=false); + + //- Can we write this file format? Also checks friend types. + static bool canWriteType(const word& fileType, bool verbose=false); //- Can we read this file format? static bool canRead(const fileName& name, bool verbose=false); - //- Can we write this file format type? - static bool canWriteType(const word& ext, bool verbose=false); - - //- Known readable file-types - static wordHashSet readTypes(); - - //- Known writable file-types - static wordHashSet writeTypes(); - // Constructors @@ -220,8 +220,9 @@ public: //- Construct from file name (uses extension to determine type) explicit UnsortedMeshedSurface(const fileName& name); - //- Construct from file name (uses extension to determine type) - UnsortedMeshedSurface(const fileName& name, const word& ext); + //- Construct from file name with given format type. + // If the format type is "", uses the file extension. + UnsortedMeshedSurface(const fileName& name, const word& fileType); //- Construct from Istream explicit UnsortedMeshedSurface(Istream& is); @@ -233,8 +234,11 @@ public: UnsortedMeshedSurface(const Time& runTime, const word& surfName); //- Read construct using IO to find the file location. - // Dictionary may contain optional 'file' entry, and an - // optional 'scale' entry (eg, 0.001: mm -> m) + // Dictionary may contain the following entries: + // - \c file = alternative file name (default is dictionary name) + // - \c fileType = file format (default is from file extension) + // - \c scale (eg, 0.001: mm to m) + // . UnsortedMeshedSurface ( const IOobject& io, @@ -259,14 +263,15 @@ public: // Selectors - //- Select constructed from filename (explicit extension) + //- Read construct from filename with given file type static autoPtr New ( const fileName& name, - const word& ext + const word& fileType, + bool mandatory = true ); - //- Select constructed from filename (implicit extension) + //- Read construct from filename (implicit extension) static autoPtr New(const fileName& name); @@ -300,11 +305,12 @@ public: const dictionary& options = dictionary::null ); - //- Write to file, selected based on given extension + //- Write to file with given format type. + // If the format type is "", uses the file extension. static void write ( const fileName& name, - const word& ext, + const word& fileType, const UnsortedMeshedSurface& surf, IOstreamOption streamOpt = IOstreamOption(), const dictionary& options = dictionary::null @@ -428,8 +434,9 @@ public: // Read - //- Read from file. Chooses reader based on explicit extension - bool read(const fileName& name, const word& ext); + //- Read from file with given format type. + // If the format type is "", uses the file extension. + bool read(const fileName& name, const word& fileType); //- Read from file. Chooses reader based on detected extension virtual bool read(const fileName& name); @@ -437,7 +444,7 @@ public: // Write - //- Generic write routine. Chooses writer based on its extension. + //- Write to file, choosing writer based on the file extension. virtual void write ( const fileName& name, @@ -448,6 +455,19 @@ public: write(name, *this, streamOpt, options); } + //- Write to file with given format type. + // If the format type is "", uses the file extension. + virtual void write + ( + const fileName& name, + const word& fileType, + IOstreamOption streamOpt = IOstreamOption(), + const dictionary& options = dictionary::null + ) const + { + write(name, fileType, *this, streamOpt, options); + } + //- Write to database void write ( diff --git a/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurfaceNew.C b/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurfaceNew.C index 269e681b79..a2ff5b56ce 100644 --- a/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurfaceNew.C +++ b/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurfaceNew.C @@ -36,40 +36,43 @@ Foam::autoPtr> Foam::UnsortedMeshedSurface::New ( const fileName& name, - const word& ext + const word& fileType, + bool mandatory ) { - if (debug) + DebugInFunction + << "Construct UnsortedMeshedSurface (" << fileType << ")\n"; + + auto cstrIter = fileExtensionConstructorTablePtr_->cfind(fileType); + + if (cstrIter.found()) { - InfoInFunction << "Constructing UnsortedMeshedSurface" << endl; + return autoPtr>(cstrIter()(name)); } - auto cstrIter = fileExtensionConstructorTablePtr_->cfind(ext); - if (!cstrIter.found()) + // Delegate to friend if possible + const wordHashSet delegate(MeshReference::readTypes()); + + if (delegate.found(fileType)) { - // No direct reader, delegate to parent if possible - const wordHashSet& delegate = MeshReference::readTypes(); + // OK, can create indirectly + auto surf = autoPtr>::New(); + surf->transfer(*(MeshReference::New(name, fileType))); - if (delegate.found(ext)) - { - // Create indirectly - auto surf = autoPtr>::New(); - surf().transfer(*(MeshReference::New(name, ext))); - - return surf; - } - else - { - FatalErrorInFunction - << "Unknown file extension " << ext << nl << nl - << "Valid types:" << nl - << flatOutput((delegate | readTypes()).sortedToc()) << nl - << exit(FatalError); - } + return surf; + } + else if (mandatory) + { + FatalErrorInFunction + << "Unknown surface format " << fileType << nl << nl + << "Valid types:" << nl + << flatOutput((delegate | readTypes()).sortedToc()) << nl + << exit(FatalError); } - return autoPtr>(cstrIter()(name)); + // Failed, but was optional + return nullptr; } @@ -77,7 +80,7 @@ template Foam::autoPtr> Foam::UnsortedMeshedSurface::New(const fileName& name) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { ext = name.lessExt().ext(); diff --git a/src/surfMesh/surfaceFormats/nas/NASsurfaceFormat.H b/src/surfMesh/surfaceFormats/nas/NASsurfaceFormat.H index 47efac2d43..780a273767 100644 --- a/src/surfMesh/surfaceFormats/nas/NASsurfaceFormat.H +++ b/src/surfMesh/surfaceFormats/nas/NASsurfaceFormat.H @@ -28,7 +28,7 @@ Class Foam::fileFormats::NASsurfaceFormat Description - Nastran surface reader. + Nastran surface reader/writer. - Uses the Ansa "$ANSA_NAME" or the Hypermesh "$HMNAME COMP" extensions to obtain zone names. diff --git a/src/surfMesh/surfaceFormats/nas/NASsurfaceFormatRunTime.C b/src/surfMesh/surfaceFormats/nas/NASsurfaceFormatRunTime.C index 7bdadeb96a..9e83b98ead 100644 --- a/src/surfMesh/surfaceFormats/nas/NASsurfaceFormatRunTime.C +++ b/src/surfMesh/surfaceFormats/nas/NASsurfaceFormatRunTime.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2016-2017 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,7 +27,6 @@ License \*---------------------------------------------------------------------------*/ #include "NASsurfaceFormat.H" - #include "addToRunTimeSelectionTable.H" #include "addToMemberFunctionSelectionTable.H" @@ -38,7 +37,15 @@ namespace Foam namespace fileFormats { -// read MeshedSurface - .bdf (Bulk Data Format) and nas (Nastran) +// Read MeshedSurface - .bdf (Bulk Data Format) and nas (Nastran) +addNamedTemplatedToRunTimeSelectionTable +( + MeshedSurface, + NASsurfaceFormat, + face, + fileExtension, + nastran +); addNamedTemplatedToRunTimeSelectionTable ( MeshedSurface, @@ -56,6 +63,14 @@ addNamedTemplatedToRunTimeSelectionTable nas ); +addNamedTemplatedToRunTimeSelectionTable +( + MeshedSurface, + NASsurfaceFormat, + triFace, + fileExtension, + nastran +); addNamedTemplatedToRunTimeSelectionTable ( MeshedSurface, @@ -73,6 +88,14 @@ addNamedTemplatedToRunTimeSelectionTable nas ); +addNamedTemplatedToRunTimeSelectionTable +( + MeshedSurface, + NASsurfaceFormat, + labelledTri, + fileExtension, + nastran +); addNamedTemplatedToRunTimeSelectionTable ( MeshedSurface, @@ -91,7 +114,16 @@ addNamedTemplatedToRunTimeSelectionTable ); -// write MeshedSurfaceProxy +// Write MeshedSurfaceProxy +addNamedTemplatedToMemberFunctionSelectionTable +( + MeshedSurfaceProxy, + NASsurfaceFormat, + face, + write, + fileExtension, + nastran +); addNamedTemplatedToMemberFunctionSelectionTable ( MeshedSurfaceProxy, @@ -102,6 +134,15 @@ addNamedTemplatedToMemberFunctionSelectionTable nas ); addNamedTemplatedToMemberFunctionSelectionTable +( + MeshedSurfaceProxy, + NASsurfaceFormat, + triFace, + write, + fileExtension, + nastran +); +addNamedTemplatedToMemberFunctionSelectionTable ( MeshedSurfaceProxy, NASsurfaceFormat, @@ -111,6 +152,15 @@ addNamedTemplatedToMemberFunctionSelectionTable nas ); addNamedTemplatedToMemberFunctionSelectionTable +( + MeshedSurfaceProxy, + NASsurfaceFormat, + labelledTri, + write, + fileExtension, + nastran +); +addNamedTemplatedToMemberFunctionSelectionTable ( MeshedSurfaceProxy, NASsurfaceFormat, diff --git a/src/surfMesh/surfaceFormats/starcd/STARCDsurfaceFormatRunTime.C b/src/surfMesh/surfaceFormats/starcd/STARCDsurfaceFormatRunTime.C index cfdc8dbffd..61366fd466 100644 --- a/src/surfMesh/surfaceFormats/starcd/STARCDsurfaceFormatRunTime.C +++ b/src/surfMesh/surfaceFormats/starcd/STARCDsurfaceFormatRunTime.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2016 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,7 +27,6 @@ License \*---------------------------------------------------------------------------*/ #include "STARCDsurfaceFormat.H" - #include "addToRunTimeSelectionTable.H" #include "addToMemberFunctionSelectionTable.H" @@ -38,14 +37,14 @@ namespace Foam namespace fileFormats { -// read MeshedSurface +// Read MeshedSurface addNamedTemplatedToRunTimeSelectionTable ( MeshedSurface, STARCDsurfaceFormat, face, fileExtension, - inp + starcd ); addNamedTemplatedToRunTimeSelectionTable ( @@ -53,7 +52,7 @@ addNamedTemplatedToRunTimeSelectionTable STARCDsurfaceFormat, triFace, fileExtension, - inp + starcd ); addNamedTemplatedToRunTimeSelectionTable ( @@ -61,10 +60,10 @@ addNamedTemplatedToRunTimeSelectionTable STARCDsurfaceFormat, labelledTri, fileExtension, - inp + starcd ); -// write MeshedSurfaceProxy +// Write MeshedSurfaceProxy addNamedTemplatedToMemberFunctionSelectionTable ( MeshedSurfaceProxy, @@ -72,7 +71,7 @@ addNamedTemplatedToMemberFunctionSelectionTable face, write, fileExtension, - inp + starcd ); addNamedTemplatedToMemberFunctionSelectionTable ( @@ -81,7 +80,7 @@ addNamedTemplatedToMemberFunctionSelectionTable triFace, write, fileExtension, - inp + starcd ); addNamedTemplatedToMemberFunctionSelectionTable ( @@ -90,7 +89,7 @@ addNamedTemplatedToMemberFunctionSelectionTable labelledTri, write, fileExtension, - inp + starcd ); } diff --git a/src/surfMesh/surfaceFormats/surfaceFormatsCore.C b/src/surfMesh/surfaceFormats/surfaceFormatsCore.C index c3062b0a74..83d2c15962 100644 --- a/src/surfMesh/surfaceFormats/surfaceFormatsCore.C +++ b/src/surfMesh/surfaceFormats/surfaceFormatsCore.C @@ -29,7 +29,6 @@ License #include "surfaceFormatsCore.H" #include "Time.H" #include "ListOps.H" -#include "Fstream.H" #include "surfMesh.H" #include "stringListOps.H" @@ -326,21 +325,27 @@ Foam::fileName Foam::fileFormats::surfaceFormatsCore::checkFile bool Foam::fileFormats::surfaceFormatsCore::checkSupport ( const wordHashSet& available, - const word& ext, + const word& fileType, const bool verbose, - const word& functionName + const char* functionName ) { - if (available.found(ext)) + if (available.found(fileType)) { return true; } else if (verbose) { - Info<<"Unknown file extension for " << functionName - << " : " << ext << nl + Info<< "Unknown file type"; + + if (functionName) + { + Info<< " for " << functionName; + } + + Info<< " : " << fileType << nl << "Valid types: " << flatOutput(available.sortedToc()) << nl - << endl; + << nl; } return false; diff --git a/src/surfMesh/surfaceFormats/surfaceFormatsCore.H b/src/surfMesh/surfaceFormats/surfaceFormatsCore.H index 6160519380..79d9afe12f 100644 --- a/src/surfMesh/surfaceFormats/surfaceFormatsCore.H +++ b/src/surfMesh/surfaceFormats/surfaceFormatsCore.H @@ -143,13 +143,13 @@ public: // Static Member Functions - //- Helper function when checking if a file extension is supported. + //- Verbose checking of fileType in the list of available types static bool checkSupport ( const wordHashSet& available, - const word& ext, - const bool verbose, - const word& functionName + const word& fileType, + const bool verbose = false, + const char* functionName = nullptr ); //- Use IOobject information to resolve file to load from, diff --git a/src/surfMesh/triSurface/triSurface.C b/src/surfMesh/triSurface/triSurface.C index 2f08b8155e..9b126732c7 100644 --- a/src/surfMesh/triSurface/triSurface.C +++ b/src/surfMesh/triSurface/triSurface.C @@ -499,13 +499,13 @@ Foam::triSurface::triSurface Foam::triSurface::triSurface ( const fileName& name, - const word& ext, + const word& fileType, const scalar scaleFactor ) : triSurface() { - read(name, ext); + read(name, fileType); scalePoints(scaleFactor); setDefaultPatches(); } diff --git a/src/surfMesh/triSurface/triSurface.H b/src/surfMesh/triSurface/triSurface.H index 4986f3415c..d75ab32c83 100644 --- a/src/surfMesh/triSurface/triSurface.H +++ b/src/surfMesh/triSurface/triSurface.H @@ -91,12 +91,8 @@ class triSurface // (face ordering nFaces/startFace only used during reading, writing) geometricSurfacePatchList patches_; - static wordHashSet readTypes_; - static wordHashSet writeTypes_; - - - // Demand driven + // Demand Driven //- Edge-face addressing (sorted) mutable unique_ptr sortedEdgeFacesPtr_; @@ -136,11 +132,12 @@ class triSurface //- Read in STL format bool readSTL(const fileName& filename, bool forceBinary=false); - //- Generic read routine. Chooses reader based on extension. + //- Generic read routine for given format type. + // If the format type is "", uses the file extension. bool read ( const fileName& filename, - const word& ext, + const word& fileType, const bool check = true ); @@ -154,17 +151,8 @@ class triSurface //- Write GTS (Gnu Tri Surface library) format. void writeGTS(const fileName& filename, const bool sort) const; - //- Generic write routine. Chooses writer based on extension. - // The sort option may not have an effect. - void write - ( - const fileName& filename, - const word& ext, - const bool sort - ) const; - - // Static private functions + // Static Private Functions //- Convert faces to labelledTri. All get same region. static List convertToTri @@ -240,20 +228,20 @@ public: //- Name of triSurface directory to use. static fileName triSurfInstance(const Time&); - //- Can we read this file format? - static bool canRead(const fileName& name, const bool verbose=false); + //- Known readable file-types, including via friends or proxies + static wordHashSet readTypes(); + + //- Known writable file-types, including via friends or proxies + static wordHashSet writeTypes(); //- Can we read this file format? - static bool canReadType(const word& ext, const bool verbose=false); + static bool canReadType(const word& fileType, bool verbose=false); //- Can we write this file format? - static bool canWriteType(const word& ext, const bool verbose=false); + static bool canWriteType(const word& fileType, bool verbose=false); - //- Known readable file-types - static const wordHashSet& readTypes(); - - //- Known writable file-types - static const wordHashSet& writeTypes(); + //- Can we read this file format? + static bool canRead(const fileName& name, bool verbose=false); // IO helpers @@ -356,11 +344,12 @@ public: const scalar scaleFactor = -1 ); - //- Construct from file name (uses extension to determine type) + //- Construct from file name with given format type. + // If the format type is "", uses the file extension. triSurface ( const fileName& name, - const word& ext, + const word& fileType, const scalar scaleFactor = -1 ); @@ -371,8 +360,11 @@ public: explicit triSurface(const Time& d); //- Read construct using IO to find the file location. - // Dictionary may contain optional 'file' entry, and an - // optional 'scale' entry (eg, 0.001: mm -> m) + // Dictionary may contain the following entries: + // - \c file = alternative file name (default is dictionary name) + // - \c fileType = file format (default is from file extension) + // - \c scale (eg, 0.001: mm to m) + // . triSurface ( const IOobject& io, @@ -574,9 +566,20 @@ public: //- Write to Ostream in simple FOAM format void write(Ostream& os) const; - //- Generic write routine. Chooses writer based on extension. + //- Generic write routine (uses extension to determine type). + // The sort option may not have an effect. void write(const fileName&, const bool sortByRegion = false) const; + //- Generic write routine for given format type. + // If the format type is "", uses the file extension. + // The sort option may not have an effect. + void write + ( + const fileName& filename, + const word& fileType, + const bool sortByRegion = false + ) const; + //- Write to database void write(const Time& d) const; @@ -584,7 +587,7 @@ public: void writeStats(Ostream& os) const; - // Member operators + // Member Operators //- Copy assignment void operator=(const triSurface& surf); diff --git a/src/surfMesh/triSurface/triSurfaceIO.C b/src/surfMesh/triSurface/triSurfaceIO.C index cf9fb4c579..47e77b9f87 100644 --- a/src/surfMesh/triSurface/triSurfaceIO.C +++ b/src/surfMesh/triSurface/triSurfaceIO.C @@ -35,68 +35,70 @@ License #include "MeshedSurfaceProxy.H" #include "MeshedSurface.H" -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - -Foam::wordHashSet Foam::triSurface::readTypes_; -Foam::wordHashSet Foam::triSurface::writeTypes_; - - // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // -const Foam::wordHashSet& Foam::triSurface::readTypes() +Foam::wordHashSet Foam::triSurface::readTypes() { - // Stop-gap measure until reading is handled more generally - if (readTypes_.empty()) - { - readTypes_ = { "ftr", "stl", "stlb" }; - readTypes_ += UnsortedMeshedSurface::readTypes(); - readTypes_ += MeshedSurface::readTypes(); - } + wordHashSet known + ( + UnsortedMeshedSurface::readTypes() + | MeshedSurface::readTypes() + ); - return readTypes_; + // Additional hard-coded entry points, but do not need + // {"stl", "stlb"} + // since they are shadowed by *MeshedSurface + + known.insert("ftr"); + + return known; } -const Foam::wordHashSet& Foam::triSurface::writeTypes() +Foam::wordHashSet Foam::triSurface::writeTypes() { - // Stop-gap measure until writing is handled more generally - if (writeTypes_.empty()) - { - writeTypes_ = { "ftr", "stl", "stlb", "gts" }; - writeTypes_ += MeshedSurfaceProxy::writeTypes(); - } + wordHashSet known + ( + MeshedSurfaceProxy::writeTypes() + ); - return writeTypes_; + // Additional hard-coded entry points, but do not need + // {"gts", "stl", "stlb"} + // since they are shadowed by MeshedSurfaceProxy + + known.insert("ftr"); + + return known; } -bool Foam::triSurface::canReadType(const word& ext, const bool verbose) +bool Foam::triSurface::canReadType(const word& fileType, bool verbose) { return fileFormats::surfaceFormatsCore::checkSupport ( readTypes(), - ext, + fileType, verbose, "reading" ); } -bool Foam::triSurface::canWriteType(const word& ext, const bool verbose) +bool Foam::triSurface::canWriteType(const word& fileType, bool verbose) { return fileFormats::surfaceFormatsCore::checkSupport ( writeTypes(), - ext, + fileType, verbose, "writing" ); } -bool Foam::triSurface::canRead(const fileName& name, const bool verbose) +bool Foam::triSurface::canRead(const fileName& name, bool verbose) { - word ext = name.ext(); + word ext(name.ext()); if (ext == "gz") { ext = name.lessExt().ext(); @@ -105,8 +107,6 @@ bool Foam::triSurface::canRead(const fileName& name, const bool verbose) } - - Foam::fileName Foam::triSurface::relativeFilePath ( const IOobject& io, @@ -192,7 +192,7 @@ bool Foam::triSurface::read(Istream& is) bool Foam::triSurface::read ( const fileName& name, - const word& ext, + const word& fileType, const bool check ) { @@ -203,7 +203,25 @@ bool Foam::triSurface::read << exit(FatalError); } - if (ext == "gz") + if (fileType.empty()) + { + // Handle empty/missing type + + const word ext(name.ext()); + + if (ext.empty()) + { + FatalErrorInFunction + << "Cannot determine format from filename" << nl + << " " << name << nl + << exit(FatalError); + } + + return read(name, ext, false); + } + + + if (fileType == "gz") { fileName unzipName = name.lessExt(); @@ -212,15 +230,15 @@ bool Foam::triSurface::read } // Hard-coded readers - if (ext == "ftr") + if (fileType == "ftr") { return read(IFstream(name)()); } - else if (ext == "stl") + else if (fileType == "stl") { return readSTL(name); // ASCII } - else if (ext == "stlb") + else if (fileType == "stlb") { return readSTL(name, true); // Force BINARY } @@ -228,9 +246,9 @@ bool Foam::triSurface::read // UnsortedMeshedSurface { using proxyType = UnsortedMeshedSurface; - if (proxyType::readTypes().found(ext)) + if (proxyType::readTypes().found(fileType)) { - transfer(*(proxyType::New(name, ext))); + transfer(*(proxyType::New(name, fileType))); return true; } } @@ -238,19 +256,19 @@ bool Foam::triSurface::read // MeshedSurface { using proxyType = MeshedSurface; - if (proxyType::readTypes().found(ext)) + if (proxyType::readTypes().found(fileType)) { - transfer(*(proxyType::New(name, ext))); + transfer(*(proxyType::New(name, fileType))); return true; } } FatalErrorInFunction - << "unknown file extension " << ext - << " for reading file " << name - << ". Supported extensions:" << nl - << " " << flatOutput(readTypes_.sortedToc()) << nl + << "Unknown surface format " << fileType + << " for reading file " << name << nl + << "Valid types:" << nl + << " " << flatOutput(readTypes().sortedToc()) << nl << exit(FatalError); return false; @@ -260,30 +278,49 @@ bool Foam::triSurface::read void Foam::triSurface::write ( const fileName& name, - const word& ext, - const bool sort + const word& fileType, + const bool sortByRegion ) const { + if (fileType.empty()) + { + // Handle empty/missing type + + const word ext(name.ext()); + + if (ext.empty()) + { + FatalErrorInFunction + << "Cannot determine format from filename" << nl + << " " << name << nl + << exit(FatalError); + } + + write(name, ext, sortByRegion); + return; + } + + // Hard-coded readers - if (ext == "ftr") + if (fileType == "ftr") { OFstream os(name); write(os); } - else if (ext == "stl") + else if (fileType == "stl") { - writeSTLASCII(name, sort); + writeSTLASCII(name, sortByRegion); } - else if (ext == "stlb") + else if (fileType == "stlb") { writeSTLBINARY(name); } - else if (ext == "gts") + else if (fileType == "gts") { - writeGTS(name, sort); + writeGTS(name, sortByRegion); } - else if (MeshedSurfaceProxy::canWriteType(ext)) + else if (MeshedSurfaceProxy::canWriteType(fileType)) { labelList faceMap; List zoneLst = this->sortedZones(faceMap); @@ -296,15 +333,15 @@ void Foam::triSurface::write faceMap ); - proxy.write(name, ext); + proxy.write(name, fileType); } else { FatalErrorInFunction - << "unknown file extension " << ext - << " for writing file " << name - << ". Supported extensions:" << nl - << " " << flatOutput(writeTypes_.sortedToc()) << nl + << "Unknown surface format " << fileType + << " for writing file " << name << nl + << "Valid types:" << nl + << " " << flatOutput(writeTypes().sortedToc()) << nl << exit(FatalError); } } @@ -326,13 +363,12 @@ Foam::triSurface::triSurface(const Time& d) : triSurface() { - fileName foamFile(d.caseName() + ".ftr"); + IFstream is + ( + d.path()/triSurfInstance(d)/typeName/(d.caseName() + ".ftr") + ); - fileName foamPath(d.path()/triSurfInstance(d)/typeName/foamFile); - - IFstream foamStream(foamPath); - - read(foamStream); + read(is); setDefaultPatches(); } @@ -349,11 +385,7 @@ Foam::triSurface::triSurface { fileName fName(checkFile(io, dict, isGlobal)); - // TBD: - // word fileExt = dict.getOrDefault("surfaceType", fName.ext()); - // read(fName, ext); - - read(fName, fName.ext()); + read(fName, dict.getOrDefault("fileType", word::null)); scalePoints(dict.getOrDefault("scale", 0)); @@ -381,20 +413,18 @@ void Foam::triSurface::write(Ostream& os) const os << points() << nl << static_cast&>(*this) << nl; - // Check state of Ostream os.check(FUNCTION_NAME); } void Foam::triSurface::write(const Time& d) const { - fileName foamFile(d.caseName() + ".ftr"); + OFstream os + ( + d.path()/triSurfInstance(d)/typeName/(d.caseName() + ".ftr") + ); - fileName foamPath(d.path()/triSurfInstance(d)/typeName/foamFile); - - OFstream foamStream(foamPath); - - write(foamStream); + write(os); } @@ -402,9 +432,9 @@ void Foam::triSurface::writeStats(Ostream& os) const { // Unfortunately nPoints constructs meshPoints() so do compact version // ourselves. + bitSet pointIsUsed(points().size()); - label nPoints = 0; boundBox bb(boundBox::invertedBox); labelHashSet regionsUsed; @@ -412,20 +442,18 @@ void Foam::triSurface::writeStats(Ostream& os) const { regionsUsed.insert(f.region()); - forAll(f, fp) + for (const label pointi : f) { - const label pointi = f[fp]; if (pointIsUsed.set(pointi)) { bb.add(points()[pointi]); - ++nPoints; } } } os << "Triangles : " << size() << " in " << regionsUsed.size() << " region(s)" << nl - << "Vertices : " << nPoints << nl + << "Vertices : " << pointIsUsed.count() << nl << "Bounding Box : " << bb << endl; } diff --git a/src/surfMesh/writers/starcd/starcdSurfaceWriter.C b/src/surfMesh/writers/starcd/starcdSurfaceWriter.C index 2f31924bed..bad0418f44 100644 --- a/src/surfMesh/writers/starcd/starcdSurfaceWriter.C +++ b/src/surfMesh/writers/starcd/starcdSurfaceWriter.C @@ -148,7 +148,7 @@ Foam::fileName Foam::surfaceWriters::starcdWriter::write() MeshedSurfaceProxy(surf.points(), surf.faces()).write ( outputFile, - "inp", + "starcd", // Canonical selection name streamOpt_ ); }