Skip to main content

CAD to Geant4 GDML converter with automatic mesh repair

Project description

CAD to Geant4 Converter Suite

A collection of tools for converting CAD files (STEP, STL) to GDML for use in Geant4 simulations.

Requirements

  • Python 3.10 or above
  • pyg4ometry >= 1.0.0
  • vtk >= 9.0.0
  • trimesh (for automatic mesh repair)
  • pymeshlab (optional, for advanced mesh repair of stubborn cases)

Install dependencies:

pip install -r requirements.txt
pip install pymeshlab  # Optional but recommended

Overview

Converting CAD to GDML/Geant4 requires care; a few export best-practices greatly improve results (trim small parts, preserve named components and hierarchy, avoid intended overlaps).

What's new (concise):

  • Automatic mesh check and repair on all tessellated volumes before GDML export
  • CI added (pytest + Black + Flake8) to keep quality steady
  • Geometry overlap checking with Geant4 via check_overlaps.py

Note: GDML output typically lacks detailed material assignments. Post-process the GDML with gdml-editor to add or correct materials: https://github.com/drflei/gdml-editor

Main Application: cad_g4_conv.py

Unified converter supporting three workflows. All conversions automatically check and repair tessellated meshes before saving to GDML.

Quick Start

# STEP native conversion
python cad_g4_conv.py --step-file assembly.STEP

# STL+STEP mesh conversion
python cad_g4_conv.py --step-file assembly.STEP --stl-dir STLs/

# Single STL conversion
python cad_g4_conv.py --stl-file mesh.stl

All conversions automatically check and repair tessellated meshes before export.

Automatic Mesh Repair

The converter includes a comprehensive 10-phase mesh repair system that automatically fixes common issues:

  1. Basic cleanup: Removes duplicate/unreferenced vertices
  2. Normal fixing: Ensures consistent face orientations
  3. Degenerate removal: Removes zero-area faces and duplicates
  4. Broken face repair: Detects and removes topologically broken faces
  5. Hole filling: Iteratively attempts to close surface holes (up to 3 attempts)
  6. Inversion fix: Corrects inverted face orientations
  7. Final cleanup: Merges vertices after all repairs
  8. Winding correction: Fixes inconsistent face winding
  9. Fallback strategies: Uses convex hull or voxelization for severely broken meshes
  10. PyMeshLab fallback (optional): For very stubborn meshes, uses MeshLab's industrial-strength repair algorithms

Most meshes are repaired automatically. The system tries progressively more aggressive techniques:

  • Phases 1-8 handle 90%+ of issues using trimesh (lightweight, fast)
  • Phase 9 applies convex hull for small holes or voxelization for complex geometry
  • Phase 10 uses PyMeshLab's powerful algorithms for the most challenging cases (requires pip install pymeshlab)

Example repair output:

stl_solid_8: watertight_before=False watertight_after=True 
  notes=removed_92_broken_faces;subdivided;used_convex_hull(vol_diff=0.04);

Optional: PyMeshLab for stubborn meshes

pip install pymeshlab  # Optional but recommended for complex meshes

If installed, PyMeshLab provides a final fallback using MeshLab's industrial-strength repair algorithms.

Files in This Directory

Main Tools

  • cad_g4_conv.py - Unified CAD to GDML converter
  • run_vtkviewer.py - VTK-based geometry viewer for GDML/STL/STEP files
  • check_overlaps.py - Geant4 geometry overlap checker

Documentation

  • README.md - This file, main project documentation
  • docs/QUICKREF.md - Quick reference and cheatsheet
  • docs/DETAILED_USAGE.md - Complete usage guide
  • docs/TESTING.md - Test suite documentation

Scripts

  • demo_all_workflows.sh - Demonstrates all three workflows
  • test_cad_g4_conv.sh - Automated testing script

Installation

From PyPI (Recommended)

pip install cad-g4-conv

From Source

git clone https://github.com/drflei/cad_g4_conv.git
cd cad_g4_conv
pip install -e .

Requirements

  • Python 3.10+
  • pyg4ometry >= 1.0.0
  • VTK >= 9.0.0
  • trimesh
  • geant4_pybind (for overlap checking)

Usage

See docs/DETAILED_USAGE.md for complete documentation.

Three Workflows

Workflow Command Best For
STEP Native --step-file X.STEP Assemblies with hierarchy
STL+STEP --step-file X.STEP --stl-dir STLs/ High-quality meshes
Single STL --stl-file mesh.stl Single meshes, prototyping

Examples

# 1. STEP native with hierarchy
python cad_g4_conv.py --step-file detector.STEP -o detector.gdml

# 2. STL+STEP with auto-sized world
python cad_g4_conv.py \
    --step-file assembly.STEP \
    --stl-dir parts/ \
    -o assembly.gdml

# 3. Single STL (simplest)
python cad_g4_conv.py --stl-file housing.stl -o housing.gdml

# 4. With overlap checking
python cad_g4_conv.py --step-file detector.STEP --check-overlaps

# 5. Flat mode (robust fallback)
python cad_g4_conv.py --step-file complex.STEP --flat

# 6. Center geometry at world origin
python cad_g4_conv.py --step-file detector.STEP --center-origin

# 7. Logging example
# Write detailed logs to 'convert.log'
python cad_g4_conv.py --stl-file mesh.stl --log-file convert.log --log-level DEBUG

# Notes:
# - All tessellated meshes are automatically checked and repaired before export
# - Repairs use trimesh (fix_normals, merge_vertices, remove degenerates, fill_holes, fix_inversion)
# - `--log-file` / `--log-level` control logging output for debugging and reproducibility

Running from Other Directories

You can run the tools from anywhere by specifying the full path:

# From any directory
python ~/cad_g4_conv/cad_g4_conv.py --stl-file /path/to/mesh.stl

Or add to PATH:

# Add to ~/.bashrc
export PATH="$HOME/cad_g4_conv:$PATH"

# Then use directly
cad_g4_conv.py --stl-file mesh.stl

Demo

Run the comprehensive demo to see all workflows in action:

cd ~/cad_g4_conv
./demo_all_workflows.sh

Testing

Run automated tests:

cd ~/cad_g4_conv
./test_cad_g4_conv.sh

Visualization

After conversion, visualize the GDML with the built-in VTK viewer:

python run_vtkviewer.py output.gdml

# Or use the viewer from gdml_editor package
python -m gdml_editor.run_vtkviewer output.gdml

The viewer supports:

  • GDML files (Geant4 geometry)
  • STL files (triangular meshes)
  • STEP files (CAD assemblies)
  • FLUKA input files (.inp)

See run_vtkviewer.py --help for all options.

Overlap Checking

Check for geometry overlaps using Geant4's built-in overlap checker:

python check_overlaps.py output.gdml

# With custom parameters
python check_overlaps.py output.gdml 2000 0.01 20.0

Arguments:

  • gdml_file - Path to GDML geometry file
  • resolution - Number of points to check (default: 1000)
  • tolerance - Tolerance in mm (default: 0.001)
  • world_size_m - World half-size in meters (default: 10.0)

Requirements:

pip install geant4_pybind

The tool loads the geometry into Geant4 and runs CheckOverlaps() on all volumes, reporting:

  • Overlapping volume pairs
  • Surface defects/holes in tessellated solids
  • Detailed overlap locations and partners

Example output:

⚠️  FOUND 2 UNIQUE OVERLAPPING PAIR(S):

   1. sensor_1 === housing
   2. pcb_volume === sensor_2

Help

python cad_g4_conv.py --help

Features (short)

  • Supports STEP-native conversion (hierarchy + CSG where possible), STL+STEP mesh-based conversion, and single-STL conversions.
  • Automatic 10-phase mesh check and repair on all tessellated volumes before GDML export (trimesh + optional pymeshlab).
  • Auto-sized and centered world volume, fuzzy name matching for STL→STEP associations, and overlap diagnostics.

Documentation

For detailed usage, see the docs/ folder:

License

GNU V3.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cad_g4_conv-1.0.1.tar.gz (48.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cad_g4_conv-1.0.1-py3-none-any.whl (40.7 kB view details)

Uploaded Python 3

File details

Details for the file cad_g4_conv-1.0.1.tar.gz.

File metadata

  • Download URL: cad_g4_conv-1.0.1.tar.gz
  • Upload date:
  • Size: 48.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for cad_g4_conv-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ffe295a8c8003b40c0cb400492b1ac7e350cd4394a9282afd9ac60b019c30d07
MD5 8a88d7ec50d255039a3f96ff91737854
BLAKE2b-256 a957100520a39a45bc91ae15d18b0963ec2e7fcc7d9b352e6248ad5ec1232c9e

See more details on using hashes here.

File details

Details for the file cad_g4_conv-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: cad_g4_conv-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 40.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for cad_g4_conv-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6957d64040157d8d7fcf5f7d0d6cef0f20f5662d84ebe0cceae29b72857aa942
MD5 31650c8c4367581c48322db62dd62358
BLAKE2b-256 8716b3014e56ae77a85281b751898429cda2251eaeea9c837dfa5d14ae19cc1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page