Geant4-11
G4VRML2FileSceneHandler.cc
Go to the documentation of this file.
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//
28// G4VRML2FileSceneHandler.cc
29// Satoshi Tanaka & Yasuhide Sawada
30
31//#define DEBUG_FR_SCENE
32
33#include <fstream>
34#include <stdio.h>
35#include <string.h>
36#include <stdlib.h>
37#include <cmath>
38#include <sstream>
39#include <iomanip>
40
41#include "globals.hh"
42#include "G4VPhysicalVolume.hh"
43#include "G4LogicalVolume.hh"
44#include "G4VisManager.hh"
45#include "G4Point3D.hh"
46#include "G4VisAttributes.hh"
47#include "G4VModel.hh"
48#include "G4Scene.hh"
49#include "G4Polyhedron.hh"
50#include "G4Box.hh"
51#include "G4Cons.hh"
52#include "G4Polyline.hh"
53#include "G4Trd.hh"
54#include "G4Tubs.hh"
55#include "G4Text.hh"
56#include "G4Circle.hh"
57#include "G4Square.hh"
58
60#include "G4VRML2FileViewer.hh"
61#include "G4VRML2File.hh"
62
63// CONST
64
65const char WRL_FILE_HEADER[] = "g4_";
66const char ENV_WRL_FILE_HEADER[] = "G4VRMLFILE_HEADER";
67const char ENV_WRL_FILE_NAME[] = "G4VRMLFILE_FILE_NAME";
68const char ENV_VRML_VIEWER[] = "G4VRMLFILE_VIEWER";
69const char NO_VRML_VIEWER[] = "NONE";
70const char VRMLFILE_DEST_DIR[] = "G4VRMLFILE_DEST_DIR";
72
74 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(std::getenv(VRMLFILE_DEST_DIR) == NULL)
86 {
87 strcpy(fVRMLFileDestDir, "");
88 }
89 else
90 {
91 strcpy(fVRMLFileDestDir, std::getenv(VRMLFILE_DEST_DIR));
92 }
93
94 // maximum number of g4.prim files in the dest directory
95 fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM; // initialization
96 if(std::getenv("G4VRMLFILE_MAX_FILE_NUM") != NULL)
97 {
98 sscanf(std::getenv("G4VRMLFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum);
99 }
100 else
101 {
103 }
104 if(fMaxFileNum < 1)
105 {
106 fMaxFileNum = 1;
107 }
108
109 // PV name pickability
110 if(std::getenv("G4VRML_PV_PICKABLE") != NULL)
111 {
112 int is_pickable;
113 sscanf(std::getenv("G4VRML_PV_PICKABLE"), "%d", &is_pickable);
114
115 if(is_pickable)
116 {
117 SetPVPickability(true);
118 }
119 }
120
121 // PV Transparency
123}
124
126{
127#if defined DEBUG_FR_SCENE
129 G4cout << "***** ~G4VRML2FileSceneHandler" << G4endl;
130#endif
132}
133
134#define G4VRML2SCENEHANDLER G4VRML2FileSceneHandler
135#define IS_CONNECTED this->isConnected()
137#undef IS_CONNECTED
138#undef G4VRML2SCENEHANDLER
139
141{
142 // g4_00.wrl, g4_01.wrl, ..., g4_MAX_FILE_INDEX.wrl
143 const int MAX_FILE_INDEX = fMaxFileNum - 1;
144
145 // dest directory (null if no environmental variables is set)
147 // add filename if environment variable supplied
148 if(std::getenv(ENV_WRL_FILE_NAME))
149 {
150 strcat(fVRMLFileName, std::getenv(ENV_WRL_FILE_NAME));
151 }
152 else
153 {
154 // Determine VRML file name
155 for(int i = 0; i < fMaxFileNum; i++)
156 {
157 // Message in the final execution
158 if(i == MAX_FILE_INDEX)
159 {
161 {
162 G4cout << "===========================================" << G4endl;
163 G4cout << "WARNING MESSAGE from VRML2FILE driver: " << G4endl;
164 G4cout << " This file name is the final one in the " << G4endl;
165 G4cout << " automatic updation of the output file name." << G4endl;
166 G4cout << " You may overwrite existing files, i.e. " << G4endl;
167 G4cout << " g4_XX.wrl. " << G4endl;
168 G4cout << "===========================================" << G4endl;
169 }
170 }
171
172 // re-determine file name as G4VRMLFILE_DEST_DIR/g4_XX.wrl
173 std::ostringstream filename;
174 filename << fVRMLFileDestDir;
175 if(std::getenv(ENV_WRL_FILE_HEADER) == NULL)
176 {
177 filename << WRL_FILE_HEADER;
178 }
179 else
180 {
181 filename << std::getenv(ENV_WRL_FILE_HEADER) << '_';
182 }
183 filename << std::setw(2) << std::setfill('0') << i << ".wrl";
184 strncpy(fVRMLFileName, filename.str().c_str(), sizeof(fVRMLFileName) - 1);
185 fVRMLFileName[sizeof(fVRMLFileName) - 1] = '\0';
186
187 // check validity of the file name
188 std::ifstream fin;
189 fin.open(fVRMLFileName);
190 if(!fin)
191 {
192 // new file
193 fin.close();
194 break;
195 }
196 else
197 {
198 // already exists (try next)
199 fin.close();
200 }
201
202 } // for
203 }
204
205 // open a VRML 2.0 file with determined file name
207 {
208 G4cout << "===========================================" << G4endl;
209 G4cout << "Output VRML 2.0 file: " << fVRMLFileName << G4endl;
210 G4cout << "Maximum number of files in the destination directory: "
211 << fMaxFileNum << G4endl;
212 G4cout << " (Customizable with the environment variable: "
213 "G4VRMLFILE_MAX_FILE_NUM) "
214 << G4endl;
215 G4cout << "===========================================" << G4endl;
216 }
217 fDest.open(fVRMLFileName);
218 fFlagDestOpen = true;
219}
220
222{
223 char command[256];
224 char viewer[256];
225 strcpy(viewer, NO_VRML_VIEWER); // initialization
226 if(std::getenv(ENV_VRML_VIEWER))
227 {
228 strcpy(viewer, std::getenv(ENV_VRML_VIEWER));
229 }
230
231 // close VRML file
232 fDest.close();
233 fFlagDestOpen = false;
235 G4cout << "*** VRML 2.0 File " << fVRMLFileName << " is generated."
236 << G4endl;
237
238 // Invoke viewer
239
240 if(!strcmp(viewer, NO_VRML_VIEWER))
241 {
243 {
244 G4cout << "MESSAGE from VRML2FILE driver:" << G4endl;
245 G4cout << " Set an environmental variable ";
247 G4cout << " if you want to visualize the generated VRML file"
248 << G4endl;
249 G4cout << " automatically. For example, " << G4endl;
250 G4cout << " setenv " << ENV_VRML_VIEWER << " vrwave " << G4endl;
251 G4cout << "ALSO you may change the file header with "
253 G4cout << " or the whole filename with " << ENV_WRL_FILE_NAME
254 << G4endl;
255 }
256 }
257 else
258 {
259 std::ostringstream ossCommand;
260 ossCommand << viewer << ' ' << fVRMLFileName;
261 strncpy(command, ossCommand.str().c_str(), sizeof(command) - 1);
262 command[sizeof(command) - 1] = '\0';
263 int iErr = system(command);
264 if(iErr != 0)
265 {
267 ed << "Error " << iErr << " when calling system with \"" << command
268 << "\".";
269 G4Exception("G4VRML2FileSceneHandler::closePort()", "VRML-2006",
270 JustWarning, ed);
271 }
272 }
273}
274
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
int G4int
Definition: G4Types.hh:85
const int DEFAULT_MAX_WRL_FILE_NUM
const char ENV_WRL_FILE_NAME[]
const char WRL_FILE_HEADER[]
const char VRMLFILE_DEST_DIR[]
const char NO_VRML_VIEWER[]
const char ENV_WRL_FILE_HEADER[]
const char ENV_VRML_VIEWER[]
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
G4VRML2FileSceneHandler(G4VRML2File &system, const G4String &name="")
G4double SetPVTransparency()
void SetPVPickability(G4bool on_off)
static Verbosity GetVerbosity()
const char * name(G4int ptype)