Remove trailing whitespace systematically
[foam-extend-3.2.git] / applications / utilities / surface / surfaceMeshImport / surfaceMeshImport.C
blob7e089da0d097dffaf28856f56812b378086d31ff
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     |
5     \\  /    A nd           | For copyright notice see file Copyright
6      \\/     M anipulation  |
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     surfaceMeshImport
27 Description
28     Import from various third-party surface formats into surfMesh
29     with optional scaling or transformations (rotate/translate)
30     on a coordinateSystem.
32 Usage
33     - surfaceMeshImport inputFile [OPTION]
35     @param -clean \n
36     Perform some surface checking/cleanup on the input surface.
38     @param -name \<name\> \n
39     Specify an alternative surface name when writing.
41     @param -scaleIn \<scale\> \n
42     Specify a scaling factor when reading files.
44     @param -scaleOut \<scale\> \n
45     Specify a scaling factor when writing files.
47     @param -dict \<dictionary\> \n
48     Specify an alternative dictionary for constant/coordinateSystems.
50     @param -from \<coordinateSystem\> \n
51     Specify a coordinate System when reading files.
53     @param -to \<coordinateSystem\> \n
54     Specify a coordinate System when writing files.
56 Note
57     The filename extensions are used to determine the file format type.
59 \*---------------------------------------------------------------------------*/
61 #include "argList.H"
62 #include "timeSelector.H"
63 #include "objectRegistry.H"
64 #include "Time.H"
66 #include "MeshedSurfaces.H"
67 #include "coordinateSystems.H"
69 using namespace Foam;
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 //  Main program:
74 int main(int argc, char *argv[])
76     argList::noParallel();
77     argList::validArgs.append("inputFile");
78     argList::validOptions.insert("name",  "name");
79     argList::validOptions.insert("clean", "");
80     argList::validOptions.insert("scaleIn",  "scale");
81     argList::validOptions.insert("scaleOut", "scale");
82     argList::validOptions.insert("dict", "coordinateSystemsDict");
83     argList::validOptions.insert("from", "sourceCoordinateSystem");
84     argList::validOptions.insert("to",   "targetCoordinateSystem");
86 #   include "setRootCase.H"
87 #   include "createTime.H"
89     const stringList& params = args.additionalArgs();
91     // try for the latestTime, but create "constant" as needed
92     instantList Times = runTime.times();
93     if (Times.size())
94     {
95         label startTime = Times.size()-1;
96         runTime.setTime(Times[startTime], startTime);
97     }
98     else
99     {
100         runTime.setTime(instant(0, runTime.constant()), 0);
101     }
104     fileName importName(params[0]);
105     word exportName("default");
106     args.optionReadIfPresent("name", exportName);
108     // check that reading is supported
109     if (!MeshedSurface<face>::canRead(importName, true))
110     {
111         return 1;
112     }
115     // get the coordinate transformations
116     autoPtr<coordinateSystem> fromCsys;
117     autoPtr<coordinateSystem> toCsys;
119     if (args.optionFound("from") || args.optionFound("to"))
120     {
121         autoPtr<IOobject> ioPtr;
123         if (args.optionFound("dict"))
124         {
125             fileName dictPath(args.option("dict"));
127             ioPtr.set
128             (
129                 new IOobject
130                 (
131                     (
132                         isDir(dictPath)
133                       ? dictPath/coordinateSystems::typeName
134                       : dictPath
135                     ),
136                     runTime,
137                     IOobject::MUST_READ,
138                     IOobject::NO_WRITE,
139                     false
140                 )
141             );
142         }
143         else
144         {
145             ioPtr.set
146             (
147                 new IOobject
148                 (
149                     coordinateSystems::typeName,
150                     runTime.constant(),
151                     runTime,
152                     IOobject::MUST_READ,
153                     IOobject::NO_WRITE,
154                     false
155                 )
156             );
157         }
160         if (!ioPtr->headerOk())
161         {
162             FatalErrorIn(args.executable())
163                 << "Cannot open coordinateSystems file\n    "
164                 << ioPtr->objectPath() << nl
165                 << exit(FatalError);
166         }
168         coordinateSystems csLst(ioPtr());
170         if (args.optionFound("from"))
171         {
172             const word csName(args.option("from"));
174             label csId = csLst.find(csName);
175             if (csId < 0)
176             {
177                 FatalErrorIn(args.executable())
178                     << "Cannot find -from " << csName << nl
179                     << "available coordinateSystems: " << csLst.toc() << nl
180                     << exit(FatalError);
181             }
183             fromCsys.reset(new coordinateSystem(csLst[csId]));
184         }
186         if (args.optionFound("to"))
187         {
188             const word csName(args.option("to"));
190             label csId = csLst.find(csName);
191             if (csId < 0)
192             {
193                 FatalErrorIn(args.executable())
194                     << "Cannot find -to " << csName << nl
195                     << "available coordinateSystems: " << csLst.toc() << nl
196                     << exit(FatalError);
197             }
199             toCsys.reset(new coordinateSystem(csLst[csId]));
200         }
203         // maybe fix this later
204         if (fromCsys.valid() && toCsys.valid())
205         {
206             FatalErrorIn(args.executable())
207                 << "Only allowed  '-from' or '-to' option at the moment."
208                 << exit(FatalError);
209         }
210     }
214     MeshedSurface<face> surf(importName);
216     if (args.optionFound("clean"))
217     {
218         surf.cleanup(true);
219     }
222     scalar scaleIn = 0;
223     if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
224     {
225         Info<< " -scaleIn " << scaleIn << endl;
226         surf.scalePoints(scaleIn);
227     }
229     if (fromCsys.valid())
230     {
231         Info<< " -from " << fromCsys().name() << endl;
232         tmp<pointField> tpf = fromCsys().localPosition(surf.points());
233         surf.movePoints(tpf());
234     }
236     if (toCsys.valid())
237     {
238         Info<< " -to " << toCsys().name() << endl;
239         tmp<pointField> tpf = toCsys().globalPosition(surf.points());
240         surf.movePoints(tpf());
241     }
243     scalar scaleOut = 0;
244     if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
245     {
246         Info<< " -scaleOut " << scaleOut << endl;
247         surf.scalePoints(scaleOut);
248     }
250     surfMesh smesh
251     (
252         IOobject
253         (
254             exportName,
255             runTime.constant(),
256             runTime
257         ),
258         surf.xfer()
259     );
262     Info<< "writing surfMesh:\n  " << smesh.objectPath() << endl;
263     smesh.write();
265     Info<< "\nEnd\n" << endl;
267     return 0;
270 // ************************************************************************* //