source: trunk/source/visualization/VRML/src/G4VRML2FileSceneHandler.cc@ 1357

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

geant4 tag 9.4

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