@@ -32,8 +32,6 @@ class CadToCsg:
32
32
"""Base class for the conversion of CAD to CSG models
33
33
34
34
Args:
35
- stepFile (str, optional): Name of the CAD file (in STEP format) to
36
- be converted. Defaults to "".
37
35
options (geouned.Options, optional): An instance of a geouned.Options
38
36
class with the attributes set for the desired conversion. Defaults
39
37
to a geouned.Options with default attributes values.
@@ -53,14 +51,12 @@ class with the attributes set for the desired conversion. Defaults
53
51
54
52
def __init__ (
55
53
self ,
56
- stepFile : str = "" ,
57
54
options : Options = Options (),
58
55
tolerances : Tolerances = Tolerances (),
59
56
numeric_format : NumericFormat = NumericFormat (),
60
57
settings : Settings = Settings (),
61
58
):
62
59
63
- self .stepFile = stepFile
64
60
self .options = options
65
61
self .tolerances = tolerances
66
62
self .numeric_format = numeric_format
@@ -69,16 +65,8 @@ def __init__(
69
65
# define later when running the code
70
66
self .geometry_bounding_box = None
71
67
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 = []
82
70
83
71
@property
84
72
def options (self ):
@@ -209,20 +197,23 @@ def export_csg(
209
197
volCARD = volCARD ,
210
198
UCARD = UCARD ,
211
199
dummyMat = dummyMat ,
212
- stepFile = self .stepFile ,
200
+ step_filename = self .filename ,
213
201
)
214
202
215
203
logger .info ("End of Monte Carlo code translation phase" )
216
204
217
205
@classmethod
218
206
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.
223
214
224
215
Args:
225
- filename str: The filename of the config file. Defaults to "config.json".
216
+ filename str: The filename of the config file.
226
217
227
218
Raises:
228
219
FileNotFoundError: If the config file is not found
@@ -238,10 +229,13 @@ def from_json(cls, filename: str):
238
229
with open (filename ) as f :
239
230
config = json .load (f )
240
231
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
+
242
236
for key in config .keys ():
243
237
244
- if key in ["stepFile " , "export_csg" ]:
238
+ if key in ["load_step_file " , "export_csg" ]:
245
239
pass # these two keys are used before or after this loop
246
240
247
241
elif key == "Tolerances" :
@@ -258,7 +252,7 @@ def from_json(cls, filename: str):
258
252
259
253
else :
260
254
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'"
262
256
)
263
257
264
258
cad_to_csg .start ()
@@ -446,25 +440,39 @@ def set(self, kwrd, value):
446
440
else :
447
441
self .__dict__ ["geometryName" ] == value [:- 4 ]
448
442
449
- def _load_step_file (
443
+ def load_step_file (
450
444
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 ] = [],
454
447
):
455
448
"""
456
449
Load STEP file(s) and extract solid volumes and enclosure volumes.
457
450
458
451
Args:
459
452
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 .
461
454
462
455
Returns:
463
456
tuple: A tuple containing the solid volumes list and enclosure volumes list extracted from the STEP files.
464
457
"""
465
-
466
458
logger .info ("Start of step file loading phase" )
467
459
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
+
468
476
if isinstance (filename , (list , tuple )):
469
477
step_files = filename
470
478
else :
@@ -484,9 +492,10 @@ def _load_step_file(
484
492
self .meta_list = join_meta_lists (MetaChunk )
485
493
self .enclosure_list = join_meta_lists (EnclosureChunk )
486
494
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 ]
490
499
491
500
logger .info ("End of step file loading phase" )
492
501
@@ -501,7 +510,7 @@ def _export_solids(self, filename: str):
501
510
# export in STEP format solids read from input file
502
511
if self .meta_list == []:
503
512
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 "
505
514
)
506
515
solids = []
507
516
for m in self .meta_list :
@@ -552,9 +561,6 @@ def start(self):
552
561
553
562
startTime = datetime .now ()
554
563
555
- # sets the self.meta_list and self.enclosure_list
556
- self ._load_step_file (filename = self .stepFile , cell_range = self .settings .cellRange )
557
-
558
564
if self .settings .exportSolids :
559
565
self ._export_solids (filename = self .settings .exportSolids )
560
566
0 commit comments