Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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////////////// /vis/sceneHandler/attach ///////////////////////////////////////
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
135////////////// /vis/sceneHandler/create ///////////////////////////////////////
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 (nickname != name) candidates += nickname + ' ';
157 }
158 }
159 candidates = candidates.strip ();
160 parameter -> SetParameterCandidates(candidates);
161 fpCommand -> SetParameter (parameter);
162 parameter = new G4UIparameter
163 ("scene-handler-name", 's', omitable = true);
164 parameter -> SetCurrentAsDefault (true);
165 fpCommand -> SetParameter (parameter);
166}
167
169 delete fpCommand;
170}
171
172G4String G4VisCommandSceneHandlerCreate::NextName () {
173 std::ostringstream oss;
174 oss << "scene-handler-" << fId;
175 return oss.str();
176}
177
179
180 G4String graphicsSystemName;
181 const G4VGraphicsSystem* graphicsSystem =
182 fpVisManager -> GetCurrentGraphicsSystem ();
183 if (graphicsSystem) {
184 graphicsSystemName = graphicsSystem -> GetName ();
185 }
186 else {
187 const G4GraphicsSystemList& gslist =
188 fpVisManager -> GetAvailableGraphicsSystems ();
189 if (gslist.size ()) {
190 graphicsSystemName = gslist [0] -> GetName ();
191 }
192 else {
193 graphicsSystemName = "none";
194 }
195 }
196
197 return graphicsSystemName + " " + NextName ();
198}
199
201 G4String newValue) {
202
204
205 G4String graphicsSystem, newName;
206 std::istringstream is (newValue);
207 is >> graphicsSystem >> newName;
208
209 const G4GraphicsSystemList& gsl =
210 fpVisManager -> GetAvailableGraphicsSystems ();
211 G4int nSystems = gsl.size ();
212 if (nSystems <= 0) {
213 if (verbosity >= G4VisManager::errors) {
214 G4cerr << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
215 " no graphics systems available."
216 "\n Did you instantiate any in"
217 " YourVisManager::RegisterGraphicsSystems()?"
218 << G4endl;
219 }
220 return;
221 }
222 G4int iGS; // Selector index.
223 G4bool found = false;
224 for (iGS = 0; iGS < nSystems; iGS++) {
225 const auto& gs = gsl[iGS];
226 if (graphicsSystem.compareTo(gs->GetName(), G4String::ignoreCase) == 0) {
227 found = true;
228 break; // Match found
229 } else {
230 const auto& nicknames = gs->GetNicknames();
231 for (size_t i = 0; i < nicknames.size(); ++i) {
232 const auto& nickname = nicknames[i];
233 if (graphicsSystem.compareTo (nickname, G4String::ignoreCase) == 0) {
234 found = true;
235 break; // Match found
236 }
237 }
238 if (found) {
239 break; // Match found
240 }
241 }
242 }
243 if (!found) {
244 // Invalid command line argument or none.
245 // This shouldn't happen!!!!!!
246 if (verbosity >= G4VisManager::errors) {
247 G4cerr <<
248 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
249 "\n invalid graphics system \""
250 << graphicsSystem
251 << "\" requested."
252 << G4endl;
253 }
254 return;
255 }
256
257 // Check UI session compatibility.
258 G4bool fallback = false;
259 G4int loopCounter = 0;
260 while (!gsl[iGS]->IsUISessionCompatible()) {
261 G4int iGSBeingTested = iGS;
262 // Not compatible, search for a fallback
263 fallback = false;
264 G4String fallbackNickname = gsl[iGS]->GetNickname() + "_FALLBACK";
265 for (iGS = 0; iGS < nSystems; iGS++) {
266 const auto& nicknames = gsl[iGS]->GetNicknames();
267 for (size_t i = 0; i < nicknames.size(); ++i) {
268 const auto& nickname = nicknames[i];
269 if (fallbackNickname.compareTo (nickname, G4String::ignoreCase) == 0) {
270 fallback = true;
271 break; // Match found
272 }
273 }
274 if (fallback) {
275 break; // Match found
276 }
277 }
278 if (iGS < 0 || iGS >= nSystems || loopCounter >=3) {
279 std::ostringstream oss;
280 oss << "\"" << gsl[iGSBeingTested]->GetNickname()
281 << "\" is not compatible with your chosen session,"
282 " and no fallback system found.";
283 if (verbosity >= G4VisManager::errors) {
284 G4cerr << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue: "
285 << oss.str() << G4endl;
286 }
287 command->CommandFailed(oss);
288 return;
289 }
290 // A fallback system found...but go back and check this too.
291 ++loopCounter;
292 }
293
294 // A graphics system has been found
295 G4VGraphicsSystem* pSystem = gsl [iGS];
296
297 if (fallback && verbosity >= G4VisManager::warnings) {
298 G4cout << "WARNING: G4VisCommandSceneHandlerCreate::SetNewValue:"
299 "\n Using fallback graphics system: "
300 << pSystem -> GetName ()
301 << " ("
302 << pSystem -> GetNickname ()
303 << ')'
304 << G4endl;
305 }
306
307 // Set current graphics system in preparation for
308 // creating scene handler.
309 fpVisManager -> SetCurrentGraphicsSystem (pSystem);
310 if (verbosity >= G4VisManager::confirmations) {
311 G4cout << "Graphics system set to "
312 << pSystem -> GetName ()
313 << " ("
314 << pSystem -> GetNickname ()
315 << ')'
316 << G4endl;
317 }
318
319 // Now deal with name of scene handler.
320 G4String nextName = NextName ();
321 if (newName == "") {
322 newName = nextName;
323 }
324 if (newName == nextName) fId++;
325
326 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
327 size_t iScene;
328 for (iScene = 0; iScene < list.size (); iScene++) {
329 G4VSceneHandler* sceneHandler = list [iScene];
330 if (sceneHandler -> GetName () == newName) {
331 if (verbosity >= G4VisManager::errors) {
332 G4cerr << "ERROR: Scene handler \"" << newName
333 << "\" already exists." << G4endl;
334 }
335 return;
336 }
337 }
338
339 //Create scene handler.
340 fpVisManager -> CreateSceneHandler (newName);
341 if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) {
342 if (verbosity >= G4VisManager::errors) {
343 G4cerr << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
344 " Curious name mismatch."
345 "\n Current name \""
346 << fpVisManager -> GetCurrentSceneHandler () -> GetName ()
347 << "\" is not the new name \""
348 << newName
349 << "\".\n Please report to vis coordinator."
350 << G4endl;
351 }
352 return;
353 }
354
355 if (verbosity >= G4VisManager::confirmations) {
356 G4cout << "New scene handler \"" << newName << "\" created." << G4endl;
357 }
358
359 // Attach scene.
360 if (fpVisManager -> GetCurrentScene ())
361 G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach");
362}
363
364////////////// /vis/sceneHandler/list ///////////////////////////////////////
365
367 G4bool omitable;
368 fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this);
369 fpCommand -> SetGuidance ("Lists scene handler(s).");
370 fpCommand -> SetGuidance
371 ("\"help /vis/verbose\" for definition of verbosity.");
372 G4UIparameter* parameter;
373 parameter = new G4UIparameter("scene-handler-name", 's', omitable = true);
374 parameter -> SetDefaultValue ("all");
375 fpCommand -> SetParameter (parameter);
376 parameter = new G4UIparameter ("verbosity", 's', omitable = true);
377 parameter -> SetDefaultValue ("warnings");
378 fpCommand -> SetParameter (parameter);
379}
380
382 delete fpCommand;
383}
384
386 return "";
387}
388
390 G4String newValue) {
391 G4String name, verbosityString;
392 std::istringstream is (newValue);
393 is >> name >> verbosityString;
394 G4VisManager::Verbosity verbosity =
395 fpVisManager->GetVerbosityValue(verbosityString);
396 const G4VSceneHandler* currentSceneHandler =
397 fpVisManager -> GetCurrentSceneHandler ();
398 G4String currentName;
399 if (currentSceneHandler) currentName = currentSceneHandler->GetName();
400
401 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
402 G4bool found = false;
403 for (size_t iSH = 0; iSH < list.size (); iSH++) {
404 const G4String& iName = list [iSH] -> GetName ();
405 if (name != "all") {
406 if (name != iName) continue;
407 }
408 found = true;
409 if (iName == currentName) {
410 G4cout << " (current)";
411 }
412 else {
413 G4cout << " ";
414 }
415 G4cout << " scene handler \"" << list [iSH] -> GetName () << "\""
416 << " (" << list [iSH] -> GetGraphicsSystem () -> GetName () << ")";
417 if (verbosity >= G4VisManager::parameters) {
418 G4cout << "\n " << *(list [iSH]);
419 }
420 G4cout << G4endl;
421 }
422 if (!found) {
423 G4cout << "No scene handlers found";
424 if (name != "all") {
425 G4cout << " of name \"" << name << "\"";
426 }
427 G4cout << "." << G4endl;
428 }
429}
430
431////////////// /vis/sceneHandler/select ///////////////////////////////////////
432
434 G4bool omitable;
435 fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this);
436 fpCommand -> SetGuidance ("Selects a scene handler.");
437 fpCommand -> SetGuidance
438 ("Makes the scene handler current. \"/vis/sceneHandler/list\" to see"
439 "\n possible scene handler names.");
440 fpCommand -> SetParameterName ("scene-handler-name",
441 omitable = false);
442}
443
445 delete fpCommand;
446}
447
449 return "";
450}
451
453 G4String newValue) {
454
456
457 G4String& selectName = newValue;
458 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
459
460 size_t iSH;
461 for (iSH = 0; iSH < list.size (); iSH++) {
462 if (list [iSH] -> GetName () == selectName) break;
463 }
464 if (iSH < list.size ()) {
465 if (fpVisManager -> GetCurrentSceneHandler () -> GetName ()
466 == selectName) {
467 if (verbosity >= G4VisManager::confirmations) {
468 G4cout << "Scene handler \"" << selectName << "\""
469 << " already selected." << G4endl;
470 }
471 }
472 else {
473 if (verbosity >= G4VisManager::confirmations) {
474 G4cout << "Scene handler \"" << selectName << "\""
475 << " being selected." << G4endl;
476 }
477 fpVisManager -> SetCurrentSceneHandler (list [iSH]);
478 }
479 }
480 else {
481 if (verbosity >= G4VisManager::errors) {
482 G4cerr << "ERROR: Scene handler \"" << selectName << "\""
483 << " not found - \"/vis/sceneHandler/list\" to see possibilities."
484 << G4endl;
485 }
486 }
487}
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
@ ignoreCase
Definition: G4String.hh:60
G4String strip(G4int strip_Type=trailing, char c=' ')
G4int compareTo(const char *, caseCompare mode=exact) const
void CommandFailed(G4int errCode, G4ExceptionDescription &ed)
Definition: G4UIcommand.hh:179
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
const G4String & GetName() const
static G4VisManager * fpVisManager
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)
static Verbosity GetVerbosity()
static Verbosity GetVerbosityValue(const G4String &)