Skip to content

Commit 2f3f547

Browse files
shimwellpsauvan
authored andcommitted
Changing cellRange to discrete numbers to allow more flexability when loading geometry (#205)
* moved two methods out of start * removed unnecessary logger line * changed cellRange to skip_solids * improved doc string * lowercase args pep8
1 parent 12fc39b commit 2f3f547

13 files changed

+139
-126
lines changed

docs/usage/python_api_usage.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ The example makes use of default attributes.
1212
.. code-block:: python
1313
1414
import geouned
15-
geo = geouned.CadToCsg(stepFile='cuboid.stp')
15+
geo = geouned.CadToCsg()
16+
geo.load_step_file(filename='cuboid.stp')
1617
geo.start()
1718
geo.export_csg()
1819
@@ -43,7 +44,6 @@ The following example shows a usage with every attributes specified.
4344
debug=False,
4445
compSolids=True,
4546
simplify="no",
46-
cellRange=[],
4747
exportSolids="",
4848
minVoidSize=200.0,
4949
maxSurf=50,
@@ -90,13 +90,17 @@ The following example shows a usage with every attributes specified.
9090
)
9191
9292
geo = geouned.CadToCsg(
93-
stepFile="cuboid.stp",
9493
options=my_options,
9594
settings=my_settings,
9695
tolerances=my_tolerances,
9796
numeric_format=my_numeric_format,
9897
)
9998
99+
geo.load_step_file(
100+
filename="cuboid.stp",
101+
skip_solids=[],
102+
)
103+
100104
geo.start()
101105
102106
geo.export_csg(

docs/usage/python_cli_usage.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ First create a JSON file called "config.json" containing the following.
1212
.. code-block:: json
1313
1414
{
15-
"stepFile": "cuboid.stp"
15+
"load_step_file": {
16+
"filename":"cuboid.stp"
17+
}
1618
}
1719
1820
Then execute the command line interface tool to convert your STEP file to CSG files with the default configuration.
@@ -32,7 +34,10 @@ Here is a complete JSON file specification
3234
.. code-block:: json
3335
3436
{
35-
"stepFile": "cuboid.stp",
37+
"load_step_file": {
38+
"filename": "cuboid.stp",
39+
"skip_solids": []
40+
},
3641
"Options": {
3742
"forceCylinder": false,
3843
"newSplitPlane": true,
@@ -85,7 +90,6 @@ Here is a complete JSON file specification
8590
"debug": false,
8691
"compSolids": true,
8792
"simplify": "no",
88-
"cellRange": [],
8993
"exportSolids": "",
9094
"minVoidSize": 200.0,
9195
"maxSurf": 50,

src/geouned/GEOUNED/core.py

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class CadToCsg:
3232
"""Base class for the conversion of CAD to CSG models
3333
3434
Args:
35-
stepFile (str, optional): Name of the CAD file (in STEP format) to
36-
be converted. Defaults to "".
3735
options (geouned.Options, optional): An instance of a geouned.Options
3836
class with the attributes set for the desired conversion. Defaults
3937
to a geouned.Options with default attributes values.
@@ -53,14 +51,12 @@ class with the attributes set for the desired conversion. Defaults
5351

5452
def __init__(
5553
self,
56-
stepFile: str = "",
5754
options: Options = Options(),
5855
tolerances: Tolerances = Tolerances(),
5956
numeric_format: NumericFormat = NumericFormat(),
6057
settings: Settings = Settings(),
6158
):
6259

63-
self.stepFile = stepFile
6460
self.options = options
6561
self.tolerances = tolerances
6662
self.numeric_format = numeric_format
@@ -69,16 +65,8 @@ def __init__(
6965
# define later when running the code
7066
self.geometry_bounding_box = None
7167
self.meta_list = []
72-
73-
@property
74-
def stepFile(self):
75-
return self._stepFile
76-
77-
@stepFile.setter
78-
def stepFile(self, value: str):
79-
if not isinstance(value, str):
80-
raise TypeError(f"geouned.CadToCsg.stepFile should be a str, not a {type(value)}")
81-
self._stepFile = value
68+
self.filename = None
69+
self.skip_solids = []
8270

8371
@property
8472
def options(self):
@@ -209,20 +197,23 @@ def export_csg(
209197
volCARD=volCARD,
210198
UCARD=UCARD,
211199
dummyMat=dummyMat,
212-
stepFile=self.stepFile,
200+
step_filename=self.filename,
213201
)
214202

215203
logger.info("End of Monte Carlo code translation phase")
216204

217205
@classmethod
218206
def from_json(cls, filename: str):
219-
"""Creates a CadToCsg instance and returns the instance. Populating the
220-
Options, Tolerance, Settings and NumericFormat attributes from matching
221-
key names in the JSON. If export_to_csg key is present then this method
222-
also runs .start() and .export_to_csg() on the instance.
207+
"""Creates a CadToCsg instance, runs CadToCsg.load_Step_file(), runs
208+
CadToCsg.start() and returns the instance. Populating the arguments for
209+
the methods that are run by looking for keys with the same name as the
210+
method in the JSON file. For example CadToCsg.start() accepts arguments
211+
for Options, Tolerance, Settings and NumericFormat and can be populated
212+
from matching key names in the JSON. If export_to_csg key is present
213+
then this method also runs CadToCsg.export_to_csg() on the instance.
223214
224215
Args:
225-
filename str: The filename of the config file. Defaults to "config.json".
216+
filename str: The filename of the config file.
226217
227218
Raises:
228219
FileNotFoundError: If the config file is not found
@@ -238,10 +229,13 @@ def from_json(cls, filename: str):
238229
with open(filename) as f:
239230
config = json.load(f)
240231

241-
cad_to_csg = cls(stepFile=config["stepFile"])
232+
cad_to_csg = cls()
233+
234+
cad_to_csg.load_step_file(**config["load_step_file"])
235+
242236
for key in config.keys():
243237

244-
if key in ["stepFile", "export_csg"]:
238+
if key in ["load_step_file", "export_csg"]:
245239
pass # these two keys are used before or after this loop
246240

247241
elif key == "Tolerances":
@@ -258,7 +252,7 @@ def from_json(cls, filename: str):
258252

259253
else:
260254
raise ValueError(
261-
f"Invalid key '{key}' found in config file {filename}. Acceptable key names are 'stepFile', 'export_csg', 'Settings', 'Parameters', 'Tolerances' and 'NumericFormat'"
255+
f"Invalid key '{key}' found in config file {filename}. Acceptable key names are 'load_step_file', 'export_csg', 'Settings', 'Parameters', 'Tolerances' and 'NumericFormat'"
262256
)
263257

264258
cad_to_csg.start()
@@ -446,25 +440,39 @@ def set(self, kwrd, value):
446440
else:
447441
self.__dict__["geometryName"] == value[:-4]
448442

449-
def _load_step_file(
443+
def load_step_file(
450444
self,
451-
filename: str,
452-
# TODO consider having discrete indexes (1,5,7) instead of range (1,7) as this offers more flexibility to the user
453-
cell_range: typing.Union[None, typing.Tuple[int, int]] = None,
445+
filename: typing.Union[str, typing.Sequence[str]],
446+
skip_solids: typing.Sequence[int] = [],
454447
):
455448
"""
456449
Load STEP file(s) and extract solid volumes and enclosure volumes.
457450
458451
Args:
459452
filename (str): The path to the STEP file or a list of paths to multiple STEP files.
460-
cell_range (tuple[int, int], optional): A tuple representing the range of solids to select from the original STEP solids. Defaults to None.
453+
skip_solids (Sequence[int], optional): A sequence (list or tuple) of indexes of solids to not load for conversion.
461454
462455
Returns:
463456
tuple: A tuple containing the solid volumes list and enclosure volumes list extracted from the STEP files.
464457
"""
465-
466458
logger.info("Start of step file loading phase")
467459

460+
if not isinstance(skip_solids, (list, tuple)):
461+
raise TypeError(f"skip_solids should be a list, tuple of ints, not a {type(skip_solids)}")
462+
for entry in skip_solids:
463+
if not isinstance(entry, int):
464+
raise TypeError(f"skip_solids should contain only ints, not a {type(entry)}")
465+
466+
if not isinstance(filename, (str, list, tuple)):
467+
raise TypeError(f"filename should be a str or a sequence of str, not a {type(filename)}")
468+
if isinstance(filename, (list, tuple)):
469+
for entry in filename:
470+
if not isinstance(entry, str):
471+
raise TypeError(f"filename should contain only str, not a {type(entry)}")
472+
473+
self.filename = filename
474+
self.skip_solids = skip_solids
475+
468476
if isinstance(filename, (list, tuple)):
469477
step_files = filename
470478
else:
@@ -484,9 +492,10 @@ def _load_step_file(
484492
self.meta_list = join_meta_lists(MetaChunk)
485493
self.enclosure_list = join_meta_lists(EnclosureChunk)
486494

487-
# Select a specific solid range from original STEP solids
488-
if cell_range:
489-
self.meta_list = self.meta_list[cell_range[0] : cell_range[1]]
495+
# deleting the solid index in reverse order so the indexes don't change for subsequent deletions
496+
for solid_index in sorted(skip_solids, reverse=True):
497+
logger.info(f"Removing solid index: {solid_index} from list of {len(self.meta_list)} solids")
498+
del self.meta_list[solid_index]
490499

491500
logger.info("End of step file loading phase")
492501

@@ -501,7 +510,7 @@ def _export_solids(self, filename: str):
501510
# export in STEP format solids read from input file
502511
if self.meta_list == []:
503512
raise ValueError(
504-
"No solids in CadToCsg.meta_list to export. Try loading the STEP file first with CadToCsg._load_step_file"
513+
"No solids in CadToCsg.meta_list to export. Try loading the STEP file first with CadToCsg.load_step_file"
505514
)
506515
solids = []
507516
for m in self.meta_list:
@@ -552,9 +561,6 @@ def start(self):
552561

553562
startTime = datetime.now()
554563

555-
# sets the self.meta_list and self.enclosure_list
556-
self._load_step_file(filename=self.stepFile, cell_range=self.settings.cellRange)
557-
558564
if self.settings.exportSolids:
559565
self._export_solids(filename=self.settings.exportSolids)
560566

0 commit comments

Comments
 (0)