Skip to content

Commit 430e4a1

Browse files
committed
feat: allow for "undefined" material types and units (#949)
1 parent df811f4 commit 430e4a1

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

src/ansys/geometry/core/designer/design.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from beartype.typing import Dict, List, Optional, Union
4545
from google.protobuf.empty_pb2 import Empty
4646
import numpy as np
47-
from pint import Quantity
47+
from pint import Quantity, UndefinedUnitError
4848

4949
from ansys.geometry.core.connection.conversions import (
5050
grpc_frame_to_frame,
@@ -687,11 +687,34 @@ def __read_existing_design(self) -> None:
687687
properties = []
688688
density = Quantity(0)
689689
for property in material.material_properties:
690-
mp = MaterialProperty(
691-
MaterialPropertyType.from_id(property.id),
692-
property.display_name,
693-
Quantity(property.value, property.units),
694-
)
690+
# TODO: Add support for more material properties...
691+
# - Need to add support for more MaterialPropertyTypes
692+
# - Need to add support for more Quantity units
693+
try:
694+
mp_type = MaterialPropertyType.from_id(property.id)
695+
except ValueError as err: # TODO: Errors coming from MaterialPropertyType.from_id
696+
# because of unsupported MaterialPropertyType entries...
697+
self._grpc_client.log.warning(
698+
f"Material property {property.display_name} of type {property.id} is not supported." # noqa : E501
699+
" Storing as string."
700+
)
701+
self._grpc_client.log.warning(f"Error: {err}")
702+
mp_type = property.id
703+
704+
try:
705+
mp_quantity = Quantity(property.value, property.units)
706+
except (
707+
UndefinedUnitError,
708+
TypeError,
709+
) as err: # TODO: Errors coming from Quantity ctor because of unsupported units...
710+
self._grpc_client.log.warning(
711+
f"Material property {property.display_name} with units {property.units} is not fully supported." # noqa : E501
712+
" Storing value only as float."
713+
)
714+
self._grpc_client.log.warning(f"Error: {err}")
715+
mp_quantity = property.value
716+
717+
mp = MaterialProperty(mp_type, property.display_name, mp_quantity)
695718
properties.append(mp)
696719
if mp.type == MaterialPropertyType.DENSITY:
697720
density = mp.quantity

src/ansys/geometry/core/materials/property.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
from enum import Enum, unique
2525

2626
from beartype import beartype as check_input_types
27+
from beartype.typing import Union
2728
from pint import Quantity
2829

30+
from ansys.geometry.core.typing import Real
31+
2932

3033
@unique
3134
class MaterialPropertyType(Enum):
@@ -71,29 +74,36 @@ class MaterialProperty:
7174
7275
Parameters
7376
----------
74-
type : MaterialPropertyType
75-
Type of the material property.
77+
type : Union[MaterialPropertyType, str]
78+
Type of the material property. If the type is a string, it must be a valid
79+
material property type - though it might not be supported by the MaterialPropertyType
80+
enum.
7681
name: str
7782
Material property name.
78-
quantity: ~pint.Quantity
79-
Value and unit.
83+
quantity: Union[~pint.Quantity, Real]
84+
Value and unit in case of a supported Quantity. If the type is not supported, it
85+
must be a Real value (float or integer).
8086
"""
8187

8288
@check_input_types
8389
def __init__(
8490
self,
85-
type: MaterialPropertyType,
91+
type: Union[MaterialPropertyType, str],
8692
name: str,
87-
quantity: Quantity,
93+
quantity: Union[Quantity, Real],
8894
):
8995
"""Initialize ``MaterialProperty`` class."""
9096
self._type = type
9197
self._name = name
9298
self._quantity = quantity
9399

94100
@property
95-
def type(self) -> MaterialPropertyType:
96-
"""Material property ID."""
101+
def type(self) -> Union[MaterialPropertyType, str]:
102+
"""
103+
Material property ID.
104+
105+
If the type is not supported, it will be a string.
106+
"""
97107
return self._type
98108

99109
@property
@@ -102,6 +112,10 @@ def name(self) -> str:
102112
return self._name
103113

104114
@property
105-
def quantity(self) -> Quantity:
106-
"""Material property quantity and unit."""
115+
def quantity(self) -> Union[Quantity, Real]:
116+
"""
117+
Material property quantity and unit.
118+
119+
If the type is not supported, it will be a Real value (float or integer).
120+
"""
107121
return self._quantity

0 commit comments

Comments
 (0)