G4OCCT 0.1.0
Geant4 interface to Open CASCADE Technology (OCCT) geometry definitions
Loading...
Searching...
No Matches
G4OCCT_STEPAssembly.cc
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2// Copyright (C) 2026 G4OCCT Contributors
3
30
31// Two header worlds must not meet in the same TU:
32// DD4hep → ROOT → TString.h declares: extern void Printf(...)
33// G4OCCT → OCCT → Standard_CString.h declares: int Printf(...)
34// Keeping them in separate TUs (firewall pattern) resolves both this Printf
35// conflict and the Handle(Class) macro collision.
36// G4OCCT_STEPAssembly_impl.{hh,cc} is the OCCT-side TU; this file is the
37// DD4hep-side TU.
38#include <DD4hep/DetFactoryHelper.h>
39#include <DD4hep/Printout.h>
40
42
43#include <G4Material.hh>
44#include <G4NistManager.hh>
45
46#include <map>
47#include <stdexcept>
48#include <string>
49
50using namespace dd4hep;
51
52static Ref_t create_step_assembly(Detector& description, xml_h e, SensitiveDetector /*sens*/) {
53 xml_det_t x_det = e;
54 xml_comp_t x_step = x_det.child(_Unicode(step_file));
55 xml_comp_t x_pos = x_det.child(_Unicode(position));
56 xml_comp_t x_map = x_det.child(_Unicode(material_map));
57
58 std::string name = x_det.nameStr();
59 std::string path = x_step.attr<std::string>(_Unicode(path));
60
61 // ── Build the material map from the compact XML entries ───────────────────
62 std::map<std::string, G4Material*> materials;
63 for (xml_coll_t it(x_map, _Unicode(entry)); it; ++it) {
64 xml_comp_t x_entry = it;
65 std::string stepName = x_entry.attr<std::string>(_Unicode(step_name));
66 std::string dd4hepMatName = x_entry.attr<std::string>(_Unicode(dd4hep_material));
67
68 // Resolve the G4Material via NIST manager (already constructed by DD4hep
69 // material loading) — look up by the Geant4 material name.
70 G4Material* g4mat = G4NistManager::Instance()->FindOrBuildMaterial(dd4hepMatName);
71 if (!g4mat) {
72 // Fall back to the Geant4 material table (materials defined inline in the
73 // compact file are registered under their compact name, not a NIST name).
74 g4mat = G4Material::GetMaterial(dd4hepMatName, /*warn=*/false);
75 }
76 if (!g4mat) {
77 throw std::runtime_error("G4OCCT_STEPAssembly: Geant4 material '" + dd4hepMatName +
78 "' not found for STEP name '" + stepName + "'");
79 }
80 materials[stepName] = g4mat;
81 printout(DEBUG, "G4OCCT_STEPAssembly", "Mapped STEP material '%s' → G4Material '%s'",
82 stepName.c_str(), dd4hepMatName.c_str());
83 }
84
85 // ── Import the STEP assembly (OCCT side, separate TU) ────────────────────
86 int nConstituents = G4OCCT_ImportSTEPAssembly(path, materials);
87
88 // ── Create a DD4hep assembly container (no per-solid placements here) ────
89 // Phase 1 limitation: the STEP assembly is imported on the OCCT side and its
90 // constituent G4LogicalVolumes live in the Geant4 volume store, but they are
91 // not placed into this dd4hep::Assembly. The assembly is therefore empty
92 // from the TGeo/DD4hep perspective.
93 // @todo Phase 2: return per-solid placement data from the OCCT-side TU and
94 // populate dd4hepAssembly with TGeo placeholder volumes so DD4hep and
95 // visualisation tools can see the constituent geometry.
96 Assembly dd4hepAssembly(name + "_assembly");
97
98 printout(INFO, "G4OCCT_STEPAssembly",
99 "Imported STEP assembly '%s' from '%s'; %d constituent solid(s) on "
100 "OCCT side; created empty dd4hep::Assembly '%s' as top-level container",
101 name.c_str(), path.c_str(), nConstituents, (name + "_assembly").c_str());
102
103 // ── DetElement and placement ─────────────────────────────────────────────
104 DetElement det(name, x_det.id());
105 Position pos(x_pos.x(), x_pos.y(), x_pos.z());
106
107 PlacedVolume pv = description.pickMotherVolume(det).placeVolume(dd4hepAssembly, pos);
108 pv.addPhysVolID("system", x_det.id());
109 det.setPlacement(pv);
110 return det;
111}
112
113DECLARE_DETELEMENT(G4OCCT_STEPAssembly, create_step_assembly)
static Ref_t create_step_assembly(Detector &description, xml_h e, SensitiveDetector)
int G4OCCT_ImportSTEPAssembly(const std::string &path, const std::map< std::string, G4Material * > &materials)
Firewall bridge between the DD4hep plugin TU and G4OCCT/OCCT.