BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSGeometryWriter.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
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 "G4String.hh"
30#include "G4TransportationManager.hh"
31
32class G4VPhysicalVolume;
33
34BDSGeometryWriter::~BDSGeometryWriter()
35{;}
36
37BDSGeometryWriter::BDSGeometryWriter()
38{;}
39
40void BDSGeometryWriter::ExportGeometry(G4String geometryType,
41 G4String geometryFileName)
42{
43 if (geometryType == "gdml")
44 {
45#ifdef USE_GDML
46 WriteToGDML(geometryFileName);
47#else
48 throw BDSException(__METHOD_NAME__, "Unable to write out " + geometryFileName + ", as compiled without GDML support");
49#endif
50 }
51}
52
53#ifdef USE_GDML
54void BDSGeometryWriter::WriteToGDML(G4String outputFileName,
55 G4VPhysicalVolume* volume)
56{
57 if (!volume)
58 {volume = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();}
59 // gdml parser throws exception if file exists. Delete file in that case
60 if (BDS::FileExists(outputFileName))
61 {
62 G4FileUtilities fileUtilities;
63 G4cout << "Removing existing file: \"" << outputFileName << "\"" << G4endl;
64 fileUtilities.DeleteFile(outputFileName, "");
65 }
66
67 // if the filename is in part a path to a directory, check that directory exists
68 // otherwise the GDML writer will give an abort trap bad access
69 if (BDS::StrContains(outputFileName, "/"))
70 {
71 G4String dirName = BDS::GetFullPath(outputFileName, true);
72 if (!BDS::DirectoryExists(dirName))
73 {throw BDSException(__METHOD_NAME__, "directory \"" + dirName + "\" does not exist - please create it first.");}
74 }
75
76 G4GDMLParser parser;
77 parser.Write(outputFileName, volume, true);
78}
79#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 StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
Definition: BDSUtilities.cc:66
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)
G4bool DirectoryExists(const G4String &path)
Check if directory exists.
G4bool FileExists(const G4String &filename)
Checks if filename exists.