Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4VisCommandsSceneHandler.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27
28// /vis/sceneHandler commands - John Allison 10th October 1998
29
31
32#include "G4VisManager.hh"
34#include "G4VisCommandsScene.hh"
35#include "G4UImanager.hh"
36#include "G4UIcommand.hh"
37#include "G4UIcmdWithAString.hh"
38#include "G4ios.hh"
39#include <sstream>
40
41#define G4warn G4cout
42
43////////////// /vis/sceneHandler/attach ///////////////////////////////////////
44
46 G4bool omitable, currentAsDefault;
47 fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/attach", this);
48 fpCommand -> SetGuidance ("Attaches scene to current scene handler.");
49 fpCommand -> SetGuidance
50 ("If scene-name is omitted, current scene is attached. To see scenes and"
51 "\nscene handlers, use \"/vis/scene/list\" and \"/vis/sceneHandler/list\"");
52 fpCommand -> SetParameterName ("scene-name",
53 omitable = true,
54 currentAsDefault = true);
55}
56
58 delete fpCommand;
59}
60
62 G4Scene* pScene = fpVisManager -> GetCurrentScene ();
63 return pScene ? pScene -> GetName () : G4String("");
64}
65
67 G4String newValue) {
68
70
71 G4String& sceneName = newValue;
72
73 if (sceneName.length () == 0) {
74 if (verbosity >= G4VisManager::warnings) {
75 G4cout <<
76 "WARNING: No scene specified. Maybe there are no scenes available"
77 "\n yet. Please create one." << G4endl;
78 }
79 return;
80 }
81
82 G4VSceneHandler* pSceneHandler = fpVisManager -> GetCurrentSceneHandler ();
83 if (!pSceneHandler) {
84 if (verbosity >= G4VisManager::errors) {
85 G4warn <<
86 "ERROR: Current scene handler not defined. Please select or create one."
87 << G4endl;
88 }
89 return;
90 }
91
92 G4SceneList& sceneList = fpVisManager -> SetSceneList ();
93
94 if (sceneList.empty ()) {
95 if (verbosity >= G4VisManager::errors) {
96 G4warn <<
97 "ERROR: No valid scenes available yet. Please create one."
98 << G4endl;
99 }
100 return;
101 }
102
103 std::size_t iScene, nScenes = sceneList.size ();
104 for (iScene = 0; iScene < nScenes; ++iScene) {
105 if (sceneList [iScene] -> GetName () == sceneName) break;
106 }
107 if (iScene < nScenes) {
108 G4Scene* pScene = sceneList [iScene];
109 pSceneHandler -> SetScene (pScene);
110 // Make sure scene is current...
111 fpVisManager -> SetCurrentScene (pScene);
112 // Refresh viewer, if any (only if auto-refresh)...
113 G4VViewer* pViewer = pSceneHandler -> GetCurrentViewer();
114 if (pViewer && pViewer -> GetViewParameters().IsAutoRefresh()) {
115 pViewer -> SetView ();
116 pViewer -> ClearView ();
117 pViewer -> DrawView ();
118 }
119 if (verbosity >= G4VisManager::confirmations) {
120 G4cout << "Scene \"" << sceneName
121 << "\" attached to scene handler \""
122 << pSceneHandler -> GetName () <<
123 ".\n (You may have to refresh with \"/vis/viewer/flush\" if view"
124 " is not \"auto-refresh\".)"
125 << G4endl;
126 }
127 }
128 else {
129 if (verbosity >= G4VisManager::errors) {
130 G4warn << "ERROR: Scene \"" << sceneName
131 << "\" not found. Use \"/vis/scene/list\" to see possibilities."
132 << G4endl;
133 }
134 }
135}
136
137////////////// /vis/sceneHandler/create ///////////////////////////////////////
138
140 G4bool omitable;
141 fpCommand = new G4UIcommand ("/vis/sceneHandler/create", this);
142 fpCommand -> SetGuidance
143 ("Creates an scene handler for a specific graphics system.");
144 fpCommand -> SetGuidance
145 ("Attaches current scene, if any. (You can change attached scenes with"
146 "\n\"/vis/sceneHandler/attach\".) Invents a scene handler name if not"
147 "\nsupplied. This scene handler becomes current.");
148 G4UIparameter* parameter;
149 parameter = new G4UIparameter ("graphics-system-name",
150 's', omitable = false);
151 const G4GraphicsSystemList& gslist =
152 fpVisManager -> GetAvailableGraphicsSystems ();
153 G4String candidates;
154 for (const auto gs: gslist) {
155 const G4String& name = gs -> GetName ();
156 candidates += name + ' ';
157 for (const auto& nickname: gs -> GetNicknames ()) {
158 if (G4StrUtil::contains(nickname, "FALLBACK")) continue;
159 if (nickname != name) candidates += nickname + ' ';
160 }
161 }
162 G4StrUtil::strip(candidates);
163 parameter -> SetParameterCandidates(candidates);
164 fpCommand -> SetParameter (parameter);
165 parameter = new G4UIparameter
166 ("scene-handler-name", 's', omitable = true);
167 parameter -> SetCurrentAsDefault (true);
168 fpCommand -> SetParameter (parameter);
169}
170
172 delete fpCommand;
173}
174
175G4String G4VisCommandSceneHandlerCreate::NextName () {
176 std::ostringstream oss;
177 oss << "scene-handler-" << fId;
178 return oss.str();
179}
180
182
183 G4String graphicsSystemName;
184 const G4VGraphicsSystem* graphicsSystem =
185 fpVisManager -> GetCurrentGraphicsSystem ();
186 if (graphicsSystem) {
187 graphicsSystemName = graphicsSystem -> GetName ();
188 }
189 else {
190 const G4GraphicsSystemList& gslist =
191 fpVisManager -> GetAvailableGraphicsSystems ();
192 if (gslist.size ()) {
193 graphicsSystemName = gslist [0] -> GetName ();
194 }
195 else {
196 graphicsSystemName = "none";
197 }
198 }
199
200 return graphicsSystemName + " " + NextName ();
201}
202
204 G4String newValue) {
205
207
208 G4String graphicsSystem, newName;
209 std::istringstream is (newValue);
210 is >> graphicsSystem >> newName;
211
212 const G4GraphicsSystemList& gsl =
213 fpVisManager -> GetAvailableGraphicsSystems ();
214 std::size_t nSystems = gsl.size ();
215 if (nSystems <= 0) {
217 ed <<
218 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
219 " no graphics systems available."
220 "\n Did you instantiate any in"
221 " YourVisManager::RegisterGraphicsSystems()?";
222 command->CommandFailed(ed);
223 return;
224 }
225 std::size_t iGS; // Selector index.
226 G4bool found = false;
227 for (iGS = 0; iGS < nSystems; ++iGS) {
228 const auto& gs = gsl[iGS];
229 if (G4StrUtil::icompare(graphicsSystem, gs->GetName()) == 0) {
230 found = true;
231 break; // Match found
232 } else {
233 const auto& nicknames = gs->GetNicknames();
234 for (std::size_t i = 0; i < nicknames.size(); ++i) {
235 const auto& nickname = nicknames[i];
236 if (G4StrUtil::icompare(graphicsSystem, nickname) == 0) {
237 found = true;
238 break; // Match found
239 }
240 }
241 if (found) {
242 break; // Match found
243 }
244 }
245 }
246 if (!found) {
247 // Shouldn't happen, since graphicsSystem should be a candidate
249 ed <<
250 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
251 "\n Invalid graphics system \""
252 << graphicsSystem
253 << "\" requested."
254 << "\n Candidates are:";
256 command->CommandFailed(ed);
257 return;
258 }
259
260 // Check UI session compatibility.
261 G4bool fallback = false;
262 G4int loopCounter = 0;
263 while (!gsl[iGS]->IsUISessionCompatible()) {
264 std::size_t iGSBeingTested = iGS;
265 // Not compatible, search for a fallback
266 fallback = false;
267 G4String fallbackNickname = gsl[iGS]->GetNickname() + "_FALLBACK";
268 for (iGS = 0; iGS < nSystems; iGS++) {
269 const auto& nicknames = gsl[iGS]->GetNicknames();
270 for (std::size_t i = 0; i < nicknames.size(); ++i) {
271 const auto& nickname = nicknames[i];
272 if (G4StrUtil::icompare(fallbackNickname, nickname) == 0) {
273 fallback = true;
274 break; // Match found
275 }
276 }
277 if (fallback) {
278 break; // Match found
279 }
280 }
281 if (iGS >= nSystems || loopCounter >=3) {
283 ed << "\"" << gsl[iGSBeingTested]->GetNickname()
284 << "\" is not compatible with your chosen session,"
285 " and no fallback system found.";
286 command->CommandFailed(ed);
287 return;
288 }
289 // A fallback system found...but go back and check this too.
290 ++loopCounter;
291 }
292
293 // A graphics system has been found
294 G4VGraphicsSystem* pSystem = gsl [iGS];
295
296 if (fallback && verbosity >= G4VisManager::warnings) {
297 G4warn << "WARNING: G4VisCommandSceneHandlerCreate::SetNewValue:"
298 "\n Using fallback graphics system: "
299 << pSystem -> GetName ()
300 << " ("
301 << pSystem -> GetNickname ()
302 << ')'
303 << G4endl;
304 }
305
306 // Now deal with name of scene handler.
307 G4String nextName = NextName ();
308 if (newName == "") {
309 newName = nextName;
310 }
311 if (newName == nextName) fId++;
312
313 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
314 std::size_t iScene;
315 for (iScene = 0; iScene < list.size (); ++iScene) {
316 G4VSceneHandler* sceneHandler = list [iScene];
317 if (sceneHandler -> GetName () == newName) {
319 ed <<
320 "ERROR: Scene handler \"" << newName
321 << "\" already exists.";
322 command->CommandFailed(ed);
323 return;
324 }
325 }
326
327 // If there is an existing viewer, store its view parameters
329 fThereWasAViewer = true;
331 }
332
333 // Set current graphics system in preparation for
334 // creating scene handler.
335 fpVisManager -> SetCurrentGraphicsSystem (pSystem);
336 if (verbosity >= G4VisManager::confirmations) {
337 G4cout << "Graphics system set to "
338 << pSystem -> GetName ()
339 << " ("
340 << pSystem -> GetNickname ()
341 << ')'
342 << G4endl;
343 }
344
345 //Create scene handler.
346 fpVisManager -> CreateSceneHandler (newName);
347 if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) {
349 ed <<
350 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
351 " Curious name mismatch."
352 "\n Current name \""
353 << fpVisManager -> GetCurrentSceneHandler () -> GetName ()
354 << "\" is not the new name \""
355 << newName
356 << "\".\n Please report to vis coordinator.";
357 command->CommandFailed(ed);
358 return;
359 }
360
361 if (verbosity >= G4VisManager::confirmations)
362 G4cout << "New scene handler \"" << newName << "\" created." << G4endl;
363
364 if (fpVisManager -> GetCurrentScene ()) {
365 auto errorCode = G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach");
366 if (errorCode) {
368 ed << "sub-command \"/vis/sceneHandler/attach\" failed.";
369 command->CommandFailed(errorCode,ed);
370 return;
371 }
372 }
373}
374
375////////////// /vis/sceneHandler/list ///////////////////////////////////////
376
378 G4bool omitable;
379 fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this);
380 fpCommand -> SetGuidance ("Lists scene handler(s).");
381 fpCommand -> SetGuidance
382 ("\"help /vis/verbose\" for definition of verbosity.");
383 G4UIparameter* parameter;
384 parameter = new G4UIparameter("scene-handler-name", 's', omitable = true);
385 parameter -> SetDefaultValue ("all");
386 fpCommand -> SetParameter (parameter);
387 parameter = new G4UIparameter ("verbosity", 's', omitable = true);
388 parameter -> SetDefaultValue ("warnings");
389 fpCommand -> SetParameter (parameter);
390}
391
393 delete fpCommand;
394}
395
397 return "";
398}
399
401 G4String newValue) {
402 G4String name, verbosityString;
403 std::istringstream is (newValue);
404 is >> name >> verbosityString;
405 G4VisManager::Verbosity verbosity =
406 fpVisManager->GetVerbosityValue(verbosityString);
407 const G4VSceneHandler* currentSceneHandler =
408 fpVisManager -> GetCurrentSceneHandler ();
409 G4String currentName;
410 if (currentSceneHandler) currentName = currentSceneHandler->GetName();
411
412 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
413 G4bool found = false;
414 for (std::size_t iSH = 0; iSH < list.size (); ++iSH) {
415 const G4String& iName = list [iSH] -> GetName ();
416 if (name != "all") {
417 if (name != iName) continue;
418 }
419 found = true;
420 if (iName == currentName) {
421 G4cout << " (current)";
422 }
423 else {
424 G4cout << " ";
425 }
426 G4cout << " scene handler \"" << list [iSH] -> GetName () << "\""
427 << " (" << list [iSH] -> GetGraphicsSystem () -> GetName () << ")";
428 if (verbosity >= G4VisManager::parameters) {
429 G4cout << "\n " << *(list [iSH]);
430 }
431 G4cout << G4endl;
432 }
433 if (!found) {
434 G4cout << "No scene handlers found";
435 if (name != "all") {
436 G4cout << " of name \"" << name << "\"";
437 }
438 G4cout << "." << G4endl;
439 }
440}
441
442////////////// /vis/sceneHandler/select ///////////////////////////////////////
443
445 G4bool omitable;
446 fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this);
447 fpCommand -> SetGuidance ("Selects a scene handler.");
448 fpCommand -> SetGuidance
449 ("Makes the scene handler current. \"/vis/sceneHandler/list\" to see"
450 "\n possible scene handler names.");
451 fpCommand -> SetParameterName ("scene-handler-name",
452 omitable = false);
453}
454
456 delete fpCommand;
457}
458
460 return "";
461}
462
464 G4String newValue) {
465
467
468 G4String& selectName = newValue;
469 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
470
471 std::size_t iSH;
472 for (iSH = 0; iSH < list.size (); iSH++) {
473 if (list [iSH] -> GetName () == selectName) break;
474 }
475 if (iSH < list.size ()) {
476 if (fpVisManager -> GetCurrentSceneHandler () -> GetName ()
477 == selectName) {
478 if (verbosity >= G4VisManager::confirmations) {
479 G4cout << "Scene handler \"" << selectName << "\""
480 << " already selected." << G4endl;
481 }
482 }
483 else {
484 if (verbosity >= G4VisManager::confirmations) {
485 G4cout << "Scene handler \"" << selectName << "\""
486 << " being selected." << G4endl;
487 }
488 fpVisManager -> SetCurrentSceneHandler (list [iSH]);
489 }
490 }
491 else {
492 if (verbosity >= G4VisManager::errors) {
493 G4warn << "ERROR: Scene handler \"" << selectName << "\""
494 << " not found - \"/vis/sceneHandler/list\" to see possibilities."
495 << G4endl;
496 }
497 }
498}
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
#define G4warn
Definition: G4Scene.cc:41
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
void CommandFailed(G4int errCode, G4ExceptionDescription &ed)
Definition: G4UIcommand.hh:179
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
const G4String & GetName() const
const G4ViewParameters & GetViewParameters() const
static G4ViewParameters fExistingVP
static G4VisManager * fpVisManager
static G4bool fThereWasAViewer
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
void PrintAvailableGraphicsSystems(Verbosity, std::ostream &=G4cout) const
G4VViewer * GetCurrentViewer() const
static Verbosity GetVerbosity()
static Verbosity GetVerbosityValue(const G4String &)
G4int icompare(std::string_view lhs, std::string_view rhs)
Case insensitive comparison of two strings.
void strip(G4String &str, char ch=' ')
Remove leading and trailing characters from string.
G4bool contains(const G4String &str, std::string_view ss)
Check if a string contains a given substring.