FreeFOAM The Cross-Platform CFD Toolkit
surfaceMeshImport.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  surfaceMeshImport
26 
27 Description
28  Import from various third-party surface formats into surfMesh
29  with optional scaling or transformations (rotate/translate)
30  on a coordinateSystem.
31 
32 Usage
33  - surfaceMeshImport inputFile [OPTION]
34 
35  @param <inputFile> \n
36  File from which to read the imported 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("inputFile");
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 # include <OpenFOAM/setRootCase.H>
101 # include <OpenFOAM/createTime.H>
102 
103  const stringList& params = args.additionalArgs();
104 
105  // try for the latestTime, but create "constant" as needed
106  instantList Times = runTime.times();
107  if (Times.size())
108  {
109  label startTime = Times.size()-1;
110  runTime.setTime(Times[startTime], startTime);
111  }
112  else
113  {
114  runTime.setTime(instant(0, runTime.constant()), 0);
115  }
116 
117 
118  fileName importName(params[0]);
119  word exportName("default");
120  args.optionReadIfPresent("name", exportName);
121 
122  // check that reading is supported
123  if (!MeshedSurface<face>::canRead(importName, true))
124  {
125  return 1;
126  }
127 
128 
129  // get the coordinate transformations
130  autoPtr<coordinateSystem> fromCsys;
132 
133  if (args.optionFound("from") || args.optionFound("to"))
134  {
135  autoPtr<IOobject> ioPtr;
136 
137  if (args.optionFound("dict"))
138  {
139  fileName dictPath(args.option("dict"));
140 
141  ioPtr.set
142  (
143  new IOobject
144  (
145  (
146  isDir(dictPath)
147  ? dictPath/coordinateSystems::typeName
148  : dictPath
149  ),
150  runTime,
153  false
154  )
155  );
156  }
157  else
158  {
159  ioPtr.set
160  (
161  new IOobject
162  (
163  coordinateSystems::typeName,
164  runTime.constant(),
165  runTime,
168  false
169  )
170  );
171  }
172 
173 
174  if (!ioPtr->headerOk())
175  {
177  << "Cannot open coordinateSystems file\n "
178  << ioPtr->objectPath() << nl
179  << exit(FatalError);
180  }
181 
182  coordinateSystems csLst(ioPtr());
183 
184  if (args.optionFound("from"))
185  {
186  const word csName(args.option("from"));
187 
188  label csId = csLst.find(csName);
189  if (csId < 0)
190  {
192  << "Cannot find -from " << csName << nl
193  << "available coordinateSystems: " << csLst.toc() << nl
194  << exit(FatalError);
195  }
196 
197  fromCsys.reset(new coordinateSystem(csLst[csId]));
198  }
199 
200  if (args.optionFound("to"))
201  {
202  const word csName(args.option("to"));
203 
204  label csId = csLst.find(csName);
205  if (csId < 0)
206  {
208  << "Cannot find -to " << csName << nl
209  << "available coordinateSystems: " << csLst.toc() << nl
210  << exit(FatalError);
211  }
212 
213  toCsys.reset(new coordinateSystem(csLst[csId]));
214  }
215 
216 
217  // maybe fix this later
218  if (fromCsys.valid() && toCsys.valid())
219  {
221  << "Only allowed '-from' or '-to' option at the moment."
222  << exit(FatalError);
223  }
224  }
225 
226 
227 
228  MeshedSurface<face> surf(importName);
229 
230  if (args.optionFound("clean"))
231  {
232  surf.cleanup(true);
233  }
234 
235 
236  scalar scaleIn = 0;
237  if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
238  {
239  Info<< " -scaleIn " << scaleIn << endl;
240  surf.scalePoints(scaleIn);
241  }
242 
243  if (fromCsys.valid())
244  {
245  Info<< " -from " << fromCsys().name() << endl;
246  tmp<pointField> tpf = fromCsys().localPosition(surf.points());
247  surf.movePoints(tpf());
248  }
249 
250  if (toCsys.valid())
251  {
252  Info<< " -to " << toCsys().name() << endl;
253  tmp<pointField> tpf = toCsys().globalPosition(surf.points());
254  surf.movePoints(tpf());
255  }
256 
257  scalar scaleOut = 0;
258  if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
259  {
260  Info<< " -scaleOut " << scaleOut << endl;
261  surf.scalePoints(scaleOut);
262  }
263 
264  surfMesh smesh
265  (
266  IOobject
267  (
268  exportName,
269  runTime.constant(),
270  runTime
271  ),
272  surf.xfer()
273  );
274 
275 
276  Info<< "writing surfMesh:\n " << smesh.objectPath() << endl;
277  smesh.write();
278 
279  Info<< "\nEnd\n" << endl;
280 
281  return 0;
282 }
283 
284 // ************************ vim: set sw=4 sts=4 et: ************************ //