Skip to content

Commit

Permalink
Merge pull request #107 from fusion-energy/adding-option-for-paths-in…
Browse files Browse the repository at this point in the history
…-files

Allowing output files to contain a folder, create folder if it does not exist
  • Loading branch information
shimwell authored Dec 13, 2024
2 parents c67932c + dd69afb commit 17d3203
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/cad_to_dagmc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def _vertices_to_h5m(
triangles:
material_tags:
h5m_filename:
implicit_complement_material_tag:
"""

if len(material_tags) != len(triangles_by_solid_by_face):
Expand Down Expand Up @@ -184,6 +185,10 @@ def _vertices_to_h5m(

moab_core.add_entities(file_set, all_sets)

# makes the folder if it does not exist
if Path(h5m_filename).parent:
Path(h5m_filename).parent.mkdir(parents=True, exist_ok=True)

# moab_core.write_file only accepts strings
if isinstance(h5m_filename, Path):
moab_core.write_file(str(h5m_filename))
Expand Down Expand Up @@ -234,8 +239,7 @@ def _mesh_brep(
mesh_algorithm: int = 1,
dimensions: int = 2,
):
"""Creates a conformal surface meshes of the volumes in a Brep file using
Gmsh.
"""Creates a conformal surface meshes of the volumes in a Brep file using Gmsh.
Args:
occ_shape: the occ_shape of the Brep file to convert
Expand Down Expand Up @@ -361,6 +365,7 @@ def __init__(self, filename: str):

# TODO add export_unstructured_mesh_file
# TODO add add_gmsh_msh_file
# TODO test for exports result in files

def export_dagmc_h5m_file(
self,
Expand Down Expand Up @@ -560,6 +565,10 @@ def export_unstructured_mesh_file(
dimensions=3,
)

# makes the folder if it does not exist
if Path(filename).parent:
Path(filename).parent.mkdir(parents=True, exist_ok=True)

# gmsh.write only accepts strings
if isinstance(filename, Path):
gmsh.write(str(filename))
Expand Down Expand Up @@ -629,6 +638,10 @@ def export_gmsh_mesh_file(
dimensions=dimensions,
)

# makes the folder if it does not exist
if Path(filename).parent:
Path(filename).parent.mkdir(parents=True, exist_ok=True)

# gmsh.write only accepts strings
if isinstance(filename, Path):
gmsh.write(str(filename))
Expand Down
70 changes: 70 additions & 0 deletions tests/test_python_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from pathlib import Path
import pytest
import cadquery as cq
from cad_to_dagmc import CadToDagmc

Expand Down Expand Up @@ -138,3 +139,72 @@ def test_add_stp_file_returned_volumes():
c2d = CadToDagmc()
vols = c2d.add_stp_file("tests/two_disconnected_cubes.stp")
assert vols == 2


@pytest.mark.parametrize(
"filename",
[
"test_dagmc1.h5m",
"out_folder1/test_dagmc2.h5m",
Path("test_dagmc3.h5m"),
Path("out_folder2/test_dagmc4.h5m"),
],
)
def test_export_dagmc_h5m_file_handel_paths_folders_strings(filename):
"""Checks that a h5m file is created"""

box = cq.Workplane().box(1, 1, 1)
c2d = CadToDagmc()
c2d.add_cadquery_object(box, material_tags=["mat1"])

c2d.export_dagmc_h5m_file(filename=filename)

assert Path(filename).is_file()

os.system(f"rm -rf {filename}")


@pytest.mark.parametrize(
"filename",
[
"test_dagmc1.vtk",
"out_folder3/test_dagmc2.vtk",
Path("test_dagmc3.vtk"),
Path("out_folder4/test_dagmc4.vtk"),
],
)
def test_export_unstructured_mesh_file_handel_paths_folders_strings(filename):
"""Checks that a vtk file is created"""

box = cq.Workplane().box(1, 1, 1)
c2d = CadToDagmc()
c2d.add_cadquery_object(box, material_tags=["mat1"])

c2d.export_unstructured_mesh_file(filename=filename)

assert Path(filename).is_file()

os.system(f"rm -rf {filename}")


@pytest.mark.parametrize(
"filename",
[
"test_dagmc1.msh",
"out_folder5/test_dagmc2.msh",
Path("test_dagmc3.msh"),
Path("out_folder6/test_dagmc4.msh"),
],
)
def test_export_gmsh_mesh_file_handel_paths_folders_strings(filename):
"""Checks that a vtk file is created"""

box = cq.Workplane().box(1, 1, 1)
c2d = CadToDagmc()
c2d.add_cadquery_object(box, material_tags=["mat1"])

c2d.export_gmsh_mesh_file(filename=filename)

assert Path(filename).is_file()

os.system(f"rm -rf {filename}")

0 comments on commit 17d3203

Please sign in to comment.