|
| 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 components service implementation for v0.""" |
| 23 | + |
| 24 | +import grpc |
| 25 | + |
| 26 | +from ansys.geometry.core.errors import protect_grpc |
| 27 | + |
| 28 | +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 | +) |
| 36 | + |
| 37 | + |
| 38 | +class GRPCComponentsServiceV0(GRPCComponentsService): |
| 39 | + """Components service for gRPC communication with the Geometry server. |
| 40 | +
|
| 41 | + This class provides methods to call components in the |
| 42 | + Geometry server using gRPC. It is specifically designed for the v0 |
| 43 | + version of the Geometry API. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + channel : grpc.Channel |
| 48 | + The gRPC channel to the server. |
| 49 | + """ |
| 50 | + |
| 51 | + @protect_grpc |
| 52 | + def __init__(self, channel: grpc.Channel): # noqa: D102 |
| 53 | + from ansys.api.geometry.v0.components_pb2_grpc import ComponentsStub |
| 54 | + |
| 55 | + self.stub = ComponentsStub(channel) |
| 56 | + |
| 57 | + @protect_grpc |
| 58 | + def create(self, **kwargs) -> dict: # noqa: D102 |
| 59 | + from ansys.api.geometry.v0.components_pb2 import CreateRequest |
| 60 | + |
| 61 | + # Create the request - assumes all inputs are valid and of the proper type |
| 62 | + request = CreateRequest( |
| 63 | + name=kwargs["name"], |
| 64 | + parent=kwargs["parent_id"], |
| 65 | + template=kwargs["template_id"], |
| 66 | + instance_name=kwargs["instance_name"], |
| 67 | + ) |
| 68 | + |
| 69 | + # Call the gRPC service |
| 70 | + response = self.stub.Create(request) |
| 71 | + |
| 72 | + # Return the response - formatted as a dictionary |
| 73 | + return { |
| 74 | + "id": response.component.id, |
| 75 | + "name": response.component.name, |
| 76 | + "instance_name": response.component.instance_name, |
| 77 | + } |
| 78 | + |
| 79 | + @protect_grpc |
| 80 | + def set_name(self, **kwargs) -> dict: # noqa: D102 |
| 81 | + from ansys.api.geometry.v0.models_pb2 import SetObjectNameRequest |
| 82 | + |
| 83 | + # Create the request - assumes all inputs are valid and of the proper type |
| 84 | + request = SetObjectNameRequest(id=build_grpc_id(kwargs["id"]), name=kwargs["name"]) |
| 85 | + |
| 86 | + # Call the gRPC service |
| 87 | + _ = self.stub.SetName(request) |
| 88 | + |
| 89 | + # Return the response - formatted as a dictionary |
| 90 | + return {} |
| 91 | + |
| 92 | + @protect_grpc |
| 93 | + def set_placement(self, **kwargs) -> dict: # noqa: D102 |
| 94 | + from ansys.api.geometry.v0.components_pb2 import SetPlacementRequest |
| 95 | + |
| 96 | + # Create the direction and point objects |
| 97 | + translation = ( |
| 98 | + from_unit_vector_to_grpc_direction(kwargs["translation"].normalize()) |
| 99 | + if kwargs["translation"] is not None |
| 100 | + else None |
| 101 | + ) |
| 102 | + origin = ( |
| 103 | + from_point3d_to_grpc_point(kwargs["rotation_axis_origin"]) |
| 104 | + if kwargs["rotation_axis_origin"] is not None |
| 105 | + else None |
| 106 | + ) |
| 107 | + direction = ( |
| 108 | + from_unit_vector_to_grpc_direction(kwargs["rotation_axis_direction"]) |
| 109 | + if kwargs["rotation_axis_direction"] is not None |
| 110 | + else None |
| 111 | + ) |
| 112 | + |
| 113 | + # Create the request - assumes all inputs are valid and of the proper type |
| 114 | + request = SetPlacementRequest( |
| 115 | + id=kwargs["id"], |
| 116 | + translation=translation, |
| 117 | + rotation_axis_origin=origin, |
| 118 | + rotation_axis_direction=direction, |
| 119 | + rotation_angle=from_measurement_to_server_angle(kwargs["rotation_angle"]), |
| 120 | + ) |
| 121 | + |
| 122 | + # Call the gRPC service |
| 123 | + response = self.stub.SetPlacement(request) |
| 124 | + |
| 125 | + # Return the response - formatted as a dictionary |
| 126 | + return {"matrix": from_grpc_matrix_to_matrix(response.matrix)} |
| 127 | + |
| 128 | + @protect_grpc |
| 129 | + def set_shared_topology(self, **kwargs) -> dict: # noqa: D102 |
| 130 | + from ansys.api.geometry.v0.components_pb2 import SetSharedTopologyRequest |
| 131 | + |
| 132 | + # Create the request - assumes all inputs are valid and of the proper type |
| 133 | + request = SetSharedTopologyRequest( |
| 134 | + id=kwargs["id"], |
| 135 | + share_type=kwargs["share_type"].value, |
| 136 | + ) |
| 137 | + |
| 138 | + # Call the gRPC service |
| 139 | + _ = self.stub.SetSharedTopology(request) |
| 140 | + |
| 141 | + # Return the response - formatted as a dictionary |
| 142 | + return {} |
| 143 | + |
| 144 | + @protect_grpc |
| 145 | + def delete(self, **kwargs) -> dict: # noqa: D102 |
| 146 | + # Create the request - assumes all inputs are valid and of the proper type |
| 147 | + request = build_grpc_id(kwargs["id"]) |
| 148 | + |
| 149 | + # Call the gRPC service |
| 150 | + _ = self.stub.Delete(request) |
| 151 | + |
| 152 | + # Return the response - formatted as a dictionary |
| 153 | + return {} |
| 154 | + |
| 155 | + @protect_grpc |
| 156 | + def import_groups(self, **kwargs) -> dict: # noqa: D102 |
| 157 | + from ansys.api.geometry.v0.components_pb2 import ImportGroupsRequest |
| 158 | + |
| 159 | + # Create the request - assumes all inputs are valid and of the proper type |
| 160 | + request = ImportGroupsRequest( |
| 161 | + id=build_grpc_id(kwargs["id"]), |
| 162 | + ) |
| 163 | + |
| 164 | + # Call the gRPC service |
| 165 | + _ = self.stub.ImportGroups(request) |
| 166 | + |
| 167 | + # Return the response - formatted as a dictionary |
| 168 | + return {} |
| 169 | + |
| 170 | + @protect_grpc |
| 171 | + def make_independent(self, **kwargs) -> dict: # noqa: D102 |
| 172 | + from ansys.api.geometry.v0.components_pb2 import MakeIndependentRequest |
| 173 | + |
| 174 | + # Create the request - assumes all inputs are valid and of the proper type |
| 175 | + request = MakeIndependentRequest( |
| 176 | + ids=[build_grpc_id(id) for id in kwargs["ids"]], |
| 177 | + ) |
| 178 | + |
| 179 | + # Call the gRPC service |
| 180 | + _ = self.stub.MakeIndependent(request) |
| 181 | + |
| 182 | + # Return the response - formatted as a dictionary |
| 183 | + return {} |
0 commit comments