BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSGeometryWriter.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSGeometryWriter.hh"
22#include "BDSUtilities.hh"
23
24#include "globals.hh" // geant4 types / globals
25#ifdef USE_GDML
26#include "G4GDMLParser.hh"
27#endif
28#include "G4FileUtilities.hh"
29#include "G4TransportationManager.hh"
30
31class G4VPhysicalVolume;
32
33BDSGeometryWriter::~BDSGeometryWriter()
34{;}
35
36BDSGeometryWriter::BDSGeometryWriter()
37{;}
38
39void BDSGeometryWriter::ExportGeometry(G4String geometryType,
40 G4String geometryFileName)
41{
42 if (geometryType == "gdml")
43 {
44#ifdef USE_GDML
45 WriteToGDML(geometryFileName);
46#else
47 throw BDSException(__METHOD_NAME__, "Unable to write out " + geometryFileName + ", as compiled without GDML support");
48#endif
49 }
50}
51
52#ifdef USE_GDML
53void BDSGeometryWriter::WriteToGDML(G4String outputFileName,
54 G4VPhysicalVolume* volume)
55{
56 if (!volume)
57 {volume = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();}
58 // gdml parser throws exception if file exists. Delete file in that case
59 if (BDS::FileExists(outputFileName))
60 {
61 G4FileUtilities fileUtilities;
62 fileUtilities.DeleteFile(outputFileName, "");
63 }
64 G4GDMLParser parser;
65 parser.Write(outputFileName, volume, true);
66}
67#endif
General exception with possible name of object and message.
Definition: BDSException.hh:35
void WriteToGDML(G4String outputFileName, G4VPhysicalVolume *volume=nullptr)
void ExportGeometry(G4String geometryType, G4String geometryFileName)
G4bool FileExists(const G4String &filename)
Checks if filename exists.