FreeFOAM The Cross-Platform CFD Toolkit
surfaceMeshExport.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  surfaceMeshExport
26 
27 Description
28  Export from surfMesh to various third-party surface formats with
29  optional scaling or transformations (rotate/translate) on a
30  coordinateSystem.
31 
32 Usage
33  - surfaceMeshExport outputFile [OPTION]
34 
35  @param <outputFile> \n
36  File into which to write exported surface.
37 
38  @param -clean \n
39  Perform some surface checking/cleanup on the input surface.
40 
41  @param -name <name> \n
42  Specify an alternative surface name when writing.
43 
44  @param -scaleIn <scale> \n
45  Specify a scaling factor when reading files.
46 
47  @param -scaleOut <scale> \n
48  Specify a scaling factor when writing files.
49 
50  @param -dict <dictionary> \n
51  Specify an alternative dictionary for constant/coordinateSystems.
52 
53  @param -from <coordinateSystem> \n
54  Specify a coordinate System when reading files.
55 
56  @param -to <coordinateSystem> \n
57  Specify a coordinate System when writing files.
58 
59  @param -case <dir> \n
60  Specify the case directory
61 
62  @param -help \n
63  Display short usage message
64 
65  @param -doc \n
66  Display Doxygen documentation page
67 
68  @param -srcDoc \n
69  Display source code
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("outputFile");
92  argList::validOptions.insert("name", "name");
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 exportName(params[0]);
105  word importName("default");
106  args.optionReadIfPresent("name", importName);
107 
108  // check that writing is supported
109  if (!MeshedSurface<face>::canWriteType(exportName.ext(), true))
110  {
111  return 1;
112  }
113 
114 
115  // get the coordinate transformations
116  autoPtr<coordinateSystem> fromCsys;
118 
119  if (args.optionFound("from") || args.optionFound("to"))
120  {
121  autoPtr<IOobject> ioPtr;
122 
123  if (args.optionFound("dict"))
124  {
125  fileName dictPath(args.option("dict"));
126 
127  ioPtr.set
128  (
129  new IOobject
130  (
131  (
132  isDir(dictPath)
133  ? dictPath/coordinateSystems::typeName
134  : dictPath
135  ),
136  runTime,
139  false
140  )
141  );
142  }
143  else
144  {
145  ioPtr.set
146  (
147  new IOobject
148  (
149  coordinateSystems::typeName,
150  runTime.constant(),
151  runTime,
154  false
155  )
156  );
157  }
158 
159 
160  if (!ioPtr->headerOk())
161  {
163  << "Cannot open coordinateSystems file\n "
164  << ioPtr->objectPath() << nl
165  << exit(FatalError);
166  }
167 
168  coordinateSystems csLst(ioPtr());
169 
170  if (args.optionFound("from"))
171  {
172  const word csName(args.option("from"));
173 
174  label csId = csLst.find(csName);
175  if (csId < 0)
176  {
178  << "Cannot find -from " << csName << nl
179  << "available coordinateSystems: " << csLst.toc() << nl
180  << exit(FatalError);
181  }
182 
183  fromCsys.reset(new coordinateSystem(csLst[csId]));
184  }
185 
186  if (args.optionFound("to"))
187  {
188  const word csName(args.option("to"));
189 
190  label csId = csLst.find(csName);
191  if (csId < 0)
192  {
194  << "Cannot find -to " << csName << nl
195  << "available coordinateSystems: " << csLst.toc() << nl
196  << exit(FatalError);
197  }
198 
199  toCsys.reset(new coordinateSystem(csLst[csId]));
200  }
201 
202 
203  // maybe fix this later
204  if (fromCsys.valid() && toCsys.valid())
205  {
207  << "Only allowed '-from' or '-to' option at the moment."
208  << exit(FatalError);
209  }
210  }
211 
212 
213  surfMesh smesh
214  (
215  IOobject
216  (
217  importName,
218  runTime.constant(),
219  runTime,
222  )
223  );
224 
225  Info<< "read surfMesh:\n " << smesh.objectPath() << endl;
226 
227 
228  // Simply copy for now, but really should have a separate write method
229 
230  MeshedSurface<face> surf(smesh);
231 
232  if (args.optionFound("clean"))
233  {
234  surf.cleanup(true);
235  }
236 
237  scalar scaleIn = 0;
238  if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
239  {
240  Info<< " -scaleIn " << scaleIn << endl;
241  surf.scalePoints(scaleIn);
242  }
243 
244  if (fromCsys.valid())
245  {
246  Info<< " -from " << fromCsys().name() << endl;
247  tmp<pointField> tpf = fromCsys().localPosition(surf.points());
248  surf.movePoints(tpf());
249  }
250 
251  if (toCsys.valid())
252  {
253  Info<< " -to " << toCsys().name() << endl;
254  tmp<pointField> tpf = toCsys().globalPosition(surf.points());
255  surf.movePoints(tpf());
256  }
257 
258  scalar scaleOut = 0;
259  if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
260  {
261  Info<< " -scaleOut " << scaleOut << endl;
262  surf.scalePoints(scaleOut);
263  }
264 
265 
266  surf.writeStats(Info);
267  Info<< endl;
268 
269  Info<< "writing " << exportName << endl;
270  surf.write(exportName);
271 
272  Info<< "\nEnd\n" << endl;
273 
274  return 0;
275 }
276 
277 // ************************ vim: set sw=4 sts=4 et: ************************ //