Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / applications / utilities / surface / surfaceMeshConvert / surfaceMeshConvert.C
blob7ed64ab105fc49ae19f495d3746b50df9f12597a
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Application
25     surfaceMeshConvert
27 Description
29     Convert between surface formats with optional scaling or
30     transformations (rotate/translate) on a coordinateSystem.
32 Usage
33     - surfaceMeshConvert inputFile outputFile [OPTION]
35     @param -clean \n
36     Perform some surface checking/cleanup on the input surface.
38     @param -scaleIn \<scale\> \n
39     Specify a scaling factor when reading files.
41     @param -scaleOut \<scale\> \n
42     Specify a scaling factor when writing files.
44     @param -dict \<dictionary\> \n
45     Specify an alternative dictionary for constant/coordinateSystems.
47     @param -from \<coordinateSystem\> \n
48     Specify a coordinate System when reading files.
50     @param -to \<coordinateSystem\> \n
51     Specify a coordinate System when writing files.
53 Note
54     The filename extensions are used to determine the file format type.
56 \*---------------------------------------------------------------------------*/
58 #include "argList.H"
59 #include "timeSelector.H"
60 #include "objectRegistry.H"
61 #include "foamTime.H"
62 #include "MeshedSurfaces.H"
63 #include "coordinateSystems.H"
65 using namespace Foam;
67 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
68 //  Main program:
70 int main(int argc, char *argv[])
72     argList::noParallel();
73     argList::validArgs.append("inputFile");
74     argList::validArgs.append("outputFile");
75     argList::validOptions.insert("clean", "");
76     argList::validOptions.insert("scaleIn",  "scale");
77     argList::validOptions.insert("scaleOut", "scale");
78     argList::validOptions.insert("dict", "coordinateSystemsDict");
79     argList::validOptions.insert("from", "sourceCoordinateSystem");
80     argList::validOptions.insert("to",   "targetCoordinateSystem");
82     argList args(argc, argv);
83     Time runTime(args.rootPath(), args.caseName());
84     const stringList& params = args.additionalArgs();
86     fileName importName(params[0]);
87     fileName exportName(params[1]);
89     // disable inplace editing
90     if (importName == exportName)
91     {
92         FatalErrorIn(args.executable())
93             << "Output file " << exportName << " would overwrite input file."
94             << exit(FatalError);
95     }
97     // check that reading/writing is supported
98     if
99     (
100         !MeshedSurface<face>::canRead(importName, true)
101      || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
102     )
103     {
104         return 1;
105     }
108     // get the coordinate transformations
109     autoPtr<coordinateSystem> fromCsys;
110     autoPtr<coordinateSystem> toCsys;
112     if (args.optionFound("from") || args.optionFound("to"))
113     {
114         autoPtr<IOobject> csDictIoPtr;
116         if (args.optionFound("dict"))
117         {
118             fileName dictPath(args.option("dict"));
120             csDictIoPtr.set
121             (
122                 new IOobject
123                 (
124                     (
125                         isDir(dictPath)
126                       ? dictPath/coordinateSystems::typeName
127                       : dictPath
128                     ),
129                     runTime,
130                     IOobject::MUST_READ,
131                     IOobject::NO_WRITE,
132                     false
133                 )
134             );
135         }
136         else
137         {
138             csDictIoPtr.set
139             (
140                 new IOobject
141                 (
142                     coordinateSystems::typeName,
143                     runTime.constant(),
144                     runTime,
145                     IOobject::MUST_READ,
146                     IOobject::NO_WRITE,
147                     false
148                 )
149             );
150         }
153         if (!csDictIoPtr->headerOk())
154         {
155             FatalErrorIn(args.executable())
156                 << "Cannot open coordinateSystems file\n    "
157                 << csDictIoPtr->objectPath() << nl
158                 << exit(FatalError);
159         }
161         coordinateSystems csLst(csDictIoPtr());
163         if (args.optionFound("from"))
164         {
165             const word csName(args.option("from"));
167             label csId = csLst.find(csName);
168             if (csId < 0)
169             {
170                 FatalErrorIn(args.executable())
171                     << "Cannot find -from " << csName << nl
172                     << "available coordinateSystems: " << csLst.toc() << nl
173                     << exit(FatalError);
174             }
176             fromCsys.reset(new coordinateSystem(csLst[csId]));
177         }
179         if (args.optionFound("to"))
180         {
181             const word csName(args.option("to"));
183             label csId = csLst.find(csName);
184             if (csId < 0)
185             {
186                 FatalErrorIn(args.executable())
187                     << "Cannot find -to " << csName << nl
188                     << "available coordinateSystems: " << csLst.toc() << nl
189                     << exit(FatalError);
190             }
192             toCsys.reset(new coordinateSystem(csLst[csId]));
193         }
196         // maybe fix this later
197         if (fromCsys.valid() && toCsys.valid())
198         {
199             FatalErrorIn(args.executable())
200                 << "Only allowed  '-from' or '-to' option at the moment."
201                 << exit(FatalError);
202         }
203     }
206     {
207         MeshedSurface<face> surf(importName);
209         if (args.optionFound("clean"))
210         {
211             surf.cleanup(true);
212         }
214         scalar scaleIn = 0;
215         if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
216         {
217             Info<< " -scaleIn " << scaleIn << endl;
218             surf.scalePoints(scaleIn);
219         }
222         if (fromCsys.valid())
223         {
224             Info<< " -from " << fromCsys().name() << endl;
225             tmp<pointField> tpf = fromCsys().localPosition(surf.points());
226             surf.movePoints(tpf());
227         }
229         if (toCsys.valid())
230         {
231             Info<< " -to " << toCsys().name() << endl;
232             tmp<pointField> tpf = toCsys().globalPosition(surf.points());
233             surf.movePoints(tpf());
234         }
236         scalar scaleOut = 0;
237         if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
238         {
239             Info<< " -scaleOut " << scaleOut << endl;
240             surf.scalePoints(scaleOut);
241         }
243         Info<< "writing " << exportName;
244         surf.write(exportName);
245     }
247     Info<< "\nEnd\n" << endl;
249     return 0;
252 // ************************************************************************* //