source: trunk/source/visualization/VRML/src/G4VRML1FileSceneHandler.cc @ 1347

Last change on this file since 1347 was 1347, checked in by garnier, 13 years ago

geant4 tag 9.4

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