foamGltfSceneWriter.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "foamGltfSceneWriter.H"
29 #include "OFstream.H"
30 #include "OSspecific.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 :
36  ofile_(nullptr),
37  scene_(nullptr)
38 {
39  open(outputFile);
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
44 
46 {
47  close();
48 }
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
54 {
55  return (ofile_ && scene_);
56 }
57 
58 
60 {
61  return (ofile_ ? ofile_->name() : fileName::null);
62 }
63 
64 
66 {
67  return *scene_;
68 }
69 
70 
72 {
73  return *scene_;
74 }
75 
76 
77 void Foam::glTF::sceneWriter::open(const fileName& outputFile)
78 {
79  close();
80 
81  fileName jsonFile(outputFile);
82  jsonFile.replace_ext("gltf");
83 
84  // Note: called on master only
85  if (!isDir(jsonFile.path()))
86  {
87  mkDir(jsonFile.path());
88  }
89 
90  ofile_.reset(new OFstream(jsonFile));
91  scene_.reset(new glTF::scene());
92 }
93 
94 
96 {
97  if (ofile_ && scene_)
98  {
99  scene_->write(*ofile_);
100  }
101  ofile_.reset(nullptr);
102  scene_.reset(nullptr);
103 }
104 
105 
106 // ************************************************************************* //
~sceneWriter()
Destructor - calls close()
const scene & getScene() const
Const access to the scene. Error if good() is not true!
A class for handling file names.
Definition: fileName.H:72
bool good() const noexcept
True if output file and scene exist.
Output to file stream, using an OSstream.
Definition: OFstream.H:49
static const fileName null
An empty fileName.
Definition: fileName.H:111
void close()
Write scene and close file.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:614
Main class to assemble glTF components into a scene.
Definition: foamGltfScene.H:64
const fileName & path() const
The json file name. Empty with !good()
const direction noexcept
Definition: Scalar.H:258
sceneWriter()=default
Default construct.
void open(const fileName &outputFile)
Flush, output and open a new file for output.