G4OCCT 0.1.0
Geant4 interface to Open CASCADE Technology (OCCT) geometry definitions
Loading...
Searching...
No Matches
DetectorConstruction.cc
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2// Copyright (C) 2026 G4OCCT Contributors
3
6
7#include "DetectorConstruction.hh"
8
10
11#include <G4Box.hh>
12#include <G4LogicalVolume.hh>
13#include <G4NistManager.hh>
14#include <G4PVPlacement.hh>
15#include <G4SystemOfUnits.hh>
16
17#include <IFSelect_ReturnStatus.hxx>
18#include <STEPControl_Reader.hxx>
19#include <TopoDS_Shape.hxx>
20
21#include <stdexcept>
22#include <string>
23
24namespace {
25
28TopoDS_Shape LoadStepFile(const std::string& path) {
29 STEPControl_Reader reader;
30 if (reader.ReadFile(path.c_str()) != IFSelect_RetDone) {
31 throw std::runtime_error("Failed to read STEP file: " + path);
32 }
33 if (reader.TransferRoots() <= 0) {
34 throw std::runtime_error("No STEP roots transferred from: " + path);
35 }
36 TopoDS_Shape shape = reader.OneShape();
37 if (shape.IsNull()) {
38 throw std::runtime_error("Null shape loaded from STEP file: " + path);
39 }
40 return shape;
41}
42
43} // namespace
44
45namespace B1 {
46
47G4VPhysicalVolume* DetectorConstruction::Construct() {
48 G4NistManager* nist = G4NistManager::Instance();
49
50 // ── Materials ─────────────────────────────────────────────────────────────
51
52 G4Material* matAir = nist->FindOrBuildMaterial("G4_AIR");
53 G4Material* matWater = nist->FindOrBuildMaterial("G4_WATER");
54 G4Material* matShape1 = nist->FindOrBuildMaterial("G4_A-150_TISSUE");
55 G4Material* matShape2 = nist->FindOrBuildMaterial("G4_BONE_COMPACT_ICRU");
56
57 // ── Envelope and world dimensions ─────────────────────────────────────────
58
59 const G4double envSizeXY = 20.0 * cm;
60 const G4double envSizeZ = 30.0 * cm;
61
62 // ── World ─────────────────────────────────────────────────────────────────
63
64 auto* worldSolid = new G4Box("World", 0.6 * envSizeXY, 0.6 * envSizeXY, 0.8 * envSizeZ);
65 auto* worldLV = new G4LogicalVolume(worldSolid, matAir, "World");
66 auto* worldPV =
67 new G4PVPlacement(nullptr, G4ThreeVector(), worldLV, "World", nullptr, false, 0, true);
68
69 // ── Envelope ─────────────────────────────────────────────────────────────
70
71 auto* envSolid = new G4Box("Envelope", 0.5 * envSizeXY, 0.5 * envSizeXY, 0.5 * envSizeZ);
72 auto* envLV = new G4LogicalVolume(envSolid, matWater, "Envelope");
73 new G4PVPlacement(nullptr, G4ThreeVector(), envLV, "Envelope", worldLV, false, 0, true);
74
75 // ── Shape 1 — sphere (r = 15 mm) from STEP ───────────────────────────────
76 // Material: G4_A-150_TISSUE, placed in the upstream half of the envelope.
77
78 const std::string stepDir = G4OCCT_B1_STEP_DIR;
79 TopoDS_Shape occtShape1 = LoadStepFile(stepDir + "/shape1.step");
80 auto* solid1 = new G4OCCTSolid("Shape1", occtShape1);
81 auto* lv1 = new G4LogicalVolume(solid1, matShape1, "Shape1");
82 new G4PVPlacement(nullptr, G4ThreeVector(0, 2.0 * cm, -7.0 * cm), lv1, "Shape1", envLV, false, 0,
83 true);
84
85 // ── Shape 2 — box (20 × 30 × 40 mm) from STEP ────────────────────────────
86 // Material: G4_BONE_COMPACT_ICRU, placed in the downstream half.
87 // The OCCT box has its corner at the STEP origin (0,0,0); the placement
88 // point below is therefore the corner of the box, not its centre.
89
90 TopoDS_Shape occtShape2 = LoadStepFile(stepDir + "/shape2.step");
91 auto* solid2 = new G4OCCTSolid("Shape2", occtShape2);
92 auto* lv2 = new G4LogicalVolume(solid2, matShape2, "Shape2");
93 new G4PVPlacement(nullptr, G4ThreeVector(-1.0 * cm, -1.5 * cm, 5.0 * cm), lv2, "Shape2", envLV,
94 false, 0, true);
95
96 fScoringVolume = lv2;
97 return worldPV;
98}
99
100} // namespace B1
Declaration of G4OCCTSolid.
G4VPhysicalVolume * Construct() override
Geant4 solid wrapping an Open CASCADE Technology (OCCT) TopoDS_Shape.