OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

surfaceMeshImport.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Application
00026     surfaceMeshImport
00027 
00028 Description
00029     Import from various third-party surface formats into surfMesh
00030     with optional scaling or transformations (rotate/translate)
00031     on a coordinateSystem.
00032 
00033 Usage
00034     - surfaceMeshImport inputFile [OPTION]
00035 
00036     @param -clean \n
00037     Perform some surface checking/cleanup on the input surface.
00038 
00039     @param -name <name> \n
00040     Specify an alternative surface name when writing.
00041 
00042     @param -scaleIn <scale> \n
00043     Specify a scaling factor when reading files.
00044 
00045     @param -scaleOut <scale> \n
00046     Specify a scaling factor when writing files.
00047 
00048     @param -dict <dictionary> \n
00049     Specify an alternative dictionary for constant/coordinateSystems.
00050 
00051     @param -from <coordinateSystem> \n
00052     Specify a coordinate System when reading files.
00053 
00054     @param -to <coordinateSystem> \n
00055     Specify a coordinate System when writing files.
00056 
00057 Note
00058     The filename extensions are used to determine the file format type.
00059 
00060 \*---------------------------------------------------------------------------*/
00061 
00062 #include "argList.H"
00063 #include "timeSelector.H"
00064 #include "Time.H"
00065 
00066 #include "MeshedSurfaces.H"
00067 #include "coordinateSystems.H"
00068 
00069 using namespace Foam;
00070 
00071 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00072 //  Main program:
00073 
00074 int main(int argc, char *argv[])
00075 {
00076     argList::noParallel();
00077     argList::validArgs.append("inputFile");
00078     argList::validOptions.insert("name",  "name");
00079     argList::validOptions.insert("clean", "");
00080     argList::validOptions.insert("scaleIn",  "scale");
00081     argList::validOptions.insert("scaleOut", "scale");
00082     argList::validOptions.insert("dict", "coordinateSystemsDict");
00083     argList::validOptions.insert("from", "sourceCoordinateSystem");
00084     argList::validOptions.insert("to",   "targetCoordinateSystem");
00085 
00086 #   include "setRootCase.H"
00087 #   include "createTime.H"
00088 
00089     const stringList& params = args.additionalArgs();
00090 
00091     // try for the latestTime, but create "constant" as needed
00092     instantList Times = runTime.times();
00093     if (Times.size())
00094     {
00095         label startTime = Times.size()-1;
00096         runTime.setTime(Times[startTime], startTime);
00097     }
00098     else
00099     {
00100         runTime.setTime(instant(0, runTime.constant()), 0);
00101     }
00102 
00103 
00104     fileName importName(params[0]);
00105     word exportName("default");
00106     args.optionReadIfPresent("name", exportName);
00107 
00108     // check that reading is supported
00109     if (!MeshedSurface<face>::canRead(importName, true))
00110     {
00111         return 1;
00112     }
00113 
00114 
00115     // get the coordinate transformations
00116     autoPtr<coordinateSystem> fromCsys;
00117     autoPtr<coordinateSystem> toCsys;
00118 
00119     if (args.optionFound("from") || args.optionFound("to"))
00120     {
00121         autoPtr<IOobject> ioPtr;
00122 
00123         if (args.optionFound("dict"))
00124         {
00125             fileName dictPath(args.option("dict"));
00126 
00127             ioPtr.set
00128             (
00129                 new IOobject
00130                 (
00131                     (
00132                         isDir(dictPath)
00133                       ? dictPath/coordinateSystems::typeName
00134                       : dictPath
00135                     ),
00136                     runTime,
00137                     IOobject::MUST_READ,
00138                     IOobject::NO_WRITE,
00139                     false
00140                 )
00141             );
00142         }
00143         else
00144         {
00145             ioPtr.set
00146             (
00147                 new IOobject
00148                 (
00149                     coordinateSystems::typeName,
00150                     runTime.constant(),
00151                     runTime,
00152                     IOobject::MUST_READ,
00153                     IOobject::NO_WRITE,
00154                     false
00155                 )
00156             );
00157         }
00158 
00159 
00160         if (!ioPtr->headerOk())
00161         {
00162             FatalErrorIn(args.executable())
00163                 << "Cannot open coordinateSystems file\n    "
00164                 << ioPtr->objectPath() << nl
00165                 << exit(FatalError);
00166         }
00167 
00168         coordinateSystems csLst(ioPtr());
00169 
00170         if (args.optionFound("from"))
00171         {
00172             const word csName(args.option("from"));
00173 
00174             label csId = csLst.find(csName);
00175             if (csId < 0)
00176             {
00177                 FatalErrorIn(args.executable())
00178                     << "Cannot find -from " << csName << nl
00179                     << "available coordinateSystems: " << csLst.toc() << nl
00180                     << exit(FatalError);
00181             }
00182 
00183             fromCsys.reset(new coordinateSystem(csLst[csId]));
00184         }
00185 
00186         if (args.optionFound("to"))
00187         {
00188             const word csName(args.option("to"));
00189 
00190             label csId = csLst.find(csName);
00191             if (csId < 0)
00192             {
00193                 FatalErrorIn(args.executable())
00194                     << "Cannot find -to " << csName << nl
00195                     << "available coordinateSystems: " << csLst.toc() << nl
00196                     << exit(FatalError);
00197             }
00198 
00199             toCsys.reset(new coordinateSystem(csLst[csId]));
00200         }
00201 
00202 
00203         // maybe fix this later
00204         if (fromCsys.valid() && toCsys.valid())
00205         {
00206             FatalErrorIn(args.executable())
00207                 << "Only allowed  '-from' or '-to' option at the moment."
00208                 << exit(FatalError);
00209         }
00210     }
00211 
00212 
00213 
00214     MeshedSurface<face> surf(importName);
00215 
00216     if (args.optionFound("clean"))
00217     {
00218         surf.cleanup(true);
00219     }
00220 
00221 
00222     scalar scaleIn = 0;
00223     if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
00224     {
00225         Info<< " -scaleIn " << scaleIn << endl;
00226         surf.scalePoints(scaleIn);
00227     }
00228 
00229     if (fromCsys.valid())
00230     {
00231         Info<< " -from " << fromCsys().name() << endl;
00232         tmp<pointField> tpf = fromCsys().localPosition(surf.points());
00233         surf.movePoints(tpf());
00234     }
00235 
00236     if (toCsys.valid())
00237     {
00238         Info<< " -to " << toCsys().name() << endl;
00239         tmp<pointField> tpf = toCsys().globalPosition(surf.points());
00240         surf.movePoints(tpf());
00241     }
00242 
00243     scalar scaleOut = 0;
00244     if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
00245     {
00246         Info<< " -scaleOut " << scaleOut << endl;
00247         surf.scalePoints(scaleOut);
00248     }
00249 
00250     surfMesh smesh
00251     (
00252         IOobject
00253         (
00254             exportName,
00255             runTime.constant(),
00256             runTime
00257         ),
00258         surf.xfer()
00259     );
00260 
00261 
00262     Info<< "writing surfMesh:\n  " << smesh.objectPath() << endl;
00263     smesh.write();
00264 
00265     Info<< "\nEnd\n" << endl;
00266 
00267     return 0;
00268 }
00269 
00270 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd