Skip to content

Commit aaac27c

Browse files
chore: cleanup miscellaneous grpc refactors (#2272)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 6e61f4f commit aaac27c

File tree

22 files changed

+482
-225
lines changed

22 files changed

+482
-225
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cleanup miscellaneous grpc refactors

src/ansys/geometry/core/_grpc/_services/_service.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from .base.named_selection import GRPCNamedSelectionService
4343
from .base.parts import GRPCPartsService
4444
from .base.patterns import GRPCPatternsService
45+
from .base.points import GRPCPointsService
4546
from .base.prepare_tools import GRPCPrepareToolsService
4647
from .base.repair_tools import GRPCRepairToolsService
4748
from .base.unsupported import GRPCUnsupportedService
@@ -104,6 +105,7 @@ def __init__(self, channel: grpc.Channel, version: GeometryApiProtos | str | Non
104105
self._named_selection = None
105106
self._parts = None
106107
self._patterns = None
108+
self._points = None
107109
self._prepare_tools = None
108110
self._repair_tools = None
109111
self._unsupported = None
@@ -602,6 +604,32 @@ def patterns(self) -> GRPCPatternsService:
602604

603605
return self._patterns
604606

607+
@property
608+
def points(self) -> GRPCPointsService:
609+
"""
610+
Get the points service for the specified version.
611+
612+
Returns
613+
-------
614+
GRPCPointsService
615+
The points service for the specified version.
616+
"""
617+
if not self._points:
618+
# Import the appropriate points service based on the version
619+
from .v0.points import GRPCPointsServiceV0
620+
from .v1.points import GRPCPointsServiceV1
621+
622+
if self.version == GeometryApiProtos.V0:
623+
self._points = GRPCPointsServiceV0(self.channel)
624+
elif self.version == GeometryApiProtos.V1: # pragma: no cover
625+
# V1 is not implemented yet
626+
self._points = GRPCPointsServiceV1(self.channel)
627+
else: # pragma: no cover
628+
# This should never happen as the version is set in the constructor
629+
raise ValueError(f"Unsupported version: {self.version}")
630+
631+
return self._points
632+
605633
@property
606634
def prepare_tools(self) -> GRPCPrepareToolsService:
607635
"""

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ def __init__(self, channel: grpc.Channel):
4242
def revolve_edges(self, **kwargs) -> dict:
4343
"""Revolve edges around an axis to create a surface of revolution."""
4444
pass
45+
46+
@abstractmethod
47+
def intersect_curves(self, **kwargs) -> dict:
48+
"""Get intersection points of curves."""
49+
pass

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,13 @@ def insert(self, **kwargs) -> dict:
8383
def get_active(self, **kwargs) -> dict:
8484
"""Get the active design on the service."""
8585
pass
86+
87+
@abstractmethod
88+
def upload_file(self, **kwargs) -> dict:
89+
"""Upload a file to the server."""
90+
pass
91+
92+
@abstractmethod
93+
def upload_file_stream(self, **kwargs) -> dict:
94+
"""Upload a file to the server using streaming."""
95+
pass

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,8 @@ def get_round_info(self, **kwargs) -> dict:
153153
def offset_faces(self, **kwargs) -> dict:
154154
"""Offset a selection of faces."""
155155
pass
156+
157+
@abstractmethod
158+
def setup_offset_relationship(self, **kwargs) -> dict:
159+
"""Set up an offset relationship between two faces."""
160+
pass
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
"""Module containing the points service implementation (abstraction layer)."""
23+
24+
from abc import ABC, abstractmethod
25+
26+
import grpc
27+
28+
29+
class GRPCPointsService(ABC): # pragma: no cover
30+
"""Points service for gRPC communication with the Geometry server.
31+
32+
Parameters
33+
----------
34+
channel : grpc.Channel
35+
The gRPC channel to the server.
36+
"""
37+
38+
def __init__(self, channel: grpc.Channel):
39+
"""Initialize the GRPCPointsService class."""
40+
pass
41+
42+
@abstractmethod
43+
def create_design_points(self, **kwargs) -> dict:
44+
"""Create design points."""
45+
pass

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828
from ..base.conversions import from_measurement_to_server_angle
2929
from ..base.curves import GRPCCurvesService
30-
from .conversions import from_line_to_grpc_line, from_trimmed_curve_to_grpc_trimmed_curve
30+
from .conversions import (
31+
from_grpc_point_to_point3d,
32+
from_line_to_grpc_line,
33+
from_trimmed_curve_to_grpc_trimmed_curve,
34+
)
3135

3236

3337
class GRPCCurvesServiceV0(GRPCCurvesService): # pragma: no cover
@@ -66,3 +70,22 @@ def revolve_edges(self, **kwargs) -> dict: # noqa: D102
6670

6771
# Return the result - formatted as a dictionary
6872
return {}
73+
74+
@protect_grpc
75+
def intersect_curves(self, **kwargs) -> dict: # noqa: D102
76+
from ansys.api.geometry.v0.commands_pb2 import IntersectCurvesRequest
77+
78+
# Create the request - assumes all inputs are valid and of the proper type
79+
request = IntersectCurvesRequest(
80+
first=from_trimmed_curve_to_grpc_trimmed_curve(kwargs["first"]),
81+
second=from_trimmed_curve_to_grpc_trimmed_curve(kwargs["second"]),
82+
)
83+
84+
# Call the gRPC service
85+
response = self.stub.IntersectCurves(request)
86+
87+
# Return the result - formatted as a dictionary
88+
return {
89+
"intersect": response.intersect,
90+
"points": [from_grpc_point_to_point3d(point) for point in response.points],
91+
}

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

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ class GRPCDesignsServiceV0(GRPCDesignsService): # pragma: no cover
4949
@protect_grpc
5050
def __init__(self, channel: grpc.Channel): # noqa: D102
5151
from ansys.api.dbu.v0.designs_pb2_grpc import DesignsStub
52+
from ansys.api.geometry.v0.commands_pb2_grpc import CommandsStub
5253

53-
self.stub = DesignsStub(channel)
54+
self.designs_stub = DesignsStub(channel)
55+
self.commands_stub = CommandsStub(channel)
5456

5557
@protect_grpc
5658
def open(self, **kwargs) -> dict: # noqa: D102
@@ -63,7 +65,7 @@ def open(self, **kwargs) -> dict: # noqa: D102
6365
)
6466

6567
# Call the gRPC service
66-
_ = self.stub.Open(request)
68+
_ = self.designs_stub.Open(request)
6769

6870
# Return the response - formatted as a dictionary
6971
return {}
@@ -76,7 +78,7 @@ def new(self, **kwargs) -> dict: # noqa: D102
7678
request = NewRequest(name=kwargs["name"])
7779

7880
# Call the gRPC service
79-
response = self.stub.New(request)
81+
response = self.designs_stub.New(request)
8082

8183
# Return the response - formatted as a dictionary
8284
return {
@@ -90,7 +92,7 @@ def close(self, **kwargs) -> dict: # noqa: D102
9092
request = build_grpc_id(id=kwargs["design_id"])
9193

9294
# Call the gRPC service
93-
_ = self.stub.Close(request)
95+
_ = self.designs_stub.Close(request)
9496

9597
# Return the response - formatted as a dictionary
9698
return {}
@@ -101,7 +103,7 @@ def put_active(self, **kwargs) -> dict: # noqa: D102
101103
request = build_grpc_id(id=kwargs["design_id"])
102104

103105
# Call the gRPC service
104-
_ = self.stub.PutActive(request)
106+
_ = self.designs_stub.PutActive(request)
105107

106108
# Return the response - formatted as a dictionary
107109
return {}
@@ -118,7 +120,7 @@ def save_as(self, **kwargs) -> dict: # noqa: D102
118120
)
119121

120122
# Call the gRPC service
121-
_ = self.stub.SaveAs(request)
123+
_ = self.designs_stub.SaveAs(request)
122124

123125
# Return the response - formatted as a dictionary
124126
return {}
@@ -136,7 +138,7 @@ def download_export(self, **kwargs) -> dict: # noqa: D102
136138
)
137139

138140
# Call the gRPC service
139-
response = self.stub.DownloadExportFile(request)
141+
response = self.designs_stub.DownloadExportFile(request)
140142

141143
# Return the response - formatted as a dictionary
142144
data = bytes()
@@ -156,7 +158,7 @@ def stream_download_export(self, **kwargs) -> dict: # noqa: D102
156158
)
157159

158160
# Call the gRPC service
159-
response = self.stub.StreamDownloadExportFile(request)
161+
response = self.designs_stub.StreamDownloadExportFile(request)
160162

161163
# Return the response - formatted as a dictionary
162164
data = bytes()
@@ -175,7 +177,7 @@ def insert(self, **kwargs) -> dict: # noqa: D102
175177
)
176178

177179
# Call the gRPC service
178-
_ = self.stub.Insert(request)
180+
_ = self.designs_stub.Insert(request)
179181

180182
# Return the response - formatted as a dictionary
181183
return {}
@@ -185,7 +187,7 @@ def get_active(self, **kwargs) -> dict: # noqa: D102
185187
from google.protobuf.empty_pb2 import Empty
186188

187189
# Call the gRPC service
188-
response = self.stub.GetActive(request=Empty())
190+
response = self.designs_stub.GetActive(request=Empty())
189191

190192
# Return the response - formatted as a dictionary
191193
if response:
@@ -194,3 +196,63 @@ def get_active(self, **kwargs) -> dict: # noqa: D102
194196
"main_part_id": response.main_part.id,
195197
"name": response.name,
196198
}
199+
200+
@protect_grpc
201+
def upload_file(self, **kwargs) -> dict: # noqa: D102
202+
from ansys.api.geometry.v0.commands_pb2 import UploadFileRequest
203+
204+
# Create the request - assumes all inputs are valid and of the proper type
205+
request = UploadFileRequest(
206+
data=kwargs["data"],
207+
file_name=kwargs["file_name"],
208+
open=kwargs["open_file"],
209+
import_options=kwargs["import_options"].to_dict(),
210+
)
211+
212+
# Call the gRPC service
213+
response = self.commands_stub.UploadFile(request)
214+
215+
# Return the response - formatted as a dictionary
216+
return {"file_path": response.file_path}
217+
218+
@protect_grpc
219+
def upload_file_stream(self, **kwargs) -> dict: # noqa: D102
220+
from pathlib import Path
221+
from typing import TYPE_CHECKING, Generator
222+
223+
from ansys.api.geometry.v0.commands_pb2 import UploadFileRequest
224+
225+
import ansys.geometry.core.connection.defaults as pygeom_defaults
226+
227+
if TYPE_CHECKING: # pragma: no cover
228+
from ansys.geometry.core.misc.options import ImportOptions
229+
230+
def request_generator(
231+
file_path: Path, open_file: bool, import_options: "ImportOptions"
232+
) -> Generator[UploadFileRequest, None, None]:
233+
"""Generate requests for streaming file upload."""
234+
msg_buffer = 5 * 1024 # 5KB - for additional message data
235+
if pygeom_defaults.MAX_MESSAGE_LENGTH - msg_buffer < 0: # pragma: no cover
236+
raise ValueError("MAX_MESSAGE_LENGTH is too small for file upload.")
237+
238+
chunk_size = pygeom_defaults.MAX_MESSAGE_LENGTH - msg_buffer
239+
with Path.open(file_path, "rb") as file:
240+
while chunk := file.read(chunk_size):
241+
yield UploadFileRequest(
242+
data=chunk,
243+
file_name=file_path.name,
244+
open=open_file,
245+
import_options=import_options.to_dict(),
246+
)
247+
248+
# Call the gRPC service
249+
response = self.commands_stub.StreamFileUpload(
250+
request_generator(
251+
file_path=kwargs["file_path"],
252+
open_file=kwargs["open_file"],
253+
import_options=kwargs["import_options"],
254+
)
255+
)
256+
257+
# Return the response - formatted as a dictionary
258+
return {"file_path": response.file_path}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,23 @@ def offset_faces(self, **kwargs) -> dict: # noqa: D102
531531
for response_data in response.response_data
532532
]
533533
}
534+
535+
@protect_grpc
536+
def setup_offset_relationship(self, **kwargs) -> dict: # noqa: D102
537+
from ansys.api.geometry.v0.commands_pb2 import FaceOffsetRequest
538+
539+
# Create the request - assumes all inputs are valid and of the proper type
540+
request = FaceOffsetRequest(
541+
face1=build_grpc_id(kwargs["face1_id"]),
542+
face2=build_grpc_id(kwargs["face2_id"]),
543+
set_baselines=kwargs["set_baselines"],
544+
process_adjacent_faces=kwargs["process_adjacent_faces"],
545+
)
546+
547+
# Call the gRPC service
548+
response = self.commands_stub.FaceOffset(request=request)
549+
550+
# Return the response - formatted as a dictionary
551+
return {
552+
"success": response.success,
553+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def export(self, **kwargs) -> dict: # noqa: D102
6060
# Call the gRPC service
6161
response = self.stub.Export(request)
6262

63-
# Return the response
63+
# Return the response - formatted as a dictionary
6464
data = bytes()
6565
data += response.data
6666
return {

0 commit comments

Comments
 (0)