Geant4 Cross Reference |
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 30 #include "G4VisCommandsSceneHandler.hh" 31 32 #include "G4VisManager.hh" 33 #include "G4GraphicsSystemList.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 45 G4VisCommandSceneHandlerAttach::G4VisCommandSceneHandlerAttach () { 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 57 G4VisCommandSceneHandlerAttach::~G4VisCommandSceneHandlerAttach () { 58 delete fpCommand; 59 } 60 61 G4String G4VisCommandSceneHandlerAttach::GetCurrentValue (G4UIcommand*) { 62 G4Scene* pScene = fpVisManager -> GetCurrentScene (); 63 return pScene ? pScene -> GetName () : G4String(""); 64 } 65 66 void G4VisCommandSceneHandlerAttach::SetNewValue (G4UIcommand*, 67 G4String newValue) { 68 69 G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity(); 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 139 G4VisCommandSceneHandlerCreate::G4VisCommandSceneHandlerCreate (): fId (0) { 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 = "NO_UI_SESSION "; // A dummy candidate to catch issue 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 171 G4VisCommandSceneHandlerCreate::~G4VisCommandSceneHandlerCreate () { 172 delete fpCommand; 173 } 174 175 G4String G4VisCommandSceneHandlerCreate::NextName () { 176 std::ostringstream oss; 177 oss << "scene-handler-" << fId; 178 return oss.str(); 179 } 180 181 G4String G4VisCommandSceneHandlerCreate::GetCurrentValue(G4UIcommand*) { 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 196 void G4VisCommandSceneHandlerCreate::SetNewValue (G4UIcommand* command, 197 G4String newValue) { 198 199 G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity(); 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) { 209 G4ExceptionDescription ed; 210 ed << 211 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:" 212 " no graphics systems available." 213 "\n Did you instantiate any in" 214 " YourVisManager::RegisterGraphicsSystems()?"; 215 command->CommandFailed(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 if (graphicsSystem == "NO_UI_SESSION") { 241 G4Exception 242 ("G4VisCommandSceneHandlerCreate::SetNewValue","visman1001",JustWarning, 243 "This looks like an attempt to use run-time vis driver selection." 244 "\nYou have issued \"/vis/open\" or \"/vis/sceneHandler/create\" without" 245 "\na parameter for the vis driver. This is allowed only if you instantiate" 246 "\na UI session, and only if it is instantiated *before* the first" 247 "\n\"/vis/open\" command. So:" 248 "\na) It is not allowed in batch mode. If you really want to create" 249 "\n some graphics with a file-writing driver in batch mode, you must" 250 "\n request a specific driver on the \"/vis/open\" command line, e.g.," 251 "\n \"/vis/open TSG_OFFSCREEN\". See, examples/basic/B1/tsg_offscreen.mac." 252 "\nb) If you want to exploit this feature in interactive mode, simply move" 253 "\n the instantiation of the UI session earlier. In any case, this is good" 254 "\n practice in order to capture output in a GUI session."); 255 return; 256 } 257 // Shouldn't happen, since graphicsSystem should be a candidate 258 G4ExceptionDescription ed; 259 ed << 260 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:" 261 "\n Invalid graphics system \"" 262 << graphicsSystem 263 << "\" requested." 264 << "\n Candidates are:"; 265 fpVisManager->PrintAvailableGraphicsSystems(verbosity,ed); 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 std::size_t 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 (std::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 >= nSystems || loopCounter >=3) { 292 G4ExceptionDescription ed; 293 ed << "\"" << gsl[iGSBeingTested]->GetNickname() 294 << "\" is not compatible with the session," 295 "\nand no fallback system found. Make sure your session is" 296 "\ninstantiated _before_ you create a graphics system."; 297 G4Exception("G4VisCommandSceneHandlerCreate::SetNewValue", 298 "visman1002", JustWarning, ed); 299 return; 300 } 301 // A fallback system found...but go back and check this too. 302 ++loopCounter; 303 } 304 305 // A graphics system has been found 306 G4VGraphicsSystem* pSystem = gsl [iGS]; 307 308 if (fallback && verbosity >= G4VisManager::warnings) { 309 G4warn << "WARNING: G4VisCommandSceneHandlerCreate::SetNewValue:" 310 "\n Using fallback graphics system: " 311 << pSystem -> GetName () 312 << " (" 313 << pSystem -> GetNickname () 314 << ')' 315 << G4endl; 316 } 317 318 // Now deal with name of scene handler. 319 G4String nextName = NextName (); 320 if (newName == "") { 321 newName = nextName; 322 } 323 if (newName == nextName) fId++; 324 325 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers (); 326 std::size_t iScene; 327 for (iScene = 0; iScene < list.size (); ++iScene) { 328 G4VSceneHandler* sceneHandler = list [iScene]; 329 if (sceneHandler -> GetName () == newName) { 330 G4ExceptionDescription ed; 331 ed << 332 "ERROR: Scene handler \"" << newName 333 << "\" already exists."; 334 command->CommandFailed(ed); 335 return; 336 } 337 } 338 339 // If there is an existing viewer, store its view parameters and scene tree 340 if (fpVisManager->GetCurrentViewer()) { 341 fThereWasAViewer = true; 342 auto viewer = fpVisManager->GetCurrentViewer(); 343 fExistingVP = viewer->GetViewParameters(); 344 fExistingSceneTree = viewer->AccessSceneTree(); 345 } 346 347 // Set current graphics system in preparation for 348 // creating scene handler. 349 fpVisManager -> SetCurrentGraphicsSystem (pSystem); 350 if (verbosity >= G4VisManager::confirmations) { 351 G4cout << "Graphics system set to " 352 << pSystem -> GetName () 353 << " (" 354 << pSystem -> GetNickname () 355 << ')' 356 << G4endl; 357 } 358 359 //Create scene handler. 360 fpVisManager -> CreateSceneHandler (newName); 361 if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) { 362 G4ExceptionDescription ed; 363 ed << 364 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:" 365 " Curious name mismatch." 366 "\n Current name \"" 367 << fpVisManager -> GetCurrentSceneHandler () -> GetName () 368 << "\" is not the new name \"" 369 << newName 370 << "\".\n Please report to vis coordinator."; 371 command->CommandFailed(ed); 372 return; 373 } 374 375 if (verbosity >= G4VisManager::confirmations) 376 G4cout << "New scene handler \"" << newName << "\" created." << G4endl; 377 378 if (fpVisManager -> GetCurrentScene ()) { 379 auto errorCode = G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach"); 380 if (errorCode) { 381 G4ExceptionDescription ed; 382 ed << "sub-command \"/vis/sceneHandler/attach\" failed."; 383 command->CommandFailed(errorCode,ed); 384 return; 385 } 386 } 387 } 388 389 ////////////// /vis/sceneHandler/list /////////////////////////////////////// 390 391 G4VisCommandSceneHandlerList::G4VisCommandSceneHandlerList () { 392 G4bool omitable; 393 fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this); 394 fpCommand -> SetGuidance ("Lists scene handler(s)."); 395 fpCommand -> SetGuidance 396 ("\"help /vis/verbose\" for definition of verbosity."); 397 G4UIparameter* parameter; 398 parameter = new G4UIparameter("scene-handler-name", 's', omitable = true); 399 parameter -> SetDefaultValue ("all"); 400 fpCommand -> SetParameter (parameter); 401 parameter = new G4UIparameter ("verbosity", 's', omitable = true); 402 parameter -> SetDefaultValue ("warnings"); 403 fpCommand -> SetParameter (parameter); 404 } 405 406 G4VisCommandSceneHandlerList::~G4VisCommandSceneHandlerList () { 407 delete fpCommand; 408 } 409 410 G4String G4VisCommandSceneHandlerList::GetCurrentValue (G4UIcommand*) { 411 return ""; 412 } 413 414 void G4VisCommandSceneHandlerList::SetNewValue (G4UIcommand*, 415 G4String newValue) { 416 G4String name, verbosityString; 417 std::istringstream is (newValue); 418 is >> name >> verbosityString; 419 G4VisManager::Verbosity verbosity = 420 fpVisManager->GetVerbosityValue(verbosityString); 421 const G4VSceneHandler* currentSceneHandler = 422 fpVisManager -> GetCurrentSceneHandler (); 423 G4String currentName; 424 if (currentSceneHandler) currentName = currentSceneHandler->GetName(); 425 426 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers (); 427 G4bool found = false; 428 for (std::size_t iSH = 0; iSH < list.size (); ++iSH) { 429 const G4String& iName = list [iSH] -> GetName (); 430 if (name != "all") { 431 if (name != iName) continue; 432 } 433 found = true; 434 if (iName == currentName) { 435 G4cout << " (current)"; 436 } 437 else { 438 G4cout << " "; 439 } 440 G4cout << " scene handler \"" << list [iSH] -> GetName () << "\"" 441 << " (" << list [iSH] -> GetGraphicsSystem () -> GetName () << ")"; 442 if (verbosity >= G4VisManager::parameters) { 443 G4cout << "\n " << *(list [iSH]); 444 } 445 G4cout << G4endl; 446 } 447 if (!found) { 448 G4cout << "No scene handlers found"; 449 if (name != "all") { 450 G4cout << " of name \"" << name << "\""; 451 } 452 G4cout << "." << G4endl; 453 } 454 } 455 456 ////////////// /vis/sceneHandler/select /////////////////////////////////////// 457 458 G4VisCommandSceneHandlerSelect::G4VisCommandSceneHandlerSelect () { 459 G4bool omitable; 460 fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this); 461 fpCommand -> SetGuidance ("Selects a scene handler."); 462 fpCommand -> SetGuidance 463 ("Makes the scene handler current. \"/vis/sceneHandler/list\" to see" 464 "\n possible scene handler names."); 465 fpCommand -> SetParameterName ("scene-handler-name", 466 omitable = false); 467 } 468 469 G4VisCommandSceneHandlerSelect::~G4VisCommandSceneHandlerSelect () { 470 delete fpCommand; 471 } 472 473 G4String G4VisCommandSceneHandlerSelect::GetCurrentValue (G4UIcommand*) { 474 return ""; 475 } 476 477 void G4VisCommandSceneHandlerSelect::SetNewValue (G4UIcommand*, 478 G4String newValue) { 479 480 G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity(); 481 482 G4String& selectName = newValue; 483 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers (); 484 485 std::size_t iSH; 486 for (iSH = 0; iSH < list.size (); iSH++) { 487 if (list [iSH] -> GetName () == selectName) break; 488 } 489 if (iSH < list.size ()) { 490 if (fpVisManager -> GetCurrentSceneHandler () -> GetName () 491 == selectName) { 492 if (verbosity >= G4VisManager::confirmations) { 493 G4cout << "Scene handler \"" << selectName << "\"" 494 << " already selected." << G4endl; 495 } 496 } 497 else { 498 if (verbosity >= G4VisManager::confirmations) { 499 G4cout << "Scene handler \"" << selectName << "\"" 500 << " being selected." << G4endl; 501 } 502 fpVisManager -> SetCurrentSceneHandler (list [iSH]); 503 } 504 } 505 else { 506 if (verbosity >= G4VisManager::errors) { 507 G4warn << "ERROR: Scene handler \"" << selectName << "\"" 508 << " not found - \"/vis/sceneHandler/list\" to see possibilities." 509 << G4endl; 510 } 511 } 512 } 513