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) 2011-2015 OpenFOAM Foundation
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  Converts 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 -clean \n
35  Perform some surface checking/cleanup on the input surface.
36 
37  \param -scaleIn <scale> \n
38  Specify a scaling factor when reading files.
39 
40  \param -scaleOut <scale> \n
41  Specify a scaling factor when writing files.
42 
43  \param -dict <dictionary> \n
44  Specify an alternative dictionary for constant/coordinateSystems.
45 
46  \param -from <coordinateSystem> \n
47  Specify a coordinate System when reading files.
48 
49  \param -to <coordinateSystem> \n
50  Specify a coordinate System when writing files.
51 
52  \param -tri \n
53  Triangulate surface.
54 
55 Note
56  The filename extensions are used to determine the file format type.
57 
58 \*---------------------------------------------------------------------------*/
59 
60 #include "argList.H"
61 #include "Time.H"
62 
63 #include "MeshedSurfaces.H"
64 #include "coordinateSystems.H"
65 
66 using namespace Foam;
67 
68 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
69 
70 int main(int argc, char *argv[])
71 {
73  (
74  "convert between surface formats"
75  );
76 
78  argList::validArgs.append("inputFile");
79  argList::validArgs.append("outputFile");
80 
82  (
83  "clean",
84  "perform some surface checking/cleanup on the input surface"
85  );
87  (
88  "scaleIn",
89  "factor",
90  "geometry scaling factor on input"
91  );
93  (
94  "scaleOut",
95  "factor",
96  "geometry scaling factor on output"
97  );
98  #include "addDictOption.H"
100  (
101  "from",
102  "system",
103  "specify the source coordinate system, applied after '-scaleIn'"
104  );
106  (
107  "to",
108  "system",
109  "specify the target coordinate system, applied before '-scaleOut'"
110  );
112  (
113  "tri",
114  "triangulate surface"
115  );
116 
117 
118  argList args(argc, argv);
119  Time runTime(args.rootPath(), args.caseName());
120 
121  const fileName importName = args[1];
122  const fileName exportName = args[2];
123 
124  // disable inplace editing
125  if (importName == exportName)
126  {
128  << "Output file " << exportName << " would overwrite input file."
129  << exit(FatalError);
130  }
131 
132  // check that reading/writing is supported
133  if
134  (
135  !MeshedSurface<face>::canRead(importName, true)
136  || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
137  )
138  {
139  return 1;
140  }
141 
142 
143  // get the coordinate transformations
144  autoPtr<coordinateSystem> fromCsys;
146 
147  if (args.optionFound("from") || args.optionFound("to"))
148  {
149  autoPtr<IOobject> csDictIoPtr;
150 
151  const word dictName("coordinateSystems::typeName");
152 
153  // Note: cannot use setSystemRunTimeDictionaryIO.H since dictionary
154  // is in constant
155 
156  fileName dictPath = "";
157  if (args.optionFound("dict"))
158  {
159  dictPath = args["dict"];
160  if (isDir(dictPath))
161  {
163  }
164  }
165 
166  if (dictPath.size())
167  {
168  csDictIoPtr.set
169  (
170  new IOobject
171  (
172  dictPath,
173  runTime,
176  false
177  )
178  );
179  }
180  else
181  {
182  csDictIoPtr.set
183  (
184  new IOobject
185  (
186  dictName,
187  runTime.constant(),
188  runTime,
191  false
192  )
193  );
194  }
195 
196 
197  if (!csDictIoPtr->headerOk())
198  {
200  << csDictIoPtr->objectPath() << nl
201  << exit(FatalError);
202  }
203 
204  coordinateSystems csLst(csDictIoPtr());
205 
206  if (args.optionFound("from"))
207  {
208  const word csName = args["from"];
209 
210  const label csIndex = csLst.findIndex(csName);
211  if (csIndex < 0)
212  {
214  << "Cannot find -from " << csName << nl
215  << "available coordinateSystems: " << csLst.toc() << nl
216  << exit(FatalError);
217  }
218 
219  fromCsys.reset(new coordinateSystem(csLst[csIndex]));
220  }
221 
222  if (args.optionFound("to"))
223  {
224  const word csName = args["to"];
225 
226  const label csIndex = csLst.findIndex(csName);
227  if (csIndex < 0)
228  {
230  << "Cannot find -to " << csName << nl
231  << "available coordinateSystems: " << csLst.toc() << nl
232  << exit(FatalError);
233  }
234 
235  toCsys.reset(new coordinateSystem(csLst[csIndex]));
236  }
237 
238 
239  // maybe fix this later
240  if (fromCsys.valid() && toCsys.valid())
241  {
243  << exit(FatalError);
244  }
245  }
246 
247 
248  {
249  MeshedSurface<face> surf(importName);
250 
251  if (args.optionFound("clean"))
252  {
253  surf.cleanup(true);
254  }
255 
256  scalar scaleIn = 0;
257  if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
258  {
259  Info<< " -scaleIn " << scaleIn << endl;
260  surf.scalePoints(scaleIn);
261  }
262 
263 
264  if (fromCsys.valid())
265  {
266  Info<< " -from " << fromCsys().name() << endl;
267  tmp<pointField> tpf = fromCsys().localPosition(surf.points());
268  surf.movePoints(tpf());
269  }
270 
271  if (toCsys.valid())
272  {
273  Info<< " -to " << toCsys().name() << endl;
274  tmp<pointField> tpf = toCsys().globalPosition(surf.points());
275  surf.movePoints(tpf());
276  }
277 
278  scalar scaleOut = 0;
279  if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
280  {
281  Info<< " -scaleOut " << scaleOut << endl;
282  surf.scalePoints(scaleOut);
283  }
284 
285  if (args.optionFound("tri"))
286  {
287  Info<< "triangulate" << endl;
288  surf.triangulate();
289  }
290 
291  Info<< "writing " << exportName;
292  surf.write(exportName);
293  }
294 
295  Info<< "\nEnd\n" << endl;
296 
297  return 0;
298 }
299 
300 // ************************************************************************* //
Foam::argList::validArgs
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:143
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::argList::addOption
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:108
Foam::argList::addNote
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:139
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:118
Foam::argList::addBoolOption
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:98
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:97
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:108
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::argList::rootPath
const fileName & rootPath() const
Return root path.
Definition: argListI.H:36
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
addDictOption.H
dictName
const word dictName("particleTrackDict")
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
argList.H
Foam::autoPtr::set
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
main
int main(int argc, char *argv[])
Definition: postCalc.C:54
Foam::FatalError
error FatalError
Foam::coordinateSystems
Provides a centralized coordinateSystem collection.
Definition: coordinateSystems.H:71
Foam::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:329
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::isDir
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:615
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::autoPtr::valid
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set).
Definition: autoPtrI.H:83
MeshedSurfaces.H
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Foam::autoPtr::reset
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
Foam::argList::caseName
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:42
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:161
Foam::MeshedSurface
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: MeshedSurface.H:72
args
Foam::argList args(argc, argv)
dictPath
fileName dictPath
Definition: setConstantMeshDictionaryIO.H:5
Foam::argList::optionReadIfPresent
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
coordinateSystems.H
Foam::coordinateSystem
Base class for other coordinate system specifications.
Definition: coordinateSystem.H:85