Geant4 11.2.2
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
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", 's', omitable = true);
150 parameter -> SetCurrentAsDefault(true);
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
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 graphicsSystemName = fpVisManager->GetDefaultGraphicsSystemName();
191 }
192
193 return graphicsSystemName + " " + NextName ();
194}
195
197 G4String newValue) {
198
200
201 G4String graphicsSystem, newName;
202 std::istringstream is (newValue);
203 is >> graphicsSystem >> newName;
204
205 const G4GraphicsSystemList& gsl =
206 fpVisManager -> GetAvailableGraphicsSystems ();
207 std::size_t nSystems = gsl.size ();
208 if (nSystems <= 0) {
210 ed <<
211 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
212 " no graphics systems available."
213 "\n Did you instantiate any in"
214 " YourVisManager::RegisterGraphicsSystems()?";
215 command->CommandFailed(JustWarning,ed);
216 return;
217 }
218 std::size_t iGS; // Selector index.
219 G4bool found = false;
220 for (iGS = 0; iGS < nSystems; ++iGS) {
221 const auto& gs = gsl[iGS];
222 if (G4StrUtil::icompare(graphicsSystem, gs->GetName()) == 0) {
223 found = true;
224 break; // Match found
225 } else {
226 const auto& nicknames = gs->GetNicknames();
227 for (std::size_t i = 0; i < nicknames.size(); ++i) {
228 const auto& nickname = nicknames[i];
229 if (G4StrUtil::icompare(graphicsSystem, nickname) == 0) {
230 found = true;
231 break; // Match found
232 }
233 }
234 if (found) {
235 break; // Match found
236 }
237 }
238 }
239 if (!found) {
240 // Shouldn't happen, since graphicsSystem should be a candidate
242 ed <<
243 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
244 "\n Invalid graphics system \""
245 << graphicsSystem
246 << "\" requested."
247 << "\n Candidates are:";
249 command->CommandFailed(JustWarning,ed);
250 return;
251 }
252
253 // Check UI session compatibility.
254 G4bool fallback = false;
255 G4int loopCounter = 0;
256 while (!gsl[iGS]->IsUISessionCompatible()) {
257 std::size_t iGSBeingTested = iGS;
258 // Not compatible, search for a fallback
259 fallback = false;
260 G4String fallbackNickname = gsl[iGS]->GetNickname() + "_FALLBACK";
261 for (iGS = 0; iGS < nSystems; iGS++) {
262 const auto& nicknames = gsl[iGS]->GetNicknames();
263 for (std::size_t i = 0; i < nicknames.size(); ++i) {
264 const auto& nickname = nicknames[i];
265 if (G4StrUtil::icompare(fallbackNickname, nickname) == 0) {
266 fallback = true;
267 break; // Match found
268 }
269 }
270 if (fallback) {
271 break; // Match found
272 }
273 }
274 if (iGS >= nSystems || loopCounter >=3) {
276 ed << "\"" << gsl[iGSBeingTested]->GetNickname()
277 << "\" is not compatible with your chosen session,"
278 " and no fallback system found.";
279 command->CommandFailed(JustWarning,ed);
280 return;
281 }
282 // A fallback system found...but go back and check this too.
283 ++loopCounter;
284 }
285
286 // A graphics system has been found
287 G4VGraphicsSystem* pSystem = gsl [iGS];
288
289 if (fallback && verbosity >= G4VisManager::warnings) {
290 G4warn << "WARNING: G4VisCommandSceneHandlerCreate::SetNewValue:"
291 "\n Using fallback graphics system: "
292 << pSystem -> GetName ()
293 << " ("
294 << pSystem -> GetNickname ()
295 << ')'
296 << G4endl;
297 }
298
299 // Now deal with name of scene handler.
300 G4String nextName = NextName ();
301 if (newName == "") {
302 newName = nextName;
303 }
304 if (newName == nextName) fId++;
305
306 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
307 std::size_t iScene;
308 for (iScene = 0; iScene < list.size (); ++iScene) {
309 G4VSceneHandler* sceneHandler = list [iScene];
310 if (sceneHandler -> GetName () == newName) {
312 ed <<
313 "ERROR: Scene handler \"" << newName
314 << "\" already exists.";
315 command->CommandFailed(JustWarning,ed);
316 return;
317 }
318 }
319
320 // If there is an existing viewer, store its view parameters and scene tree
322 fThereWasAViewer = true;
323 auto viewer = fpVisManager->GetCurrentViewer();
324 fExistingVP = viewer->GetViewParameters();
325 fExistingSceneTree = viewer->AccessSceneTree();
326 }
327
328 // Set current graphics system in preparation for
329 // creating scene handler.
330 fpVisManager -> SetCurrentGraphicsSystem (pSystem);
331 if (verbosity >= G4VisManager::confirmations) {
332 G4cout << "Graphics system set to "
333 << pSystem -> GetName ()
334 << " ("
335 << pSystem -> GetNickname ()
336 << ')'
337 << G4endl;
338 }
339
340 //Create scene handler.
341 fpVisManager -> CreateSceneHandler (newName);
342 if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) {
344 ed <<
345 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
346 " Curious name mismatch."
347 "\n Current name \""
348 << fpVisManager -> GetCurrentSceneHandler () -> GetName ()
349 << "\" is not the new name \""
350 << newName
351 << "\".\n Please report to vis coordinator.";
352 command->CommandFailed(JustWarning,ed);
353 return;
354 }
355
356 if (verbosity >= G4VisManager::confirmations)
357 G4cout << "New scene handler \"" << newName << "\" created." << G4endl;
358
359 if (fpVisManager -> GetCurrentScene ()) {
360 auto errorCode = G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach");
361 if (errorCode) {
363 ed << "sub-command \"/vis/sceneHandler/attach\" failed.";
364 command->CommandFailed(errorCode,ed);
365 return;
366 }
367 }
368}
369
370////////////// /vis/sceneHandler/list ///////////////////////////////////////
371
373 G4bool omitable;
374 fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this);
375 fpCommand -> SetGuidance ("Lists scene handler(s).");
376 fpCommand -> SetGuidance
377 ("\"help /vis/verbose\" for definition of verbosity.");
378 G4UIparameter* parameter;
379 parameter = new G4UIparameter("scene-handler-name", 's', omitable = true);
380 parameter -> SetDefaultValue ("all");
381 fpCommand -> SetParameter (parameter);
382 parameter = new G4UIparameter ("verbosity", 's', omitable = true);
383 parameter -> SetDefaultValue ("warnings");
384 fpCommand -> SetParameter (parameter);
385}
386
390
394
396 G4String newValue) {
397 G4String name, verbosityString;
398 std::istringstream is (newValue);
399 is >> name >> verbosityString;
400 G4VisManager::Verbosity verbosity =
401 fpVisManager->GetVerbosityValue(verbosityString);
402 const G4VSceneHandler* currentSceneHandler =
403 fpVisManager -> GetCurrentSceneHandler ();
404 G4String currentName;
405 if (currentSceneHandler) currentName = currentSceneHandler->GetName();
406
407 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
408 G4bool found = false;
409 for (std::size_t iSH = 0; iSH < list.size (); ++iSH) {
410 const G4String& iName = list [iSH] -> GetName ();
411 if (name != "all") {
412 if (name != iName) continue;
413 }
414 found = true;
415 if (iName == currentName) {
416 G4cout << " (current)";
417 }
418 else {
419 G4cout << " ";
420 }
421 G4cout << " scene handler \"" << list [iSH] -> GetName () << "\""
422 << " (" << list [iSH] -> GetGraphicsSystem () -> GetName () << ")";
423 if (verbosity >= G4VisManager::parameters) {
424 G4cout << "\n " << *(list [iSH]);
425 }
426 G4cout << G4endl;
427 }
428 if (!found) {
429 G4cout << "No scene handlers found";
430 if (name != "all") {
431 G4cout << " of name \"" << name << "\"";
432 }
433 G4cout << "." << G4endl;
434 }
435}
436
437////////////// /vis/sceneHandler/select ///////////////////////////////////////
438
440 G4bool omitable;
441 fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this);
442 fpCommand -> SetGuidance ("Selects a scene handler.");
443 fpCommand -> SetGuidance
444 ("Makes the scene handler current. \"/vis/sceneHandler/list\" to see"
445 "\n possible scene handler names.");
446 fpCommand -> SetParameterName ("scene-handler-name",
447 omitable = false);
448}
449
453
457
459 G4String newValue) {
460
462
463 G4String& selectName = newValue;
464 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
465
466 std::size_t iSH;
467 for (iSH = 0; iSH < list.size (); iSH++) {
468 if (list [iSH] -> GetName () == selectName) break;
469 }
470 if (iSH < list.size ()) {
471 if (fpVisManager -> GetCurrentSceneHandler () -> GetName ()
472 == selectName) {
473 if (verbosity >= G4VisManager::confirmations) {
474 G4cout << "Scene handler \"" << selectName << "\""
475 << " already selected." << G4endl;
476 }
477 }
478 else {
479 if (verbosity >= G4VisManager::confirmations) {
480 G4cout << "Scene handler \"" << selectName << "\""
481 << " being selected." << G4endl;
482 }
483 fpVisManager -> SetCurrentSceneHandler (list [iSH]);
484 }
485 }
486 else {
487 if (verbosity >= G4VisManager::errors) {
488 G4warn << "ERROR: Scene handler \"" << selectName << "\""
489 << " not found - \"/vis/sceneHandler/list\" to see possibilities."
490 << G4endl;
491 }
492 }
493}
@ JustWarning
std::ostringstream G4ExceptionDescription
#define G4warn
Definition G4Scene.cc:41
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
void CommandFailed(G4int errCode, G4ExceptionDescription &ed)
static G4UImanager * GetUIpointer()
const G4String & GetName() const
static G4ViewParameters fExistingVP
static G4VisManager * fpVisManager
static G4SceneTreeItem fExistingSceneTree
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
const G4String & GetDefaultGraphicsSystemName() const
G4VViewer * GetCurrentViewer() const
static Verbosity GetVerbosity()
static Verbosity GetVerbosityValue(const G4String &)