Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / applications / utilities / surface / surfaceMeshConvert / surfaceMeshConvert.C
blobab112c4d9c3ea22af1827de731e55455aa7d0f2e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2009-2011 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
24 Application
25     surfaceMeshConvert
27 Description
28     Convert between surface formats with optional scaling or
29     transformations (rotate/translate) on a coordinateSystem.
31 Usage
32     - surfaceMeshConvert inputFile outputFile [OPTION]
34     \param -clean \n
35     Perform some surface checking/cleanup on the input surface.
37     \param -scaleIn \<scale\> \n
38     Specify a scaling factor when reading files.
40     \param -scaleOut \<scale\> \n
41     Specify a scaling factor when writing files.
43     \param -dict \<dictionary\> \n
44     Specify an alternative dictionary for constant/coordinateSystems.
46     \param -from \<coordinateSystem\> \n
47     Specify a coordinate System when reading files.
49     \param -to \<coordinateSystem\> \n
50     Specify a coordinate System when writing files.
52     \param -tri \n
53     Triangulate surface.
55 Note
56     The filename extensions are used to determine the file format type.
58 \*---------------------------------------------------------------------------*/
60 #include "argList.H"
61 #include "timeSelector.H"
62 #include "Time.H"
64 #include "MeshedSurfaces.H"
65 #include "coordinateSystems.H"
67 using namespace Foam;
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70 //  Main program:
72 int main(int argc, char *argv[])
74     argList::addNote
75     (
76         "convert between surface formats"
77     );
79     argList::noParallel();
80     argList::validArgs.append("inputFile");
81     argList::validArgs.append("outputFile");
83     argList::addBoolOption
84     (
85         "clean",
86         "perform some surface checking/cleanup on the input surface"
87     );
88     argList::addOption
89     (
90         "scaleIn",
91         "factor",
92         "geometry scaling factor on input"
93     );
94     argList::addOption
95     (
96         "scaleOut",
97         "factor",
98         "geometry scaling factor on output"
99     );
100     argList::addOption
101     (
102         "dict",
103         "file",
104         "specify alternative dictionary for the coordinateSystems descriptions"
105     );
106     argList::addOption
107     (
108         "from",
109         "system",
110         "specify the source coordinate system, applied after '-scaleIn'"
111     );
112     argList::addOption
113     (
114         "to",
115         "system",
116         "specify the target coordinate system, applied before '-scaleOut'"
117     );
118     argList::addBoolOption
119     (
120         "tri",
121         "triangulate surface"
122     );
125     argList args(argc, argv);
126     Time runTime(args.rootPath(), args.caseName());
128     const fileName importName = args[1];
129     const fileName exportName = args[2];
131     // disable inplace editing
132     if (importName == exportName)
133     {
134         FatalErrorIn(args.executable())
135             << "Output file " << exportName << " would overwrite input file."
136             << exit(FatalError);
137     }
139     // check that reading/writing is supported
140     if
141     (
142         !MeshedSurface<face>::canRead(importName, true)
143      || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
144     )
145     {
146         return 1;
147     }
150     // get the coordinate transformations
151     autoPtr<coordinateSystem> fromCsys;
152     autoPtr<coordinateSystem> toCsys;
154     if (args.optionFound("from") || args.optionFound("to"))
155     {
156         autoPtr<IOobject> csDictIoPtr;
158         if (args.optionFound("dict"))
159         {
160             const fileName dictPath = args["dict"];
162             csDictIoPtr.set
163             (
164                 new IOobject
165                 (
166                     (
167                         isDir(dictPath)
168                       ? dictPath/coordinateSystems::typeName
169                       : dictPath
170                     ),
171                     runTime,
172                     IOobject::MUST_READ,
173                     IOobject::NO_WRITE,
174                     false
175                 )
176             );
177         }
178         else
179         {
180             csDictIoPtr.set
181             (
182                 new IOobject
183                 (
184                     coordinateSystems::typeName,
185                     runTime.constant(),
186                     runTime,
187                     IOobject::MUST_READ,
188                     IOobject::NO_WRITE,
189                     false
190                 )
191             );
192         }
195         if (!csDictIoPtr->headerOk())
196         {
197             FatalErrorIn(args.executable())
198                 << "Cannot open coordinateSystems file\n    "
199                 << csDictIoPtr->objectPath() << nl
200                 << exit(FatalError);
201         }
203         coordinateSystems csLst(csDictIoPtr());
205         if (args.optionFound("from"))
206         {
207             const word csName = args["from"];
209             const label csIndex = csLst.findIndex(csName);
210             if (csIndex < 0)
211             {
212                 FatalErrorIn(args.executable())
213                     << "Cannot find -from " << csName << nl
214                     << "available coordinateSystems: " << csLst.toc() << nl
215                     << exit(FatalError);
216             }
218             fromCsys.reset(new coordinateSystem(csLst[csIndex]));
219         }
221         if (args.optionFound("to"))
222         {
223             const word csName = args["to"];
225             const label csIndex = csLst.findIndex(csName);
226             if (csIndex < 0)
227             {
228                 FatalErrorIn(args.executable())
229                     << "Cannot find -to " << csName << nl
230                     << "available coordinateSystems: " << csLst.toc() << nl
231                     << exit(FatalError);
232             }
234             toCsys.reset(new coordinateSystem(csLst[csIndex]));
235         }
238         // maybe fix this later
239         if (fromCsys.valid() && toCsys.valid())
240         {
241             FatalErrorIn(args.executable())
242                 << "Only allowed  '-from' or '-to' option at the moment."
243                 << exit(FatalError);
244         }
245     }
248     {
249         MeshedSurface<face> surf(importName);
251         if (args.optionFound("clean"))
252         {
253             surf.cleanup(true);
254         }
256         scalar scaleIn = 0;
257         if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
258         {
259             Info<< " -scaleIn " << scaleIn << endl;
260             surf.scalePoints(scaleIn);
261         }
264         if (fromCsys.valid())
265         {
266             Info<< " -from " << fromCsys().name() << endl;
267             tmp<pointField> tpf = fromCsys().localPosition(surf.points());
268             surf.movePoints(tpf());
269         }
271         if (toCsys.valid())
272         {
273             Info<< " -to " << toCsys().name() << endl;
274             tmp<pointField> tpf = toCsys().globalPosition(surf.points());
275             surf.movePoints(tpf());
276         }
278         scalar scaleOut = 0;
279         if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
280         {
281             Info<< " -scaleOut " << scaleOut << endl;
282             surf.scalePoints(scaleOut);
283         }
285         if (args.optionFound("tri"))
286         {
287             Info<< "triangulate" << endl;
288             surf.triangulate();
289         }
291         Info<< "writing " << exportName;
292         surf.write(exportName);
293     }
295     Info<< "\nEnd\n" << endl;
297     return 0;
300 // ************************************************************************* //