-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update reverse to JSON and rename to CsgToCad (#229)
* moved two methods out of start * removed unnecessary logger line * started CsgToCad class definition * added class * added cli for csgtocad method * restructure docs * adding csgtocad to docs * improved csg to cad examples * format * corrected json path * moved doc string to method * corrected file path * quotes used in dict * removed old code
- Loading branch information
Showing
23 changed files
with
467 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/usage/python_api_usage.rst → docs/usage/python_cadtocsg_api_usage.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .core import CsgToCad |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Oops, something went wrong.