FreeFOAM The Cross-Platform CFD Toolkit
surfaceMeshConvert.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Application
25  surfaceMeshConvert
26 
27 Description
28  Convert between surface formats with optional scaling or
29  transformations (rotate/translate) on a coordinateSystem.
30 
31 Usage
32  - surfaceMeshConvert inputFile outputFile [OPTION]
33 
34  @param <inputfile> \n
35  Surface file to be converted.
36 
37  @param <outputFile> \n
38  File into which to write the converted surface.
39 
40  @param -clean \n
41  Perform some surface checking/cleanup on the input surface.
42 
43  @param -scaleIn <scale> \n
44  Specify a scaling factor when reading files.
45 
46  @param -scaleOut <scale> \n
47  Specify a scaling factor when writing files.
48 
49  @param -dict <dictionary> \n
50  Specify an alternative dictionary for constant/coordinateSystems.
51 
52  @param -from <coordinateSystem> \n
53  Specify a coordinate System when reading files.
54 
55  @param -to <coordinateSystem> \n
56  Specify a coordinate System when writing files.
57 
58  @param -case <dir> \n
59  Specify the case directory
60 
61  @param -help \n
62  Display short usage message
63 
64  @param -doc \n
65  Display Doxygen documentation page
66 
67  @param -srcDoc \n
68  Display source code
69 
70 
71 Note
72  The filename extensions are used to determine the file format type.
73 
74 \*---------------------------------------------------------------------------*/
75 
76 #include <OpenFOAM/argList.H>
77 #include <OpenFOAM/timeSelector.H>
78 #include <OpenFOAM/Time.H>
79 
82 
83 using namespace Foam;
84 
85 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
86 // Main program:
87 
88 int main(int argc, char *argv[])
89 {
91  argList::validArgs.append("inputFile");
92  argList::validArgs.append("outputFile");
93  argList::validOptions.insert("clean", "");
94  argList::validOptions.insert("scaleIn", "scale");
95  argList::validOptions.insert("scaleOut", "scale");
96  argList::validOptions.insert("dict", "coordinateSystemsDict");
97  argList::validOptions.insert("from", "sourceCoordinateSystem");
98  argList::validOptions.insert("to", "targetCoordinateSystem");
99 
100  argList args(argc, argv);
101  Time runTime(args.rootPath(), args.caseName());
102  const stringList& params = args.additionalArgs();
103 
104  fileName importName(params[0]);
105  fileName exportName(params[1]);
106 
107  // disable inplace editing
108  if (importName == exportName)
109  {
111  << "Output file " << exportName << " would overwrite input file."
112  << exit(FatalError);
113  }
114 
115  // check that reading/writing is supported
116  if
117  (
118  !MeshedSurface<face>::canRead(importName, true)
119  || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
120  )
121  {
122  return 1;
123  }
124 
125 
126  // get the coordinate transformations
127  autoPtr<coordinateSystem> fromCsys;
129 
130  if (args.optionFound("from") || args.optionFound("to"))
131  {
132  autoPtr<IOobject> csDictIoPtr;
133 
134  if (args.optionFound("dict"))
135  {
136  fileName dictPath(args.option("dict"));
137 
138  csDictIoPtr.set
139  (
140  new IOobject
141  (
142  (
143  isDir(dictPath)
144  ? dictPath/coordinateSystems::typeName
145  : dictPath
146  ),
147  runTime,
150  false
151  )
152  );
153  }
154  else
155  {
156  csDictIoPtr.set
157  (
158  new IOobject
159  (
160  coordinateSystems::typeName,
161  runTime.constant(),
162  runTime,
165  false
166  )
167  );
168  }
169 
170 
171  if (!csDictIoPtr->headerOk())
172  {
174  << "Cannot open coordinateSystems file\n "
175  << csDictIoPtr->objectPath() << nl
176  << exit(FatalError);
177  }
178 
179  coordinateSystems csLst(csDictIoPtr());
180 
181  if (args.optionFound("from"))
182  {
183  const word csName(args.option("from"));
184 
185  label csId = csLst.find(csName);
186  if (csId < 0)
187  {
189  << "Cannot find -from " << csName << nl
190  << "available coordinateSystems: " << csLst.toc() << nl
191  << exit(FatalError);
192  }
193 
194  fromCsys.reset(new coordinateSystem(csLst[csId]));
195  }
196 
197  if (args.optionFound("to"))
198  {
199  const word csName(args.option("to"));
200 
201  label csId = csLst.find(csName);
202  if (csId < 0)
203  {
205  << "Cannot find -to " << csName << nl
206  << "available coordinateSystems: " << csLst.toc() << nl
207  << exit(FatalError);
208  }
209 
210  toCsys.reset(new coordinateSystem(csLst[csId]));
211  }
212 
213 
214  // maybe fix this later
215  if (fromCsys.valid() && toCsys.valid())
216  {
218  << "Only allowed '-from' or '-to' option at the moment."
219  << exit(FatalError);
220  }
221  }
222 
223 
224  {
225  MeshedSurface<face> surf(importName);
226 
227  if (args.optionFound("clean"))
228  {
229  surf.cleanup(true);
230  }
231 
232  scalar scaleIn = 0;
233  if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
234  {
235  Info<< " -scaleIn " << scaleIn << endl;
236  surf.scalePoints(scaleIn);
237  }
238 
239 
240  if (fromCsys.valid())
241  {
242  Info<< " -from " << fromCsys().name() << endl;
243  tmp<pointField> tpf = fromCsys().localPosition(surf.points());
244  surf.movePoints(tpf());
245  }
246 
247  if (toCsys.valid())
248  {
249  Info<< " -to " << toCsys().name() << endl;
250  tmp<pointField> tpf = toCsys().globalPosition(surf.points());
251  surf.movePoints(tpf());
252  }
253 
254  scalar scaleOut = 0;
255  if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
256  {
257  Info<< " -scaleOut " << scaleOut << endl;
258  surf.scalePoints(scaleOut);
259  }
260 
261  Info<< "writing " << exportName;
262  surf.write(exportName);
263  }
264 
265  Info<< "\nEnd\n" << endl;
266 
267  return 0;
268 }
269 
270 // ************************ vim: set sw=4 sts=4 et: ************************ //