diff --git a/doc/changelog.d/2400.maintenance.md b/doc/changelog.d/2400.maintenance.md new file mode 100644 index 0000000000..6ef9a2be5f --- /dev/null +++ b/doc/changelog.d/2400.maintenance.md @@ -0,0 +1 @@ +V1 implementation of bodies stub diff --git a/src/ansys/geometry/core/_grpc/_services/v1/bodies.py b/src/ansys/geometry/core/_grpc/_services/v1/bodies.py index fe2fc0548f..c2e9963f8c 100644 --- a/src/ansys/geometry/core/_grpc/_services/v1/bodies.py +++ b/src/ansys/geometry/core/_grpc/_services/v1/bodies.py @@ -22,10 +22,19 @@ """Module containing the bodies service implementation for v1.""" import grpc +import pint +from ansys.geometry.core._grpc._services.v1.conversions import ( + from_plane_to_grpc_plane, + from_sketch_shapes_to_grpc_geometries, + from_trimmed_curve_to_grpc_trimmed_curve, +) from ansys.geometry.core.errors import protect_grpc +from ansys.geometry.core.misc.measurements import DEFAULT_UNITS from ..base.bodies import GRPCBodyService +from ..base.conversions import from_measurement_to_server_angle, from_measurement_to_server_length +from .conversions import build_grpc_id class GRPCBodyServiceV1(GRPCBodyService): # pragma: no cover @@ -44,198 +53,1085 @@ class GRPCBodyServiceV1(GRPCBodyService): # pragma: no cover @protect_grpc def __init__(self, channel: grpc.Channel): """Initialize the BodyService with the gRPC stub.""" - from ansys.api.geometry.v1.bodies_pb2_grpc import BodiesStub + from ansys.api.discovery.v1.design.geometry.body_pb2_grpc import BodyStub + from ansys.api.discovery.v1.design.geometry.face_pb2_grpc import FaceStub + from ansys.api.discovery.v1.operations.edit_pb2_grpc import EditStub - self.stub = BodiesStub(channel) + self.stub = BodyStub(channel) + self.edits_stub = EditStub(channel) + self.face_stub = FaceStub(channel) @protect_grpc def create_sphere_body(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.commonmessages_pb2 import Point + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreateSphereBodyRequest, + CreateSphereBodyRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = CreateSphereBodyRequest( + request_data=[ + CreateSphereBodyRequestData( + name=kwargs["name"], + parent_id=kwargs["parent"], + center=Point( + x=kwargs["center"].x.m_as(DEFAULT_UNITS.SERVER_LENGTH), + y=kwargs["center"].y.m_as(DEFAULT_UNITS.SERVER_LENGTH), + z=kwargs["center"].z.m_as(DEFAULT_UNITS.SERVER_LENGTH), + ), + radius=from_measurement_to_server_length(kwargs["radius"]), + ) + ] + ) + + # Call the gRPC service + resp = self.stub.CreateSphereBody(request=request) + + # Return the response - formatted as a dictionary + # Note: response.bodies is a repeated field, we return the first one + body = resp.bodies[0] + return { + "id": body.id.id, + "name": body.name, + "master_id": body.master_id.id, + "is_surface": body.is_surface, + } @protect_grpc def create_extruded_body(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.commonmessages_pb2 import Quantity as GRPCQuantity + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreateExtrudedBodyRequest, + CreateExtrudedBodyRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + # Note: This will need proper conversion functions for plane, geometries + request_data_item = CreateExtrudedBodyRequestData() + request_data_item.name = kwargs["name"] + request_data_item.parent_id.CopyFrom(build_grpc_id(kwargs["parent_id"])) + request_data_item.plane.CopyFrom(from_plane_to_grpc_plane(kwargs["sketch"].plane)) + request_data_item.geometries.CopyFrom( + from_sketch_shapes_to_grpc_geometries( + kwargs["sketch"].plane, kwargs["sketch"].edges, kwargs["sketch"].faces + ) + ) + request_data_item.distance.CopyFrom( + GRPCQuantity( + value_in_geometry_units=( + from_measurement_to_server_length(kwargs["distance"]) * kwargs["direction"] + ) + ) + ) + + request = CreateExtrudedBodyRequest(request_data=[request_data_item]) + + # Call the gRPC service + resp = self.stub.CreateExtrudedBody(request=request) + + # Return the response - formatted as a dictionary + body = resp.bodies[0] + return { + "id": body.id.id, + "name": body.name, + "master_id": body.master_id.id, + "is_surface": body.is_surface, + } @protect_grpc def create_sweeping_profile_body(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreateSweepingProfileRequest, + CreateSweepingProfileRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = CreateSweepingProfileRequest( + request_data=[ + CreateSweepingProfileRequestData( + name=kwargs["name"], + parent_id=build_grpc_id(kwargs["parent_id"]), + plane=from_plane_to_grpc_plane(kwargs["sketch"].plane), + geometries=from_sketch_shapes_to_grpc_geometries( + kwargs["sketch"].plane, kwargs["sketch"].edges, kwargs["sketch"].faces + ), + # path=[from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in kwargs["path"]], + ) + ] + ) + + # Call the gRPC service + resp = self.stub.CreateSweepingProfile(request=request) + + # Return the response - formatted as a dictionary + body = resp.bodies[0] + return { + "id": body.id.id, + "name": body.name, + "master_id": body.master_id.id, + "is_surface": body.is_surface, + } @protect_grpc def create_sweeping_chain(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreateSweepingChainRequest, + CreateSweepingChainRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = CreateSweepingChainRequest( + request_data=[ + CreateSweepingChainRequestData( + name=kwargs["name"], + parent_id=build_grpc_id(kwargs["parent_id"]), + chain=[from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in kwargs["chain"]], + path=[from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in kwargs["path"]], + ) + ] + ) + + # Call the gRPC service + resp = self.stub.CreateSweepingChain(request=request) + + # Return the response - formatted as a dictionary + body = resp.bodies[0] + return { + "id": body.id.id, + "name": body.name, + "master_id": body.master_id.id, + "is_surface": body.is_surface, + } @protect_grpc def sweep_with_guide(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import ( + SweepWithGuideRequest, + SweepWithGuideRequestData, + ) + + # Create request object - assumes all inputs are valid and of the proper type + request = SweepWithGuideRequest( + request_data=[ + SweepWithGuideRequestData( + name=data.name, + parent=build_grpc_id(data.parent_id), + # plane=from_plane_to_grpc_plane(data.sketch.plane), + # geometries=from_sketch_shapes_to_grpc_geometries( + # data.sketch.plane, data.sketch.edges, data.sketch.faces + # ), + # path=from_trimmed_curve_to_grpc_trimmed_curve(data.path), + # guide=from_trimmed_curve_to_grpc_trimmed_curve(data.guide), + tight_tolerance=data.tight_tolerance, + ) + for data in kwargs["sweep_data"] + ], + ) + + # Call the gRPC service + resp = self.edits_stub.SweepWithGuide(request=request) + + # Return the response - formatted as a dictionary + return { + "bodies": [ + { + "id": body.id.id, + "name": body.name, + "master_id": body.master_id.id, + "is_surface": body.is_surface, + } + ] + for body in resp.bodies + } @protect_grpc def create_extruded_body_from_face_profile(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreateExtrudedBodyFromFaceProfileRequest, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = CreateExtrudedBodyFromFaceProfileRequest( + name=kwargs["name"], + parent=kwargs["parent_id"], + face=kwargs["face_id"], + distance=from_measurement_to_server_length(kwargs["distance"]) * kwargs["direction"], + ) + + # Call the gRPC service + resp = self.stub.CreateExtrudedBodyFromFaceProfile(request=request) + + # Return the response - formatted as a dictionary + return { + "id": resp.id.id, + "name": resp.name, + "master_id": resp.master_id.id, + "is_surface": resp.is_surface, + } @protect_grpc def create_extruded_body_from_loft_profiles(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.designmessages_pb2 import TrimmedCurveList + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreateExtrudedBodyFromLoftProfilesRequest, + CreateExtrudedBodyFromLoftProfilesRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = CreateExtrudedBodyFromLoftProfilesRequest( + request_data=[ + CreateExtrudedBodyFromLoftProfilesRequestData( + name=kwargs["name"], + parent_id=build_grpc_id(kwargs["parent_id"]), + profiles=[ + TrimmedCurveList( + curves=[from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in profile] + ) + for profile in kwargs["profiles"] + ], + periodic=kwargs["periodic"], + ruled=kwargs["ruled"], + ) + ] + ) + + # Call the gRPC service + resp = self.stub.CreateExtrudedBodyFromLoftProfiles(request=request) + + # Return the response - formatted as a dictionary + body = resp.bodies[0] + return { + "id": body.id.id, + "name": body.name, + "master_id": body.master_id.id, + "is_surface": body.is_surface, + } @protect_grpc def create_planar_body(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreatePlanarBodyRequest, + CreatePlanarBodyRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = CreatePlanarBodyRequest( + request_data=[ + CreatePlanarBodyRequestData( + name=kwargs["name"], + parent_id=build_grpc_id(kwargs["parent_id"]), + plane=from_plane_to_grpc_plane(kwargs["sketch"].plane), + geometries=from_sketch_shapes_to_grpc_geometries( + kwargs["sketch"].plane, kwargs["sketch"].edges, kwargs["sketch"].faces + ), + ) + ] + ) + + # Call the gRPC service + resp = self.stub.CreatePlanarBody(request=request) + + # Return the response - formatted as a dictionary + body = resp.bodies[0] + return { + "id": body.id.id, + "name": body.name, + "master_id": body.master_id.id, + "is_surface": body.is_surface, + } @protect_grpc def create_body_from_face(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import CreateBodyFromFaceRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = CreateBodyFromFaceRequest( + name=kwargs["name"], + parent=kwargs["parent_id"], + face=kwargs["face_id"], + ) + + # Call the gRPC service + resp = self.stub.CreateFromFace(request=request) + + # Return the response - formatted as a dictionary + return { + "id": resp.id.id, + "name": resp.name, + "master_id": resp.master_id.id, + "is_surface": resp.is_surface, + } @protect_grpc def create_surface_body(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreateSurfaceBodyRequest, + CreateSurfaceBodyRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = CreateSurfaceBodyRequest( + request_data=[ + CreateSurfaceBodyRequestData( + name=kwargs["name"], + parent_id=build_grpc_id(kwargs["parent_id"]), + # trimmed_surface=from_trimmed_surface_to_grpc_trimmed_surface(kwargs["trimmed_surface"]), + ) + ] + ) + + # Call the gRPC service + resp = self.stub.CreateSurfaceBody(request=request) + + # Return the response - formatted as a dictionary + body = resp.bodies[0] + return { + "id": body.id.id, + "name": body.name, + "master_id": body.master_id.id, + "is_surface": body.is_surface, + } @protect_grpc def create_surface_body_from_trimmed_curves(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreateSurfaceBodyFromTrimmedCurvesRequest, + CreateSurfaceBodyFromTrimmedCurvesRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = CreateSurfaceBodyFromTrimmedCurvesRequest( + request_data=[ + CreateSurfaceBodyFromTrimmedCurvesRequestData( + name=kwargs["name"], + parent_id=build_grpc_id(kwargs["parent_id"]), + trimmed_curves=[ + # from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in kwargs["trimmed_curves"] + ], + ) + ] + ) + + # Call the gRPC service + resp = self.stub.CreateSurfaceBodyFromTrimmedCurves(request=request) + + # Return the response - formatted as a dictionary + body = resp.bodies[0] + return { + "id": body.id.id, + "name": body.name, + "master_id": body.master_id.id, + "is_surface": body.is_surface, + } @protect_grpc def translate(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.commonmessages_pb2 import Quantity as GRPCQuantity + from ansys.api.discovery.v1.operations.edit_pb2 import ( + MoveTranslateRequest, + MoveTranslateRequestData, + ) + + # Create the request data with repeated selection_ids + request_data = MoveTranslateRequestData() + for body_id in kwargs["ids"]: + request_data.selection_ids.append(build_grpc_id(body_id)) + + # Set the distance using GRPCQuantity + request_data.distance.CopyFrom( + GRPCQuantity( + value_in_geometry_units=from_measurement_to_server_length(kwargs["distance"]) + ) + ) + + # direction=from_unit_vector_to_grpc_direction(kwargs["direction"]), + + # Create the request with request_data + request = MoveTranslateRequest(request_data=[request_data]) + + # Call the gRPC service + self.edits_stub.MoveTranslate(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def delete(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + # Call the gRPC service + self.stub.Delete(request=build_grpc_id(kwargs["id"])) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def is_suppressed(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + # Call the gRPC service + resp = self.stub.GetIsSuppressed(request=build_grpc_id(kwargs["id"])) + + # Return the response - formatted as a dictionary + return {"result": resp.result} @protect_grpc def get_color(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.designmessages_pb2 import MultipleEntitiesRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = MultipleEntitiesRequest(ids=[build_grpc_id(kwargs["id"])]) + + # Call the gRPC service + resp = self.stub.GetColor(request=request) + + # Return the response - formatted as a dictionary (get first color from map) + color = next(iter(resp.colors.values())) if resp.colors else None + return {"color": color} + + @protect_grpc + def set_color(self, **kwargs) -> dict: # noqa: D102 + from ansys.api.discovery.v1.design.designmessages_pb2 import SetColorRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = SetColorRequest(body_id=kwargs["id"], color=kwargs["color"]) + + # Call the gRPC service + self.stub.SetColor(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def get_faces(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + # Call the gRPC service + resp = self.stub.GetFaces(request=build_grpc_id(kwargs["id"])) + + # Return the response - formatted as a dictionary + return { + "faces": [ + { + "id": face.id, + "surface_type": face.surface_type, + "is_reversed": face.is_reversed, + } + for face in resp.faces + ] + } @protect_grpc def get_edges(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + # Call the gRPC service + resp = self.stub.GetEdges(request=build_grpc_id(kwargs["id"])) + + # Return the response - formatted as a dictionary + return { + "edges": [ + { + "id": edge.id, + "curve_type": edge.curve_type, + "is_reversed": edge.is_reversed, + } + for edge in resp.edges + ] + } @protect_grpc def get_vertices(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.commonmessages_pb2 import Point + + # Call the gRPC service + resp = self.stub.GetVertices(request=build_grpc_id(kwargs["id"])) + + # Return the response - formatted as a dictionary + return { + "vertices": [ + { + "id": vertex.id.id, + "position": Point( + x=vertex.position.x, y=vertex.position.y, z=vertex.position.z + ), + } + for vertex in resp.vertices + ] + } @protect_grpc def get_volume(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + # Call the gRPC service + resp = self.stub.GetVolume(request=build_grpc_id(kwargs["id"])) + + # Return the response - formatted as a dictionary + return {"volume": pint.Quantity(resp.volume, DEFAULT_UNITS.SERVER_VOLUME)} @protect_grpc def get_bounding_box(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.commonmessages_pb2 import Point + + # Call the gRPC service + resp = self.stub.GetBoundingBox(request=build_grpc_id(kwargs["id"])) + + # Return the response - formatted as a dictionary + return { + "min": Point(x=resp.box.min.x, y=resp.box.min.y, z=resp.box.min.z), + "max": Point(x=resp.box.max.x, y=resp.box.max.y, z=resp.box.max.z), + "center": Point(x=resp.box.center.x, y=resp.box.center.y, z=resp.box.center.z), + } @protect_grpc def set_assigned_material(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + SetAssignedCADMaterialRequest, + SetAssignedCADMaterialRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = SetAssignedCADMaterialRequest( + request_data=[ + SetAssignedCADMaterialRequestData( + id=build_grpc_id(kwargs["id"]), + material=kwargs["material"], + ) + ] + ) + + # Call the gRPC service + self.stub.SetAssignedCADMaterial(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def get_assigned_material(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + # Call the gRPC service + resp = self.stub.GetAssignedCADMaterial(request=build_grpc_id(kwargs["id"])) + + # Return the response - formatted as a dictionary + # return {"material": from_grpc_material_to_material(resp)} + return {"material": resp} @protect_grpc def remove_assigned_material(self, **kwargs): # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + RemoveAssignedCADMaterialRequest, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = RemoveAssignedCADMaterialRequest(ids=[build_grpc_id(id) for id in kwargs["ids"]]) + + # Call the gRPC service + resp = self.stub.RemoveAssignedCADMaterial(request=request) + + # Return the response - formatted as a dictionary + return {"successfully_removed": [id for id in resp.successfully_removed_ids]} @protect_grpc def set_name(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.physics.physicsentity_pb2 import SetNameRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = SetNameRequest(object_id=build_grpc_id(kwargs["id"]), name=kwargs["name"]) + + # Call the gRPC service + self.stub.SetName(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def set_fill_style(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + SetFillStyleRequest, + SetFillStyleRequestData, + ) + + # Create request data + request_data = SetFillStyleRequestData( + id=build_grpc_id(kwargs["id"]), fill_style=kwargs["fill_style"].value + ) + + # Create the request with request_data + request = SetFillStyleRequest(request_data=[request_data]) + + # Call the gRPC service + self.stub.SetFillStyle(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def set_suppressed(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import SetSuppressedRequest - @protect_grpc - def set_color(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + # Create the request - assumes all inputs are valid and of the proper type + request = SetSuppressedRequest( + bodies=[build_grpc_id(body_id) for body_id in kwargs["bodies"]], + is_suppressed=kwargs["is_suppressed"], + ) + + # Call the gRPC service + self.stub.SetSuppressed(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def rotate(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.commonmessages_pb2 import Point + from ansys.api.discovery.v1.operations.edit_pb2 import RotateRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = RotateRequest( + id=kwargs["id"], + axis_origin=Point( + x=kwargs["axis_origin"].x.m_as(DEFAULT_UNITS.SERVER_LENGTH), + y=kwargs["axis_origin"].y.m_as(DEFAULT_UNITS.SERVER_LENGTH), + z=kwargs["axis_origin"].z.m_as(DEFAULT_UNITS.SERVER_LENGTH), + ), + # axis_direction=from_unit_vector_to_grpc_direction(kwargs["axis_direction"]), + angle=from_measurement_to_server_angle(kwargs["angle"]), + ) + + # Call the gRPC service + self.stub.Rotate(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def scale(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import ScaleRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = ScaleRequest( + id=kwargs["id"], + scale=kwargs["value"], + ) + + # Call the gRPC service + self.edits_stub.Scale(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def mirror(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import MirrorRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = MirrorRequest( + id=kwargs["id"], + # plane=from_plane_to_grpc_plane(kwargs["plane"]), + ) + + # Call the gRPC service + self.edits_stub.Mirror(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def map(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import MapRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = MapRequest( + id=kwargs["id"], + # frame=from_frame_to_grpc_frame(kwargs["frame"]), + ) + + # Call the gRPC service + self.edits_stub.Map(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def get_collision(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + GetCollisionRequest, + GetCollisionRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = GetCollisionRequest( + request_data=[ + GetCollisionRequestData( + body_1_id=build_grpc_id(kwargs["id"]), + body_2_id=build_grpc_id(kwargs["other_id"]), + ) + ] + ) + + # Call the gRPC service + resp = self.stub.GetCollision(request=request) + # Return the response - formatted as a dictionary + return {"collision_type": resp.response_data[0].collision} + + # TODO:find new method name, @protect_grpc def copy(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import CopyRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = CopyRequest( + id=kwargs["id"], + parent=kwargs["parent_id"], + name=kwargs["name"], + ) + + # Call the gRPC service + resp = self.edits_stub.Copy(request=request) + + # Return the response - formatted as a dictionary + return {"master_id": resp.master_id, "name": resp.name} @protect_grpc def get_tesellation(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import GetTessellationRequest + + tess_map = {} + resp = [] # For compatibility with stream response + try: + request = GetTessellationRequest(id=build_grpc_id(kwargs["id"])) + resp = [self.stub.GetTessellation(request=request)] + except grpc.RpcError: + request = GetTessellationRequest(id=build_grpc_id(kwargs["id"])) + resp = self.stub.GetTessellation(request=request) + + for elem in resp: + for face_id, face_tess in elem.face_tessellation.items(): + # tess_map[face_id] = from_grpc_tess_to_raw_data(face_tess) + tess_map[face_id] = face_tess + + return {"tessellation": tess_map} @protect_grpc def get_tesellation_with_options(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import GetTessellationRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = GetTessellationRequest( + id=build_grpc_id(kwargs["id"]), + # options=from_tess_options_to_grpc_tess_options(kwargs["options"]), + include_faces=kwargs["include_faces"], + include_edges=kwargs["include_edges"], + ) + + tess_map = {} + resp = [] # For compatibility with stream response + try: + resp = [self.stub.GetTessellation(request=request)] + except grpc.RpcError: + resp = self.stub.GetTessellationStream(request=request) + + for elem in resp: + for face_id, face_tess in elem.face_tessellation.items(): + # tess_map[face_id] = from_grpc_tess_to_raw_data(face_tess) + tess_map[face_id] = face_tess + for edge_id, edge_tess in elem.edge_tessellation.items(): + # tess_map[edge_id] = from_grpc_edge_tess_to_raw_data(edge_tess) + tess_map[edge_id] = edge_tess + + return {"tessellation": tess_map} @protect_grpc def boolean(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.commonmessages_pb2 import SetBooleanRequest + + # Call the gRPC service and build the requests accordingly + response_success = 0 + serialized_tracker_response = {} + try: + request = SetBooleanRequest( + target=kwargs["target"], + tool=kwargs["tool"], + type=kwargs["type"], + keep_tool=kwargs["keep_tool"], + ) + response = self.edits_stub.Boolean(request=request) + response_success = 1 + except grpc.RpcError: + response_success = 0 + + if response_success == 1: + # serialized_tracker_response = serialize_tracker_command_response(response=response) + serialized_tracker_response = response + + # Return the response - formatted as a dictionary + return {"complete_command_response": serialized_tracker_response} @protect_grpc def combine(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import ( + CombineIntersectBodiesRequest, + CombineMergeBodiesRequest, + CombineMergeBodiesRequestData, + ) + + target_body = kwargs["target"] + other_bodies = kwargs["other"] + type_bool_op = kwargs["type_bool_op"] + keep_other = kwargs["keep_other"] + + if type_bool_op == "intersect": + request = CombineIntersectBodiesRequest( + target_selection=build_grpc_id(target_body), + tool_selection=[build_grpc_id(id) for id in other_bodies], + preserve_tools=keep_other, + ) + response = self.edits_stub.CombineIntersectBodies(request=request) + elif type_bool_op == "subtract": + request = CombineIntersectBodiesRequest( + target_selection=build_grpc_id(target_body), + tool_selection=[build_grpc_id(id) for id in other_bodies], + preserve_tools=keep_other, + ) + response = self.edits_stub.CombineSubtractBodies(request=request) + elif type_bool_op == "unite": + # Create request data with repeated target_selection_ids + request_data = CombineMergeBodiesRequestData() + request_data.target_selection_ids.append(build_grpc_id(target_body)) + for body_id in other_bodies: + request_data.target_selection_ids.append(build_grpc_id(body_id)) + + request = CombineMergeBodiesRequest(request_data=[request_data]) + response = self.edits_stub.CombineMergeBodies(request=request) + else: + raise ValueError(f"Invalid boolean operation type: {type_bool_op}") + + if not response.success: + raise ValueError(f"Boolean operation failed: {response}") + + # Return the response - formatted as a dictionary + # return {"complete_command_response": serialize_tracker_command_response(response=response)} + return {"complete_command_response": response} @protect_grpc def split_body(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import SplitBodyRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = SplitBodyRequest( + selection=[build_grpc_id(id) for id in kwargs["body_ids"]], + split_by_plane=from_plane_to_grpc_plane(kwargs["plane"]) if kwargs["plane"] else None, + split_by_slicer=[build_grpc_id(id) for id in kwargs["slicer_ids"]], + split_by_faces=[build_grpc_id(id) for id in kwargs["face_ids"]], + extend_surfaces=kwargs["extend_surfaces"], + ) + + # Call the gRPC service + resp = self.edits_stub.SplitBodies(request=request) + + # Return the response - formatted as a dictionary + return { + "success": resp.success, + } @protect_grpc def create_body_from_loft_profiles_with_guides(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.designmessages_pb2 import TrimmedCurveList + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + CreateBodyFromLoftWithGuidesRequest, + CreateBodyFromLoftWithGuidesRequestData, + ) + + # Create request object - assumes all inputs are valid and of the proper type + request = CreateBodyFromLoftWithGuidesRequest( + request_data=[ + CreateBodyFromLoftWithGuidesRequestData( + name=kwargs["name"], + parent=build_grpc_id(kwargs["parent_id"]), + profiles=[ + TrimmedCurveList( + # curves=[from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in profile] + ) + for profile in kwargs["profiles"] + ], + guides=TrimmedCurveList( + # curves=[from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in kwargs["guides"]] + ), + ) + ] + ) + + # Call the gRPC service + response = self.stub.CreateFromLoftWithGuides(request) + + # Return the response - formatted as a dictionary + new_body = response.created_bodies[0] + return { + "id": new_body.id.id, + "name": new_body.name, + "master_id": new_body.master_id.id, + "is_surface": new_body.is_surface, + } @protect_grpc def combine_merge(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import ( + CombineMergeBodiesRequest, + CombineMergeBodiesRequestData, + ) + + # Create request data with repeated target_selection_ids + request_data = CombineMergeBodiesRequestData() + for body_id in kwargs["body_ids"]: + request_data.target_selection_ids.append(build_grpc_id(body_id)) + + # Create the request with request_data + request = CombineMergeBodiesRequest(request_data=[request_data]) + + # Call the gRPC service + _ = self.edits_stub.CombineMergeBodies(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def assign_midsurface_thickness(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + SetMidSurfaceThicknessRequest, + SetMidSurfaceThicknessRequestData, + ) + + request = SetMidSurfaceThicknessRequest( + request_data=[ + SetMidSurfaceThicknessRequestData( + ids=[build_grpc_id(id) for id in kwargs["ids"]], + thickness=from_measurement_to_server_length(kwargs["thickness"]), + ) + ] + ) + + _ = self.stub.SetMidSurfaceThickness(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def assign_midsurface_offset(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.body_pb2 import ( + SetMidSurfaceOffsetTypeRequest, + SetMidSurfaceOffsetTypeRequestData, + ) + + # Create the request - assumes all inputs are valid and of the proper type + request = SetMidSurfaceOffsetTypeRequest( + request_data=[ + SetMidSurfaceOffsetTypeRequestData( + ids=[build_grpc_id(id) for id in kwargs["ids"]], + offset_type=kwargs["offset_type"].value, + ) + ] + ) + + # Call the gRPC service + _ = self.stub.SetMidSurfaceOffsetType(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def shell(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import ShellRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = ShellRequest( + bodies=[build_grpc_id(id) for id in kwargs["bodies"]], + thickness=from_measurement_to_server_length(kwargs["thickness"]), + remove_faces=[build_grpc_id(id) for id in kwargs["remove_faces"]], + ) + + # Call the gRPC service + _ = self.edits_stub.Shell(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def remove_faces(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.design.geometry.face_pb2 import RemoveFacesRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = RemoveFacesRequest( + bodies=[build_grpc_id(id) for id in kwargs["bodies"]], + faces=[build_grpc_id(id) for id in kwargs["faces"]], + ) + + # Call the gRPC service + _ = self.face_stub.Remove(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def imprint_curves(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import ImprintCurvesRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = ImprintCurvesRequest( + faces=[build_grpc_id(id) for id in kwargs["faces"]], + # curves=[from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in kwargs["curves"]], + ) + + # Call the gRPC service + _ = self.edits_stub.ImprintCurves(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc def project_curves(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import ProjectCurvesRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = ProjectCurvesRequest( + faces=[build_grpc_id(id) for id in kwargs["faces"]], + # curves=[from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in kwargs["curves"]], + # direction=from_unit_vector_to_grpc_direction(kwargs["direction"]), + distance=from_measurement_to_server_length(kwargs["distance"]), + ) + + # Call the gRPC service + resp = self.edits_stub.ProjectCurves(request=request) + + # Return the response - formatted as a dictionary + return {"curves": resp.curves} @protect_grpc def imprint_projected_curves(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + from ansys.api.discovery.v1.operations.edit_pb2 import ImprintProjectedCurvesRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = ImprintProjectedCurvesRequest( + faces=[build_grpc_id(id) for id in kwargs["faces"]], + # curves=[from_trimmed_curve_to_grpc_trimmed_curve(tc) for tc in kwargs["curves"]], + # direction=from_unit_vector_to_grpc_direction(kwargs["direction"]), + distance=from_measurement_to_server_length(kwargs["distance"]), + only_one_curve=kwargs["only_one_curve"], + closest_face=kwargs["closest_face"], + ) + + # Call the gRPC service + _ = self.edits_stub.ImprintProjectedCurves(request=request) + + # Return the response - formatted as a dictionary + return {} @protect_grpc - def get_full_tessellation(self, **kwargs) -> dict: # noqa: D102 - raise NotImplementedError + def get_full_tessellation(self, **kwargs): # noqa: D102 + from ansys.api.discovery.v1.design.geometry.body_pb2 import GetTessellationRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = GetTessellationRequest( + id=build_grpc_id(kwargs["id"]), + # options=from_tess_options_to_grpc_tess_options(kwargs["options"]), + include_faces=kwargs["include_faces"], + include_edges=kwargs["include_edges"], + ) + + # Call the gRPC service - using streaming + resp = self.stub.GetTessellationStream(request=request) + + # Return the response - formatted as a dictionary + # return {"tessellation": from_grpc_tess_to_pd(resp)} + return {"tessellation": resp} diff --git a/src/ansys/geometry/core/_grpc/_services/v1/conversions.py b/src/ansys/geometry/core/_grpc/_services/v1/conversions.py index 61944d450e..71d4f2584b 100644 --- a/src/ansys/geometry/core/_grpc/_services/v1/conversions.py +++ b/src/ansys/geometry/core/_grpc/_services/v1/conversions.py @@ -226,13 +226,15 @@ def from_point2d_to_grpc_point(plane: "Plane", point2d: "Point2D") -> GRPCPoint: GRPCPoint Geometry service gRPC point message. The unit is meters. """ + from ansys.api.discovery.v1.commonmessages_pb2 import Quantity as GRPCQuantity + from ansys.geometry.core.misc.measurements import DEFAULT_UNITS point3d = plane.transform_point2d_local_to_global(point2d) return GRPCPoint( - x=point3d.x.m_as(DEFAULT_UNITS.SERVER_LENGTH), - y=point3d.y.m_as(DEFAULT_UNITS.SERVER_LENGTH), - z=point3d.z.m_as(DEFAULT_UNITS.SERVER_LENGTH), + x=GRPCQuantity(value_in_geometry_units=point3d.x.m_as(DEFAULT_UNITS.SERVER_LENGTH)), + y=GRPCQuantity(value_in_geometry_units=point3d.y.m_as(DEFAULT_UNITS.SERVER_LENGTH)), + z=GRPCQuantity(value_in_geometry_units=point3d.z.m_as(DEFAULT_UNITS.SERVER_LENGTH)), ) @@ -627,9 +629,8 @@ def from_sketch_nurbs_to_grpc_nurbs_curve(curve: "SketchNurbs", plane: "Plane") GRPCNurbsCurve Geometry service gRPC NURBS curve message. The unit is meters. """ - from ansys.api.geometry.v0.models_pb2 import ( + from ansys.api.discovery.v1.design.designmessages_pb2 import ( ControlPoint as GRPCControlPoint, - NurbsData as GRPCNurbsData, ) # Convert control points @@ -641,16 +642,10 @@ def from_sketch_nurbs_to_grpc_nurbs_curve(curve: "SketchNurbs", plane: "Plane") for i, pt in enumerate(curve.control_points) ] - # Convert nurbs data - nurbs_data = GRPCNurbsData( - degree=curve.degree, - knots=from_knots_to_grpc_knots(curve.knots), - order=curve.degree + 1, - ) - return GRPCNurbsCurve( control_points=control_points, - nurbs_data=nurbs_data, + degree=curve.degree, + knots=from_knots_to_grpc_knots(curve.knots), ) @@ -667,13 +662,19 @@ def from_sketch_ellipse_to_grpc_ellipse(ellipse: "SketchEllipse", plane: "Plane" GRPCEllipse Geometry service gRPC ellipse message. The unit is meters. """ + from ansys.api.discovery.v1.commonmessages_pb2 import Quantity as GRPCQuantity + from ansys.geometry.core.misc.measurements import DEFAULT_UNITS return GRPCEllipse( center=from_point2d_to_grpc_point(plane, ellipse.center), - majorradius=ellipse.major_radius.m_as(DEFAULT_UNITS.SERVER_LENGTH), - minorradius=ellipse.minor_radius.m_as(DEFAULT_UNITS.SERVER_LENGTH), - angle=ellipse.angle.m_as(DEFAULT_UNITS.SERVER_ANGLE), + majorradius=GRPCQuantity( + value_in_geometry_units=ellipse.major_radius.m_as(DEFAULT_UNITS.SERVER_LENGTH) + ), + minorradius=GRPCQuantity( + value_in_geometry_units=ellipse.minor_radius.m_as(DEFAULT_UNITS.SERVER_LENGTH) + ), + angle=GRPCQuantity(value_in_geometry_units=ellipse.angle.m_as(DEFAULT_UNITS.SERVER_ANGLE)), ) @@ -692,11 +693,15 @@ def from_sketch_circle_to_grpc_circle(circle: "SketchCircle", plane: "Plane") -> GRPCCircle Geometry service gRPC circle message. The unit is meters. """ + from ansys.api.discovery.v1.commonmessages_pb2 import Quantity as GRPCQuantity + from ansys.geometry.core.misc.measurements import DEFAULT_UNITS return GRPCCircle( center=from_point2d_to_grpc_point(plane, circle.center), - radius=circle.radius.m_as(DEFAULT_UNITS.SERVER_LENGTH), + radius=GRPCQuantity( + value_in_geometry_units=circle.radius.m_as(DEFAULT_UNITS.SERVER_LENGTH) + ), ) @@ -713,13 +718,17 @@ def from_sketch_polygon_to_grpc_polygon(polygon: "Polygon", plane: "Plane") -> G GRPCPolygon Geometry service gRPC polygon message. The unit is meters. """ + from ansys.api.discovery.v1.commonmessages_pb2 import Quantity as GRPCQuantity + from ansys.geometry.core.misc.measurements import DEFAULT_UNITS return GRPCPolygon( center=from_point2d_to_grpc_point(plane, polygon.center), - radius=polygon.inner_radius.m_as(DEFAULT_UNITS.SERVER_LENGTH), + radius=GRPCQuantity( + value_in_geometry_units=polygon.inner_radius.m_as(DEFAULT_UNITS.SERVER_LENGTH) + ), numberofsides=polygon.n_sides, - angle=polygon.angle.m_as(DEFAULT_UNITS.SERVER_ANGLE), + angle=GRPCQuantity(value_in_geometry_units=polygon.angle.m_as(DEFAULT_UNITS.SERVER_ANGLE)), ) @@ -828,9 +837,8 @@ def from_nurbs_curve_to_grpc_nurbs_curve(curve: "NURBSCurve") -> GRPCNurbsCurve: GRPCNurbsCurve Geometry service gRPC ``NURBSCurve`` message. """ - from ansys.api.geometry.v0.models_pb2 import ( + from ansys.api.discovery.v1.design.designmessages_pb2 import ( ControlPoint as GRPCControlPoint, - NurbsData as GRPCNurbsData, ) # Convert control points @@ -842,16 +850,10 @@ def from_nurbs_curve_to_grpc_nurbs_curve(curve: "NURBSCurve") -> GRPCNurbsCurve: for i, pt in enumerate(curve.control_points) ] - # Convert nurbs data - nurbs_data = GRPCNurbsData( - degree=curve.degree, - knots=from_knots_to_grpc_knots(curve.knots), - order=curve.degree + 1, - ) - return GRPCNurbsCurve( control_points=control_points, - nurbs_data=nurbs_data, + degree=curve.degree, + knots=from_knots_to_grpc_knots(curve.knots), ) @@ -868,9 +870,8 @@ def from_nurbs_surface_to_grpc_nurbs_surface(surface: "NURBSSurface") -> GRPCNur GRPCNurbsSurface Geometry service gRPC ``NURBSSurface`` message. """ - from ansys.api.geometry.v0.models_pb2 import ( + from ansys.api.discovery.v1.design.designmessages_pb2 import ( ControlPoint as GRPCControlPoint, - NurbsData as GRPCNurbsData, ) # Convert control points @@ -882,23 +883,12 @@ def from_nurbs_surface_to_grpc_nurbs_surface(surface: "NURBSSurface") -> GRPCNur for weight, point in zip(surface.weights, surface.control_points) ] - # Convert nurbs data - nurbs_data_u = GRPCNurbsData( - degree=surface.degree_u, - knots=from_knots_to_grpc_knots(surface.knotvector_u), - order=surface.degree_u + 1, - ) - - nurbs_data_v = GRPCNurbsData( - degree=surface.degree_v, - knots=from_knots_to_grpc_knots(surface.knotvector_v), - order=surface.degree_v + 1, - ) - return GRPCNurbsSurface( control_points=control_points, - nurbs_data_u=nurbs_data_u, - nurbs_data_v=nurbs_data_v, + degree_u=surface.degree_u, + degree_v=surface.degree_v, + knots_u=from_knots_to_grpc_knots(surface.knotvector_u), + knots_v=from_knots_to_grpc_knots(surface.knotvector_v), )