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 #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
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
00081 strcpy(fVRMLFileName, "");
00082
00083
00084 if ( getenv( VRMLFILE_DEST_DIR ) == NULL ) {
00085 strcpy( fVRMLFileDestDir, "" );
00086 } else {
00087 strcpy( fVRMLFileDestDir, getenv( VRMLFILE_DEST_DIR ) );
00088 }
00089
00090
00091
00092 fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ;
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
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
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
00138 const int MAX_FILE_INDEX = fMaxFileNum - 1 ;
00139
00140
00141 strcpy ( fVRMLFileName, fVRMLFileDestDir) ;
00142
00143
00144 strcat ( fVRMLFileName, DEFAULT_WRL_FILE_NAME );
00145
00146
00147 for( int i = 0 ; i < fMaxFileNum ; i++) {
00148
00149
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
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
00171 std::ifstream fin ;
00172 fin.open(fVRMLFileName) ;
00173 if(!fin) {
00174
00175 fin.close();
00176 break;
00177 } else {
00178
00179 fin.close();
00180 }
00181
00182 }
00183
00184
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 );
00201 if( getenv( ENV_VRML_VIEWER ) ) {
00202 strcpy( viewer, getenv( ENV_VRML_VIEWER ) ) ;
00203 }
00204
00205
00206 fDest.close(); fFlagDestOpen = false ;
00207 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00208 G4cout << "*** VRML 2.0 File " << fVRMLFileName << " is generated." << G4endl;
00209
00210
00211
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;