G4VRML1FileSceneHandler.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 // G4VRML1FileSceneHandler.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 
00040 #include "globals.hh"
00041 #include "G4VisManager.hh"
00042 #include "G4Scene.hh"
00043 #include "G4VPhysicalVolume.hh"
00044 #include "G4LogicalVolume.hh"
00045 #include "G4Point3D.hh"
00046 #include "G4VisAttributes.hh"
00047 #include "G4Polyhedron.hh"
00048 #include "G4Box.hh"
00049 #include "G4Cons.hh"
00050 #include "G4Polyline.hh"
00051 #include "G4Trd.hh"
00052 #include "G4Tubs.hh"
00053 #include "G4Trap.hh"
00054 #include "G4Para.hh"
00055 #include "G4Torus.hh"
00056 #include "G4Sphere.hh"
00057 #include "G4Text.hh"
00058 #include "G4Circle.hh"
00059 #include "G4Square.hh"
00060 
00061 #include "G4VRML1FileSceneHandler.hh"
00062 #include "G4VRML1FileViewer.hh"
00063 #include "G4VRML1File.hh"
00064 
00065 // CONST
00066 
00067 const char  WRL_FILE_HEADER      [] = "g4_";
00068 const char  DEFAULT_WRL_FILE_NAME[] = "g4.wrl";
00069 const char  ENV_VRML_VIEWER      [] = "G4VRMLFILE_VIEWER";
00070 const char  NO_VRML_VIEWER       [] = "NONE";
00071 const char  VRMLFILE_DEST_DIR    [] = "G4VRMLFILE_DEST_DIR";
00072 const int   DEFAULT_MAX_WRL_FILE_NUM = 100 ;
00073 
00074 G4VRML1FileSceneHandler::G4VRML1FileSceneHandler(G4VRML1File& system, const G4String& name) :
00075         G4VSceneHandler(system, fSceneIdCount++, name),
00076         fSystem(system),
00077         fDest()   ,
00078         fFlagDestOpen( false ) 
00079 {
00080         fCurrentDEF = "";
00081         strcpy(fVRMLFileName, "");
00082 
00083         if ( getenv( VRMLFILE_DEST_DIR ) == NULL ) {
00084                 strcpy( fVRMLFileDestDir, "" );
00085         } else {
00086                 strcpy( fVRMLFileDestDir, getenv( VRMLFILE_DEST_DIR ) );
00087         }
00088 
00089         // maximum number of g4.wrl files in the dest directory
00090         fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ; // initialization
00091         if ( getenv( "G4VRMLFILE_MAX_FILE_NUM" ) != NULL ) {    
00092                 
00093                 sscanf( getenv("G4VRMLFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum ) ;
00094 
00095         } else {
00096                 fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ;
00097         }
00098         if( fMaxFileNum < 1 ) { fMaxFileNum = 1; }
00099 
00100 }
00101 
00102 
00103 G4VRML1FileSceneHandler::~G4VRML1FileSceneHandler()
00104 {
00105 #if defined DEBUG_FR_SCENE
00106   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00107         G4cout << "***** ~G4VRML1FileSceneHandler" << G4endl;
00108 #endif 
00109         VRMLEndModeling();
00110 }
00111 
00112 
00113 #define  G4VRML1SCENEHANDLER   G4VRML1FileSceneHandler
00114 #define  IS_CONNECTED   this->isConnected() 
00115 #include "G4VRML1SceneHandlerFunc.icc"
00116 #undef   IS_CONNECTED
00117 #undef   G4VRML1SCENEHANDLER
00118 
00119 
00120 void G4VRML1FileSceneHandler::connectPort()
00121 {
00122         // g4_00.wrl, g4_01.wrl, ..., g4_MAX_FILE_INDEX.wrl
00123         const int MAX_FILE_INDEX = fMaxFileNum - 1 ;
00124 
00125         // dest directory (null if no environmental variables is set)
00126         strcpy ( fVRMLFileName, fVRMLFileDestDir) ; 
00127 
00128         // create (full) path name (default)
00129         strcat ( fVRMLFileName, DEFAULT_WRL_FILE_NAME );
00130 
00131         // Determine VRML file name
00132         for( int i = 0 ; i < fMaxFileNum ; i++) { 
00133 
00134                 // Message in the final execution
00135                 if( i == MAX_FILE_INDEX ) 
00136                 {
00137                   if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00138                     G4cout << "==========================================="   << G4endl; 
00139                     G4cout << "WARNING MESSAGE from VRML1FILE driver:     "   << G4endl;
00140                     G4cout << "  This file name is the final one in the   "   << G4endl;
00141                     G4cout << "  automatic updation of the output file name." << G4endl; 
00142                     G4cout << "  You may overwrite existing files, i.e.   "   << G4endl; 
00143                     G4cout << "  g4_XX.wrl.                               "   << G4endl;
00144                     G4cout << "==========================================="   << G4endl; 
00145                   }
00146                 }
00147 
00148                 // re-determine file name as G4VRMLFILE_DEST_DIR/g4_XX.wrl 
00149                 if( i >=  0 && i <= 9 ) { 
00150                         sprintf( fVRMLFileName, "%s%s%s%d.wrl" , fVRMLFileDestDir,  WRL_FILE_HEADER, "0", i );
00151                 } else {
00152                         sprintf( fVRMLFileName, "%s%s%d.wrl"   , fVRMLFileDestDir,  WRL_FILE_HEADER, i );
00153                 }
00154 
00155                 // check validity of the file name
00156                 std::ifstream  fin ; 
00157                 fin.open(fVRMLFileName) ;
00158                 if(!fin) { 
00159                         // new file     
00160                         fin.close();  
00161                         break; 
00162                 } else { 
00163                         // already exists (try next) 
00164                         fin.close(); 
00165                 } 
00166 
00167 
00168         } // for 
00169 
00170         // open a VRML 1.0 file with determined file name
00171         if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00172           G4cout << "==========================================="  << G4endl; 
00173           G4cout << "Output VRML 1.0 file: " << fVRMLFileName << G4endl; 
00174           G4cout << "Maximum number of files in the destination directory: " << fMaxFileNum << G4endl; 
00175           G4cout << "  (Customizable with the environment variable: G4VRMLFILE_MAX_FILE_NUM) " << G4endl;
00176           G4cout << "===========================================" << G4endl; 
00177         }
00178         fDest.open(fVRMLFileName) ;  fFlagDestOpen =  true ;
00179 }
00180 
00181 
00182 void G4VRML1FileSceneHandler::closePort()
00183 {
00184         char command[256] ;
00185         char viewer [256] ; 
00186         strcpy( viewer, NO_VRML_VIEWER ); // initialization
00187         if( getenv( ENV_VRML_VIEWER ) ) {
00188                 strcpy( viewer, getenv( ENV_VRML_VIEWER ) ) ;
00189         }
00190 
00191         // close VRML file      
00192         fDest.close();  fFlagDestOpen = false ;
00193         if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00194           G4cout << "*** VRML 1.0 File  " << fVRMLFileName << "  is generated." << G4endl;
00195 
00196         
00197         // Invoke viewer 
00198 
00199         if ( !strcmp(viewer, NO_VRML_VIEWER )) {
00200           if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00201                 G4cout << "MESSAGE from VRML1FILE driver:"     << G4endl;
00202                 G4cout << "    Set an environmental variable  " ;
00203                 G4cout <<      ENV_VRML_VIEWER << G4endl;
00204                 G4cout << "    if you want to visualize the generated VRML file" << G4endl; 
00205                 G4cout << "    automatically.  For example, " << G4endl;
00206                 G4cout << "    setenv  " << ENV_VRML_VIEWER << "  vrweb " << G4endl;
00207           }
00208         } else {
00209                 sprintf( command, "%s %s", viewer, fVRMLFileName  );   
00210                 (void) system( command );
00211         }
00212 }
00213 
00214 G4int G4VRML1FileSceneHandler::fSceneIdCount = 0;

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