Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update reverse to JSON and rename to CsgToCad #229

Merged
merged 17 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,24 @@ jobs:
geouned_cadtocsg --help
python -c 'import geouned'
python -c 'from geouned import CadToCsg'
python -c 'from geouned.GEOReverse import reverse'
python -c 'from geouned import CsgToCad'
python -m pip install .[tests]

- name: testing package version
run: |
python -m pytest -v tests/test_version.py

- name: testing GEOUNED functionality
- name: testing GEOUNED CadToCsg functionality
run: |
python -m pytest -v tests/test_convert.py
geouned_cadtocsg -i tests/config_complete_defaults.json
geouned_cadtocsg -i tests/config_non_defaults.json
python -m pytest -v tests/test_cadtocsg.py
geouned_cadtocsg -i tests/config_cadtocsg_complete_defaults.json
geouned_cadtocsg -i tests/config_cadtocsg_non_defaults.json

- name: testing GEOUNED CsgToCad functionality
run: |
python -m pytest -v tests/test_csgtocad.py
geouned_csgtocad -i tests/config_csgtocad_complete.json
geouned_csgtocad -i tests/config_csgtocad_minimal.json

- name: install openmc
if: ${{ matrix.os == 'ubuntu-latest'}}
Expand Down
3 changes: 3 additions & 0 deletions docs/developer_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,19 @@ However we need one more dependency to run the tests.
Then we can run the tests with the following command from the root of the repository.

.. code-block:: sh

python -m pytest

We can run individual test files by specifying the file path

.. code-block:: sh

python -m pytest tests/test_convert.py

We can run individual test functions by specifying the file path and function name

.. code-block:: sh

python -m pytest tests/test_convert.py -k 'test_conversion'

Additional pytest options that might be useful are including -s for standard output and -vv for very verbose output.
Expand Down
6 changes: 5 additions & 1 deletion docs/python_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Python API reference

.. currentmodule:: geouned

.. autoclass:: CsgToCad
:members:
:show-inheritance:

.. autoclass:: CadToCsg
:members:
:show-inheritance:
Expand All @@ -22,4 +26,4 @@ Python API reference

.. autoclass:: Tolerances
:members:
:show-inheritance:
:show-inheritance:
6 changes: 4 additions & 2 deletions docs/usage/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ GEOUNED can be used as a Python package with the API or as a command line tool w
:numbered:
:maxdepth: 1

python_api_usage
python_cli_usage
python_cadtocsg_api_usage
python_cadtocsg_cli_usage
python_csgtocad_api_usage
python_csgtocad_cli_usage
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Python Package Usage
====================
Python Package Usage, CAD to CSG conversion
===========================================

The main class is ``CadToCsg()`` which converts CAD geometry to Constructive Solid Geometry (CSG).
There are many arguments that can be passed into the ``CadToCsg()`` class which are documented in the `Python API reference section <../python_api.html>`_ of the documentation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Command Line Tool Usage
=======================
Command Line Tool Usage, CAD to CSG conversion
==============================================

GEOUNED can be used in the command line.
GEOUNED CAD to CSG conversion can be performed in the command line.

These examples assumes you have a CAD STEP file in the current working directory of the terminal called "cuboid.stp"

Expand Down Expand Up @@ -112,6 +112,7 @@ Here is a complete JSON file specification
"cellSummaryFile": true
}
}

Note that JSON requires ```null``` to be passed in which gets translated to ```None``` in Python.
This is converted in the same way as the minimal JSON config file

Expand Down
41 changes: 41 additions & 0 deletions docs/usage/python_csgtocad_api_usage.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Python Package Usage, CSG to CAD conversion
===========================================

The main class is ``CsgToCad()`` which converts Constructive Solid Geometry (CSG) to CAD.
There are a few arguments that can be passed into the ``CsgToCad().export_cad()`` method which are documented in the `Python API reference section <../python_api.html>`_ of the documentation.


If you have install GEOUNED and FreeCAD into your system Python then you can simply run a .py script with Python.
The most minimal use case below shows GEOUNED being imported and the CsgToCad being used to convert a CSG geometry into a STEP CAD file.
The example makes use of default attributes.

.. code-block:: python

import geouned

geo = geouned.CsgToCad()

geo.export_cad(
csg_format='openmc_xml',
input_filename='cylinder_box.xml',
)


Users can change the default arguments to suit the conversion desired.
The following example shows a usage with every attributes specified.
Remember that the arguments are described in the `Python API reference section <../python_api.html>`_ of the documentation.

.. code-block:: python

import geouned

geo = geouned.CsgToCad()

geo.export_cad(
input_filename='cylinder_box.xml',
csg_format='openmc_xml',
bounding_box=[-1000.0, -500.0, -1000.0, 0,0,0.0 ],
cell_range_type='exclude',
cell_range=(2,3,4),
output_filename='openmc_xml',
)
78 changes: 78 additions & 0 deletions docs/usage/python_csgtocad_cli_usage.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Command Line Tool Usage, CSG to CAD conversion
==============================================

GEOUNED CSG to CAD conversion can be performed in the command line.

Both OpenMC XML CSG and MCNP CSG formats are supported.

The first example assumes you have an OpenMC XML CSG file in the current working directory of the terminal called "cylinder_box.xml".

The most minimal use case below shows a minimal config_openmc.json file being used.

First create a JSON file called "config_openmc.json" containing the following.

.. code-block:: json

{
"export_cad":{
"input_filename": "cylinder_box.xml",
"csg_format": "openmc_xml"
}
}


Then execute the command line interface tool to convert your OpenMC XML CSG file to a STEP CAD file with the default configuration.

.. code-block:: bash

geouned_csgtocad -i config_openmc.json

MCNP CSG files can also be converted, this example assumes you have a MCNP CSG file in te current working directory called "cylinder_box.mcnp".

For MCNP CSG files, the JSON config file should be as follows and is assumed to be named "config_mcnp.json".


.. code-block:: json

{
"export_cad":{
"input_filename": "cylinder_box.mcnp",
"csg_format": "mcnp"
}
}

Then execute the command line interface tool to convert your MCNP CSG file to a STEP CAD file with the default configuration.

.. code-block:: bash

geouned_csgtocad -i config_mcnp.json


The following example shows a usage with every attributes specified in the config.json file.

The contents of the JSON file closely matches the Class arguments and method arguments when using the Python package.

For a full description of each keyword see the `Python API reference section <../python_api.html>`_ of the documentation.

Here is a complete JSON file specification

.. code-block:: json

{
"export_cad":{
"input_filename": "tests/csg_files/cylinder_box.xml",
"csg_format": "openmc_xml",
"output_filename" : "cad_from_csg",
"bounding_box": [-1000, -1000, -1000, 1000, 1000, 1000],
"universe_start": 0,
"level_max": "all",
"cell_range_type": "all",
"cell_range": [],
"mat_range_type": "all",
"mat_range": []
}
}

.. code-block:: bash

geouned_csgtocad -i config.json
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ docs = [

[project.scripts]
geouned_cadtocsg = "geouned.GEOUNED.scripts.geouned_cadtocsg:main"
geouned_csgtocad = "geouned.GEOReverse.scripts.geouned_csgtocad:main"

[tool.black]
line-length = 128
Expand Down
1 change: 1 addition & 0 deletions src/geouned/GEOReverse/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .core import CsgToCad
113 changes: 113 additions & 0 deletions src/geouned/GEOReverse/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import typing
import FreeCAD
import Import

from pathlib import Path

from .Modules.buildCAD import buildCAD, makeTree
from .Modules.MCNPinput import McnpInput
from .Modules.Objects import CadCell
from .Modules.XMLinput import XmlInput


class CsgToCad:
"""Base class for the conversion of CSG to CAD models"""

def __init__(self):
pass

def export_cad(
self,
input_filename: str,
csg_format: str,
output_filename: str = "cad_from_csg",
bounding_box: typing.Tuple[int, int, int, int, int, int] = (-1000, -1000, -1000, 1000, 1000, 1000),
universe_start: int = 0,
level_max: str = "all",
cell_range_type: str = "all",
cell_range: typing.Tuple[int] = (),
mat_range_type: str = "all",
mat_range: typing.Tuple[int] = (),
# TODO add these to the method signature
# splitTolerance in the Options class
# mat = this is in the CADselection dictionary but not in the docs https://github.com/GEOUNED-org/GEOUNED/blob/76ef697c7dca6a828c7498996ff3313859c872f2/docs/User_Guide_GEOUNED_v2.0.pdf
):
"""export the CSG geometry in OpenMC or MCNP format to a CAD model.

Args:
input_filename (str): The filename and path of the input CSG text file.
csg_format (str): The format of the CSG input file, options are 'openmc_xml' or 'mcnp'
output_filename (str, optional): The filename stem and path of the output file created.
Two files will be created with the '.step' suffix and one with the 'FCStd' suffix.
Defaults to 'cad_from_csg'.
bounding_box (typing.Tuple[int, int, int, int, int, int], optional): The bounding box
coordinates of the CSG geometry. This should encompass the entire CSG geometry.
Format is (xmin, ymin, zmin, xmax, ymax, zmax) Defaults to (-1000, -1000, -1000,
1000, 1000, 1000).
universe_start (int, optional): The Universe ID to be converted to CAD. If universe_start
is left as 0 then all the universes and any nested universes are converted. If set
then the universe and all its nested universes are converted. Defaults to 0.
level_max (str, optional): Level maximum of nested universe to be translated. If
level_max < highest nested universe level, cells inside the container cell whose
level is level_max will not be translated. This container cell will be the CAD
solid written in the CAD file. Defaults to "all".
cell_range_type (str, optional): Define how to consider the range values.
setting to 'all' results in all the cells with any cell ID being converted (range a no effect).
setting to 'include' results in only cells defined in 'range' being converted.
setting to 'exclude' results in exclude all cells defined in range. Defaults to "all".
cell_range (typing.Tuple[int], optional): list of cells to be included/excluded for the conversion.
Defaults to ().
mat_range_type (str, optional): Define how to consider the range values.
setting to 'all' results in all the materials with any cell ID being converted (range a no effect).
setting to 'include' results in only materials defined in 'range' being converted.
setting to 'exclude' results in exclude all materials defined in range. Defaults to "all".
mat_range (typing.Tuple[int], optional): list of materials to be included/excluded for the conversion.
Defaults to ().

Raises:
ValueError: If the csg_format is not 'openmc_xml' or 'mcnp' then a ValueError is raised.
"""

# TODO check file extensions are correct
# if Path(output_filename).suffix not in ['.stp', '.step']:
# raise ValueError(f"output file must have a .stp or .step extension, not {universe_start.suffix}")

# get geometry definition from OpenMC XML or MCNP input
if csg_format == "mcnp":
geo = McnpInput(input_filename)
elif csg_format == "openmc_xml":
geo = XmlInput(input_filename)
else:
msg = f"input format type {csg_format} is not supported. Supported options are 'openmc_xml' or 'mcnp'"
raise ValueError(msg)

Path(output_filename).parent.mkdir(parents=True, exist_ok=True)

UnivCell = CadCell()
UnivCell.shape = UnivCell.makeBox(FreeCAD.BoundBox(*bounding_box))

# TODO make these variable names lower case in the downstream code

CADselection = {
"Ustart": universe_start,
"levelMax": level_max,
"cell": [cell_range_type, cell_range],
"mat": [mat_range_type, mat_range],
"format": csg_format,
"cell_range_type": cell_range_type,
"cell_range": cell_range,
"mat_range_type": mat_range_type,
"mat_range": mat_range,
}

# TODO don't return fails variable, just fail in the method and raise the error there
CADCells, fails = buildCAD(UnivCell, geo, CADselection)

if fails:
print("failed in conversion", fails)

CADdoc = FreeCAD.newDocument("converted_with_geouned")

makeTree(CADdoc, CADCells)
Import.export(CADdoc.Objects[0:1], f"{output_filename}.step")
CADdoc.saveAs(f"{output_filename}.FCStd")
Loading