Skip to content

Commit 225652e

Browse files
amichel0205pre-commit-ci[bot]MaxJPReyPipKatmaxcapodi78
authored
Add parameter transparency to material appearance - issue 4252 (#4278)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxime Rey <[email protected]> Co-authored-by: Kathy Pippert <[email protected]> Co-authored-by: Massimo Capodiferro <[email protected]> Co-authored-by: maxcapodi78 <Shark78>
1 parent cdf0a38 commit 225652e

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

_unittest/test_03_Materials.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,18 @@ def test_02_create_material(self):
9292
assert mat1.get_curve_coreloss_type() == "Power Ferrite"
9393
assert isinstance(mat1.material_appearance, list)
9494

95-
mat1.material_appearance = [11, 22, 0]
96-
assert mat1.material_appearance == [11, 22, 0]
97-
mat1.material_appearance = ["11", "22", "10"]
98-
assert mat1.material_appearance == [11, 22, 10]
95+
mat1.material_appearance = [11, 22, 0, 0.5]
96+
assert mat1.material_appearance == [11, 22, 0, 0.5]
97+
mat1.material_appearance = ["11", "22", "10", "0.5"]
98+
assert mat1.material_appearance == [11, 22, 10, 0.5]
9999
with pytest.raises(ValueError):
100-
mat1.material_appearance = [11, 22, 300]
100+
mat1.material_appearance = [11, 22, 300, 0.5]
101101
with pytest.raises(ValueError):
102-
mat1.material_appearance = [11, -22, 0]
102+
mat1.material_appearance = [11, 22, 100, 1.5]
103+
with pytest.raises(ValueError):
104+
mat1.material_appearance = [11, -22, 0, 0.5]
105+
with pytest.raises(ValueError):
106+
mat1.material_appearance = [11, 22, 0, -1]
103107
with pytest.raises(ValueError):
104108
mat1.material_appearance = [11, 22]
105109

examples/02-HFSS/HFSS_Choke.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import os
1313
import pyaedt
1414

15-
project_name = pyaedt.generate_unique_project_name(project_name="choke")
15+
project_name = pyaedt.generate_unique_project_name(rootname=r"C:\Data\Support\Test", folder_name="choke", project_name="choke")
1616

1717
##########################################################
1818
# Set AEDT version

pyaedt/modules/Material.py

+38-20
Original file line numberDiff line numberDiff line change
@@ -1301,13 +1301,15 @@ def __init__(self, materiallib, name, props=None, material_update=True):
13011301
self._material_appearance.append(self._props["AttachedData"]["MatAppearanceData"]["Red"])
13021302
self._material_appearance.append(self._props["AttachedData"]["MatAppearanceData"]["Green"])
13031303
self._material_appearance.append(self._props["AttachedData"]["MatAppearanceData"]["Blue"])
1304+
self._material_appearance.append(self._props["AttachedData"]["MatAppearanceData"].get("Transparency", 0.0))
13041305
else:
13051306
vals = list(CSS4_COLORS.values())
13061307
if (materiallib._color_id) >= len(vals):
13071308
materiallib._color_id = 0
13081309
h = vals[materiallib._color_id].lstrip("#")
13091310
self._material_appearance = list(int(h[i : i + 2], 16) for i in (0, 2, 4))
13101311
materiallib._color_id += 1
1312+
self._material_appearance.append(0)
13111313
self._props["AttachedData"] = OrderedDict(
13121314
{
13131315
"MatAppearanceData": OrderedDict(
@@ -1316,6 +1318,7 @@ def __init__(self, materiallib, name, props=None, material_update=True):
13161318
"Red": self._material_appearance[0],
13171319
"Green": self._material_appearance[1],
13181320
"Blue": self._material_appearance[2],
1321+
"Transparency": self._material_appearance[3],
13191322
}
13201323
)
13211324
}
@@ -1366,46 +1369,61 @@ def _update_material(self):
13661369

13671370
@property
13681371
def material_appearance(self):
1369-
"""Material Appearance specified as an RGB list.
1372+
"""Material appearance specified as a list where the first three items are
1373+
RGB color and the fourth one is transparency.
13701374
13711375
Returns
13721376
-------
13731377
list
1374-
Color of the material in RGB. Values are in the range ``[0, 255]``.
1378+
Color of the material in RGB and transparency.
1379+
Color values are in the range ``[0, 255]``.
1380+
Transparency is a float in the range ``[0,1]``.
13751381
13761382
Examples
13771383
--------
1378-
Create a new material with color ``[0, 153, 153]`` (darker cyan).
1384+
Create a material with color ``[0, 153, 153]`` (darker cyan) and transparency ``0.5``.
13791385
13801386
>>> from pyaedt import Hfss
13811387
>>> hfss = Hfss(specified_version="2021.2")
13821388
>>> mat1 = hfss.materials.add_material("new_material")
1383-
>>> rgbcolor = mat1.material_appearance
1384-
>>> mat1.material_appearance = [0, 153, 153]
1389+
>>> appearance_props = mat1.material_appearance
1390+
>>> mat1.material_appearance = [0, 153, 153, 0.5]
13851391
"""
13861392
return self._material_appearance
13871393

13881394
@material_appearance.setter
1389-
def material_appearance(self, rgb):
1390-
if not isinstance(rgb, (list, tuple)):
1391-
raise TypeError("`material_apperance` must be a list or tuple")
1392-
if len(rgb) != 3:
1393-
raise ValueError("`material_appearance` must be three items (RGB)")
1394-
value_int = []
1395-
for rgb_item in rgb:
1396-
rgb_int = int(rgb_item)
1397-
if rgb_int < 0 or rgb_int > 255:
1398-
raise ValueError("RGB value must be between 0 and 255")
1399-
value_int.append(rgb_int)
1400-
self._material_appearance = value_int
1395+
def material_appearance(self, appearance_props):
1396+
if not isinstance(appearance_props, (list, tuple)):
1397+
raise TypeError("`material_appearance` must be a list or tuple.")
1398+
if len(appearance_props) != 3 and len(appearance_props) != 4:
1399+
raise ValueError("`material_appearance` must be four items (R, G, B, transparency).")
1400+
elif len(appearance_props) == 3:
1401+
transparency_value = self.material_appearance[3]
1402+
msg = "Only RGB specified. Transparency is set to " + str(transparency_value)
1403+
self.logger.info(msg)
1404+
appearance_props.append(transparency_value)
1405+
value = []
1406+
for i in range(len(appearance_props)):
1407+
if i < 3:
1408+
rgb_int = int(appearance_props[i])
1409+
if rgb_int < 0 or rgb_int > 255:
1410+
raise ValueError("RGB value must be between 0 and 255.")
1411+
value.append(rgb_int)
1412+
else:
1413+
transparency = float(appearance_props[i])
1414+
if transparency < 0 or transparency > 1:
1415+
raise ValueError("Transparency value must be between 0 and 1.")
1416+
value.append(transparency)
1417+
self._material_appearance = value
14011418
self._props["AttachedData"] = OrderedDict(
14021419
{
14031420
"MatAppearanceData": OrderedDict(
14041421
{
14051422
"property_data": "appearance_data",
1406-
"Red": value_int[0],
1407-
"Green": value_int[1],
1408-
"Blue": value_int[2],
1423+
"Red": value[0],
1424+
"Green": value[1],
1425+
"Blue": value[2],
1426+
"Transparency": value[3],
14091427
}
14101428
)
14111429
}

0 commit comments

Comments
 (0)