Skip to content

Commit b9faa09

Browse files
chore: v1 implementation of components stub (#2401)
1 parent 295caaa commit b9faa09

File tree

3 files changed

+149
-10
lines changed

3 files changed

+149
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
V1 implementation of components stub

src/ansys/geometry/core/_grpc/_services/v1/components.py

Lines changed: 146 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
from ansys.geometry.core.errors import protect_grpc
2727

2828
from ..base.components import GRPCComponentsService
29+
from ..base.conversions import from_measurement_to_server_angle
30+
from .conversions import (
31+
build_grpc_id,
32+
from_grpc_matrix_to_matrix,
33+
from_point3d_to_grpc_point,
34+
from_unit_vector_to_grpc_direction,
35+
)
2936

3037

3138
class GRPCComponentsServiceV1(GRPCComponentsService):
@@ -43,34 +50,164 @@ class GRPCComponentsServiceV1(GRPCComponentsService):
4350

4451
@protect_grpc
4552
def __init__(self, channel: grpc.Channel): # noqa: D102
46-
from ansys.api.geometry.v1.components_pb2_grpc import ComponentsStub
53+
from ansys.api.discovery.v1.design.geometry.component_pb2_grpc import ComponentStub
4754

48-
self.stub = ComponentsStub(channel)
55+
self.stub = ComponentStub(channel)
4956

5057
@protect_grpc
5158
def create(self, **kwargs) -> dict: # noqa: D102
52-
raise NotImplementedError
59+
from ansys.api.discovery.v1.design.geometry.component_pb2 import (
60+
CreateComponentData,
61+
CreateRequest,
62+
)
63+
64+
# Create the request - assumes all inputs are valid and of the proper type
65+
request = CreateRequest(
66+
components=[
67+
CreateComponentData(
68+
name=kwargs["name"],
69+
parent_id=build_grpc_id(kwargs["parent_id"]),
70+
template_id=build_grpc_id(kwargs["template_id"]),
71+
instance_name=kwargs["instance_name"],
72+
)
73+
]
74+
)
75+
76+
# Call the gRPC service
77+
response = self.stub.Create(request)
78+
79+
# Return the response - formatted as a dictionary
80+
# Note: response.components is a repeated field, we return the first one
81+
component = response.components[0]
82+
return {
83+
"id": component.id,
84+
"name": component.name,
85+
"instance_name": component.instance_name,
86+
"template": kwargs["template_id"], # template_id from input
87+
"component": component,
88+
}
5389

5490
@protect_grpc
5591
def set_name(self, **kwargs) -> dict: # noqa: D102
56-
raise NotImplementedError
92+
from ansys.api.discovery.v1.design.designmessages_pb2 import SetDesignEntityNameRequest
93+
94+
# Create the request - assumes all inputs are valid and of the proper type
95+
request = SetDesignEntityNameRequest(id=build_grpc_id(kwargs["id"]), name=kwargs["name"])
96+
97+
# Call the gRPC service
98+
_ = self.stub.SetName(request)
99+
100+
# Return the response - formatted as a dictionary
101+
return {}
57102

58103
@protect_grpc
59104
def set_placement(self, **kwargs) -> dict: # noqa: D102
60-
raise NotImplementedError
105+
from ansys.api.discovery.v1.design.geometry.component_pb2 import (
106+
PlacementData,
107+
SetPlacementRequest,
108+
)
109+
110+
# Create the direction and point objects
111+
translation = (
112+
from_unit_vector_to_grpc_direction(kwargs["translation"].normalize())
113+
if kwargs["translation"] is not None
114+
else None
115+
)
116+
origin = (
117+
from_point3d_to_grpc_point(kwargs["rotation_axis_origin"])
118+
if kwargs["rotation_axis_origin"] is not None
119+
else None
120+
)
121+
direction = (
122+
from_unit_vector_to_grpc_direction(kwargs["rotation_axis_direction"])
123+
if kwargs["rotation_axis_direction"] is not None
124+
else None
125+
)
126+
127+
# Create the request with repeated ids and placements
128+
request = SetPlacementRequest(
129+
ids=[build_grpc_id(kwargs["id"])],
130+
placements=[
131+
PlacementData(
132+
translation=translation,
133+
rotation_axis_origin=origin,
134+
rotation_axis_direction=direction,
135+
rotation_angle=from_measurement_to_server_angle(kwargs["rotation_angle"]),
136+
)
137+
],
138+
)
139+
140+
# Call the gRPC service
141+
response = self.stub.SetPlacement(request)
142+
143+
# Return the response - formatted as a dictionary
144+
# Note: response.matrices is a map<string, Matrix>
145+
# Get the matrix for our component ID
146+
matrix_value = response.matrices.get(kwargs["id"].id)
147+
return {"matrix": from_grpc_matrix_to_matrix(matrix_value) if matrix_value else None}
61148

62149
@protect_grpc
63150
def set_shared_topology(self, **kwargs) -> dict: # noqa: D102
64-
raise NotImplementedError
151+
from ansys.api.discovery.v1.design.geometry.component_pb2 import (
152+
SetSharedTopologyRequest,
153+
SharedTopologyData,
154+
)
155+
156+
# Create the request - assumes all inputs are valid and of the proper type
157+
request = SetSharedTopologyRequest(
158+
shared_topologies=[
159+
SharedTopologyData(
160+
id=build_grpc_id(kwargs["id"]),
161+
share_type=kwargs["share_type"].value,
162+
)
163+
]
164+
)
165+
166+
# Call the gRPC service
167+
_ = self.stub.SetSharedTopology(request)
168+
169+
# Return the response - formatted as a dictionary
170+
return {}
65171

66172
@protect_grpc
67173
def delete(self, **kwargs) -> dict: # noqa: D102
68-
raise NotImplementedError
174+
from ansys.api.discovery.v1.commonmessages_pb2 import MultipleEntitiesRequest
175+
176+
# Create the request - assumes all inputs are valid and of the proper type
177+
request = MultipleEntitiesRequest(ids=[build_grpc_id(kwargs["id"])])
178+
179+
# Call the gRPC service
180+
_ = self.stub.Delete(request)
181+
182+
# Return the response - formatted as a dictionary
183+
return {}
69184

70185
@protect_grpc
71186
def import_groups(self, **kwargs) -> dict: # noqa: D102
72-
raise NotImplementedError
187+
from ansys.api.discovery.v1.design.geometry.component_pb2 import ImportGroupsRequest
188+
189+
# Create the request - assumes all inputs are valid and of the proper type
190+
request = ImportGroupsRequest(
191+
id=build_grpc_id(kwargs["id"]),
192+
)
193+
194+
# Call the gRPC service
195+
_ = self.stub.ImportGroups(request)
196+
197+
# Return the response - formatted as a dictionary
198+
return {}
73199

74200
@protect_grpc
75201
def make_independent(self, **kwargs) -> dict: # noqa: D102
76-
raise NotImplementedError
202+
from ansys.api.discovery.v1.design.geometry.component_pb2 import MakeIndependentRequest
203+
204+
# Create the request - assumes all inputs are valid and of the proper type
205+
request = MakeIndependentRequest(
206+
ids=[build_grpc_id(id) for id in kwargs["ids"]],
207+
)
208+
209+
# Call the gRPC service
210+
_ = self.stub.MakeIndependent(request)
211+
212+
# Return the response - formatted as a dictionary
213+
return {}

src/ansys/geometry/core/_grpc/_services/v1/coordinate_systems.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import grpc
2525

26+
from ansys.geometry.core._grpc._services.v1.conversions import build_grpc_id
2627
from ansys.geometry.core.errors import protect_grpc
2728

2829
from ..base.coordinate_systems import GRPCCoordinateSystemService
@@ -61,7 +62,7 @@ def create(self, **kwargs) -> dict: # noqa: D102
6162
request = CreateRequest(
6263
request_data=[
6364
CreateRequestData(
64-
parent_id=kwargs["parent_id"],
65+
parent_id=build_grpc_id(kwargs["parent_id"]),
6566
name=kwargs["name"],
6667
frame=from_frame_to_grpc_frame(kwargs["frame"]),
6768
)

0 commit comments

Comments
 (0)