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

surfaceMeshConvert.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     surfaceMeshConvert
00027 
00028 Description
00029 
00030     Convert between surface formats with optional scaling or
00031     transformations (rotate/translate) on a coordinateSystem.
00032 
00033 Usage
00034     - surfaceMeshConvert inputFile outputFile [OPTION]
00035 
00036     @param -clean \n
00037     Perform some surface checking/cleanup on the input surface.
00038 
00039     @param -scaleIn <scale> \n
00040     Specify a scaling factor when reading files.
00041 
00042     @param -scaleOut <scale> \n
00043     Specify a scaling factor when writing files.
00044 
00045     @param -dict <dictionary> \n
00046     Specify an alternative dictionary for constant/coordinateSystems.
00047 
00048     @param -from <coordinateSystem> \n
00049     Specify a coordinate System when reading files.
00050 
00051     @param -to <coordinateSystem> \n
00052     Specify a coordinate System when writing files.
00053 
00054 Note
00055     The filename extensions are used to determine the file format type.
00056 
00057 \*---------------------------------------------------------------------------*/
00058 
00059 #include "argList.H"
00060 #include "timeSelector.H"
00061 #include "Time.H"
00062 
00063 #include "MeshedSurfaces.H"
00064 #include "coordinateSystems.H"
00065 
00066 using namespace Foam;
00067 
00068 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00069 //  Main program:
00070 
00071 int main(int argc, char *argv[])
00072 {
00073     argList::noParallel();
00074     argList::validArgs.append("inputFile");
00075     argList::validArgs.append("outputFile");
00076     argList::validOptions.insert("clean", "");
00077     argList::validOptions.insert("scaleIn",  "scale");
00078     argList::validOptions.insert("scaleOut", "scale");
00079     argList::validOptions.insert("dict", "coordinateSystemsDict");
00080     argList::validOptions.insert("from", "sourceCoordinateSystem");
00081     argList::validOptions.insert("to",   "targetCoordinateSystem");
00082 
00083     argList args(argc, argv);
00084     Time runTime(args.rootPath(), args.caseName());
00085     const stringList& params = args.additionalArgs();
00086 
00087     fileName importName(params[0]);
00088     fileName exportName(params[1]);
00089 
00090     // disable inplace editing
00091     if (importName == exportName)
00092     {
00093         FatalErrorIn(args.executable())
00094             << "Output file " << exportName << " would overwrite input file."
00095             << exit(FatalError);
00096     }
00097 
00098     // check that reading/writing is supported
00099     if
00100     (
00101         !MeshedSurface<face>::canRead(importName, true)
00102      || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
00103     )
00104     {
00105         return 1;
00106     }
00107 
00108 
00109     // get the coordinate transformations
00110     autoPtr<coordinateSystem> fromCsys;
00111     autoPtr<coordinateSystem> toCsys;
00112 
00113     if (args.optionFound("from") || args.optionFound("to"))
00114     {
00115         autoPtr<IOobject> csDictIoPtr;
00116 
00117         if (args.optionFound("dict"))
00118         {
00119             fileName dictPath(args.option("dict"));
00120 
00121             csDictIoPtr.set
00122             (
00123                 new IOobject
00124                 (
00125                     (
00126                         isDir(dictPath)
00127                       ? dictPath/coordinateSystems::typeName
00128                       : dictPath
00129                     ),
00130                     runTime,
00131                     IOobject::MUST_READ,
00132                     IOobject::NO_WRITE,
00133                     false
00134                 )
00135             );
00136         }
00137         else
00138         {
00139             csDictIoPtr.set
00140             (
00141                 new IOobject
00142                 (
00143                     coordinateSystems::typeName,
00144                     runTime.constant(),
00145                     runTime,
00146                     IOobject::MUST_READ,
00147                     IOobject::NO_WRITE,
00148                     false
00149                 )
00150             );
00151         }
00152 
00153 
00154         if (!csDictIoPtr->headerOk())
00155         {
00156             FatalErrorIn(args.executable())
00157                 << "Cannot open coordinateSystems file\n    "
00158                 << csDictIoPtr->objectPath() << nl
00159                 << exit(FatalError);
00160         }
00161 
00162         coordinateSystems csLst(csDictIoPtr());
00163 
00164         if (args.optionFound("from"))
00165         {
00166             const word csName(args.option("from"));
00167 
00168             label csId = csLst.find(csName);
00169             if (csId < 0)
00170             {
00171                 FatalErrorIn(args.executable())
00172                     << "Cannot find -from " << csName << nl
00173                     << "available coordinateSystems: " << csLst.toc() << nl
00174                     << exit(FatalError);
00175             }
00176 
00177             fromCsys.reset(new coordinateSystem(csLst[csId]));
00178         }
00179 
00180         if (args.optionFound("to"))
00181         {
00182             const word csName(args.option("to"));
00183 
00184             label csId = csLst.find(csName);
00185             if (csId < 0)
00186             {
00187                 FatalErrorIn(args.executable())
00188                     << "Cannot find -to " << csName << nl
00189                     << "available coordinateSystems: " << csLst.toc() << nl
00190                     << exit(FatalError);
00191             }
00192 
00193             toCsys.reset(new coordinateSystem(csLst[csId]));
00194         }
00195 
00196 
00197         // maybe fix this later
00198         if (fromCsys.valid() && toCsys.valid())
00199         {
00200             FatalErrorIn(args.executable())
00201                 << "Only allowed  '-from' or '-to' option at the moment."
00202                 << exit(FatalError);
00203         }
00204     }
00205 
00206 
00207     {
00208         MeshedSurface<face> surf(importName);
00209 
00210         if (args.optionFound("clean"))
00211         {
00212             surf.cleanup(true);
00213         }
00214 
00215         scalar scaleIn = 0;
00216         if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
00217         {
00218             Info<< " -scaleIn " << scaleIn << endl;
00219             surf.scalePoints(scaleIn);
00220         }
00221 
00222 
00223         if (fromCsys.valid())
00224         {
00225             Info<< " -from " << fromCsys().name() << endl;
00226             tmp<pointField> tpf = fromCsys().localPosition(surf.points());
00227             surf.movePoints(tpf());
00228         }
00229 
00230         if (toCsys.valid())
00231         {
00232             Info<< " -to " << toCsys().name() << endl;
00233             tmp<pointField> tpf = toCsys().globalPosition(surf.points());
00234             surf.movePoints(tpf());
00235         }
00236 
00237         scalar scaleOut = 0;
00238         if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
00239         {
00240             Info<< " -scaleOut " << scaleOut << endl;
00241             surf.scalePoints(scaleOut);
00242         }
00243 
00244         Info<< "writing " << exportName;
00245         surf.write(exportName);
00246     }
00247 
00248     Info<< "\nEnd\n" << endl;
00249 
00250     return 0;
00251 }
00252 
00253 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd