G4VRML2FileSceneHandler.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // G4VRML2FileSceneHandler.cc
00030 // Satoshi Tanaka & Yasuhide Sawada
00031 
00032 
00033 //#define DEBUG_FR_SCENE
00034 
00035 #include <fstream>
00036 #include <stdio.h>
00037 #include <string.h>
00038 #include <stdlib.h>
00039 #include <cmath>
00040 
00041 #include "globals.hh"
00042 #include "G4VPhysicalVolume.hh"
00043 #include "G4LogicalVolume.hh"
00044 #include "G4VisManager.hh"
00045 #include "G4Point3D.hh"
00046 #include "G4VisAttributes.hh"
00047 #include "G4VModel.hh"
00048 #include "G4Scene.hh"
00049 #include "G4Polyhedron.hh"
00050 #include "G4Box.hh"
00051 #include "G4Cons.hh"
00052 #include "G4Polyline.hh"
00053 #include "G4Trd.hh"
00054 #include "G4Tubs.hh"
00055 #include "G4Text.hh"
00056 #include "G4Circle.hh"
00057 #include "G4Square.hh"
00058 
00059 #include "G4VRML2FileSceneHandler.hh"
00060 #include "G4VRML2FileViewer.hh"
00061 #include "G4VRML2File.hh"
00062 
00063 // CONST
00064 
00065 const char  WRL_FILE_HEADER      [] = "g4_";
00066 const char  DEFAULT_WRL_FILE_NAME[] = "g4.wrl";
00067 const char  ENV_VRML_VIEWER      [] = "G4VRMLFILE_VIEWER";
00068 const char  NO_VRML_VIEWER       [] = "NONE";
00069 const char  VRMLFILE_DEST_DIR    [] = "G4VRMLFILE_DEST_DIR";
00070 const int   DEFAULT_MAX_WRL_FILE_NUM = 100 ;
00071 
00072 
00073 G4VRML2FileSceneHandler::G4VRML2FileSceneHandler(G4VRML2File& system, const G4String& name) :
00074         G4VSceneHandler(system, fSceneIdCount++, name),
00075         fSystem(system),
00076         fFlagDestOpen( false ),
00077         fPVPickable  ( false ),
00078         fDest()
00079 {
00080         // output file name
00081         strcpy(fVRMLFileName, "");
00082 
00083         // destination directory
00084         if ( getenv( VRMLFILE_DEST_DIR ) == NULL ) {
00085                 strcpy( fVRMLFileDestDir, "" );
00086         } else {
00087                 strcpy( fVRMLFileDestDir, getenv( VRMLFILE_DEST_DIR ) );
00088         }
00089 
00090 
00091         // maximum number of g4.prim files in the dest directory
00092         fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ; // initialization
00093         if ( getenv( "G4VRMLFILE_MAX_FILE_NUM" ) != NULL ) {    
00094                 
00095                 sscanf( getenv("G4VRMLFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum ) ;
00096 
00097         } else {
00098                 fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ;
00099         }
00100         if( fMaxFileNum < 1 ) { fMaxFileNum = 1; }
00101 
00102 
00103         // PV name pickability  
00104         if( getenv( "G4VRML_PV_PICKABLE" ) != NULL ) {
00105 
00106                 int is_pickable ;
00107                 sscanf( getenv("G4VRML_PV_PICKABLE"), "%d", &is_pickable ) ;
00108 
00109                 if ( is_pickable ) { SetPVPickability ( true ) ; }
00110         } 
00111 
00112         // PV Transparency
00113         SetPVTransparency ();
00114 
00115 }
00116 
00117 
00118 G4VRML2FileSceneHandler::~G4VRML2FileSceneHandler()
00119 {
00120 #if defined DEBUG_FR_SCENE
00121   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00122         G4cout << "***** ~G4VRML2FileSceneHandler" << G4endl;
00123 #endif 
00124         VRMLEndModeling();
00125 }
00126 
00127 
00128 #define  G4VRML2SCENEHANDLER   G4VRML2FileSceneHandler
00129 #define  IS_CONNECTED   this->isConnected() 
00130 #include "G4VRML2SceneHandlerFunc.icc"
00131 #undef   IS_CONNECTED
00132 #undef   G4VRML2SCENEHANDLER
00133 
00134 
00135 void G4VRML2FileSceneHandler::connectPort()
00136 {
00137         // g4_00.wrl, g4_01.wrl, ..., g4_MAX_FILE_INDEX.wrl
00138         const int MAX_FILE_INDEX = fMaxFileNum - 1 ;
00139 
00140         // dest directory (null if no environmental variables is set)
00141         strcpy ( fVRMLFileName, fVRMLFileDestDir) ; 
00142 
00143         // create full path name (default)
00144         strcat ( fVRMLFileName, DEFAULT_WRL_FILE_NAME );
00145 
00146         // Determine VRML file name
00147         for( int i = 0 ; i < fMaxFileNum ; i++) { 
00148 
00149                 // Message in the final execution
00150                 if( i == MAX_FILE_INDEX ) 
00151                 {
00152                   if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00153                     G4cout << "==========================================="   << G4endl; 
00154                     G4cout << "WARNING MESSAGE from VRML2FILE driver:     "   << G4endl;
00155                     G4cout << "  This file name is the final one in the   "   << G4endl;
00156                     G4cout << "  automatic updation of the output file name." << G4endl; 
00157                     G4cout << "  You may overwrite existing files, i.e.   "   << G4endl; 
00158                     G4cout << "  g4_XX.wrl.                               "   << G4endl;
00159                     G4cout << "==========================================="   << G4endl; 
00160                   }
00161                 }
00162 
00163                 // re-determine file name as G4VRMLFILE_DEST_DIR/g4_XX.wrl 
00164                 if( i >=  0 && i <= 9 ) { 
00165                         sprintf( fVRMLFileName, "%s%s%s%d.wrl" , fVRMLFileDestDir,  WRL_FILE_HEADER, "0", i );
00166                 } else {
00167                         sprintf( fVRMLFileName, "%s%s%d.wrl"   , fVRMLFileDestDir,  WRL_FILE_HEADER, i );
00168                 }
00169 
00170                 // check validity of the file name
00171                 std::ifstream  fin ; 
00172                 fin.open(fVRMLFileName) ;
00173                 if(!fin) { 
00174                         // new file     
00175                         fin.close();  
00176                         break; 
00177                 } else { 
00178                         // already exists (try next) 
00179                         fin.close(); 
00180                 } 
00181 
00182         } // for 
00183 
00184         // open a VRML 2.0 file with determined file name
00185         if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00186           G4cout << "==========================================="  << G4endl; 
00187           G4cout << "Output VRML 2.0 file: " <<    fVRMLFileName << G4endl; 
00188           G4cout << "Maximum number of files in the destination directory: " << fMaxFileNum << G4endl; 
00189           G4cout << "  (Customizable with the environment variable: G4VRMLFILE_MAX_FILE_NUM) " << G4endl;
00190           G4cout << "===========================================" << G4endl; 
00191         }
00192         fDest.open(fVRMLFileName) ;  fFlagDestOpen =  true ;
00193 }
00194 
00195 
00196 void G4VRML2FileSceneHandler::closePort()
00197 {
00198         char command[256] ;
00199         char viewer [256] ; 
00200         strcpy( viewer, NO_VRML_VIEWER ); // initialization
00201         if( getenv( ENV_VRML_VIEWER ) ) {
00202                 strcpy( viewer, getenv( ENV_VRML_VIEWER ) ) ;
00203         }
00204 
00205         // close VRML file      
00206         fDest.close();  fFlagDestOpen = false ;
00207         if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00208               G4cout << "*** VRML 2.0 File  " << fVRMLFileName << "  is generated." << G4endl;
00209 
00210         
00211         // Invoke viewer 
00212 
00213         if ( !strcmp(viewer, NO_VRML_VIEWER )) {
00214           if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00215                 G4cout << "MESSAGE from VRML2FILE driver:"     << G4endl;
00216                 G4cout << "    Set an environmental variable  " ;
00217                 G4cout <<      ENV_VRML_VIEWER << G4endl;
00218                 G4cout << "    if you want to visualize the generated VRML file" << G4endl; 
00219                 G4cout << "    automatically.  For example, " << G4endl;
00220                 G4cout << "    setenv  " << ENV_VRML_VIEWER << "  vrwave " << G4endl;
00221           }
00222         } else {
00223                 sprintf( command, "%s %s", viewer, fVRMLFileName  );   
00224                 (void) system( command );
00225         }
00226 }
00227 
00228 G4int G4VRML2FileSceneHandler::fSceneIdCount = 0;

Generated on Mon May 27 17:50:21 2013 for Geant4 by  doxygen 1.4.7