Commit 560c053b authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: support independent specification of surface read/write format (#1600)

- adjustments to internal handling to improve run-time addition of
  other formats (eg, with additional user library)

  For example, to write a binary STL with a '.stl' extension:

    $ surfaceMeshConvert input.obj  -write-format stlb  output.stl

  Or in a sampler,
  to specify the input type without ambiguity:

  surf
  {
      type        meshedSurface;
      surface     sampling.inp;

      fileType    starcd;
      scale       0.001;
      ...
  }

STYLE: regularize naming for input/output scaling

  * -read-scale   (compat: -scaleIn)
  * -write-scale  (compat: -scaleOut)

CONFIG: change edge/surface selection name for STARCD format

- now select as "starcd" instead of "inp" to avoid naming ambiguity
  with abaqus
parent ae14a1ef
......@@ -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);
}
......
......@@ -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")
{
......
......@@ -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")
{
......
......@@ -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 \<type\>
Specify input file format
- \par -write-format \<type\>
Specify output file format
- \par -scale \<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<word>("read-format", getExtension(importName))
);
const word writeFileType
(
args.getOrDefault<word>("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<scalar>("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;
......
......@@ -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,9 +42,39 @@ 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[])
{
argList::addNote
......@@ -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
(
args.getOrDefault<word>("read-format", getExtension(importName))
);
const word writeFileType
(
!edgeMesh::canReadType(importName.ext(), true)
|| !edgeMesh::canWriteType(exportName.ext(), true)
)
args.getOrDefault<word>("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;
......
......@@ -41,26 +41,32 @@ Usage
- \par -clean
Perform some surface checking/cleanup on the input surface.
- \par -scaleIn \<scale\>
Specify a scaling factor when reading files.
- \par -read-format \<type\>
The input file format (default: use file extension)
- \par -scaleOut \<scale\>
Specify a scaling factor when writing files.
- \par -write-format \<type\>
The output file format (default: use file extension)
- \par -read-scale \<scale\>
Input geometry scaling factor.
- \par -write-scale \<scale\>
Output geometry scaling factor.
- \par -dict \<dictionary\>
Specify an alternative dictionary for constant/coordinateSystems.
Alternative dictionary for constant/coordinateSystems.
- \par -from \<coordinateSystem\>
Specify a coordinate System when reading files.
Apply specified coordinate system after reading file.
- \par -to \<coordinateSystem\>
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",
"read-format",
"type",
"Input format (default: use file extension)"
);
argList::addOption
(
"write-format",
"type",
"Output format (default: use file extension)"
);
argList::addOption
(
"read-scale",
"factor",
"Geometry scaling factor on input"
"Input geometry scaling factor"
);
argList::addOption
(
"scaleOut",
"write-scale",
"factor",
"Geometry scaling factor on output"
"Output geometry scaling factor"
);
argList::addOption("dict", "file", "Use alternative coordinateSystems");
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
(
args.getOrDefault<word>("read-format", getExtension(importName))
);
const word writeFileType
(
!MeshedSurface<face>::canRead(importName, true)
|| !MeshedSurface<face>::canWriteType(exportName.ext(), true)
)
args.getOrDefault<word>("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<coordSystem::cartesian> fromCsys;
autoPtr<coordSystem::cartesian> toCsys;
......@@ -173,7 +235,7 @@ int main(int argc, char *argv[])
if (!ioCsys.typeHeaderOk<coordinateSystems>(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<face> surf(importName);
meshedSurface surf(importName, readFileType);
if (args.found("clean"))
if (args.readIfPresent("read-scale", scaleFactor) && scaleFactor > 0)
{
surf.cleanup(true);
Info<< "scale input " << scaleFactor << nl;
surf.scalePoints(scaleFactor);
}
scalar scaleIn = 0;
if (args.readIfPresent("scaleIn", scaleIn) && scaleIn > 0)
if (args.found("clean"))
{
Info<< "scale input " << scaleIn << endl;
surf.scalePoints(scaleIn);
surf.cleanup(true);
}
if (fromCsys)
{
Info<< "move points from coordinate system: "
<< fromCsys->name() << endl;
<< fromCsys->name() << nl;
tmp<pointField> 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<pointField> tpf = toCsys->globalPosition(surf.points());
surf.movePoints(tpf());
}
scalar scaleOut = 0;
if (args.read