surfaceRenamePatch.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2026 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  surfaceRenamePatch
26 
27 Description
28  Rename a patch in a surface geometry file. The patch can be identified by
29  name or a point close to the patch.
30 
31 Usage
32  \b surfaceRenamePatch name inputFile outputFile [OPTION]
33 
34  Options:
35  - \par -point
36  Point close to the patch
37 
38  - \par -name
39  Current name of the patch
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #include "argList.H"
44 #include "MeshedSurfaces.H"
45 #include "indexedOctree.H"
46 #include "treeDataPrimitivePatch.H"
47 
48 using namespace Foam;
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 int main(int argc, char *argv[])
53 {
54  #include "removeCaseOptions.H"
55 
56  argList::validArgs.append("new name");
57  argList::validArgs.append("input surface file");
58  argList::validArgs.append("output surface file");
59 
61  (
62  "point",
63  "point",
64  "point close to the patch; e.g., '(0 0 0.5)'"
65  );
66 
68  (
69  "name",
70  "word",
71  "current name of the patch; e.g., patch0"
72  );
73 
75  (
76  "Rename an existing patch to <new name> "
77  "in a surface geometry file.\n\n"
78  "The existing patch can be identified by one of two options:\n"
79  " -name <word>: selects the patch by name\n"
80  " -point <point>: select the nearest patch to the specified point\n\n"
81  "Examples:\n"
82  "+ To rename 'patch0' to 'inlet'\n"
83  " surfaceRenamePatch -name patch0 inlet in.obj out.obj\n"
84  "+ To rename the patch nearest to the point '(1 2 3)' to 'inlet'\n"
85  " surfaceRenamePatch -point '(1 2 3)' inlet in.obj out.obj\n"
86  );
87 
88  argList args(argc, argv);
89 
90  const word newPatchName(args.argRead<word>(1));
91  const fileName inFile(args.argRead<fileName>(2));
92  const fileName outFile(args.argRead<fileName>(3));
93 
94  if (!args.optionFound("point") && !args.optionFound("name"))
95  {
97  << "Missing option: -name or -point"
98  << exit(FatalError);
99  }
100 
101  if (args.optionFound("point") && args.optionFound("name"))
102  {
104  << "Both options provided: -patch and -point"
105  << exit(FatalError);
106  }
107 
108  Info<< "Reading surface " << inFile << nl << endl;
109 
110  meshedSurface surf(inFile);
111 
112  // Get the index of the zone ...
113  label surfZonei = -1;
114  if (args.optionFound("name"))
115  {
116  const word surfZoneName = args.optionRead<word>("name");
117 
118  // Loop through all the zones looking for the one with the given name
119  forAll(surf.surfZones(), i)
120  {
121  const surfZone& sZone = surf.surfZones()[i];
122 
123  if (sZone.name() == surfZoneName)
124  {
125  surfZonei = i;
126 
127  break;
128  }
129  }
130 
131  // Error if the zone was not found
132  if (surfZonei == -1)
133  {
135  << "Patch " << surfZoneName << " not found"
136  << exit(FatalError);
137  }
138  }
139  else // (args.optionFound("point"))
140  {
141  const point p = args.optionRead<point>("point");
142 
143  // Construct a search tree for the surface
144  typedef treeDataPrimitivePatch<meshedSurface> treeType;
145  const indexedOctree<treeType> tree
146  (
147  treeType
148  (
149  false,
150  surf,
152  ),
153  treeBoundBox(surf.points()).extend(1e-4),
154  8,
155  10,
156  3
157  );
158 
159  // Find the nearest face to the tree
160  const pointIndexHit hit = tree.findNearest(p, sqr(great));
161 
162  if (!hit.hit())
163  {
165  << "Patch nearest to " << p << " not found"
166  << exit(FatalError);
167  }
168 
169  // Loop through all the zones looking for the one which contains the
170  // nearest face
171  forAll(surf.surfZones(), i)
172  {
173  const surfZone& sZone = surf.surfZones()[i];
174 
175  if
176  (
177  hit.index() >= sZone.start()
178  && hit.index() < sZone.start() + sZone.size()
179  )
180  {
181  surfZonei = i;
182 
183  Info<< "Found patch " << sZone.name()
184  << " nearest to " << p << nl << endl;
185 
186  break;
187  }
188  }
189  }
190 
191  // Rename the patch
192  Info<< "Renaming '" << surf.surfZones()[surfZonei].name()
193  << "' to '" << newPatchName << "'"
194  << nl << endl;
195 
196  surf.surfZones()[surfZonei].name() = newPatchName;
197 
198  Info<< "Writing surface " << outFile << nl << endl;
199  surf.write(outFile);
200 
201  Info<< "End\n" << endl;
202 
203  return 0;
204 }
205 
206 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
virtual Ostream & write(const token &)
Write token.
Definition: Ostream.C:51
This class describes the interaction of (usually) a face and a point. It carries the info of a succes...
Definition: PointIndexHit.H:54
label index() const
Return index.
bool hit() const
Is there a hit.
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:103
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:121
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:152
T optionRead(const word &opt) const
Read a value from the named option.
Definition: argListI.H:244
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:234
A class for handling file names.
Definition: fileName.H:82
Non-pointer based hierarchical recursive searching.
Definition: indexedOctree.H:72
const word & name() const
Return name.
A surface zone on a MeshedSurface.
Definition: surfZone.H:65
label start() const
Return start label of this zone in the face list.
Definition: surfZone.H:129
label size() const
Return size of this zone in the face list.
Definition: surfZone.H:141
Standard boundBox + extra functionality for use in octree.
Definition: treeBoundBox.H:90
treeBoundBox extend(const scalar s) const
Return asymmetrically extended bounding box, with guaranteed.
Encapsulation of data needed to search on PrimitivePatches.
A class for handling words, derived from string.
Definition: word.H:63
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
int main(int argc, char *argv[])
Definition: financialFoam.C:44
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const doubleScalar e
Definition: doubleScalar.H:106
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
messageStream Info
tmp< DimensionedField< typename outerProduct< Type, Type >::type, GeoMesh, Field >> sqr(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
error FatalError
static const char nl
Definition: Ostream.H:297
Foam::argList args(argc, argv)
volScalarField & p