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) 2011-2016 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  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  \b surfaceMeshImport inputFile [OPTION]
34 
35  Options:
36  - \par -clean \n
37  Perform some surface checking/cleanup on the input surface.
38 
39  - \par -name <name> \n
40  Specify an alternative surface name when writing.
41 
42  - \par -scaleIn <scale> \n
43  Specify a scaling factor when reading files.
44 
45  - \par -scaleOut <scale> \n
46  Specify a scaling factor when writing files.
47 
48  - \par -dict <dictionary> \n
49  Specify an alternative dictionary for constant/coordinateSystems.
50 
51  - \par -from <coordinateSystem> \n
52  Specify a coordinate system when reading files.
53 
54  - \par -to <coordinateSystem> \n
55  Specify a coordinate system when writing files.
56 
57 Note
58  The filename extensions are used to determine the file format type.
59 
60 \*---------------------------------------------------------------------------*/
61 
62 #include "argList.H"
63 #include "Time.H"
64 
65 #include "MeshedSurfaces.H"
66 #include "coordinateSystems.H"
67 
68 using namespace Foam;
69 
70 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
71 
72 int main(int argc, char *argv[])
73 {
75  (
76  "import from various third-party surface formats into surfMesh"
77  );
78 
80  argList::validArgs.append("inputFile");
81 
83  (
84  "clean",
85  "perform some surface checking/cleanup on the input surface"
86  );
88  (
89  "name",
90  "name",
91  "specify an alternative surface name when writing - "
92  "default is 'default'"
93  );
95  (
96  "scaleIn",
97  "factor",
98  "geometry scaling factor on input - default is 1"
99  );
101  (
102  "scaleOut",
103  "factor",
104  "geometry scaling factor on output - default is 1"
105  );
106  #include "addDictOption.H"
108  (
109  "from",
110  "coordinateSystem",
111  "specify a local coordinate system when reading files."
112  );
114  (
115  "to",
116  "coordinateSystem",
117  "specify a local coordinate system when writing files."
118  );
119 
120  #include "setRootCase.H"
121  #include "createTime.H"
122 
123  // try for the latestTime, but create "constant" as needed
124  instantList Times = runTime.times();
125  if (Times.size())
126  {
127  label startTime = Times.size()-1;
128  runTime.setTime(Times[startTime], startTime);
129  }
130  else
131  {
132  runTime.setTime(instant(0, runTime.constant()), 0);
133  }
134 
135 
136  const fileName importName = args[1];
137  const word exportName = args.optionLookupOrDefault<word>("name", "default");
138 
139  // check that reading is supported
140  if (!MeshedSurface<face>::canRead(importName, true))
141  {
142  return 1;
143  }
144 
145 
146  // get the coordinate transformations
147  autoPtr<coordinateSystem> fromCsys;
149 
150  if (args.optionFound("from") || args.optionFound("to"))
151  {
152  autoPtr<IOobject> ioPtr;
153 
154  if (args.optionFound("dict"))
155  {
156  const fileName dictPath = args["dict"];
157 
158  ioPtr.set
159  (
160  new IOobject
161  (
162  (
163  isDir(dictPath)
164  ? dictPath/coordinateSystems::typeName
165  : dictPath
166  ),
167  runTime,
170  false
171  )
172  );
173  }
174  else
175  {
176  ioPtr.set
177  (
178  new IOobject
179  (
180  coordinateSystems::typeName,
181  runTime.constant(),
182  runTime,
185  false
186  )
187  );
188  }
189 
190 
191  if (!ioPtr->headerOk())
192  {
194  << ioPtr->objectPath() << nl
195  << exit(FatalError);
196  }
197 
198  coordinateSystems csLst(ioPtr());
199 
200  if (args.optionFound("from"))
201  {
202  const word csName = args["from"];
203 
204  const label csIndex = csLst.findIndex(csName);
205  if (csIndex < 0)
206  {
208  << "Cannot find -from " << csName << nl
209  << "available coordinateSystems: " << csLst.toc() << nl
210  << exit(FatalError);
211  }
212 
213  fromCsys.reset(new coordinateSystem(csLst[csIndex]));
214  }
215 
216  if (args.optionFound("to"))
217  {
218  const word csName = args["to"];
219 
220  const label csIndex = csLst.findIndex(csName);
221  if (csIndex < 0)
222  {
224  << "Cannot find -to " << csName << nl
225  << "available coordinateSystems: " << csLst.toc() << nl
226  << exit(FatalError);
227  }
228 
229  toCsys.reset(new coordinateSystem(csLst[csIndex]));
230  }
231 
232 
233  // maybe fix this later
234  if (fromCsys.valid() && toCsys.valid())
235  {
237  << exit(FatalError);
238  }
239  }
240 
241 
242 
243  MeshedSurface<face> surf(importName);
244 
245  if (args.optionFound("clean"))
246  {
247  surf.cleanup(true);
248  }
249 
250 
251  scalar scaleIn = 0;
252  if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
253  {
254  Info<< " -scaleIn " << scaleIn << endl;
255  surf.scalePoints(scaleIn);
256  }
257 
258  if (fromCsys.valid())
259  {
260  Info<< " -from " << fromCsys().name() << endl;
261  tmp<pointField> tpf = fromCsys().localPosition(surf.points());
262  surf.movePoints(tpf());
263  }
264 
265  if (toCsys.valid())
266  {
267  Info<< " -to " << toCsys().name() << endl;
268  tmp<pointField> tpf = toCsys().globalPosition(surf.points());
269  surf.movePoints(tpf());
270  }
271 
272  scalar scaleOut = 0;
273  if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
274  {
275  Info<< " -scaleOut " << scaleOut << endl;
276  surf.scalePoints(scaleOut);
277  }
278 
279  surfMesh smesh
280  (
281  IOobject
282  (
283  exportName,
284  runTime.constant(),
285  runTime
286  ),
287  surf.xfer()
288  );
289 
290 
291  Info<< "writing surfMesh:\n " << smesh.objectPath() << endl;
292  smesh.write();
293 
294  Info<< "\nEnd\n" << endl;
295 
296  return 0;
297 }
298 
299 // ************************************************************************* //
Base class for other coordinate system specifications.
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
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
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: MeshedSurface.H:72
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
Provides a centralized coordinateSystem collection.
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:486
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
A class for handling words, derived from string.
Definition: word.H:59
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:93
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set).
Definition: autoPtrI.H:83
A surface mesh consisting of general polygon faces.
Definition: surfMesh.H:55
static const char nl
Definition: Ostream.H:262
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
An instant of time. Contains the time value and name.
Definition: instant.H:64
messageStream Info
bool headerOk()
Read and check header info.
Definition: IOobject.C:400
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
virtual Ostream & write(const token &)=0
Write next token to stream.
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:83
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:124
A class for managing temporary objects.
Definition: PtrList.H:54
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Namespace for OpenFOAM.