Skip to content

Commit 03661e7

Browse files
REFACTOR: Clean analysis_3d file (#5839)
1 parent d22239e commit 03661e7

File tree

13 files changed

+202
-125
lines changed

13 files changed

+202
-125
lines changed

doc/source/User_guide/pyaedt_file_data/reports.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This code shows how to create the JSON file:
1010
from ansys.aedt.core import Hfss
1111
hfss = Hfss()
1212
compfile = hfss.components3d["Dipole_Antenna_DM"]
13-
geometryparams = hfss.get_components3d_vars("Dipole_Antenna_DM")
13+
geometryparams = hfss.get_component_variables("Dipole_Antenna_DM")
1414
hfss.modeler.insert_3d_component(compfile, geometryparams)
1515
hfss.create_setup()
1616
filename = "hfss_report_example.json"

src/ansys/aedt/core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def custom_show_warning(message, category, filename, lineno, file=None, line=Non
7171
#
7272

7373
pyaedt_path = os.path.dirname(__file__)
74-
__version__ = "0.15.dev0"
74+
__version__ = "0.16.dev0"
7575
version = __version__
7676

7777
# isort: off

src/ansys/aedt/core/application/analysis_3d.py

Lines changed: 72 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@
2525
import csv
2626
import ntpath
2727
import os
28+
from pathlib import Path
29+
from typing import List
30+
from typing import Union
31+
import warnings
2832

2933
from ansys.aedt.core.application.analysis import Analysis
3034
from ansys.aedt.core.generic.checks import min_aedt_version
3135
from ansys.aedt.core.generic.configurations import Configurations
3236
from ansys.aedt.core.generic.constants import unit_converter
37+
from ansys.aedt.core.generic.file_utils import get_dxf_layers
38+
from ansys.aedt.core.generic.file_utils import read_component_file
3339
from ansys.aedt.core.generic.general_methods import generate_unique_name
3440
from ansys.aedt.core.generic.general_methods import open_file
3541
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
@@ -297,8 +303,8 @@ def plot(
297303
show_grid=show_grid,
298304
)
299305

300-
@pyaedt_function_handler(setup_name="setup", variation_string="variations")
301-
def export_mesh_stats(self, setup, variations="", mesh_path=None):
306+
@pyaedt_function_handler(setup_name="setup", variation_string="variations", mesh_path="output_file")
307+
def export_mesh_stats(self, setup, variations="", output_file=None):
302308
"""Export mesh statistics to a file.
303309
304310
Parameters
@@ -307,7 +313,7 @@ def export_mesh_stats(self, setup, variations="", mesh_path=None):
307313
Setup name.
308314
variations : str, optional
309315
Variation list. The default is ``""``.
310-
mesh_path : str, optional
316+
output_file : str, optional
311317
Full path to the mesh statistics file. The default is ``None``, in which
312318
caswe the working directory is used.
313319
@@ -320,15 +326,38 @@ def export_mesh_stats(self, setup, variations="", mesh_path=None):
320326
----------
321327
>>> oDesign.ExportMeshStats
322328
"""
323-
if not mesh_path:
324-
mesh_path = os.path.join(self.working_directory, "meshstats.ms")
325-
self.odesign.ExportMeshStats(setup, variations, mesh_path)
326-
return mesh_path
329+
if not output_file:
330+
output_file = str(Path(self.working_directory) / "meshstats.ms")
331+
self.odesign.ExportMeshStats(setup, variations, output_file)
332+
return output_file
333+
334+
@pyaedt_function_handler()
335+
def get_component_variables(self, name: Union[str, Path]) -> dict:
336+
"""Read component file and extract variables.
337+
338+
Parameters
339+
----------
340+
name : str or :class:`pathlib.Path`
341+
Name of the 3D component, which must be in the ``syslib`` or ``userlib`` directory.
342+
Otherwise, you must specify the full absolute path to the component file.
343+
344+
Returns
345+
-------
346+
dict
347+
Dictionary of variables in the component file.
348+
"""
349+
if str(name) not in self.components3d:
350+
return read_component_file(name)
351+
else:
352+
return read_component_file(self.components3d[name])
327353

328354
@pyaedt_function_handler(component3dname="component_name")
329355
def get_components3d_vars(self, component_name):
330356
"""Read the A3DCOMP file and check for variables.
331357
358+
.. deprecated:: 0.15.1
359+
Use :func:`get_component_variables` method instead.
360+
332361
Parameters
333362
----------
334363
component_name :
@@ -340,43 +369,10 @@ def get_components3d_vars(self, component_name):
340369
dict
341370
Dictionary of variables in the A3DCOMP file.
342371
"""
343-
vars = {}
344-
if component_name not in self.components3d:
345-
aedt_fh = open_file(component_name, "rb")
346-
if aedt_fh:
347-
temp = aedt_fh.read().splitlines()
348-
_all_lines = []
349-
for line in temp:
350-
try:
351-
_all_lines.append(line.decode("utf-8").lstrip("\t"))
352-
except UnicodeDecodeError:
353-
break
354-
for line in _all_lines:
355-
if "VariableProp(" in line:
356-
line_list = line.split("'")
357-
if not [
358-
c for c in line_list[len(line_list) - 2] if c in ["+", "-", "*", "'" "," "/", "(", ")"]
359-
]:
360-
self[line_list[1]] = line_list[len(line_list) - 2]
361-
else:
362-
vars[line_list[1]] = line_list[len(line_list) - 2]
363-
aedt_fh.close()
364-
return vars
365-
else:
366-
return False
367-
with open_file(self.components3d[component_name], "rb") as aedt_fh:
368-
temp = aedt_fh.read().splitlines()
369-
_all_lines = []
370-
for line in temp:
371-
try:
372-
_all_lines.append(line.decode("utf-8").lstrip("\t"))
373-
except UnicodeDecodeError:
374-
break
375-
for line in _all_lines:
376-
if "VariableProp(" in line:
377-
line_list = line.split("'")
378-
vars[line_list[1]] = line_list[len(line_list) - 2]
379-
return vars
372+
warnings.warn(
373+
"`get_components3d_vars` is deprecated. Use `get_component_variables` method instead.", DeprecationWarning
374+
)
375+
return self.get_component_variables(component_name)
380376

381377
@pyaedt_function_handler(objectname="assignment", property="property_name", type="property_type")
382378
def get_property_value(self, assignment, property_name, property_type=None):
@@ -1227,56 +1223,52 @@ def check_intersections(output, input_list, cad_in=None):
12271223
break
12281224
return nets
12291225

1230-
@pyaedt_function_handler()
1231-
def get_dxf_layers(self, file_path):
1232-
# type: (str) -> list[str]
1226+
@pyaedt_function_handler(file_path="input_file")
1227+
def get_dxf_layers(self, input_file: Union[str, Path]) -> List[str]:
12331228
"""Read a DXF file and return all layer names.
12341229
1230+
.. deprecated:: 0.15.1
1231+
Use :func:`ansys.aedt.core.generic.file_utils.get_dxf_layers` method instead.
1232+
12351233
Parameters
12361234
----------
1237-
file_path : str
1235+
input_file : str or :class:`pathlib.Path`
12381236
Full path to the DXF file.
12391237
12401238
Returns
12411239
-------
12421240
list
12431241
List of layers in the DXF file.
12441242
"""
1245-
layer_names = []
1246-
with open_file(file_path, encoding="utf8") as f:
1247-
lines = f.readlines()
1248-
indices = self._find_indices(lines, "AcDbLayerTableRecord\n")
1249-
index_offset = 1
1250-
if not indices:
1251-
indices = self._find_indices(lines, "LAYER\n")
1252-
index_offset = 3
1253-
for idx in indices:
1254-
if "2" in lines[idx + index_offset]:
1255-
layer_names.append(lines[idx + index_offset + 1].replace("\n", ""))
1256-
return layer_names
1257-
1258-
@pyaedt_function_handler(layers_list="layers")
1243+
warnings.warn(
1244+
"`get_dxf_layers` is deprecated. "
1245+
"Use `ansys.aedt.core.generic.file_utils.get_dxf_layers` method instead.",
1246+
DeprecationWarning,
1247+
)
1248+
1249+
return get_dxf_layers(input_file)
1250+
1251+
@pyaedt_function_handler(layers_list="layers", file_path="input_file")
12591252
def import_dxf(
12601253
self,
1261-
file_path,
1262-
layers,
1263-
auto_detect_close=True,
1264-
self_stitch=True,
1265-
self_stitch_tolerance=0,
1266-
scale=0.001,
1267-
defeature_geometry=False,
1268-
defeature_distance=0,
1269-
round_coordinates=False,
1270-
round_num_digits=4,
1271-
write_poly_with_width_as_filled_poly=False,
1272-
import_method=1,
1273-
): # pragma: no cover
1274-
# type: (str, list, bool, bool, float, float, bool, float, bool, int, bool, int, bool) -> bool
1254+
input_file: str,
1255+
layers: List[str],
1256+
auto_detect_close: bool = True,
1257+
self_stitch: bool = True,
1258+
self_stitch_tolerance: float = 0.0,
1259+
scale: float = 0.001,
1260+
defeature_geometry: bool = False,
1261+
defeature_distance: float = 0.0,
1262+
round_coordinates: bool = False,
1263+
round_num_digits: int = 4,
1264+
write_poly_with_width_as_filled_poly: bool = False,
1265+
import_method: Union[int, bool] = 1,
1266+
) -> bool: # pragma: no cover
12751267
"""Import a DXF file.
12761268
12771269
Parameters
12781270
----------
1279-
file_path : str
1271+
input_file : str
12801272
Path to the DXF file.
12811273
layers : list
12821274
List of layer names to import. To get the dxf_layers in the DXF file,
@@ -1306,7 +1298,7 @@ def import_dxf(
13061298
The default is ``4``.
13071299
write_poly_with_width_as_filled_poly : bool, optional
13081300
Imports wide polylines as polygons. The default is ``False``.
1309-
import_method : int, bool
1301+
import_method : int or bool, optional
13101302
Whether the import method is ``Script`` or ``Acis``.
13111303
The default is ``1``, which means that the ``Acis`` is used.
13121304
@@ -1323,7 +1315,7 @@ def import_dxf(
13231315
if self.desktop_class.non_graphical and self.desktop_class.aedt_version_id < "2024.2": # pragma: no cover
13241316
self.logger.error("Method is supported only in graphical mode.")
13251317
return False
1326-
dxf_layers = self.get_dxf_layers(file_path)
1318+
dxf_layers = self.get_dxf_layers(input_file)
13271319
for layer in layers:
13281320
if layer not in dxf_layers:
13291321
self.logger.error(f"{layer} does not exist in specified dxf.")
@@ -1335,7 +1327,7 @@ def import_dxf(
13351327
sheet_bodies_2d = True
13361328

13371329
vArg1 = ["NAME:options"]
1338-
vArg1.append("FileName:="), vArg1.append(file_path.replace(os.sep, "/"))
1330+
vArg1.append("FileName:="), vArg1.append(input_file.replace(os.sep, "/"))
13391331
vArg1.append("Scale:="), vArg1.append(scale)
13401332
vArg1.append("AutoDetectClosed:="), vArg1.append(auto_detect_close)
13411333
vArg1.append("SelfStitch:="), vArg1.append(self_stitch)
@@ -1461,22 +1453,3 @@ def import_gds_3d(self, input_file: str, mapping_layers: dict, units: str = "um"
14611453
)
14621454
self.logger.info("GDS layer imported with elevations and thickness.")
14631455
return True
1464-
1465-
@pyaedt_function_handler()
1466-
def _find_indices(self, list_to_check, item_to_find):
1467-
# type: (list, str|int) -> list
1468-
"""Given a list, returns the list of indices for all occurrences of a given element.
1469-
1470-
Parameters
1471-
----------
1472-
list_to_check: list
1473-
List to check.
1474-
item_to_find: str, int
1475-
Element to search for in the list.
1476-
1477-
Returns
1478-
-------
1479-
list
1480-
Indices of the occurrences of a given element.
1481-
"""
1482-
return [idx for idx, value in enumerate(list_to_check) if value == item_to_find]

src/ansys/aedt/core/application/analysis_3d_layout.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25-
import os
25+
from pathlib import Path
2626
import warnings
2727

2828
from ansys.aedt.core.application.analysis import Analysis
@@ -229,8 +229,8 @@ def change_design_settings(self, settings):
229229
self.odesign.DesignOptions(arg)
230230
return True
231231

232-
@pyaedt_function_handler(setup_name="setup", variation_string="variations")
233-
def export_mesh_stats(self, setup, variations="", mesh_path=None):
232+
@pyaedt_function_handler(setup_name="setup", variation_string="variations", mesh_path="output_file")
233+
def export_mesh_stats(self, setup, variations="", output_file=None):
234234
"""Export mesh statistics to a file.
235235
236236
Parameters
@@ -239,7 +239,7 @@ def export_mesh_stats(self, setup, variations="", mesh_path=None):
239239
Setup name.
240240
variations : str, optional
241241
Variation List.
242-
mesh_path : str, optional
242+
output_file : str, optional
243243
Full path to mesh statistics file. If `None` working_directory will be used.
244244
245245
Returns
@@ -251,10 +251,10 @@ def export_mesh_stats(self, setup, variations="", mesh_path=None):
251251
----------
252252
>>> oModule.ExportMeshStats
253253
"""
254-
if not mesh_path:
255-
mesh_path = os.path.join(self.working_directory, "meshstats.ms")
256-
self.odesign.ExportMeshStats(setup, variations, mesh_path)
257-
return mesh_path
254+
if not output_file:
255+
output_file = str(Path(self.working_directory) / "meshstats.ms")
256+
self.odesign.ExportMeshStats(setup, variations, output_file)
257+
return output_file
258258

259259
@property
260260
def modeler(self):

0 commit comments

Comments
 (0)