00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
00090 fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ;
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
00123 const int MAX_FILE_INDEX = fMaxFileNum - 1 ;
00124
00125
00126 strcpy ( fVRMLFileName, fVRMLFileDestDir) ;
00127
00128
00129 strcat ( fVRMLFileName, DEFAULT_WRL_FILE_NAME );
00130
00131
00132 for( int i = 0 ; i < fMaxFileNum ; i++) {
00133
00134
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
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
00156 std::ifstream fin ;
00157 fin.open(fVRMLFileName) ;
00158 if(!fin) {
00159
00160 fin.close();
00161 break;
00162 } else {
00163
00164 fin.close();
00165 }
00166
00167
00168 }
00169
00170
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 );
00187 if( getenv( ENV_VRML_VIEWER ) ) {
00188 strcpy( viewer, getenv( ENV_VRML_VIEWER ) ) ;
00189 }
00190
00191
00192 fDest.close(); fFlagDestOpen = false ;
00193 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00194 G4cout << "*** VRML 1.0 File " << fVRMLFileName << " is generated." << G4endl;
00195
00196
00197
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;