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) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSDetectorConstruction.hh"
19#include "BDSFieldQueryInfo.hh"
20#include "BDSUtilities.hh"
21#include "BDSVisCommandSceneAddQueryMagneticField.hh"
22#include "BDSVisFieldModel.hh"
23
24#include "globals.hh"
25#include "G4String.hh"
26#include "G4Types.hh"
27#include "G4UIcommand.hh"
28#include "G4UIparameter.hh"
29#include "G4Version.hh"
30#include "G4VisManager.hh"
31
32#if G4VERSION_NUMBER < 1060
33#include "G4UImanager.hh"
34#include "G4VSceneHandler.hh"
35#endif
36
37#include <vector>
38
39BDSVisCommandSceneAddQueryMagneticField::BDSVisCommandSceneAddQueryMagneticField(const BDSDetectorConstruction* realWorldIn):
40 realWorld(realWorldIn),
41 command(nullptr)
42{
43 command = new G4UIcommand("/bds/field/drawQuery", this);
44 command->SetGuidance("Adds field representation from a query to the current scene. The key \"all\" can be used to draw all defined queries.");
45
46 G4UIparameter* parameter;
47 parameter = new G4UIparameter("queryName", 's', false);
48 parameter->SetDefaultValue("");
49 command->SetParameter(parameter);
50}
51
52BDSVisCommandSceneAddQueryMagneticField::~BDSVisCommandSceneAddQueryMagneticField()
53{
54 delete command;
55}
56
57G4String BDSVisCommandSceneAddQueryMagneticField::GetCurrentValue(G4UIcommand*)
58{
59 return "";
60}
61
62void BDSVisCommandSceneAddQueryMagneticField::SetNewValue(G4UIcommand*, G4String newValue)
63{
64 G4VisManager::Verbosity verbosity = G4VisManager::GetVerbosity();
65 G4bool warn = verbosity >= G4VisManager::warnings;
66 G4Scene* scene = fpVisManager->GetCurrentScene();
67 if (!scene)
68 {
69 if (verbosity >= G4VisManager::errors)
70 {G4cerr << "ERROR: No current scene. Please create one." << G4endl;}
71 return;
72 }
73 if (newValue.empty())
74 {G4cerr << "No query name specified." << G4endl; return;}
75 std::vector<G4String> queryNames = BDS::SplitOnWhiteSpace(newValue);
76
77 std::vector<BDSFieldQueryInfo*> queries;
78
79 // Build a map of available query object names
80 const std::vector<BDSFieldQueryInfo*>& allQueries = realWorld->FieldQueries();
81 std::map<G4String, BDSFieldQueryInfo*> nameToObject;
82 for (const auto& query : allQueries)
83 {nameToObject[query->name] = query;}
84
85 // Build a vector of the queries by name
86 for (const auto& name : queryNames)
87 {
88 if (name == "all")
89 {
90 queries = allQueries;
91 break;
92 }
93
94 auto search = nameToObject.find(name);
95 if (search != nameToObject.end())
96 {queries.push_back(search->second);}
97 else
98 {G4cerr << "No such query name \"" << name << "\"" << G4endl;}
99 }
100
101 if (queries.empty())
102 {G4cerr << "No queries found to draw" << G4endl; return;}
103
104 G4VModel* model = new BDSVisFieldModel(queries);
105 G4bool successful = scene->AddRunDurationModel(model, warn);
106
107 if (successful && verbosity >= G4VisManager::confirmations)
108 {G4cout << "Magnetic field, if any, will be drawn in scene \"" << scene->GetName() << "\"" << G4endl;}
109 else
110 {
111 if (!successful && verbosity >= G4VisManager::warnings)
112 {G4cerr << "WARNING: not possible to add to the scene" << G4endl;}
113 }
114
116}
117
118#if G4VERSION_NUMBER < 1060
120{
121 if (!scene)
122 {return;}
123
124 G4VSceneHandler* sceneHandler = fpVisManager->GetCurrentSceneHandler();
125 if (!sceneHandler)
126 {return;}
127
128 if (scene == sceneHandler->GetScene())
129 {G4UImanager::GetUIpointer()->ApplyCommand("/vis/scene/notifyHandlers");}
130}
131#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'.