source: trunk/source/persistency/gdml/src/G4GDMLWrite.cc @ 1102

Last change on this file since 1102 was 987, checked in by garnier, 15 years ago

fichiers manquants

File size: 9.5 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: G4GDMLWrite.cc,v 1.50 2008/11/13 17:00:50 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-02-ref-02 $
29//
30// class G4GDMLWrite Implementation
31//
32// Original author: Zoltan Torzsok, November 2007
33//
34// --------------------------------------------------------------------
35
36#include "G4GDMLWrite.hh"
37
38G4bool G4GDMLWrite::addPointerToName = true;
39
40G4bool G4GDMLWrite::FileExists(const G4String& fname) const
41{
42  struct stat FileInfo;
43  return (stat(fname.c_str(),&FileInfo) == 0); 
44}
45
46G4GDMLWrite::VolumeMapType& G4GDMLWrite::VolumeMap()
47{
48   static VolumeMapType instance;
49   return instance;
50}
51
52G4GDMLWrite::PhysVolumeMapType& G4GDMLWrite::PvolumeMap()
53{
54   static PhysVolumeMapType instance;
55   return instance;
56}
57
58G4GDMLWrite::DepthMapType& G4GDMLWrite::DepthMap()
59{
60   static DepthMapType instance;
61   return instance;
62}
63
64G4String G4GDMLWrite::GenerateName(const G4String& name, const void* const ptr)
65{
66   std::stringstream stream; stream << name;
67   if (addPointerToName) { stream << ptr; }
68   return G4String(stream.str());
69}
70
71xercesc::DOMAttr* G4GDMLWrite::NewAttribute(const G4String& name,
72                                            const G4String& value)
73{
74   xercesc::XMLString::transcode(name,tempStr,99);
75   xercesc::DOMAttr* att = doc->createAttribute(tempStr);
76   xercesc::XMLString::transcode(value,tempStr,99);
77   att->setValue(tempStr);
78   return att;
79}
80
81xercesc::DOMAttr* G4GDMLWrite::NewAttribute(const G4String& name,
82                                            const G4double& value)
83{
84   xercesc::XMLString::transcode(name,tempStr,99);
85   xercesc::DOMAttr* att = doc->createAttribute(tempStr);
86   std::ostringstream ostream;
87   ostream.precision(15);
88   ostream << value;
89   G4String str = ostream.str();
90   xercesc::XMLString::transcode(str,tempStr,99);
91   att->setValue(tempStr);
92   return att;
93}
94
95xercesc::DOMElement* G4GDMLWrite::NewElement(const G4String& name)
96{
97   xercesc::XMLString::transcode(name,tempStr,99);
98   return doc->createElement(tempStr);
99}
100
101G4Transform3D G4GDMLWrite::Write(const G4String& fname,
102                                 const G4LogicalVolume* const logvol,
103                                 const G4String& setSchemaLocation,
104                                 const G4int depth,
105                                       G4bool refs)
106{
107   SchemaLocation = setSchemaLocation;
108   addPointerToName = refs;
109
110   if (depth==0) { G4cout << "G4GDML: Writing '" << fname << "'..." << G4endl; }
111   else   { G4cout << "G4GDML: Writing module '" << fname << "'..." << G4endl; }
112   
113   if (FileExists(fname))
114   {
115     G4String ErrorMessage = "File '"+fname+"' already exists!";
116     G4Exception("G4GDMLWrite::Write()", "InvalidSetup",
117                 FatalException, ErrorMessage);
118   }
119   
120   VolumeMap().clear(); // The module map is global for all modules,
121                        // so clear it only at once!
122
123   xercesc::XMLString::transcode("LS", tempStr, 99);
124   xercesc::DOMImplementation* impl =
125     xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
126   xercesc::XMLString::transcode("Range", tempStr, 99);
127   impl = xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
128   xercesc::XMLString::transcode("gdml", tempStr, 99);
129   doc = impl->createDocument(0,tempStr,0);
130   xercesc::DOMElement* gdml = doc->getDocumentElement();
131
132#if XERCES_VERSION_MAJOR >= 3
133                                             // DOM L3 as per Xerces 3.0 API
134    xercesc::DOMLSSerializer* writer =
135      ((xercesc::DOMImplementationLS*)impl)->createLSSerializer();
136
137    xercesc::DOMConfiguration *dc = writer->getDomConfig();
138    dc->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
139
140#else
141
142   xercesc::DOMWriter* writer =
143     ((xercesc::DOMImplementationLS*)impl)->createDOMWriter();
144
145   if (writer->canSetFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
146       writer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
147
148#endif
149
150   gdml->setAttributeNode(NewAttribute("xmlns:xsi",
151                          "http://www.w3.org/2001/XMLSchema-instance"));
152   gdml->setAttributeNode(NewAttribute("xsi:noNamespaceSchemaLocation",
153                          SchemaLocation));
154
155   DefineWrite(gdml);
156   MaterialsWrite(gdml);
157   SolidsWrite(gdml);
158   StructureWrite(gdml);
159   SetupWrite(gdml,logvol);
160
161   G4Transform3D R = TraverseVolumeTree(logvol,depth);
162
163   xercesc::XMLFormatTarget *myFormTarget =
164     new xercesc::LocalFileFormatTarget(fname.c_str());
165
166   try
167   {
168#if XERCES_VERSION_MAJOR >= 3
169                                            // DOM L3 as per Xerces 3.0 API
170      xercesc::DOMLSOutput *theOutput =
171        ((xercesc::DOMImplementationLS*)impl)->createLSOutput();
172      theOutput->setByteStream(myFormTarget);
173      writer->write(doc, theOutput);
174#else
175      writer->writeNode(myFormTarget, *doc);
176#endif
177   }
178   catch (const xercesc::XMLException& toCatch)
179   {
180      char* message = xercesc::XMLString::transcode(toCatch.getMessage());
181      G4cout << "G4GDML: Exception message is: " << message << G4endl;
182      xercesc::XMLString::release(&message);
183      return G4Transform3D::Identity;
184   }
185   catch (const xercesc::DOMException& toCatch)
186   {
187      char* message = xercesc::XMLString::transcode(toCatch.msg);
188      G4cout << "G4GDML: Exception message is: " << message << G4endl;
189      xercesc::XMLString::release(&message);
190      return G4Transform3D::Identity;
191   }
192   catch (...)
193   {   
194      G4cout << "G4GDML: Unexpected Exception!" << G4endl;
195      return G4Transform3D::Identity;
196   }       
197
198   delete myFormTarget;
199   writer->release();
200
201   if (depth==0)
202   {
203     G4cout << "G4GDML: Writing '" << fname << "' done !" << G4endl;
204   }
205   else
206   {
207     G4cout << "G4GDML: Writing module '" << fname << "' done !" << G4endl;
208   }
209
210   return R;
211}
212
213void G4GDMLWrite::AddModule(const G4VPhysicalVolume* const physvol)
214{
215   G4String fname = GenerateName(physvol->GetName(),physvol);
216   G4cout << "G4GDML: Adding module '" << fname << "'..." << G4endl;
217
218   if (physvol == 0)
219   {
220     G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
221                 "Invalid NULL pointer is specified for modularization!");
222   }
223   if (dynamic_cast<const G4PVDivision*>(physvol))
224   {
225     G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
226                 "It is not possible to modularize by divisionvol!");
227   }
228   if (physvol->IsParameterised())
229   {
230     G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
231                 "It is not possible to modularize by parameterised volume!");
232   }
233   if (physvol->IsReplicated())
234   {
235     G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
236                 "It is not possible to modularize by replicated volume!");
237   }
238
239   PvolumeMap()[physvol] = fname;
240}
241
242void G4GDMLWrite::AddModule(const G4int depth)
243{
244   if (depth<0)
245   {
246     G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
247                 "Depth must be a positive number!");
248   }
249   if (DepthMap().find(depth) != DepthMap().end())
250   {
251     G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
252                 "Adding module(s) at this depth is already requested!");
253   }
254   DepthMap()[depth] = 0;
255}
256
257G4String G4GDMLWrite::Modularize( const G4VPhysicalVolume* const physvol,
258                                  const G4int depth )
259{
260   if (PvolumeMap().find(physvol) != PvolumeMap().end())
261   {
262     return PvolumeMap()[physvol]; // Modularize via physvol
263   }
264
265   if (DepthMap().find(depth) != DepthMap().end()) // Modularize via depth
266   {
267     std::stringstream stream;
268     stream << "depth" << depth << "_module" << DepthMap()[depth] << ".gdml";
269     DepthMap()[depth]++;           // There can be more modules at this depth!
270     return G4String(stream.str());
271   }
272
273   return G4String(""); // Empty string for module name = no modularization
274                        // was requested at that level/physvol!
275}
276
277void G4GDMLWrite::SetAddPointerToName(G4bool set)
278{
279   addPointerToName = set;
280}
Note: See TracBrowser for help on using the repository browser.