Skip to content

Commit 50f53aa

Browse files
Fix units
1 parent ef107aa commit 50f53aa

File tree

3 files changed

+70
-41
lines changed

3 files changed

+70
-41
lines changed

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,16 +1522,16 @@ def update_var(self):
15221522
)
15231523

15241524
def __target_container_name(self):
1525-
"""Resolve the OO property container name for this variable."""
1525+
"""Resolve the property container name for this variable."""
15261526
name = "Variables"
15271527
if not self._app:
15281528
return name
1529-
if self.__has_definition_parameters:
1529+
if self.__has_definition_parameters and self.circuit_parameter:
15301530
# If the variable lives in DefinitionParameters, return that
15311531
try:
15321532
if self._variable_name in list(self._oo(self._app.odesign, "DefinitionParameters").GetPropNames()):
15331533
return "DefinitionParameters"
1534-
except Exception:
1534+
except Exception: # pragma: no cover
15351535
pass
15361536
# Otherwise it is either LocalVariables or Variables
15371537
return "LocalVariables"
@@ -1545,13 +1545,29 @@ def _set_prop_val(self, prop, val, n_times=10):
15451545
try:
15461546
container = self.__target_container_name()
15471547
if container == "DefinitionParameters":
1548-
_retry_ntimes(n_times, self._oo(self._aedt_obj, container).SetPropValue(prop, val))
1549-
1550-
# Fallback to variable-specific path
1551-
path = (
1552-
f"{container}/{self._variable_name}" if container != "Variables" else f"Variables/{self._variable_name}"
1553-
)
1554-
return _retry_ntimes(n_times, self._oo(self._aedt_obj, path).SetPropValue(prop, val))
1548+
prop_name, prop_to_set = prop.split("/")
1549+
self._app.odesign.ChangeProperty(
1550+
[
1551+
"NAME:AllTabs",
1552+
[
1553+
"NAME:DefinitionParameterTab",
1554+
["NAME:PropServers", f"Instance:{self._app.odesign.GetName()}"],
1555+
[
1556+
"NAME:ChangedProps",
1557+
[f"NAME:{self.name}", [f"NAME:{prop_name}", f"{prop_to_set}:=", val]],
1558+
],
1559+
],
1560+
]
1561+
)
1562+
else:
1563+
# Object-oriented set property value
1564+
path = (
1565+
f"{container}/{self._variable_name}"
1566+
if container != "Variables"
1567+
else f"Variables/{self._variable_name}"
1568+
)
1569+
_retry_ntimes(n_times, self._oo(self._aedt_obj, path).SetPropValue, prop, val)
1570+
return True
15551571
except Exception:
15561572
if self._app:
15571573
raise AEDTRuntimeError(f"Failed to set property '{prop}' value.")
@@ -1565,15 +1581,15 @@ def _get_prop_generic(self, prop, evaluated=False):
15651581
# container = self.__target_container_name()
15661582
app = self._app.odesign
15671583

1568-
# DefinitionParameters only available in circuit and Hfss 3d layout design type
1584+
# DefinitionParameters only available in circuit and HFSS 3D Layout design type
15691585
if self.__has_definition_parameters:
15701586
inst_name = f"Instance:{app.GetName()}"
15711587
if self.circuit_parameter:
15721588
# Definition parameters properties do not work with Object-Oriented-Programming API
15731589
obj = self._oo(self._aedt_obj, "DefinitionParameters")
15741590
if not obj or prop != self.name:
15751591
self._app.logger.error(
1576-
"Parameter Default variable properties can not be load. AEDT API limitation"
1592+
"Parameter Default variable properties can not be load. AEDT API limitation."
15771593
)
15781594
return None
15791595
return obj.GetPropEvaluatedValue(prop) if evaluated else obj.GetPropValue(prop)

tests/system/general/test_01_Design.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,10 @@ def test_42_save_project_with_file_name(self, aedtapp, local_scratch):
522522
def test_43_edit_notes(self, aedtapp):
523523
assert aedtapp.edit_notes("this a test")
524524
assert not aedtapp.edit_notes(1)
525+
526+
def test_value_with_units(self, aedtapp):
527+
assert aedtapp.value_with_units("10mm") == "10mm"
528+
assert aedtapp.value_with_units("10") == "10mm"
529+
assert aedtapp.value_with_units("10", units_system="Angle") == "10deg"
530+
assert aedtapp.value_with_units("10", units_system="invalid") == "10"
531+
assert aedtapp.value_with_units("A + Bmm") == "A + Bmm"

tests/system/general/test_09_VariableManager.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,11 @@ def test_intrinsics(self, app):
294294
assert app.variable_manager.dependent_variables["fc"].units == app.units.frequency
295295

296296
def test_arrays(self, app):
297-
app["arr_index"] = 0
298-
app["arr1"] = "[1, 2, 3]"
299-
app["arr2"] = [1, 2, 3]
297+
app.variable_manager.set_variable("arr_index", expression=0, circuit_parameter=False)
298+
app.variable_manager.set_variable("arr1", expression="[1, 2, 3]", circuit_parameter=False)
299+
app.variable_manager.set_variable("arr2", expression=[1, 2, 3], circuit_parameter=False)
300+
app.variable_manager.set_variable("arr_index", expression=0, circuit_parameter=False)
301+
300302
app["getvalue1"] = "arr1[arr_index]"
301303
app["getvalue2"] = "arr2[arr_index]"
302304
assert app.variable_manager["getvalue1"].numeric_value == 1.0
@@ -323,19 +325,23 @@ def test_project_variable_operation(self, app):
323325

324326
def test_test_optimization_properties(self, app):
325327
var = "v1"
326-
app[var] = "10mm"
328+
app.variable_manager.set_variable(var, "10mm", circuit_parameter=False)
327329
v = app.variable_manager
328330
assert not v[var].is_optimization_enabled
329331
v[var].is_optimization_enabled = True
330332
assert v[var].is_optimization_enabled
331333
assert v[var].optimization_min_value == "5mm"
332-
v[var].optimization_min_value = "1m"
334+
v[var].optimization_min_value = "1mm"
335+
assert v[var].optimization_min_value == "1mm"
336+
333337
assert v[var].optimization_max_value == "15mm"
334338
v[var].optimization_max_value = "14mm"
335339
assert v[var].optimization_max_value == "14mm"
340+
336341
assert not v[var].is_tuning_enabled
337342
v[var].is_tuning_enabled = True
338343
assert v[var].is_tuning_enabled
344+
339345
assert v[var].tuning_min_value == "5mm"
340346
v[var].tuning_min_value = "4mm"
341347
assert v[var].tuning_max_value == "15mm"
@@ -359,10 +365,17 @@ def test_test_optimization_properties(self, app):
359365
v[var].sensitivity_initial_disp = "0.5mm"
360366
assert v[var].sensitivity_initial_disp == "0.5mm"
361367

362-
def test_test_optimization_global_properties(self, app):
368+
if app.design_type == "Circuit Design":
369+
# Circuit parameter is not available in optimetrics
370+
app.variable_manager.set_variable("v2", "20mm", circuit_parameter=True)
371+
v = app.variable_manager
372+
assert not v["v2"].is_optimization_enabled
373+
v["v2"].is_optimization_enabled = True
374+
375+
def test_optimization_global_properties(self, hfss_app):
363376
var = "$v1"
364-
app[var] = "10mm"
365-
v = app.variable_manager
377+
hfss_app[var] = "10mm"
378+
v = hfss_app.variable_manager
366379
assert not v[var].is_optimization_enabled
367380
v[var].is_optimization_enabled = True
368381
assert v[var].is_optimization_enabled
@@ -405,26 +418,19 @@ def test_variable_with_units(self, app):
405418
assert app.variable_manager["v2"].decompose() == (6.0, "mm")
406419
assert app.variable_manager.decompose("5mm") == (5.0, "mm")
407420

408-
def test_delete_unused_variables(self, app):
409-
app.insert_design("used_variables")
410-
app["used_var"] = "1mm"
411-
app["unused_var"] = "1mm"
412-
app["$project_used_var"] = "1"
413-
app.modeler.create_rectangle(0, ["used_var", "used_var", "used_var"], [10, 20])
414-
mat1 = app.materials.add_material("new_copper2")
421+
def test_delete_unused_variables(self, hfss_app):
422+
hfss_app.insert_design("used_variables")
423+
hfss_app["used_var"] = "1mm"
424+
hfss_app["unused_var"] = "1mm"
425+
hfss_app["$project_used_var"] = "1"
426+
hfss_app.modeler.create_rectangle(0, ["used_var", "used_var", "used_var"], [10, 20])
427+
mat1 = hfss_app.materials.add_material("new_copper2")
415428
mat1.permittivity = "$project_used_var"
416-
assert app.variable_manager.is_used("used_var")
417-
assert not app.variable_manager.is_used("unused_var")
418-
assert app.variable_manager.delete_variable("unused_var")
419-
app["unused_var"] = "1mm"
420-
number_of_variables = len(app.variable_manager.variable_names)
421-
assert app.variable_manager.delete_unused_variables()
422-
new_number_of_variables = len(app.variable_manager.variable_names)
429+
assert hfss_app.variable_manager.is_used("used_var")
430+
assert not hfss_app.variable_manager.is_used("unused_var")
431+
assert hfss_app.variable_manager.delete_variable("unused_var")
432+
hfss_app["unused_var"] = "1mm"
433+
number_of_variables = len(hfss_app.variable_manager.variable_names)
434+
assert hfss_app.variable_manager.delete_unused_variables()
435+
new_number_of_variables = len(hfss_app.variable_manager.variable_names)
423436
assert number_of_variables != new_number_of_variables
424-
425-
def test_value_with_units(self, app):
426-
assert app.value_with_units("10mm") == "10mm"
427-
assert app.value_with_units("10") == "10mm"
428-
assert app.value_with_units("10", units_system="Angle") == "10deg"
429-
assert app.value_with_units("10", units_system="invalid") == "10"
430-
assert app.value_with_units("A + Bmm") == "A + Bmm"

0 commit comments

Comments
 (0)