File indexing completed on 2026-05-29 07:35:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include "Vis2XmlExtract.h"
0016 #include <DD4hep/Printout.h>
0017 #include <DD4hep/Volumes.h>
0018 #include <XML/DocumentHandler.h>
0019
0020
0021 #include <fstream>
0022 #include <iostream>
0023 #include <sstream>
0024
0025 namespace {
0026 std::string genName(const std::string& n, const void* ptr) {
0027 std::string nn = n;
0028 char text[32];
0029 ::snprintf(text,sizeof(text),"%p",ptr);
0030 nn += "_";
0031 nn += text;
0032 return nn;
0033 }
0034 }
0035
0036
0037 dd4hep::detail::Vis2XmlExtract::Vis2XmlExtract( Detector& description )
0038 : m_detDesc(description), m_dataPtr(0) {
0039 }
0040
0041 dd4hep::detail::Vis2XmlExtract::~Vis2XmlExtract() {
0042 if (m_dataPtr)
0043 delete m_dataPtr;
0044 m_dataPtr = 0;
0045 }
0046
0047
0048 xml_h dd4hep::detail::Vis2XmlExtract::handleVolume( const std::string& , Volume volume ) const {
0049 GeometryInfo& geo = *this->m_dataPtr;
0050 xml_h vol = geo.xmlVolumes[volume];
0051 if (!vol) {
0052 std::string n = genName(volume->GetName(),volume);
0053 if (volume->IsAssembly()) {
0054 vol = xml_elt_t(geo.doc, _U(assembly));
0055 vol.setAttr(_U(name), n);
0056 }
0057 else {
0058 vol = xml_elt_t(geo.doc, _U(volume));
0059 vol.setAttr(_U(name), n);
0060 }
0061 geo.doc_structure.append(vol);
0062 geo.xmlVolumes[volume] = vol;
0063 if( volume.data() ) {
0064 VisAttr vis = volume.visAttributes();
0065 if (vis.isValid()) {
0066 xml_ref_t xml_data = handleVis(vis.name(), vis);
0067 vol.setRef(_U(visref), xml_data.name());
0068 }
0069 }
0070 }
0071 return vol;
0072 }
0073
0074
0075 xml_h dd4hep::detail::Vis2XmlExtract::handleVolumeVis( const std::string& , const TGeoVolume* volume ) const {
0076 GeometryInfo& geo = *this->m_dataPtr;
0077 xml_h vol = geo.xmlVolumes[volume];
0078 if (!vol) {
0079 Volume v(volume);
0080 if( v.data() ) {
0081 VisAttr vis = v.visAttributes();
0082 if (vis.isValid()) {
0083 geo.doc_structure.append(vol = xml_elt_t(geo.doc, _U(volume)));
0084 vol.setAttr(_U(name), v->GetName());
0085 xml_ref_t xml_data = handleVis(vis.name(), vis);
0086 vol.setRef(_U(visref), xml_data.name());
0087 geo.xmlVolumes[v] = vol;
0088 }
0089 }
0090 }
0091 return vol;
0092 }
0093
0094
0095 xml_h dd4hep::detail::Vis2XmlExtract::handleVis( const std::string& , VisAttr attr ) const {
0096 GeometryInfo& geo = *this->m_dataPtr;
0097 xml_h vis = geo.xmlVis[attr];
0098 if (!vis) {
0099 float red = 0, green = 0, blue = 0;
0100 int style = attr.lineStyle();
0101 int draw = attr.drawingStyle();
0102
0103 geo.doc_display.append(vis = xml_elt_t(geo.doc, _U(vis)));
0104 vis.setAttr(_U(name), attr.name());
0105 vis.setAttr(_U(visible), attr.visible());
0106 vis.setAttr(_U(show_daughters), attr.showDaughters());
0107 if (style == VisAttr::SOLID)
0108 vis.setAttr(_U(line_style), "unbroken");
0109 else if (style == VisAttr::DASHED)
0110 vis.setAttr(_U(line_style), "broken");
0111 if (draw == VisAttr::SOLID)
0112 vis.setAttr(_U(drawing_style), "solid");
0113 else if (draw == VisAttr::WIREFRAME)
0114 vis.setAttr(_U(drawing_style), "wireframe");
0115
0116 xml_h col = xml_elt_t(geo.doc, _U(color));
0117 attr.rgb(red, green, blue);
0118 col.setAttr(_U(alpha), attr.alpha());
0119 col.setAttr(_U(R), red);
0120 col.setAttr(_U(B), blue);
0121 col.setAttr(_U(G), green);
0122 vis.append(col);
0123 geo.xmlVis[attr] = vis;
0124 }
0125 return vis;
0126 }
0127
0128
0129 xml_doc_t dd4hep::detail::Vis2XmlExtract::createVis( DetElement top ) {
0130 if (!top.isValid()) {
0131 throw std::runtime_error("Attempt to call createDetector with an invalid geometry!");
0132 }
0133
0134 GeometryInfo& geo = *(m_dataPtr = new GeometryInfo);
0135 m_data->clear();
0136 collect(top, geo);
0137 printout(ALWAYS,"Vis2XmlExtract","++ ==> Dump visualisation attributes "
0138 "from in memory detector description...");
0139 xml::DocumentHandler docH;
0140 xml_elt_t elt(0);
0141 geo.doc = docH.create("visualization", docH.defaultComment());
0142 geo.doc_root = geo.doc.root();
0143 geo.doc_root.append(geo.doc_display = xml_elt_t(geo.doc, _U(display)));
0144 geo.doc_root.append(geo.doc_structure = xml_elt_t(geo.doc, _U(structure)));
0145
0146 for( Volume v : geo.volumes )
0147 this->handleVolumeVis(v->GetName(), v.ptr());
0148 printout(ALWAYS,"Vis2XmlExtract","++ Handled %ld volumes.",geo.volumes.size());
0149 return geo.doc;
0150 }
0151
0152
0153 dd4hep::detail::Vis2XmlExtract::GeometryInfo::GeometryInfo()
0154 : doc(0), doc_root(0), doc_display(0), doc_structure(0)
0155 {
0156 }
0157
0158 static long dump_output( xml_doc_t doc, int argc, char** argv ) {
0159 dd4hep::xml::DocumentHandler docH;
0160 return docH.output(doc, argc > 0 ? argv[0] : "");
0161 }
0162
0163 static long create_vis( dd4hep::Detector& description, int argc, char** argv ) {
0164 dd4hep::detail::Vis2XmlExtract wr(description);
0165 dd4hep::xml::DocumentHolder doc(wr.createVis(description.world()).ptr());
0166 return dump_output(doc, argc, argv);
0167 }
0168
0169 static long create_visASCII( dd4hep::Detector& description, int , char** argv ) {
0170 dd4hep::detail::Vis2XmlExtract wr(description);
0171 wr.createVis(description.world());
0172 auto& geo = *wr.m_dataPtr;
0173 std::map<std::string, xml_comp_t> vis_map;
0174 for (xml_coll_t c(geo.doc_display, _U(vis)); c; ++c)
0175 vis_map.insert(make_pair(xml_comp_t(c).nameStr(), xml_comp_t(c)));
0176
0177 const char* sep = ";";
0178 std::ofstream os(argv[0]);
0179 for (xml_coll_t c(geo.doc_structure, _U(volume)); c; ++c) {
0180 xml_comp_t vol = c;
0181 xml_comp_t ref = c.child(_U(visref));
0182 auto iter = vis_map.find(ref.refStr());
0183 if ( iter != vis_map.end() ) {
0184 xml_comp_t vis = iter->second;
0185 xml_comp_t col = vis.child(_U(color));
0186 os << "vol:" << vol.nameStr() << sep << "vis:" << vis.nameStr() << sep
0187 << "visible:" << vis.visible() << sep << "r:"
0188 << col.R() << sep << "g:" << col.G() << sep << "b:" << col.B() << sep
0189 << "alpha:" << col.alpha() << sep << "line_style:"
0190 << vis.attr < std::string > (_U(line_style)) << sep
0191 << "drawing_style:" << vis.attr < std::string> (_U(drawing_style)) << sep
0192 << "show_daughters:" << vis.show_daughters() << sep << std::endl;
0193 }
0194 }
0195 os.close();
0196 return 1;
0197 }
0198
0199 DECLARE_APPLY(DD4hepGeometry2VIS, create_vis)
0200 DECLARE_APPLY(DD4hepGeometry2VISASCII, create_visASCII)