Skip to content

Commit

Permalink
removal of Options as global variable (#138)
Browse files Browse the repository at this point in the history
* instances of Options class passed by arg

* updated usage to show how to use Options

* format pep8

* added missing options arg

* added missing args options

* passing in options as arg

* bug fix for compsolid

* format
  • Loading branch information
shimwell authored May 9, 2024
1 parent 34c7aee commit 6e083e7
Show file tree
Hide file tree
Showing 22 changed files with 284 additions and 193 deletions.
3 changes: 3 additions & 0 deletions docs/python_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ Python API reference
:members:
:show-inheritance:

.. autoclass:: Options
:members:
:show-inheritance:
29 changes: 22 additions & 7 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,36 @@ Python package usage
The Python API has two main classes.
The first 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 section.


The most minimal use case below shows GEOUNED being imported and the CadToCsg being used to convert a STEP CAD file called 'cuboid.stp' into a varity of CSG format.
If you have install GEOUNED and FreeCAD into your system PYthon then you can simply run a .py script with python.


.. code-block:: python
import geouned
geo = geouned.CadToCsg(
step_file = 'cuboid.stp',
out_formats = ['openMC_XML', 'openMC_PY', 'PHITS', 'Serpent', 'MCNP']
)
geo = geouned.CadToCsg(stepFile = 'cuboid.stp')
geo.start()
geo.export_csg()
The above examples makes use of default :meth:`geouned.Options`, :meth:`geouned.Tolerances` and :meth:`geouned.NumericFormat`
Users can change any of these to suit the conversion desired.
The following example changes several default values of the conversion.

Command line usage
------------------
.. code-block:: python
import geouned
TODO
my_options = geouned.Options(
forceCylinder=True,
splitTolerance=0,
newSplitPlane=True,
nPlaneReverse=0,
)
geo = geouned.CadToCsg(
stepFile='cuboid.stp', options=my_options
)
geo.start()
17 changes: 11 additions & 6 deletions src/geouned/GEOUNED/Conversion/CellDefinition.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from ..Utils.booleanFunction import BoolSequence, insert_in_sequence
from ..Utils.BooleanSolids import build_c_table_from_solids, remove_extra_surfaces
from ..Utils.Functions import GeounedSurface
from ..Utils.Options.Classes import Options as opt
from ..Utils.Options.Classes import Tolerances as tol

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -736,7 +735,7 @@ def gen_torus_annex_v_surface(face, v_params, force_cylinder=False):
)


def cellDef(meta_obj, surfaces, universe_box):
def cellDef(meta_obj, surfaces, universe_box, options):

solids = meta_obj.Solids
del_list = []
Expand Down Expand Up @@ -887,7 +886,7 @@ def cellDef(meta_obj, surfaces, universe_box):
v_var = "%i" % idT
else:
surf_params, surf_type, in_surf = gen_torus_annex_v_surface(
face, VminMax, opt.forceCylinder
face, VminMax, options.forceCylinder
)

if surf_type == "Cone":
Expand Down Expand Up @@ -1067,7 +1066,7 @@ def append_comp(new_cell, cell_def, cell_cad, meta_complementary):
return append


def no_overlapping_cell(metaList, surfaces):
def no_overlapping_cell(metaList, surfaces, options):

Surfs = {}
for lst in surfaces.values():
Expand Down Expand Up @@ -1114,14 +1113,20 @@ def no_overlapping_cell(metaList, surfaces):
# evaluate only diagonal elements of the Constraint Table (fastest) and remove surface not
# crossing in the solid boundBox
CT = build_c_table_from_solids(
box, (tuple(t_def.get_surfaces_numbers()), Surfs), option="diag"
box,
(tuple(t_def.get_surfaces_numbers()), Surfs),
option="diag",
options=options,
)

new_def = remove_extra_surfaces(t_def, CT)

# evaluate full constraint Table with less surfaces involved
CT = build_c_table_from_solids(
box, (tuple(new_def.get_surfaces_numbers()), Surfs), option="full"
box,
(tuple(new_def.get_surfaces_numbers()), Surfs),
option="full",
options=options,
)

if new_def.operator == "AND":
Expand Down
2 changes: 1 addition & 1 deletion src/geouned/GEOUNED/Cuboid/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..Utils import Geometry_GU as GU
from ..Utils.BasicFunctions_part1 import is_opposite, is_parallel
from ..Utils.booleanFunction import BoolSequence
from ..Utils.Options.Classes import Options as opt

from ..Utils.Options.Classes import Tolerances as tol

logger = logging.getLogger(__name__)
Expand Down
62 changes: 31 additions & 31 deletions src/geouned/GEOUNED/Decompose/Decom_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
is_same_value,
)
from ..Utils.BasicFunctions_part2 import is_duplicate_in_list
from ..Utils.Options.Classes import Options as opt

from ..Utils.Options.Classes import Tolerances as tol

logger = logging.getLogger(__name__)

twoPi = math.pi * 2


def split_full_cylinder(solid):
def split_full_cylinder(solid, options):
explode = []
bases = [solid]
while True:
new_bases = []
for base in bases:
cut_solids = cut_full_cylinder(base)
cut_solids = cut_full_cylinder(base, options)
if len(cut_solids) == 1:
explode.extend(cut_solids)
else:
Expand All @@ -46,7 +46,7 @@ def split_full_cylinder(solid):
return Part.makeCompound(explode)


def cut_full_cylinder(solid):
def cut_full_cylinder(solid, options):
solid_gu = GU.SolidGu(solid)
surfaces = UF.SurfacesDict()
flag_inv = CD.is_inverted(solid_gu.solid)
Expand Down Expand Up @@ -88,7 +88,7 @@ def cut_full_cylinder(solid):

if len(planes) == 0:
return [solid]
if len(planes[-1]) < opt.nPlaneReverse:
if len(planes[-1]) < options.nPlaneReverse:
planes.reverse()

cut = False
Expand All @@ -102,7 +102,7 @@ def cut_full_cylinder(solid):
tools = (pp[0].shape,)

try:
comsolid = UF.split_bop(solid, tools, opt.splitTolerance)
comsolid = UF.split_bop(solid, tools, options.splitTolerance, options)
except:
comsolid = solid
if len(comsolid.Solids) > 1:
Expand All @@ -115,7 +115,7 @@ def cut_full_cylinder(solid):

tool = (surfaces["Cyl"][0].shape,)
try:
comsolid = UF.split_bop(solid, tool, opt.splitTolerance)
comsolid = UF.split_bop(solid, tool, options.splitTolerance, options)
except:
comsolid = solid

Expand Down Expand Up @@ -697,22 +697,22 @@ def plane_2nd_order(solid_GU, face, flag_inv, convex=True):
return planes


def split_planes(Solids, universe_box, newVersion=True):
def split_planes(Solids, universe_box, options, newVersion=True):
if newVersion:
return split_planes_new(Solids, universe_box)
return split_planes_new(Solids, universe_box, options)
else:
return split_planes_org(Solids, universe_box)


def split_planes_new(Solids, universe_box):
def split_planes_new(Solids, universe_box, options):
Bases = Solids[:]
simpleSolid = []

# Cut with real planes defining solid faces
while True:
newBases = []
for base in Bases:
cut_solids = split_p_planes_new(base, universe_box)
cut_solids = split_p_planes_new(base, universe_box, options)
if len(cut_solids) == 1:
simpleSolid.extend(cut_solids)
else:
Expand All @@ -725,7 +725,7 @@ def split_planes_new(Solids, universe_box):
return simpleSolid, 0


def split_planes_org(Solids, universe_box):
def split_planes_org(Solids, universe_box, options):
Bases = []
err = 0
for sol in Solids:
Expand Down Expand Up @@ -758,7 +758,7 @@ def split_planes_org(Solids, universe_box):
for p in Planes[imin]:
p.build_surface()
Tools.append(p.shape)
comsolid = UF.split_bop(base, Tools, opt.splitTolerance)
comsolid = UF.split_bop(base, Tools, options.splitTolerance, options)
if len(comsolid.Solids) == 1:
if (
abs(comsolid.Solids[0].Volume - base.Volume) / base.Volume
Expand Down Expand Up @@ -864,7 +864,7 @@ def sort_planes(PlaneList, sortElements=False):
return sortedPlanes


def split_p_planes_new(solid, universe_box):
def split_p_planes_new(solid, universe_box, options):
SPlanes = extract_surfaces(solid, "Planes", universe_box)

Planes = []
Expand All @@ -875,37 +875,37 @@ def split_p_planes_new(solid, universe_box):

if len(Planes) == 0:
return [solid]
if len(Planes[-1]) < opt.nPlaneReverse:
if len(Planes[-1]) < options.nPlaneReverse:
Planes.reverse()
out_solid = [solid]
for pp in Planes:
for p in pp:
p.build_surface()
tools = tuple(p.shape for p in pp)
comsolid = UF.split_bop(solid, tools, opt.splitTolerance)
comsolid = UF.split_bop(solid, tools, options.splitTolerance, options)

if len(comsolid.Solids) > 1:
out_solid = comsolid.Solids
break
return out_solid


def split_p_planes_org(solid, universe_box):
def split_p_planes_org(solid, universe_box, options):
SPlanes = extract_surfaces(solid, "Planes", universe_box)

if len(SPlanes["P"]) == 0:
return [solid]
out_solid = [solid]
for p in SPlanes["P"]:
p.build_surface()
comsolid = UF.split_bop(solid, [p.shape], opt.splitTolerance)
comsolid = UF.split_bop(solid, [p.shape], options.splitTolerance, options)
if len(comsolid.Solids) > 1:
out_solid = comsolid.Solids
break
return out_solid


def split_2nd_order(Solids, universe_box):
def split_2nd_order(Solids, universe_box, options):
err = 0
Base = Solids
for kind in ["Cyl", "Cone", "Sph", "Tor"]:
Expand All @@ -922,7 +922,7 @@ def split_2nd_order(Solids, universe_box):
s.build_surface()
try:
comsolid = UF.split_bop(
solid, [s.shape], opt.splitTolerance
solid, [s.shape], options.splitTolerance, options
)
solidsInCom = []
for s in comsolid.Solids:
Expand Down Expand Up @@ -951,14 +951,14 @@ def split_2nd_order(Solids, universe_box):
return Base, err


def split_2nd_order_planes(Solids):
def split_2nd_order_planes(Solids, options):
err = 0
simpleSolid = []
Bases = Solids
while True:
newBases = []
for base in Bases:
cut_solids, err = split_2nd_o_plane(base)
cut_solids, err = split_2nd_o_plane(base, options)
if len(cut_solids) == 1:
simpleSolid.extend(cut_solids)
else:
Expand All @@ -971,7 +971,7 @@ def split_2nd_order_planes(Solids):
return simpleSolid, err


def split_2nd_o_plane(solid):
def split_2nd_o_plane(solid, options):

err = 0
flag_inv = CD.is_inverted(solid)
Expand All @@ -981,7 +981,7 @@ def split_2nd_o_plane(solid):
return [solid], err

for p in planes:
comsolid = UF.split_bop(solid, [p], opt.splitTolerance)
comsolid = UF.split_bop(solid, [p], options.splitTolerance, options)
if not comsolid.Solids:
comsolid = solid
continue
Expand Down Expand Up @@ -1010,21 +1010,21 @@ def remove_solids(Solids):
return Solids_Clean, err


def split_component(solidShape, universe_box):
def split_component(solidShape, universe_box, options):
err = 0
err2 = 0

Volini = solidShape.Volume
Solids = solidShape.Solids
# Split with explicit planes bounding the solid and
# implicit planes interface of two 2nd order surfaces
split0, err = split_planes(Solids, universe_box, opt.newSplitPlane)
split0, err = split_planes(Solids, universe_box, options)
# Split with explicit 2nd order surfaces bounding the solid

split1, err1 = split_2nd_order(split0, universe_box)
split1, err1 = split_2nd_order(split0, universe_box, options)
err += err1

split, err2 = split_2nd_order_planes(split1)
split, err2 = split_2nd_order_planes(split1, options)
err += err2
Pieces = []
for part in split:
Expand All @@ -1049,14 +1049,14 @@ def split_component(solidShape, universe_box):


# TODO rename function but be careful as there are functions with the same name elsewhere in the code
def SplitSolid(solidShape, universe_box):
def SplitSolid(solidShape, universe_box, options):

solid_parts = []

for solid in solidShape.Solids:

explode = split_full_cylinder(solid)
piece, err = split_component(explode, universe_box)
explode = split_full_cylinder(solid, options)
piece, err = split_component(explode, universe_box, options)
solid_parts.append(piece)

return Part.makeCompound(solid_parts), err
10 changes: 5 additions & 5 deletions src/geouned/GEOUNED/LoadFile/LoadFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import FreeCAD

from ..Utils.Functions import GeounedSolid
from ..Utils.Options.Classes import Options as opt


logger = logging.getLogger(__name__)


def get_label(label):
def get_label(label, options):
"""Deleting the last word of the string if this is a number
Only if the option delLastNumber is True"""
if not opt.delLastNumber:
if not options.delLastNumber:
return label
wrd = label.split()
try:
Expand All @@ -22,11 +22,11 @@ def get_label(label):
return label


def getCommentTree(obj):
def getCommentTree(obj, options):
recursive_list = []
c_obj = obj
while c_obj.InList:
label = get_label(c_obj.InList[0].Label)
label = get_label(c_obj.InList[0].Label, options)
recursive_list.append(label)
c_obj = c_obj.InList[0]

Expand Down
Loading

0 comments on commit 6e083e7

Please sign in to comment.