Geant4 Cross Reference |
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 // 30 // -------------------------------------------------------------------- 31 32 #include "G03ColorReader.hh" 33 34 #include "G4LogicalVolume.hh" 35 #include "G4VisAttributes.hh" 36 37 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 38 39 G03ColorReader::G03ColorReader() : G4GDMLReadStructure() {} 40 41 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 42 43 G03ColorReader::~G03ColorReader() 44 { 45 std::map<G4String, G4VisAttributes*>::iterator pos; 46 for (pos = fAttribs.begin(); pos != fAttribs.end(); pos++) { 47 delete pos->second; 48 } 49 } 50 51 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 52 53 void G03ColorReader::ExtensionRead(const xercesc::DOMElement* const extElement) 54 { 55 G4cout << "G4GDML: Reading GDML extension..." << G4endl; 56 57 for (xercesc::DOMNode* iter = extElement->getFirstChild(); iter != 0; 58 iter = iter->getNextSibling()) 59 { 60 if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { 61 continue; 62 } 63 64 const xercesc::DOMElement* const child = dynamic_cast<xercesc::DOMElement*>(iter); 65 const G4String tag = Transcode(child->getTagName()); 66 67 if (tag == "color") { 68 ColorRead(child); 69 } 70 else { 71 G4String error_msg = "Unknown tag in structure: " + tag; 72 G4Exception("G03ColorReader::ExtensionRead()", "ReadError", FatalException, error_msg); 73 } 74 } 75 } 76 77 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 78 79 void G03ColorReader::VolumeRead(const xercesc::DOMElement* const volumeElement) 80 { 81 G4VSolid* solidPtr = 0; 82 G4Material* materialPtr = 0; 83 G4VisAttributes* attrPtr = 0; 84 G4GDMLAuxListType auxList; 85 86 XMLCh* name_attr = xercesc::XMLString::transcode("name"); 87 const G4String name = Transcode(volumeElement->getAttribute(name_attr)); 88 xercesc::XMLString::release(&name_attr); 89 90 for (xercesc::DOMNode* iter = volumeElement->getFirstChild(); iter != 0; 91 iter = iter->getNextSibling()) 92 { 93 if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { 94 continue; 95 } 96 97 const xercesc::DOMElement* const child = dynamic_cast<xercesc::DOMElement*>(iter); 98 const G4String tag = Transcode(child->getTagName()); 99 100 if (tag == "auxiliary") { 101 auxList.push_back(AuxiliaryRead(child)); 102 } 103 else if (tag == "materialref") { 104 materialPtr = GetMaterial(GenerateName(RefRead(child), true)); 105 } 106 else if (tag == "solidref") { 107 solidPtr = GetSolid(GenerateName(RefRead(child))); 108 } 109 else if (tag == "colorref") { 110 attrPtr = GetVisAttribute(GenerateName(RefRead(child))); 111 } 112 } 113 114 pMotherLogical = new G4LogicalVolume(solidPtr, materialPtr, GenerateName(name), 0, 0, 0); 115 pMotherLogical->SetVisAttributes(attrPtr); 116 117 if (!auxList.empty()) { 118 auxMap[pMotherLogical] = auxList; 119 } 120 121 Volume_contentRead(volumeElement); 122 } 123 124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 125 126 void G03ColorReader::ColorRead(const xercesc::DOMElement* const colorElement) 127 { 128 G4String name; 129 G4VisAttributes* color = 0; 130 G4double r = 0., g = 0., b = 0., a = 0.; 131 132 const xercesc::DOMNamedNodeMap* const attributes = colorElement->getAttributes(); 133 XMLSize_t attributeCount = attributes->getLength(); 134 135 for (XMLSize_t attribute_index = 0; attribute_index < attributeCount; attribute_index++) { 136 xercesc::DOMNode* attribute_node = attributes->item(attribute_index); 137 138 if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) { 139 continue; 140 } 141 142 const xercesc::DOMAttr* const attribute = dynamic_cast<xercesc::DOMAttr*>(attribute_node); 143 const G4String attName = Transcode(attribute->getName()); 144 const G4String attValue = Transcode(attribute->getValue()); 145 146 if (attName == "name") { 147 name = GenerateName(attValue); 148 } 149 else if (attName == "R") { 150 r = eval.Evaluate(attValue); 151 } 152 else if (attName == "G") { 153 g = eval.Evaluate(attValue); 154 } 155 else if (attName == "B") { 156 b = eval.Evaluate(attValue); 157 } 158 else if (attName == "A") { 159 a = eval.Evaluate(attValue); 160 } 161 } 162 163 G4cout << "Color attribute (R,G,B,A) is: " << r << ", " << g << ", " << b << ", " << a << " !" 164 << G4endl; 165 color = new G4VisAttributes(G4Color(r, g, b, a)); 166 fAttribs.insert(std::make_pair(name, color)); 167 } 168 169 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 170 171 G4VisAttributes* G03ColorReader::GetVisAttribute(const G4String& ref) 172 { 173 G4VisAttributes* col = 0; 174 std::map<G4String, G4VisAttributes*>::iterator pos = fAttribs.find(ref); 175 176 if (pos != fAttribs.end()) { 177 col = pos->second; 178 } 179 else { 180 G4String err_mess = "Attribute: " + ref + " NOT found !"; 181 G4Exception("G03ColorReader::GetVisAttribute()", "ReadError", FatalException, err_mess); 182 } 183 return col; 184 } 185