BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSVisCommandSceneAddQueryMagneticField.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 "BDSDetectorConstruction.hh"
20#include "BDSFieldQueryInfo.hh"
21#include "BDSUtilities.hh"
22#include "BDSVisCommandSceneAddQueryMagneticField.hh"
23#include "BDSVisFieldModel.hh"
24
25#include "globals.hh"
26#include "G4String.hh"
27#include "G4Types.hh"
28#include "G4UIcommand.hh"
29#include "G4UIparameter.hh"
30#include "G4Version.hh"
31#include "G4VisManager.hh"
32
33#if G4VERSION_NUMBER < 1060
34#include "G4UImanager.hh"
35#include "G4VSceneHandler.hh"
36#endif
37
38#include <vector>
39
40BDSVisCommandSceneAddQueryMagneticField::BDSVisCommandSceneAddQueryMagneticField(const BDSDetectorConstruction* realWorldIn):
41 realWorld(realWorldIn),
42 command(nullptr)
43{
44 command = new G4UIcommand("/bds/field/drawQuery", this);
45 command->SetGuidance("Adds field representation from a query to the current scene. The key \"all\" can be used to draw all defined queries.");
46
47 G4UIparameter* parameter;
48 parameter = new G4UIparameter("queryName", 's', false);
49 parameter->SetDefaultValue("");
50 command->SetParameter(parameter);
51}
52
53BDSVisCommandSceneAddQueryMagneticField::~BDSVisCommandSceneAddQueryMagneticField()
54{
55 delete command;
56}
57
58G4String BDSVisCommandSceneAddQueryMagneticField::GetCurrentValue(G4UIcommand*)
59{
60 return "";
61}
62
63void BDSVisCommandSceneAddQueryMagneticField::SetNewValue(G4UIcommand*, G4String newValue)
64{
65 G4VisManager::Verbosity verbosity = G4VisManager::GetVerbosity();
66 G4bool warn = verbosity >= G4VisManager::warnings;
67 G4Scene* scene = fpVisManager->GetCurrentScene();
68 if (!scene)
69 {
70 if (verbosity >= G4VisManager::errors)
71 {G4cerr << "ERROR: No current scene. Please create one." << G4endl;}
72 return;
73 }
74 if (newValue.empty())
75 {G4cerr << "No query name specified." << G4endl; return;}
76 std::vector<G4String> queryNames = BDS::SplitOnWhiteSpace(newValue);
77
78 std::vector<BDSFieldQueryInfo*> queries;
79
80 // Build a map of available query object names
81 const std::vector<BDSFieldQueryInfo*>& allQueries = realWorld->FieldQueries();
82 std::map<G4String, BDSFieldQueryInfo*> nameToObject;
83 for (const auto& query : allQueries)
84 {nameToObject[query->name] = query;}
85
86 // Build a vector of the queries by name
87 for (const auto& name : queryNames)
88 {
89 if (name == "all")
90 {
91 queries = allQueries;
92 break;
93 }
94
95 auto search = nameToObject.find(name);
96 if (search != nameToObject.end())
97 {queries.push_back(search->second);}
98 else
99 {G4cerr << "No such query name \"" << name << "\"" << G4endl;}
100 }
101
102 if (queries.empty())
103 {G4cerr << "No queries found to draw" << G4endl; return;}
104
105 G4VModel* model = new BDSVisFieldModel(queries);
106 G4bool successful = scene->AddRunDurationModel(model, warn);
107
108 if (successful && verbosity >= G4VisManager::confirmations)
109 {G4cout << "Magnetic field, if any, will be drawn in scene \"" << scene->GetName() << "\"" << G4endl;}
110 else
111 {
112 if (!successful && verbosity >= G4VisManager::warnings)
113 {G4cerr << "WARNING: not possible to add to the scene" << G4endl;}
114 }
115
117}
118
119#if G4VERSION_NUMBER < 1060
121{
122 if (!scene)
123 {return;}
124
125 G4VSceneHandler* sceneHandler = fpVisManager->GetCurrentSceneHandler();
126 if (!sceneHandler)
127 {return;}
128
129 if (scene == sceneHandler->GetScene())
130 {G4UImanager::GetUIpointer()->ApplyCommand("/vis/scene/notifyHandlers");}
131}
132#endif
Class that constructs a Geant4 model of an accelerator.
const std::vector< BDSFieldQueryInfo * > & FieldQueries() const
Access vector of query objects.
void CheckSceneAndNotifyHandlers(G4Scene *scene)
Simplified copy of G4VVisCommands::CheckSceneAndNotifyHandlers from G4 V6 and upwards.
std::vector< G4String > SplitOnWhiteSpace(const G4String &input)
Split a string on whitespace and return a vector of these 'words'.