Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G03ColorReader.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 /// \file persistency/gdml/G03/src/G03ColorReader.cc
27 /// \brief Implementation of the G03ColorReader class
28 //
29 // $Id: G03ColorReader.cc 68025 2013-03-13 13:43:46Z gcosmo $
30 //
31 // --------------------------------------------------------------------
32 
33 #include "G03ColorReader.hh"
34 
35 #include "G4LogicalVolume.hh"
36 #include "G4VisAttributes.hh"
37 
38 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
39 
42 {
43 }
44 
45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
46 
48 {
49  std::map<G4String, G4VisAttributes*>::iterator pos;
50  for (pos=fAttribs.begin(); pos!=fAttribs.end(); pos++)
51  { delete pos->second; }
52 }
53 
54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
55 
56 void G03ColorReader::ExtensionRead(const xercesc::DOMElement* const extElement)
57 {
58  G4cout << "G4GDML: Reading GDML extension..." << G4endl;
59 
60  for (xercesc::DOMNode* iter = extElement->getFirstChild();
61  iter != 0; iter = iter->getNextSibling())
62  {
63  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
64 
65  const xercesc::DOMElement* const child
66  = dynamic_cast<xercesc::DOMElement*>(iter);
67  const G4String tag = Transcode(child->getTagName());
68 
69  if (tag=="color") { ColorRead(child); }
70  else
71  {
72  G4String error_msg = "Unknown tag in structure: " + tag;
73  G4Exception("G03ColorReader::ExtensionRead()",
74  "ReadError", FatalException, error_msg);
75  }
76  }
77 }
78 
79 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
80 
81 void G03ColorReader::VolumeRead(const xercesc::DOMElement* const volumeElement)
82 {
83  G4VSolid* solidPtr = 0;
84  G4Material* materialPtr = 0;
85  G4VisAttributes* attrPtr = 0;
86  G4GDMLAuxListType auxList;
87 
88  XMLCh *name_attr = xercesc::XMLString::transcode("name");
89  const G4String name = Transcode(volumeElement->getAttribute(name_attr));
90  xercesc::XMLString::release(&name_attr);
91 
92  for (xercesc::DOMNode* iter = volumeElement->getFirstChild();
93  iter != 0; iter = iter->getNextSibling())
94  {
95  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
96 
97  const xercesc::DOMElement* const child
98  = dynamic_cast<xercesc::DOMElement*>(iter);
99  const G4String tag = Transcode(child->getTagName());
100 
101  if (tag=="auxiliary")
102  { auxList.push_back(AuxiliaryRead(child)); } else
103  if (tag=="materialref")
104  { materialPtr = GetMaterial(GenerateName(RefRead(child),true)); } else
105  if (tag=="solidref")
106  { solidPtr = GetSolid(GenerateName(RefRead(child))); } else
107  if (tag == "colorref")
108  { attrPtr = GetVisAttribute(GenerateName(RefRead(child))); }
109  }
110 
111  pMotherLogical = new G4LogicalVolume(solidPtr,materialPtr,
112  GenerateName(name),0,0,0);
114 
115  if (!auxList.empty()) { auxMap[pMotherLogical] = auxList; }
116 
117  Volume_contentRead(volumeElement);
118 }
119 
120 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
121 
122 void G03ColorReader::ColorRead(const xercesc::DOMElement* const colorElement)
123 {
124  G4String name;
125  G4VisAttributes* color = 0;
126  G4double r=0., g=0., b=0., a=0.;
127 
128  const xercesc::DOMNamedNodeMap* const attributes
129  = colorElement->getAttributes();
130  XMLSize_t attributeCount = attributes->getLength();
131 
132  for (XMLSize_t attribute_index=0;
133  attribute_index<attributeCount; attribute_index++)
134  {
135  xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
136 
137  if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
138  { continue; }
139 
140  const xercesc::DOMAttr* const attribute
141  = dynamic_cast<xercesc::DOMAttr*>(attribute_node);
142  const G4String attName = Transcode(attribute->getName());
143  const G4String attValue = Transcode(attribute->getValue());
144 
145  if (attName=="name")
146  { name = GenerateName(attValue); } else
147  if (attName=="R")
148  { r = eval.Evaluate(attValue); } else
149  if (attName=="G")
150  { g = eval.Evaluate(attValue); } else
151  if (attName=="B")
152  { b = eval.Evaluate(attValue); } else
153  if (attName=="A")
154  { a = eval.Evaluate(attValue); }
155  }
156 
157  G4cout << "Color attribute (R,G,B,A) is: "
158  << r << ", " << g << ", " << b << ", " << a << " !" << G4endl;
159  color = new G4VisAttributes(G4Color(r,g,b,a));
160  fAttribs.insert(std::make_pair(name,color));
161 }
162 
163 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
164 
166 {
167  G4VisAttributes* col = 0;
168  std::map<G4String, G4VisAttributes*>::iterator pos = fAttribs.find(ref);
169 
170  if (pos != fAttribs.end())
171  {
172  col = pos->second;
173  }
174  else
175  {
176  G4String err_mess = "Attribute: " + ref + " NOT found !";
177  G4Exception("G03ColorReader::GetVisAttribute()",
178  "ReadError", FatalException, err_mess);
179  }
180  return col;
181 }
G4GDMLEvaluator eval
Definition: G4GDMLRead.hh:144
G4Material * GetMaterial(const G4String &, G4bool verbose=true) const
Definition: xmlparse.cc:179
void ColorRead(const xercesc::DOMElement *const element)
G4String Transcode(const XMLCh *const)
Definition: G4GDMLRead.cc:55
const XML_Char * name
G4GDMLAuxPairType AuxiliaryRead(const xercesc::DOMElement *const)
G4VSolid * GetSolid(const G4String &) const
G4String RefRead(const xercesc::DOMElement *const)
function g(Y1, Y2, PT2)
Definition: hijing1.383.f:5205
G4GLOB_DLL std::ostream G4cout
Definition of the G03ColorReader class.
G4Colour G4Color
Definition: G4Color.hh:42
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4LogicalVolume * pMotherLogical
std::vector< G4GDMLAuxPairType > G4GDMLAuxListType
G4String GenerateName(const G4String &name, G4bool strip=false)
Definition: G4GDMLRead.cc:68
virtual void Volume_contentRead(const xercesc::DOMElement *const)
#define G4endl
Definition: G4ios.hh:61
void ExtensionRead(const xercesc::DOMElement *const element)
G4VisAttributes * GetVisAttribute(const G4String &ref)
double G4double
Definition: G4Types.hh:76
virtual void VolumeRead(const xercesc::DOMElement *const)
void SetVisAttributes(const G4VisAttributes *pVA)
G4double Evaluate(const G4String &)