| [834] | 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 | // $Id: G4VRML1FileSceneHandler.cc,v 1.12 2006/06/29 21:25:57 gunter Exp $
|
|---|
| [850] | 28 | // GEANT4 tag $Name: HEAD $
|
|---|
| [834] | 29 | //
|
|---|
| 30 | // G4VRML1FileSceneHandler.cc
|
|---|
| 31 | // Satoshi Tanaka & Yasuhide Sawada
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | //#define DEBUG_FR_SCENE
|
|---|
| 35 |
|
|---|
| 36 | #include <fstream>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <string.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "globals.hh"
|
|---|
| 42 | #include "G4Scene.hh"
|
|---|
| 43 | #include "G4VPhysicalVolume.hh"
|
|---|
| 44 | #include "G4LogicalVolume.hh"
|
|---|
| 45 | #include "G4Point3D.hh"
|
|---|
| 46 | #include "G4VisAttributes.hh"
|
|---|
| 47 | #include "G4Polyhedron.hh"
|
|---|
| 48 | #include "G4Box.hh"
|
|---|
| 49 | #include "G4Cons.hh"
|
|---|
| 50 | #include "G4Polyline.hh"
|
|---|
| 51 | #include "G4Trd.hh"
|
|---|
| 52 | #include "G4Tubs.hh"
|
|---|
| 53 | #include "G4Trap.hh"
|
|---|
| 54 | #include "G4Para.hh"
|
|---|
| 55 | #include "G4Torus.hh"
|
|---|
| 56 | #include "G4Sphere.hh"
|
|---|
| 57 | #include "G4Text.hh"
|
|---|
| 58 | #include "G4Circle.hh"
|
|---|
| 59 | #include "G4Square.hh"
|
|---|
| 60 |
|
|---|
| 61 | #include "G4VRML1FileSceneHandler.hh"
|
|---|
| 62 | #include "G4VRML1FileViewer.hh"
|
|---|
| 63 | #include "G4VRML1File.hh"
|
|---|
| 64 |
|
|---|
| 65 | // CONST
|
|---|
| 66 |
|
|---|
| 67 | const char WRL_FILE_HEADER [] = "g4_";
|
|---|
| 68 | const char DEFAULT_WRL_FILE_NAME[] = "g4.wrl";
|
|---|
| 69 | const char ENV_VRML_VIEWER [] = "G4VRMLFILE_VIEWER";
|
|---|
| 70 | const char NO_VRML_VIEWER [] = "NONE";
|
|---|
| 71 | const char VRMLFILE_DEST_DIR [] = "G4VRMLFILE_DEST_DIR";
|
|---|
| 72 | const int DEFAULT_MAX_WRL_FILE_NUM = 100 ;
|
|---|
| 73 |
|
|---|
| 74 | G4VRML1FileSceneHandler::G4VRML1FileSceneHandler(G4VRML1File& system, const G4String& name) :
|
|---|
| 75 | G4VSceneHandler(system, fSceneIdCount++, name),
|
|---|
| 76 | fSystem(system),
|
|---|
| 77 | fDest() ,
|
|---|
| 78 | fFlagDestOpen( false )
|
|---|
| 79 | {
|
|---|
| 80 | fCurrentDEF = "";
|
|---|
| 81 | strcpy(fVRMLFileName, "");
|
|---|
| 82 |
|
|---|
| 83 | if ( getenv( VRMLFILE_DEST_DIR ) == NULL ) {
|
|---|
| 84 | strcpy( fVRMLFileDestDir, "" );
|
|---|
| 85 | } else {
|
|---|
| 86 | strcpy( fVRMLFileDestDir, getenv( VRMLFILE_DEST_DIR ) );
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | // maximum number of g4.wrl files in the dest directory
|
|---|
| 90 | fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ; // initialization
|
|---|
| 91 | if ( getenv( "G4VRMLFILE_MAX_FILE_NUM" ) != NULL ) {
|
|---|
| 92 |
|
|---|
| 93 | sscanf( getenv("G4VRMLFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum ) ;
|
|---|
| 94 |
|
|---|
| 95 | } else {
|
|---|
| 96 | fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ;
|
|---|
| 97 | }
|
|---|
| 98 | if( fMaxFileNum < 1 ) { fMaxFileNum = 1; }
|
|---|
| 99 |
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | G4VRML1FileSceneHandler::~G4VRML1FileSceneHandler()
|
|---|
| 104 | {
|
|---|
| 105 | #if defined DEBUG_FR_SCENE
|
|---|
| 106 | G4cerr << "***** ~G4VRML1FileSceneHandler" << G4endl;
|
|---|
| 107 | #endif
|
|---|
| 108 | VRMLEndModeling();
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | #define G4VRML1SCENEHANDLER G4VRML1FileSceneHandler
|
|---|
| 113 | #define IS_CONNECTED this->isConnected()
|
|---|
| 114 | #include "G4VRML1SceneHandlerFunc.icc"
|
|---|
| 115 | #undef IS_CONNECTED
|
|---|
| 116 | #undef G4VRML1SCENEHANDLER
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | void G4VRML1FileSceneHandler::connectPort()
|
|---|
| 120 | {
|
|---|
| 121 | // g4_00.wrl, g4_01.wrl, ..., g4_MAX_FILE_INDEX.wrl
|
|---|
| 122 | const int MAX_FILE_INDEX = fMaxFileNum - 1 ;
|
|---|
| 123 |
|
|---|
| 124 | // dest directory (null if no environmental variables is set)
|
|---|
| 125 | strcpy ( fVRMLFileName, fVRMLFileDestDir) ;
|
|---|
| 126 |
|
|---|
| 127 | // create (full) path name (default)
|
|---|
| 128 | strcat ( fVRMLFileName, DEFAULT_WRL_FILE_NAME );
|
|---|
| 129 |
|
|---|
| 130 | // Determine VRML file name
|
|---|
| 131 | for( int i = 0 ; i < fMaxFileNum ; i++) {
|
|---|
| 132 |
|
|---|
| 133 | // Message in the final execution
|
|---|
| 134 | if( i == MAX_FILE_INDEX )
|
|---|
| 135 | {
|
|---|
| 136 | G4cerr << "===========================================" << G4endl;
|
|---|
| 137 | G4cerr << "WARNING MESSAGE from VRML1FILE driver: " << G4endl;
|
|---|
| 138 | G4cerr << " This file name is the final one in the " << G4endl;
|
|---|
| 139 | G4cerr << " automatic updation of the output file name." << G4endl;
|
|---|
| 140 | G4cerr << " You may overwrite existing files, i.e. " << G4endl;
|
|---|
| 141 | G4cerr << " g4_XX.wrl. " << G4endl;
|
|---|
| 142 | G4cerr << "===========================================" << G4endl;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | // re-determine file name as G4VRMLFILE_DEST_DIR/g4_XX.wrl
|
|---|
| 146 | if( i >= 0 && i <= 9 ) {
|
|---|
| 147 | sprintf( fVRMLFileName, "%s%s%s%d.wrl" , fVRMLFileDestDir, WRL_FILE_HEADER, "0", i );
|
|---|
| 148 | } else {
|
|---|
| 149 | sprintf( fVRMLFileName, "%s%s%d.wrl" , fVRMLFileDestDir, WRL_FILE_HEADER, i );
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | // check validity of the file name
|
|---|
| 153 | std::ifstream fin ;
|
|---|
| 154 | fin.open(fVRMLFileName) ;
|
|---|
| 155 | if(!fin) {
|
|---|
| 156 | // new file
|
|---|
| 157 | fin.close();
|
|---|
| 158 | break;
|
|---|
| 159 | } else {
|
|---|
| 160 | // already exists (try next)
|
|---|
| 161 | fin.close();
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 | } // for
|
|---|
| 166 |
|
|---|
| 167 | // open a VRML 1.0 file with determined file name
|
|---|
| 168 | G4cerr << "===========================================" << G4endl;
|
|---|
| 169 | G4cerr << "Output VRML 1.0 file: " << fVRMLFileName << G4endl;
|
|---|
| 170 | G4cerr << "Maximum number of files in the destination directory: " << fMaxFileNum << G4endl;
|
|---|
| 171 | G4cerr << " (Customizable with the environment variable: G4VRMLFILE_MAX_FILE_NUM) " << G4endl;
|
|---|
| 172 | G4cerr << "===========================================" << G4endl;
|
|---|
| 173 |
|
|---|
| 174 | fDest.open(fVRMLFileName) ; fFlagDestOpen = true ;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 | void G4VRML1FileSceneHandler::closePort()
|
|---|
| 179 | {
|
|---|
| 180 | char command[256] ;
|
|---|
| 181 | char viewer [256] ;
|
|---|
| 182 | strcpy( viewer, NO_VRML_VIEWER ); // initialization
|
|---|
| 183 | if( getenv( ENV_VRML_VIEWER ) ) {
|
|---|
| 184 | strcpy( viewer, getenv( ENV_VRML_VIEWER ) ) ;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | // close VRML file
|
|---|
| 188 | fDest.close(); fFlagDestOpen = false ;
|
|---|
| 189 | G4cerr << "*** VRML 1.0 File " << fVRMLFileName << " is generated." << G4endl;
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 | // Invoke viewer
|
|---|
| 193 |
|
|---|
| 194 | if ( !strcmp(viewer, NO_VRML_VIEWER )) {
|
|---|
| 195 | G4cerr << "MESSAGE from VRML1FILE driver:" << G4endl;
|
|---|
| 196 | G4cerr << " Set an environmental variable " ;
|
|---|
| 197 | G4cerr << ENV_VRML_VIEWER << G4endl;
|
|---|
| 198 | G4cerr << " if you want to visualize the generated VRML file" << G4endl;
|
|---|
| 199 | G4cerr << " automatically. For example, " << G4endl;
|
|---|
| 200 | G4cerr << " setenv " << ENV_VRML_VIEWER << " vrweb " << G4endl;
|
|---|
| 201 | } else {
|
|---|
| 202 | sprintf( command, "%s %s", viewer, fVRMLFileName );
|
|---|
| 203 | system( command );
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | G4int G4VRML1FileSceneHandler::fSceneIdCount = 0;
|
|---|