initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / surface / surfaceMeshImport / surfaceMeshImport.C
blob808eefcd76c0104f5243fe99bc398cf4e2f7023d
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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 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     surfaceMeshImport
28 Description
29     Import from various third-party surface formats into surfMesh
30     with optional scaling or transformations (rotate/translate)
31     on a coordinateSystem.
33 Usage
34     - surfaceMeshImport inputFile [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 "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 // ************************************************************************* //