Skip to content

Commit 63d9d48

Browse files
committed
feat: standarizing response usage, handling conversion and simplifying access
1 parent 7ac40e2 commit 63d9d48

File tree

6 files changed

+44
-47
lines changed

6 files changed

+44
-47
lines changed

src/ansys/geometry/core/_grpc/_services/base/conversions.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# SOFTWARE.
2222
"""Module containing server-version agnostic conversions."""
2323

24-
from ansys.geometry.core.misc.measurements import DEFAULT_UNITS, Measurement
24+
from ansys.geometry.core.misc.measurements import DEFAULT_UNITS, Distance, Measurement
2525

2626

2727
def from_measurement_to_server_length(input: Measurement) -> float:
@@ -54,3 +54,24 @@ def from_measurement_to_server_angle(input: Measurement) -> float:
5454
Angle value in server-defined units. By default, radians.
5555
"""
5656
return input.value.m_as(DEFAULT_UNITS.SERVER_ANGLE)
57+
58+
59+
def to_distance(value: float | int) -> Distance:
60+
"""Convert a server value to a Distance object.
61+
62+
Notes
63+
-----
64+
The value is converted to a Distance object using the default server length unit.
65+
The value should represent a length in the server's unit system.
66+
67+
Parameters
68+
----------
69+
value : float | int
70+
Value to convert.
71+
72+
Returns
73+
-------
74+
Distance
75+
Converted distance.
76+
"""
77+
return Distance(value, DEFAULT_UNITS.SERVER_LENGTH)

src/ansys/geometry/core/_grpc/_services/v0/measurement_tools.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,21 @@ def __init__(self, channel: grpc.Channel): # noqa: D102
5151
def min_distance_between_objects(self, **kwargs) -> dict: # noqa: D102
5252
from ansys.api.geometry.v0.measuretools_pb2 import MinDistanceBetweenObjectsRequest
5353

54-
bodies = kwargs["bodies"]
55-
selection = kwargs["selection"]
56-
backend_version = kwargs["backend_version"]
54+
from ..base.conversions import to_distance
5755

5856
# Create the request - assumes all inputs are valid and of the proper type
5957
# Request is different based on backend_version (25.2 vs. earlier)
60-
if backend_version < (25, 2, 0):
61-
request = MinDistanceBetweenObjectsRequest(bodies=bodies)
58+
if kwargs["backend_version"] < (25, 2, 0):
59+
request = MinDistanceBetweenObjectsRequest(bodies=kwargs["selection"])
6260
else:
6361
from ansys.api.dbu.v0.dbumodels_pb2 import EntityIdentifier
6462

6563
request = MinDistanceBetweenObjectsRequest(
66-
selection=[EntityIdentifier(id=selection[0]), EntityIdentifier(id=selection[1])]
64+
selection=[EntityIdentifier(id=item) for item in kwargs["selection"]]
6765
)
6866

6967
# Call the gRPC service
7068
response = self.stub.MinDistanceBetweenSelectionObjects(request)
7169

7270
# Return the response - formatted as a dictionary
73-
return {
74-
"distance": response.gap.distance,
75-
}
71+
return {"distance": to_distance(response.gap.distance)}

src/ansys/geometry/core/connection/client.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ def __init__(
189189
response = self._services.admin.get_backend()
190190

191191
# Store the backend type and version
192-
self._backend_type = response["backend"]
193-
self._backend_version = response["version"]
192+
self._backend_type = response.get("backend")
193+
self._backend_version = response.get("version")
194194

195195
# Register the close method to be called at exit - irrespectively of
196196
# the user calling it or not...
@@ -357,8 +357,7 @@ def _get_service_logs(
357357
the current logs are retrieved). The ``dump_to_file`` parameter
358358
must be set to ``True``.
359359
"""
360-
response = self._services.admin.get_logs(all_logs=all_logs)
361-
logs: dict[str, str] = response["logs"]
360+
logs: dict[str, str] = self._services.admin.get_logs(all_logs=all_logs).get("logs")
362361

363362
# Let's handle the various scenarios...
364363
if not dump_to_file:

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def __init__(
129129
response = self._grpc_client.services.named_selection.create_named_selection(
130130
name=name, members=ids
131131
)
132-
self._id = response["id"]
132+
self._id = response.get("id")
133133

134134
@property
135135
def id(self) -> str:
@@ -204,15 +204,15 @@ def __verify_ns(self) -> None:
204204
return
205205

206206
# Get all entities from the named selection
207-
resp = self._grpc_client.services.named_selection.get_named_selection(id=self._id)
207+
response = self._grpc_client.services.named_selection.get_named_selection(id=self._id)
208208

209209
# Check if the named selection has changed
210210
ids = {
211-
"bodies": resp["bodies"],
212-
"faces": resp["faces"],
213-
"edges": resp["edges"],
214-
"beams": resp["beams"],
215-
"design_points": resp["design_points"],
211+
"bodies": response.get("bodies"),
212+
"faces": response.get("faces"),
213+
"edges": response.get("edges"),
214+
"beams": response.get("beams"),
215+
"design_points": response.get("design_points"),
216216
}
217217

218218
for key in ids:

src/ansys/geometry/core/modeler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,15 @@ def run_discovery_script_file(
566566
api_version=api_version.value if api_version is not None else None,
567567
)
568568

569-
if not response["success"]:
570-
raise GeometryRuntimeError(response["message"])
569+
if not response.get("success"):
570+
raise GeometryRuntimeError(response.get("message"))
571571

572-
self.client.log.debug(f"Script result message: {response['message']}")
572+
self.client.log.debug(f"Script result message: {response.get('message')}")
573573

574574
if import_design:
575-
return response["values"], self.read_existing_design()
575+
return response.get("values"), self.read_existing_design()
576576
else:
577-
return response["values"], None
577+
return response.get("values"), None
578578

579579
@property
580580
def repair_tools(self) -> RepairTools:

src/ansys/geometry/core/tools/measurement_tools.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

2626
from ansys.geometry.core.connection import GrpcClient
2727
from ansys.geometry.core.misc.checks import min_backend_version
28-
from ansys.geometry.core.misc.measurements import DEFAULT_UNITS, Distance
29-
from ansys.geometry.core.typing import Real
28+
from ansys.geometry.core.misc.measurements import Distance
3029

3130
if TYPE_CHECKING: # pragma: no cover
3231
from ansys.geometry.core.designer.body import Body
@@ -52,23 +51,6 @@ def distance(self) -> Distance:
5251
"""Returns the closest distance between two bodies."""
5352
return self._distance
5453

55-
@classmethod
56-
def _from_distance_response(cls, distance: Real) -> "Gap":
57-
"""Construct ``Gap`` object from distance response.
58-
59-
Parameters
60-
----------
61-
response : MinDistanceBetweenObjectsResponse
62-
Response from the gRPC server.
63-
64-
Notes
65-
-----
66-
This method is used internally to construct a ``Gap`` object from a
67-
gRPC response.
68-
"""
69-
distance = Distance(distance, unit=DEFAULT_UNITS.LENGTH)
70-
return cls(distance)
71-
7254

7355
class MeasurementTools:
7456
"""Measurement tools for PyAnsys Geometry.
@@ -102,8 +84,7 @@ def min_distance_between_objects(
10284
Gap between two bodies.
10385
"""
10486
response = self._grpc_client.services.measurement_tools.min_distance_between_objects(
105-
bodies=[object1.id, object2.id],
10687
selection=[object1.id, object2.id],
10788
backend_version=self._grpc_client.backend_version,
10889
)
109-
return Gap._from_distance_response(response["distance"])
90+
return Gap(response.get("distance"))

0 commit comments

Comments
 (0)