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-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  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 -clean \n
36  Perform some surface checking/cleanup on the input surface.
37 
38  \param -name <name> \n
39  Specify an alternative surface name when writing.
40 
41  \param -scaleIn <scale> \n
42  Specify a scaling factor when reading files.
43 
44  \param -scaleOut <scale> \n
45  Specify a scaling factor when writing files.
46 
47  \param -dict <dictionary> \n
48  Specify an alternative dictionary for constant/coordinateSystems.
49 
50  \param -from <coordinateSystem> \n
51  Specify a coordinate system when reading files.
52 
53  \param -to <coordinateSystem> \n
54  Specify a coordinate system when writing files.
55 
56 Note
57  The filename extensions are used to determine the file format type.
58 
59 \*---------------------------------------------------------------------------*/
60 
61 #include "argList.H"
62 #include "Time.H"
63 
64 #include "MeshedSurfaces.H"
65 #include "coordinateSystems.H"
66 
67 using namespace Foam;
68 
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70 
71 int main(int argc, char *argv[])
72 {
74  (
75  "import from various third-party surface formats into surfMesh"
76  );
77 
79  argList::validArgs.append("inputFile");
80 
82  (
83  "clean",
84  "perform some surface checking/cleanup on the input surface"
85  );
87  (
88  "name",
89  "name",
90  "specify an alternative surface name when writing - "
91  "default is 'default'"
92  );
94  (
95  "scaleIn",
96  "factor",
97  "geometry scaling factor on input - default is 1"
98  );
100  (
101  "scaleOut",
102  "factor",
103  "geometry scaling factor on output - default is 1"
104  );
105  #include "addDictOption.H"
107  (
108  "from",
109  "coordinateSystem",
110  "specify a local coordinate system when reading files."
111  );
113  (
114  "to",
115  "coordinateSystem",
116  "specify a local coordinate system when writing files."
117  );
118 
119  #include "setRootCase.H"
120  #include "createTime.H"
121 
122  // try for the latestTime, but create "constant" as needed
123  instantList Times = runTime.times();
124  if (Times.size())
125  {
126  label startTime = Times.size()-1;
127  runTime.setTime(Times[startTime], startTime);
128  }
129  else
130  {
131  runTime.setTime(instant(0, runTime.constant()), 0);
132  }
133 
134 
135  const fileName importName = args[1];
136  const word exportName = args.optionLookupOrDefault<word>("name", "default");
137 
138  // check that reading is supported
139  if (!MeshedSurface<face>::canRead(importName, true))
140  {
141  return 1;
142  }
143 
144 
145  // get the coordinate transformations
146  autoPtr<coordinateSystem> fromCsys;
148 
149  if (args.optionFound("from") || args.optionFound("to"))
150  {
151  autoPtr<IOobject> ioPtr;
152 
153  if (args.optionFound("dict"))
154  {
155  const fileName dictPath = args["dict"];
156 
157  ioPtr.set
158  (
159  new IOobject
160  (
161  (
162  isDir(dictPath)
163  ? dictPath/coordinateSystems::typeName
164  : dictPath
165  ),
166  runTime,
169  false
170  )
171  );
172  }
173  else
174  {
175  ioPtr.set
176  (
177  new IOobject
178  (
179  coordinateSystems::typeName,
180  runTime.constant(),
181  runTime,
184  false
185  )
186  );
187  }
188 
189 
190  if (!ioPtr->headerOk())
191  {
193  << ioPtr->objectPath() << nl
194  << exit(FatalError);
195  }
196 
197  coordinateSystems csLst(ioPtr());
198 
199  if (args.optionFound("from"))
200  {
201  const word csName = args["from"];
202 
203  const label csIndex = csLst.findIndex(csName);
204  if (csIndex < 0)
205  {
207  << "Cannot find -from " << csName << nl
208  << "available coordinateSystems: " << csLst.toc() << nl
209  << exit(FatalError);
210  }
211 
212  fromCsys.reset(new coordinateSystem(csLst[csIndex]));
213  }
214 
215  if (args.optionFound("to"))
216  {
217  const word csName = args["to"];
218 
219  const label csIndex = csLst.findIndex(csName);
220  if (csIndex < 0)
221  {
223  << "Cannot find -to " << csName << nl
224  << "available coordinateSystems: " << csLst.toc() << nl
225  << exit(FatalError);
226  }
227 
228  toCsys.reset(new coordinateSystem(csLst[csIndex]));
229  }
230 
231 
232  // maybe fix this later
233  if (fromCsys.valid() && toCsys.valid())
234  {
236  << exit(FatalError);
237  }
238  }
239 
240 
241 
242  MeshedSurface<face> surf(importName);
243 
244  if (args.optionFound("clean"))
245  {
246  surf.cleanup(true);
247  }
248 
249 
250  scalar scaleIn = 0;
251  if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
252  {
253  Info<< " -scaleIn " << scaleIn << endl;
254  surf.scalePoints(scaleIn);
255  }
256 
257  if (fromCsys.valid())
258  {
259  Info<< " -from " << fromCsys().name() << endl;
260  tmp<pointField> tpf = fromCsys().localPosition(surf.points());
261  surf.movePoints(tpf());
262  }
263 
264  if (toCsys.valid())
265  {
266  Info<< " -to " << toCsys().name() << endl;
267  tmp<pointField> tpf = toCsys().globalPosition(surf.points());
268  surf.movePoints(tpf());
269  }
270 
271  scalar scaleOut = 0;
272  if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
273  {
274  Info<< " -scaleOut " << scaleOut << endl;
275  surf.scalePoints(scaleOut);
276  }
277 
278  surfMesh smesh
279  (
280  IOobject
281  (
282  exportName,
283  runTime.constant(),
284  runTime
285  ),
286  surf.xfer()
287  );
288 
289 
290  Info<< "writing surfMesh:\n " << smesh.objectPath() << endl;
291  smesh.write();
292 
293  Info<< "\nEnd\n" << endl;
294 
295  return 0;
296 }
297 
298 // ************************************************************************* //
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::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::surfMesh
A surface mesh consisting of general polygon faces.
Definition: surfMesh.H:55
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::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
addDictOption.H
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::Ostream::write
virtual Ostream & write(const token &)=0
Write next token to stream.
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::argList::optionLookupOrDefault
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
Foam::FatalError
error FatalError
Foam::coordinateSystems
Provides a centralized coordinateSystem collection.
Definition: coordinateSystems.H:71
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
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::autoPtr::valid
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set).
Definition: autoPtrI.H:83
MeshedSurfaces.H
createTime.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
startTime
Foam::label startTime
Definition: checkTimeOptions.H:5
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:64
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
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