BUGFIX: Illegal use of uninitialised value (backport)
[foam-extend-3.2.git] / applications / utilities / surface / surfaceMeshExport / surfaceMeshExport.C
blob9b4bf292aa45a3e6a1bed7a2f52602450ad5ed02
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Application
26     surfaceMeshExport
28 Description
29     Export from surfMesh to various third-party surface formats with
30     optional scaling or transformations (rotate/translate) on a
31     coordinateSystem.
33 Usage
34     - surfaceMeshExport outputFile [OPTION]
36     @param -clean \n
37     Perform some surface checking/cleanup on the input surface.
39     @param -name \<name\> \n
40     Specify an alternative surface name when writing.
42     @param -scaleIn \<scale\> \n
43     Specify a scaling factor when reading files.
45     @param -scaleOut \<scale\> \n
46     Specify a scaling factor when writing files.
48     @param -dict \<dictionary\> \n
49     Specify an alternative dictionary for constant/coordinateSystems.
51     @param -from \<coordinateSystem\> \n
52     Specify a coordinate System when reading files.
54     @param -to \<coordinateSystem\> \n
55     Specify a coordinate System when writing files.
57 Note
58     The filename extensions are used to determine the file format type.
60 \*---------------------------------------------------------------------------*/
62 #include "argList.H"
63 #include "timeSelector.H"
64 #include "objectRegistry.H"
65 #include "Time.H"
67 #include "MeshedSurfaces.H"
68 #include "coordinateSystems.H"
70 using namespace Foam;
72 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
73 //  Main program:
75 int main(int argc, char *argv[])
77     argList::noParallel();
78     argList::validArgs.append("outputFile");
79     argList::validOptions.insert("name",  "name");
80     argList::validOptions.insert("clean", "");
81     argList::validOptions.insert("scaleIn",  "scale");
82     argList::validOptions.insert("scaleOut", "scale");
83     argList::validOptions.insert("dict", "coordinateSystemsDict");
84     argList::validOptions.insert("from", "sourceCoordinateSystem");
85     argList::validOptions.insert("to",   "targetCoordinateSystem");
87     argList args(argc, argv);
88     Time runTime(args.rootPath(), args.caseName());
89     const stringList& params = args.additionalArgs();
91     fileName exportName(params[0]);
92     word importName("default");
93     args.optionReadIfPresent("name", importName);
95     // check that writing is supported
96     if (!MeshedSurface<face>::canWriteType(exportName.ext(), true))
97     {
98         return 1;
99     }
102     // get the coordinate transformations
103     autoPtr<coordinateSystem> fromCsys;
104     autoPtr<coordinateSystem> toCsys;
106     if (args.optionFound("from") || args.optionFound("to"))
107     {
108         autoPtr<IOobject> ioPtr;
110         if (args.optionFound("dict"))
111         {
112             fileName dictPath(args.option("dict"));
114             ioPtr.set
115             (
116                 new IOobject
117                 (
118                     (
119                         isDir(dictPath)
120                       ? dictPath/coordinateSystems::typeName
121                       : dictPath
122                     ),
123                     runTime,
124                     IOobject::MUST_READ,
125                     IOobject::NO_WRITE,
126                     false
127                 )
128             );
129         }
130         else
131         {
132             ioPtr.set
133             (
134                 new IOobject
135                 (
136                     coordinateSystems::typeName,
137                     runTime.constant(),
138                     runTime,
139                     IOobject::MUST_READ,
140                     IOobject::NO_WRITE,
141                     false
142                 )
143             );
144         }
147         if (!ioPtr->headerOk())
148         {
149             FatalErrorIn(args.executable())
150                 << "Cannot open coordinateSystems file\n    "
151                 << ioPtr->objectPath() << nl
152                 << exit(FatalError);
153         }
155         coordinateSystems csLst(ioPtr());
157         if (args.optionFound("from"))
158         {
159             const word csName(args.option("from"));
161             label csId = csLst.find(csName);
162             if (csId < 0)
163             {
164                 FatalErrorIn(args.executable())
165                     << "Cannot find -from " << csName << nl
166                     << "available coordinateSystems: " << csLst.toc() << nl
167                     << exit(FatalError);
168             }
170             fromCsys.reset(new coordinateSystem(csLst[csId]));
171         }
173         if (args.optionFound("to"))
174         {
175             const word csName(args.option("to"));
177             label csId = csLst.find(csName);
178             if (csId < 0)
179             {
180                 FatalErrorIn(args.executable())
181                     << "Cannot find -to " << csName << nl
182                     << "available coordinateSystems: " << csLst.toc() << nl
183                     << exit(FatalError);
184             }
186             toCsys.reset(new coordinateSystem(csLst[csId]));
187         }
190         // maybe fix this later
191         if (fromCsys.valid() && toCsys.valid())
192         {
193             FatalErrorIn(args.executable())
194                 << "Only allowed  '-from' or '-to' option at the moment."
195                 << exit(FatalError);
196         }
197     }
200     surfMesh smesh
201     (
202         IOobject
203         (
204             importName,
205             runTime.constant(),
206             runTime,
207             IOobject::MUST_READ,
208             IOobject::NO_WRITE
209         )
210     );
212     Info<< "read surfMesh:\n  " << smesh.objectPath() << endl;
215     // Simply copy for now, but really should have a separate write method
217     MeshedSurface<face> surf(smesh);
219     if (args.optionFound("clean"))
220     {
221         surf.cleanup(true);
222     }
224     scalar scaleIn = 0;
225     if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
226     {
227         Info<< " -scaleIn " << scaleIn << endl;
228         surf.scalePoints(scaleIn);
229     }
231     if (fromCsys.valid())
232     {
233         Info<< " -from " << fromCsys().name() << endl;
234         tmp<pointField> tpf = fromCsys().localPosition(surf.points());
235         surf.movePoints(tpf());
236     }
238     if (toCsys.valid())
239     {
240         Info<< " -to " << toCsys().name() << endl;
241         tmp<pointField> tpf = toCsys().globalPosition(surf.points());
242         surf.movePoints(tpf());
243     }
245     scalar scaleOut = 0;
246     if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
247     {
248         Info<< " -scaleOut " << scaleOut << endl;
249         surf.scalePoints(scaleOut);
250     }
253     surf.writeStats(Info);
254     Info<< endl;
256     Info<< "writing " << exportName << endl;
257     surf.write(exportName);
259     Info<< "\nEnd\n" << endl;
261     return 0;
264 // ************************************************************************* //