Geant4-11
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
42
44 G4bool omitable, currentAsDefault;
45 fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/attach", this);
46 fpCommand -> SetGuidance ("Attaches scene to current scene handler.");
47 fpCommand -> SetGuidance
48 ("If scene-name is omitted, current scene is attached. To see scenes and"
49 "\nscene handlers, use \"/vis/scene/list\" and \"/vis/sceneHandler/list\"");
50 fpCommand -> SetParameterName ("scene-name",
51 omitable = true,
52 currentAsDefault = true);
53}
54
56 delete fpCommand;
57}
58
60 G4Scene* pScene = fpVisManager -> GetCurrentScene ();
61 return pScene ? pScene -> GetName () : G4String("");
62}
63
65 G4String newValue) {
66
68
69 G4String& sceneName = newValue;
70
71 if (sceneName.length () == 0) {
72 if (verbosity >= G4VisManager::warnings) {
73 G4cout <<
74 "WARNING: No scene specified. Maybe there are no scenes available"
75 "\n yet. Please create one." << G4endl;
76 }
77 return;
78 }
79
80 G4VSceneHandler* pSceneHandler = fpVisManager -> GetCurrentSceneHandler ();
81 if (!pSceneHandler) {
82 if (verbosity >= G4VisManager::errors) {
83 G4cerr <<
84 "ERROR: Current scene handler not defined. Please select or create one."
85 << G4endl;
86 }
87 return;
88 }
89
90 G4SceneList& sceneList = fpVisManager -> SetSceneList ();
91
92 if (sceneList.empty ()) {
93 if (verbosity >= G4VisManager::errors) {
94 G4cerr <<
95 "ERROR: No valid scenes available yet. Please create one."
96 << G4endl;
97 }
98 return;
99 }
100
101 G4int iScene, nScenes = sceneList.size ();
102 for (iScene = 0; iScene < nScenes; iScene++) {
103 if (sceneList [iScene] -> GetName () == sceneName) break;
104 }
105 if (iScene < nScenes) {
106 G4Scene* pScene = sceneList [iScene];
107 pSceneHandler -> SetScene (pScene);
108 // Make sure scene is current...
109 fpVisManager -> SetCurrentScene (pScene);
110 // Refresh viewer, if any (only if auto-refresh)...
111 G4VViewer* pViewer = pSceneHandler -> GetCurrentViewer();
112 if (pViewer && pViewer -> GetViewParameters().IsAutoRefresh()) {
113 pViewer -> SetView ();
114 pViewer -> ClearView ();
115 pViewer -> DrawView ();
116 }
117 if (verbosity >= G4VisManager::confirmations) {
118 G4cout << "Scene \"" << sceneName
119 << "\" attached to scene handler \""
120 << pSceneHandler -> GetName () <<
121 ".\n (You may have to refresh with \"/vis/viewer/flush\" if view"
122 " is not \"auto-refresh\".)"
123 << G4endl;
124 }
125 }
126 else {
127 if (verbosity >= G4VisManager::errors) {
128 G4cerr << "ERROR: Scene \"" << sceneName
129 << "\" not found. Use \"/vis/scene/list\" to see possibilities."
130 << G4endl;
131 }
132 }
133}
134
136
138 G4bool omitable;
139 fpCommand = new G4UIcommand ("/vis/sceneHandler/create", this);
140 fpCommand -> SetGuidance
141 ("Creates an scene handler for a specific graphics system.");
142 fpCommand -> SetGuidance
143 ("Attaches current scene, if any. (You can change attached scenes with"
144 "\n\"/vis/sceneHandler/attach\".) Invents a scene handler name if not"
145 "\nsupplied. This scene handler becomes current.");
146 G4UIparameter* parameter;
147 parameter = new G4UIparameter ("graphics-system-name",
148 's', omitable = false);
149 const G4GraphicsSystemList& gslist =
150 fpVisManager -> GetAvailableGraphicsSystems ();
151 G4String candidates;
152 for (const auto gs: gslist) {
153 const G4String& name = gs -> GetName ();
154 candidates += name + ' ';
155 for (const auto& nickname: gs -> GetNicknames ()) {
156 if (G4StrUtil::contains(nickname, "FALLBACK")) continue;
157 if (nickname != name) candidates += nickname + ' ';
158 }
159 }
160 G4StrUtil::strip(candidates);
161 parameter -> SetParameterCandidates(candidates);
162 fpCommand -> SetParameter (parameter);
163 parameter = new G4UIparameter
164 ("scene-handler-name", 's', omitable = true);
165 parameter -> SetCurrentAsDefault (true);
166 fpCommand -> SetParameter (parameter);
167}
168
170 delete fpCommand;
171}
172
174 std::ostringstream oss;
175 oss << "scene-handler-" << fId;
176 return oss.str();
177}
178
180
181 G4String graphicsSystemName;
182 const G4VGraphicsSystem* graphicsSystem =
183 fpVisManager -> GetCurrentGraphicsSystem ();
184 if (graphicsSystem) {
185 graphicsSystemName = graphicsSystem -> GetName ();
186 }
187 else {
188 const G4GraphicsSystemList& gslist =
189 fpVisManager -> GetAvailableGraphicsSystems ();
190 if (gslist.size ()) {
191 graphicsSystemName = gslist [0] -> GetName ();
192 }
193 else {
194 graphicsSystemName = "none";
195 }
196 }
197
198 return graphicsSystemName + " " + NextName ();
199}
200
202 G4String newValue) {
203
205
206 G4String graphicsSystem, newName;
207 std::istringstream is (newValue);
208 is >> graphicsSystem >> newName;
209
210 const G4GraphicsSystemList& gsl =
211 fpVisManager -> GetAvailableGraphicsSystems ();
212 G4int nSystems = gsl.size ();
213 if (nSystems <= 0) {
215 ed <<
216 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
217 " no graphics systems available."
218 "\n Did you instantiate any in"
219 " YourVisManager::RegisterGraphicsSystems()?";
220 command->CommandFailed(ed);
221 return;
222 }
223 G4int iGS; // Selector index.
224 G4bool found = false;
225 for (iGS = 0; iGS < nSystems; iGS++) {
226 const auto& gs = gsl[iGS];
227 if (G4StrUtil::icompare(graphicsSystem, gs->GetName()) == 0) {
228 found = true;
229 break; // Match found
230 } else {
231 const auto& nicknames = gs->GetNicknames();
232 for (size_t i = 0; i < nicknames.size(); ++i) {
233 const auto& nickname = nicknames[i];
234 if (G4StrUtil::icompare(graphicsSystem, nickname) == 0) {
235 found = true;
236 break; // Match found
237 }
238 }
239 if (found) {
240 break; // Match found
241 }
242 }
243 }
244 if (!found) {
245 // Shouldn't happen, since graphicsSystem should be a candidate
246 // Use set to get alphabetical order
247 std::set<G4String> candidates;
248 for (const auto gs: fpVisManager -> GetAvailableGraphicsSystems()) {
249 // Just list nicknames, but exclude FALLBACK nicknames
250 for (const auto& nickname: gs->GetNicknames()) {
251 if (!G4StrUtil::contains(nickname, "FALLBACK")) {
252 candidates.insert(nickname);
253 }
254 }
255 }
257 ed <<
258 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
259 "\n Invalid graphics system \""
260 << graphicsSystem
261 << "\" requested."
262 << "\n Candidates are:";
263 for (const auto& candidate: candidates) {
264 ed << ' ' << candidate;
265 };
266 command->CommandFailed(ed);
267 return;
268 }
269
270 // Check UI session compatibility.
271 G4bool fallback = false;
272 G4int loopCounter = 0;
273 while (!gsl[iGS]->IsUISessionCompatible()) {
274 G4int iGSBeingTested = iGS;
275 // Not compatible, search for a fallback
276 fallback = false;
277 G4String fallbackNickname = gsl[iGS]->GetNickname() + "_FALLBACK";
278 for (iGS = 0; iGS < nSystems; iGS++) {
279 const auto& nicknames = gsl[iGS]->GetNicknames();
280 for (size_t i = 0; i < nicknames.size(); ++i) {
281 const auto& nickname = nicknames[i];
282 if (G4StrUtil::icompare(fallbackNickname, nickname) == 0) {
283 fallback = true;
284 break; // Match found
285 }
286 }
287 if (fallback) {
288 break; // Match found
289 }
290 }
291 if (iGS < 0 || iGS >= nSystems || loopCounter >=3) {
293 ed << "\"" << gsl[iGSBeingTested]->GetNickname()
294 << "\" is not compatible with your chosen session,"
295 " and no fallback system found.";
296 command->CommandFailed(ed);
297 return;
298 }
299 // A fallback system found...but go back and check this too.
300 ++loopCounter;
301 }
302
303 // A graphics system has been found
304 G4VGraphicsSystem* pSystem = gsl [iGS];
305
306 if (fallback && verbosity >= G4VisManager::warnings) {
307 G4cout << "WARNING: G4VisCommandSceneHandlerCreate::SetNewValue:"
308 "\n Using fallback graphics system: "
309 << pSystem -> GetName ()
310 << " ("
311 << pSystem -> GetNickname ()
312 << ')'
313 << G4endl;
314 }
315
316 // Now deal with name of scene handler.
317 G4String nextName = NextName ();
318 if (newName == "") {
319 newName = nextName;
320 }
321 if (newName == nextName) fId++;
322
323 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
324 size_t iScene;
325 for (iScene = 0; iScene < list.size (); iScene++) {
326 G4VSceneHandler* sceneHandler = list [iScene];
327 if (sceneHandler -> GetName () == newName) {
329 ed <<
330 "ERROR: Scene handler \"" << newName
331 << "\" already exists.";
332 command->CommandFailed(ed);
333 return;
334 }
335 }
336
337 // If there is an existing viewer, store its view parameters
339 fThereWasAViewer = true;
341 }
342
343 // Set current graphics system in preparation for
344 // creating scene handler.
345 fpVisManager -> SetCurrentGraphicsSystem (pSystem);
346 if (verbosity >= G4VisManager::confirmations) {
347 G4cout << "Graphics system set to "
348 << pSystem -> GetName ()
349 << " ("
350 << pSystem -> GetNickname ()
351 << ')'
352 << G4endl;
353 }
354
355 //Create scene handler.
356 fpVisManager -> CreateSceneHandler (newName);
357 if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) {
359 ed <<
360 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
361 " Curious name mismatch."
362 "\n Current name \""
363 << fpVisManager -> GetCurrentSceneHandler () -> GetName ()
364 << "\" is not the new name \""
365 << newName
366 << "\".\n Please report to vis coordinator.";
367 command->CommandFailed(ed);
368 return;
369 }
370
371 if (verbosity >= G4VisManager::confirmations)
372 G4cout << "New scene handler \"" << newName << "\" created." << G4endl;
373
374 if (fpVisManager -> GetCurrentScene ()) {
375 auto errorCode = G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach");
376 if (errorCode) {
378 ed << "sub-command \"/vis/sceneHandler/attach\" failed.";
379 command->CommandFailed(errorCode,ed);
380 return;
381 }
382 }
383}
384
386
388 G4bool omitable;
389 fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this);
390 fpCommand -> SetGuidance ("Lists scene handler(s).");
391 fpCommand -> SetGuidance
392 ("\"help /vis/verbose\" for definition of verbosity.");
393 G4UIparameter* parameter;
394 parameter = new G4UIparameter("scene-handler-name", 's', omitable = true);
395 parameter -> SetDefaultValue ("all");
396 fpCommand -> SetParameter (parameter);
397 parameter = new G4UIparameter ("verbosity", 's', omitable = true);
398 parameter -> SetDefaultValue ("warnings");
399 fpCommand -> SetParameter (parameter);
400}
401
403 delete fpCommand;
404}
405
407 return "";
408}
409
411 G4String newValue) {
412 G4String name, verbosityString;
413 std::istringstream is (newValue);
414 is >> name >> verbosityString;
415 G4VisManager::Verbosity verbosity =
416 fpVisManager->GetVerbosityValue(verbosityString);
417 const G4VSceneHandler* currentSceneHandler =
418 fpVisManager -> GetCurrentSceneHandler ();
419 G4String currentName;
420 if (currentSceneHandler) currentName = currentSceneHandler->GetName();
421
422 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
423 G4bool found = false;
424 for (size_t iSH = 0; iSH < list.size (); iSH++) {
425 const G4String& iName = list [iSH] -> GetName ();
426 if (name != "all") {
427 if (name != iName) continue;
428 }
429 found = true;
430 if (iName == currentName) {
431 G4cout << " (current)";
432 }
433 else {
434 G4cout << " ";
435 }
436 G4cout << " scene handler \"" << list [iSH] -> GetName () << "\""
437 << " (" << list [iSH] -> GetGraphicsSystem () -> GetName () << ")";
438 if (verbosity >= G4VisManager::parameters) {
439 G4cout << "\n " << *(list [iSH]);
440 }
441 G4cout << G4endl;
442 }
443 if (!found) {
444 G4cout << "No scene handlers found";
445 if (name != "all") {
446 G4cout << " of name \"" << name << "\"";
447 }
448 G4cout << "." << G4endl;
449 }
450}
451
453
455 G4bool omitable;
456 fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this);
457 fpCommand -> SetGuidance ("Selects a scene handler.");
458 fpCommand -> SetGuidance
459 ("Makes the scene handler current. \"/vis/sceneHandler/list\" to see"
460 "\n possible scene handler names.");
461 fpCommand -> SetParameterName ("scene-handler-name",
462 omitable = false);
463}
464
466 delete fpCommand;
467}
468
470 return "";
471}
472
474 G4String newValue) {
475
477
478 G4String& selectName = newValue;
479 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
480
481 size_t iSH;
482 for (iSH = 0; iSH < list.size (); iSH++) {
483 if (list [iSH] -> GetName () == selectName) break;
484 }
485 if (iSH < list.size ()) {
486 if (fpVisManager -> GetCurrentSceneHandler () -> GetName ()
487 == selectName) {
488 if (verbosity >= G4VisManager::confirmations) {
489 G4cout << "Scene handler \"" << selectName << "\""
490 << " already selected." << G4endl;
491 }
492 }
493 else {
494 if (verbosity >= G4VisManager::confirmations) {
495 G4cout << "Scene handler \"" << selectName << "\""
496 << " being selected." << G4endl;
497 }
498 fpVisManager -> SetCurrentSceneHandler (list [iSH]);
499 }
500 }
501 else {
502 if (verbosity >= G4VisManager::errors) {
503 G4cerr << "ERROR: Scene handler \"" << selectName << "\""
504 << " not found - \"/vis/sceneHandler/list\" to see possibilities."
505 << G4endl;
506 }
507 }
508}
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#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 G4VisManager * fpVisManager
static G4ViewParameters fVPExistingViewer
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)
G4VViewer * GetCurrentViewer() const
static Verbosity GetVerbosity()
static Verbosity GetVerbosityValue(const G4String &)
const char * name(G4int ptype)
G4int icompare(std::string_view lhs, std::string_view rhs)
Case insensitive comparison of two strings.
void strip(G4String &str, char c=' ')
Remove leading and trailing characters from string.
G4bool contains(const G4String &str, std::string_view ss)
Check if a string contains a given substring.
#define errorCode
Definition: xmlparse.cc:618