surfaceMeshImport.C 8.18 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2011-2016 OpenFOAM Foundation
9
    Copyright (C) 2018-2020 OpenCFD Ltd.
10 11 12 13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14 15 16 17
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
18 19 20 21 22 23 24

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26 27 28 29

Application
    surfaceMeshImport

30 31 32
Group
    grpSurfaceUtilities

33 34 35 36 37 38
Description
    Import from various third-party surface formats into surfMesh
    with optional scaling or transformations (rotate/translate)
    on a coordinateSystem.

Usage
39
    \b surfaceMeshImport inputFile [OPTION]
40

41
    Options:
42
      - \par -clean
43
        Perform some surface checking/cleanup on the input surface.
44

45
      - \par -name \<name\>
46
        Specify an alternative surface name when writing.
47

48 49
      - \par -read-format \<type\>
        Specify input file format
50

51 52 53 54 55
      - \par -read-scale \<scale\>
        Scale factor when reading files.

      - \par -write-scale \<scale\>
        Scale factor when writing files.
56

57
      - \par -dict \<dictionary\>
58
        Alternative dictionary for constant/coordinateSystems.
59

60
      - \par -from \<coordinateSystem\>
61
        Specify a coordinate system when reading files.
62

63
      - \par -to \<coordinateSystem\>
64
        Specify a coordinate system when writing files.
65 66 67 68 69 70 71 72 73 74 75

Note
    The filename extensions are used to determine the file format type.

\*---------------------------------------------------------------------------*/

#include "argList.H"
#include "Time.H"

#include "MeshedSurfaces.H"
#include "coordinateSystems.H"
76
#include "cartesianCS.H"
77 78 79

using namespace Foam;

80 81 82 83 84 85 86 87 88 89 90 91
static word getExtension(const fileName& name)
{
    word ext(name.ext());
    if (ext == "gz")
    {
        ext = name.lessExt().ext();
    }

    return ext;
}


92 93 94 95
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

int main(int argc, char *argv[])
{
96 97
    argList::addNote
    (
98
        "Import from various third-party surface formats into surfMesh"
99 100
    );

101
    argList::noParallel();
102
    argList::addArgument("surface", "The input surface file");
103

104 105 106
    argList::addBoolOption
    (
        "clean",
107
        "Perform some surface checking/cleanup on the input surface"
108 109 110 111 112
    );
    argList::addOption
    (
        "name",
        "name",
113 114 115 116 117 118 119
        "The surface name when writing (default is 'default')"
    );
    argList::addOption
    (
        "read-format",
        "type",
        "Input format (default: use file extension)"
120 121 122
    );
    argList::addOption
    (
123
        "read-scale",
124
        "factor",
125
        "Input geometry scaling factor"
126 127 128
    );
    argList::addOption
    (
129
        "write-scale",
130
        "factor",
131
        "Output geometry scaling factor"
132
    );
133 134 135 136 137

    argList::addOptionCompat("read-scale", {"scaleIn", 1912});
    argList::addOptionCompat("write-scale", {"scaleOut", 1912});

    argList::addOption("dict", "file", "Alternative coordinateSystems");
138

139 140 141
    argList::addOption
    (
        "from",
142 143
        "system",
        "The source coordinate system, applied after '-read-scale'",
144
        true // advanced
145 146 147 148
    );
    argList::addOption
    (
        "to",
149 150
        "system",
        "The target coordinate system, applied before '-write-scale'",
151
        true // advanced
152 153 154 155
    );

    #include "setRootCase.H"
    #include "createTime.H"
156 157 158 159 160 161 162 163 164 165 166 167 168 169

    // try for the latestTime, but create "constant" as needed
    instantList Times = runTime.times();
    if (Times.size())
    {
        label startTime = Times.size()-1;
        runTime.setTime(Times[startTime], startTime);
    }
    else
    {
        runTime.setTime(instant(0, runTime.constant()), 0);
    }


170
    const fileName importName(args[1]);
171
    const word exportName(args.getOrDefault<word>("name", "default"));
172 173 174 175 176

    const word readFileType
    (
        args.getOrDefault<word>("read-format", getExtension(importName))
    );
177

178 179
    // Check that reading is supported
    if (!meshedSurface::canRead(readFileType, true))
180
    {
181 182 183
        FatalError
            << "Unsupported file format(s)" << nl
            << exit(FatalError);
184 185 186
    }


187 188
    scalar scaleFactor(0);

189 190 191
    // The coordinate transformations (must be cartesian)
    autoPtr<coordSystem::cartesian> fromCsys;
    autoPtr<coordSystem::cartesian> toCsys;
192

193
    if (args.found("from") || args.found("to"))
194
    {
195 196 197
        IOobject ioCsys = IOobject::selectIO
        (
            IOobject
198
            (
199 200 201 202 203 204 205
                coordinateSystems::typeName,
                runTime.constant(),
                runTime,
                IOobject::MUST_READ,
                IOobject::NO_WRITE,
                false
            ),
206
            args.getOrDefault<fileName>("dict", "")
207 208 209
        );

        if (!ioCsys.typeHeaderOk<coordinateSystems>(false))
210
        {
211
            FatalError
212
                << ioCsys.objectPath() << nl
213 214 215
                << exit(FatalError);
        }

216
        coordinateSystems globalCoords(ioCsys);
217

218
        if (args.found("from"))
219
        {
220
            const word csName(args["from"]);
221
            const auto* csPtr = globalCoords.cfind(csName);
222

223
            if (!csPtr)
224
            {
225
                FatalError
226
                    << "Cannot find -from " << csName << nl
227 228
                    << "available coordinateSystems: "
                    << flatOutput(globalCoords.names()) << nl
229 230 231
                    << exit(FatalError);
            }

232
            fromCsys = autoPtr<coordSystem::cartesian>::New(*csPtr);
233 234
        }

235
        if (args.found("to"))
236
        {
237
            const word csName(args["to"]);
238
            const auto* csPtr = globalCoords.cfind(csName);
239

240
            if (!csPtr)
241
            {
242
                FatalError
243
                    << "Cannot find -to " << csName << nl
244 245
                    << "available coordinateSystems: "
                    << flatOutput(globalCoords.names()) << nl
246 247 248
                    << exit(FatalError);
            }

249
            toCsys = autoPtr<coordSystem::cartesian>::New(*csPtr);
250 251
        }

252 253
        // Maybe fix this later
        if (fromCsys && toCsys)
254
        {
255
            FatalError
256
                << "Only allowed '-from' or '-to' option at the moment."
257 258 259 260 261
                << exit(FatalError);
        }
    }


262
    meshedSurface surf(importName, readFileType);
263

264
    if (args.readIfPresent("read-scale", scaleFactor) && scaleFactor > 0)
265
    {
266 267
        Info<< "scale input " << scaleFactor << nl;
        surf.scalePoints(scaleFactor);
268 269
    }

270
    if (args.found("clean"))
271
    {
272
        surf.cleanup(true);
273 274
    }

275
    if (fromCsys)
276
    {
277 278 279
        Info<< "move points from coordinate system: "
            << fromCsys->name() << endl;
        tmp<pointField> tpf = fromCsys->localPosition(surf.points());
280 281 282
        surf.movePoints(tpf());
    }

283
    if (toCsys)
284
    {
285 286 287
        Info<< "move points to coordinate system: "
            << toCsys->name() << endl;
        tmp<pointField> tpf = toCsys->globalPosition(surf.points());
288 289 290
        surf.movePoints(tpf());
    }

291
    if (args.readIfPresent("write-scale", scaleFactor) && scaleFactor > 0)
292
    {
293 294
        Info<< "scale output " << scaleFactor << nl;
        surf.scalePoints(scaleFactor);
295 296 297 298 299 300 301 302 303 304
    }

    surfMesh smesh
    (
        IOobject
        (
            exportName,
            runTime.constant(),
            runTime
        ),
305
        std::move(surf)
306 307 308 309 310 311 312 313 314 315 316 317
    );


    Info<< "writing surfMesh:\n  " << smesh.objectPath() << endl;
    smesh.write();

    Info<< "\nEnd\n" << endl;

    return 0;
}

// ************************************************************************* //