diff --git a/src/ansys/geometry/core/_grpc/_services/v0/named_selection.py b/src/ansys/geometry/core/_grpc/_services/v0/named_selection.py index ac60c14355..761eaf7430 100644 --- a/src/ansys/geometry/core/_grpc/_services/v0/named_selection.py +++ b/src/ansys/geometry/core/_grpc/_services/v0/named_selection.py @@ -26,7 +26,7 @@ from ansys.geometry.core.errors import protect_grpc from ..base.named_selection import GRPCNamedSelectionService -from .conversions import build_grpc_id +from .conversions import build_grpc_id, from_grpc_point_to_point3d class GRPCNamedSelectionServiceV0(GRPCNamedSelectionService): @@ -64,7 +64,9 @@ def get_named_selection(self, **kwargs): # noqa: D102 "faces": [face.id for face in response.faces], "edges": [edge.id for edge in response.edges], "beams": [beam.id.id for beam in response.beams], - "design_points": [(dp.id, dp.points[0]) for dp in response.design_points], + "design_points": [ + (dp.id, from_grpc_point_to_point3d(dp.points[0])) for dp in response.design_points + ], "components": [comp.id for comp in response.components], "vertices": [vertex.id.id for vertex in response.vertices], } diff --git a/src/ansys/geometry/core/connection/__init__.py b/src/ansys/geometry/core/connection/__init__.py index 26957e6dac..7ed8a8edf1 100644 --- a/src/ansys/geometry/core/connection/__init__.py +++ b/src/ansys/geometry/core/connection/__init__.py @@ -23,20 +23,6 @@ from ansys.geometry.core.connection.backend import ApiVersions, BackendType from ansys.geometry.core.connection.client import GrpcClient -from ansys.geometry.core.connection.conversions import ( - curve_to_grpc_curve, - frame_to_grpc_frame, - grpc_curve_to_curve, - grpc_frame_to_frame, - grpc_matrix_to_matrix, - grpc_surface_to_surface, - line_to_grpc_line, - plane_to_grpc_plane, - point3d_to_grpc_point, - sketch_shapes_to_grpc_geometries, - trimmed_curve_to_grpc_trimmed_curve, - unit_vector_to_grpc_direction, -) import ansys.geometry.core.connection.defaults as defaults from ansys.geometry.core.connection.docker_instance import ( GeometryContainers, diff --git a/src/ansys/geometry/core/connection/conversions.py b/src/ansys/geometry/core/connection/conversions.py deleted file mode 100644 index 02f844929b..0000000000 --- a/src/ansys/geometry/core/connection/conversions.py +++ /dev/null @@ -1,667 +0,0 @@ -# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates. -# SPDX-License-Identifier: MIT -# -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -"""Module providing for conversions.""" - -from typing import TYPE_CHECKING - -from ansys.api.geometry.v0.models_pb2 import ( - Arc as GRPCArc, - Circle as GRPCCircle, - CurveGeometry as GRPCCurve, - Direction as GRPCDirection, - Ellipse as GRPCEllipse, - Frame as GRPCFrame, - Geometries as GRPCGeometries, - Line as GRPCLine, - Material as GRPCMaterial, - MaterialProperty as GRPCMaterialProperty, - Matrix as GRPCMatrix, - Plane as GRPCPlane, - Point as GRPCPoint, - Polygon as GRPCPolygon, - Surface as GRPCSurface, - TrimmedCurve as GRPCTrimmedCurve, -) -from pint import Quantity, UndefinedUnitError - -from ansys.geometry.core.materials.material import ( - Material, - MaterialProperty, - MaterialPropertyType, -) -from ansys.geometry.core.math.frame import Frame -from ansys.geometry.core.math.matrix import Matrix44 -from ansys.geometry.core.math.plane import Plane -from ansys.geometry.core.math.point import Point2D, Point3D -from ansys.geometry.core.math.vector import UnitVector3D -from ansys.geometry.core.misc.measurements import DEFAULT_UNITS, UNITS -from ansys.geometry.core.shapes.curves.circle import Circle -from ansys.geometry.core.shapes.curves.curve import Curve -from ansys.geometry.core.shapes.curves.ellipse import Ellipse -from ansys.geometry.core.shapes.curves.line import Line -from ansys.geometry.core.shapes.surfaces.cone import Cone -from ansys.geometry.core.shapes.surfaces.cylinder import Cylinder -from ansys.geometry.core.shapes.surfaces.plane import PlaneSurface -from ansys.geometry.core.shapes.surfaces.sphere import Sphere -from ansys.geometry.core.shapes.surfaces.surface import Surface -from ansys.geometry.core.shapes.surfaces.torus import Torus -from ansys.geometry.core.sketch.arc import Arc -from ansys.geometry.core.sketch.circle import SketchCircle -from ansys.geometry.core.sketch.edge import SketchEdge -from ansys.geometry.core.sketch.ellipse import SketchEllipse -from ansys.geometry.core.sketch.face import SketchFace -from ansys.geometry.core.sketch.polygon import Polygon -from ansys.geometry.core.sketch.segment import SketchSegment - -if TYPE_CHECKING: # pragma: no cover - from ansys.geometry.core.designer.face import SurfaceType - from ansys.geometry.core.shapes.curves.trimmed_curve import TrimmedCurve - - -def unit_vector_to_grpc_direction(unit_vector: UnitVector3D) -> GRPCDirection: - """Convert a ``UnitVector3D`` class to a unit vector gRPC message. - - Parameters - ---------- - unit_vector : UnitVector3D - Source vector data. - - Returns - ------- - GRPCDirection - Geometry service gRPC direction message. - """ - return GRPCDirection(x=unit_vector.x, y=unit_vector.y, z=unit_vector.z) - - -def frame_to_grpc_frame(frame: Frame) -> GRPCFrame: - """Convert a ``Frame`` class to a frame gRPC message. - - Parameters - ---------- - frame : Frame - Source frame data. - - Returns - ------- - GRPCFrame - Geometry service gRPC frame message. The unit for the frame origin is meters. - """ - return GRPCFrame( - origin=point3d_to_grpc_point(frame.origin), - dir_x=unit_vector_to_grpc_direction(frame.direction_x), - dir_y=unit_vector_to_grpc_direction(frame.direction_y), - ) - - -def plane_to_grpc_plane(plane: Plane) -> GRPCPlane: - """Convert a ``Plane`` class to a plane gRPC message. - - Parameters - ---------- - plane : Plane - Source plane data. - - Returns - ------- - GRPCPlane - Geometry service gRPC plane message. The unit is meters. - """ - return GRPCPlane( - frame=GRPCFrame( - origin=point3d_to_grpc_point(plane.origin), - dir_x=unit_vector_to_grpc_direction(plane.direction_x), - dir_y=unit_vector_to_grpc_direction(plane.direction_y), - ) - ) - - -def sketch_shapes_to_grpc_geometries( - plane: Plane, - edges: list[SketchEdge], - faces: list[SketchFace], - only_one_curve: bool = False, -) -> GRPCGeometries: - """Convert lists of ``SketchEdge`` and ``SketchFace`` to a gRPC message. - - Parameters - ---------- - plane : Plane - Plane for positioning the 2D sketches. - edges : list[SketchEdge] - Source edge data. - faces : list[SketchFace] - Source face data. - only_one_curve : bool, default: False - Whether to project one curve of the whole set of geometries to - enhance performance. - - Returns - ------- - GRPCGeometries - Geometry service gRPC geometries message. The unit is meters. - """ - geometries = GRPCGeometries() - - converted_sketch_edges = sketch_edges_to_grpc_geometries(edges, plane) - geometries.lines.extend(converted_sketch_edges[0]) - geometries.arcs.extend(converted_sketch_edges[1]) - - for face in faces: - if isinstance(face, SketchCircle): - geometries.circles.append(sketch_circle_to_grpc_circle(face, plane)) - if isinstance(face, SketchEllipse): - geometries.ellipses.append(sketch_ellipse_to_grpc_ellipse(face, plane)) - if isinstance(face, Polygon): - geometries.polygons.append(sketch_polygon_to_grpc_polygon(face, plane)) - else: - converted_face_edges = sketch_edges_to_grpc_geometries(face.edges, plane) - geometries.lines.extend(converted_face_edges[0]) - geometries.arcs.extend(converted_face_edges[1]) - - if only_one_curve: - one_curve_geometry = GRPCGeometries() - if len(geometries.lines) > 0: - one_curve_geometry.lines.append(geometries.lines[0]) - elif len(geometries.arcs) > 0: - one_curve_geometry.arcs.append(geometries.arcs[0]) - elif len(geometries.circles) > 0: - one_curve_geometry.circles.append(geometries.circles[0]) - elif len(geometries.ellipses) > 0: - one_curve_geometry.ellipses.append(geometries.ellipses[0]) - elif len(geometries.polygons) > 0: - one_curve_geometry.polygons.append(geometries.polygons[0]) - return one_curve_geometry - - else: - return geometries - - -def sketch_edges_to_grpc_geometries( - edges: list[SketchEdge], - plane: Plane, -) -> tuple[list[GRPCLine], list[GRPCArc]]: - """Convert a list of ``SketchEdge`` to a gRPC message. - - Parameters - ---------- - edges : list[SketchEdge] - Source edge data. - plane : Plane - Plane for positioning the 2D sketches. - - Returns - ------- - tuple[list[GRPCLine], list[GRPCArc]] - Geometry service gRPC line and arc messages. The unit is meters. - """ - arcs = [] - segments = [] - for edge in edges: - if isinstance(edge, SketchSegment): - segments.append(sketch_segment_to_grpc_line(edge, plane)) - elif isinstance(edge, Arc): - arcs.append(sketch_arc_to_grpc_arc(edge, plane)) - - return (segments, arcs) - - -def sketch_arc_to_grpc_arc(arc: Arc, plane: Plane) -> GRPCArc: - """Convert an ``Arc`` class to an arc gRPC message. - - Parameters - ---------- - arc : Arc - Source arc data. - plane : Plane - Plane for positioning the arc within. - - Returns - ------- - GRPCArc - Geometry service gRPC arc message. The unit is meters. - """ - axis = ( - unit_vector_to_grpc_direction(plane.direction_z) - if not arc.is_clockwise - else unit_vector_to_grpc_direction(-plane.direction_z) - ) - - return GRPCArc( - center=point2d_to_grpc_point(plane, arc.center), - start=point2d_to_grpc_point(plane, arc.start), - end=point2d_to_grpc_point(plane, arc.end), - axis=axis, - ) - - -def sketch_ellipse_to_grpc_ellipse(ellipse: SketchEllipse, plane: Plane) -> GRPCEllipse: - """Convert a ``SketchEllipse`` class to an ellipse gRPC message. - - Parameters - ---------- - ellipse : SketchEllipse - Source ellipse data. - - Returns - ------- - GRPCEllipse - Geometry service gRPC ellipse message. The unit is meters. - """ - return GRPCEllipse( - center=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), - ) - - -def sketch_circle_to_grpc_circle(circle: SketchCircle, plane: Plane) -> GRPCCircle: - """Convert a ``SketchCircle`` class to a circle gRPC message. - - Parameters - ---------- - circle : SketchCircle - Source circle data. - plane : Plane - Plane for positioning the circle. - - Returns - ------- - GRPCCircle - Geometry service gRPC circle message. The unit is meters. - """ - return GRPCCircle( - center=point2d_to_grpc_point(plane, circle.center), - radius=circle.radius.m_as(DEFAULT_UNITS.SERVER_LENGTH), - ) - - -def point3d_to_grpc_point(point: Point3D) -> GRPCPoint: - """Convert a ``Point3D`` class to a point gRPC message. - - Parameters - ---------- - point : Point3D - Source point data. - - Returns - ------- - GRPCPoint - Geometry service gRPC point message. The unit is meters. - """ - return GRPCPoint( - x=point.x.m_as(DEFAULT_UNITS.SERVER_LENGTH), - y=point.y.m_as(DEFAULT_UNITS.SERVER_LENGTH), - z=point.z.m_as(DEFAULT_UNITS.SERVER_LENGTH), - ) - - -def grpc_point_to_point3d(point: GRPCPoint) -> Point3D: - """Convert a point gRPC message class to a ``Point3D`` class. - - Parameters - ---------- - point : GRPCPoint - Source point data. - - Returns - ------- - Point3D - Converted point. - """ - return Point3D( - [point.x, point.y, point.z], - DEFAULT_UNITS.SERVER_LENGTH, - ) - - -def point2d_to_grpc_point(plane: Plane, point2d: Point2D) -> GRPCPoint: - """Convert a ``Point2D`` class to a point gRPC message. - - Parameters - ---------- - plane : Plane - Plane for positioning the 2D point. - point : Point2D - Source point data. - - Returns - ------- - GRPCPoint - Geometry service gRPC point message. The unit is meters. - """ - 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), - ) - - -def sketch_polygon_to_grpc_polygon(polygon: Polygon, plane: Plane) -> GRPCPolygon: - """Convert a ``Polygon`` class to a polygon gRPC message. - - Parameters - ---------- - polygon : Polygon - Source polygon data. - - Returns - ------- - GRPCPolygon - Geometry service gRPC polygon message. The unit is meters. - """ - return GRPCPolygon( - center=point2d_to_grpc_point(plane, polygon.center), - radius=polygon.inner_radius.m_as(DEFAULT_UNITS.SERVER_LENGTH), - numberofsides=polygon.n_sides, - angle=polygon.angle.m_as(DEFAULT_UNITS.SERVER_ANGLE), - ) - - -def sketch_segment_to_grpc_line(segment: SketchSegment, plane: Plane) -> GRPCLine: - """Convert a ``Segment`` class to a line gRPC message. - - Parameters - ---------- - segment : SketchSegment - Source segment data. - - Returns - ------- - GRPCLine - Geometry service gRPC line message. The unit is meters. - """ - return GRPCLine( - start=point2d_to_grpc_point(plane, segment.start), - end=point2d_to_grpc_point(plane, segment.end), - ) - - -def grpc_matrix_to_matrix(m: GRPCMatrix) -> Matrix44: - """Convert an ``ansys.api.geometry.Matrix`` to a ``Matrix44``.""" - import numpy as np - - return Matrix44( - np.round( - [ - [m.m00, m.m01, m.m02, m.m03], - [m.m10, m.m11, m.m12, m.m13], - [m.m20, m.m21, m.m22, m.m23], - [m.m30, m.m31, m.m32, m.m33], - ], - 8, - ) - ) - - -def grpc_frame_to_frame(frame: GRPCFrame) -> Frame: - """Convert a frame gRPC message to a ``Frame`` class. - - Parameters - ---------- - GRPCFrame - Geometry service gRPC frame message. The unit for the frame origin is meters. - - Returns - ------- - frame : Frame - Resulting converted frame. - """ - return Frame( - Point3D( - [ - frame.origin.x, - frame.origin.y, - frame.origin.z, - ], - DEFAULT_UNITS.SERVER_LENGTH, - ), - UnitVector3D( - [ - frame.dir_x.x, - frame.dir_x.y, - frame.dir_x.z, - ] - ), - UnitVector3D( - [ - frame.dir_y.x, - frame.dir_y.y, - frame.dir_y.z, - ] - ), - ) - - -def grpc_surface_to_surface(surface: GRPCSurface, surface_type: "SurfaceType") -> Surface: - """Convert a surface gRPC message to a ``Surface`` class. - - Parameters - ---------- - surface : GRPCSurface - Geometry service gRPC surface message. - - Returns - ------- - Surface - Resulting converted surface. - """ - from ansys.geometry.core.designer.face import SurfaceType - - origin = Point3D( - [surface.origin.x, surface.origin.y, surface.origin.z], DEFAULT_UNITS.SERVER_LENGTH - ) - axis = UnitVector3D([surface.axis.x, surface.axis.y, surface.axis.z]) - reference = UnitVector3D([surface.reference.x, surface.reference.y, surface.reference.z]) - - if surface_type == SurfaceType.SURFACETYPE_CONE: - result = Cone(origin, surface.radius, surface.half_angle, reference, axis) - elif surface_type == SurfaceType.SURFACETYPE_CYLINDER: - result = Cylinder(origin, surface.radius, reference, axis) - elif surface_type == SurfaceType.SURFACETYPE_SPHERE: - result = Sphere(origin, surface.radius, reference, axis) - elif surface_type == SurfaceType.SURFACETYPE_TORUS: - result = Torus(origin, surface.major_radius, surface.minor_radius, reference, axis) - elif surface_type == SurfaceType.SURFACETYPE_PLANE: - result = PlaneSurface(origin, reference, axis) - else: - result = None - return result - - -def grpc_curve_to_curve(curve: GRPCCurve) -> Curve: - """Convert a curve gRPC message to a ``Curve``. - - Parameters - ---------- - curve : GRPCCurve - Geometry service gRPC curve message. - - Returns - ------- - Curve - Resulting converted curve. - """ - origin = Point3D([curve.origin.x, curve.origin.y, curve.origin.z]) - try: - reference = UnitVector3D([curve.reference.x, curve.reference.y, curve.reference.z]) - axis = UnitVector3D([curve.axis.x, curve.axis.y, curve.axis.z]) - except ValueError: - # curve will be a line - pass - if curve.radius != 0: - result = Circle(origin, curve.radius, reference, axis) - elif curve.major_radius != 0 and curve.minor_radius != 0: - result = Ellipse(origin, curve.major_radius, curve.minor_radius, reference, axis) - elif curve.direction is not None: - result = Line( - origin, - UnitVector3D( - [ - curve.direction.x, - curve.direction.y, - curve.direction.z, - ] - ), - ) - else: - result = None - - return result - - -def curve_to_grpc_curve(curve: Curve) -> GRPCCurve: - """Convert a ``Curve`` object to a curve gRPC message. - - Parameters - ---------- - curve : Curve - Curve to convert. - - Returns - ------- - GRPCCurve - Return ``Curve`` as a ``ansys.api.geometry.CurveGeometry`` message. - """ - grpc_curve = None - origin = point3d_to_grpc_point(curve.origin) - - if isinstance(curve, Line): - direction = unit_vector_to_grpc_direction(curve.direction) - grpc_curve = GRPCCurve(origin=origin, direction=direction) - else: - reference = unit_vector_to_grpc_direction(curve.dir_x) - axis = unit_vector_to_grpc_direction(curve.dir_z) - - if isinstance(curve, Circle): - grpc_curve = GRPCCurve( - origin=origin, reference=reference, axis=axis, radius=curve.radius.m - ) - elif isinstance(curve, Ellipse): - grpc_curve = GRPCCurve( - origin=origin, - reference=reference, - axis=axis, - major_radius=curve.major_radius.m, - minor_radius=curve.minor_radius.m, - ) - - return grpc_curve - - -def trimmed_curve_to_grpc_trimmed_curve(curve: "TrimmedCurve") -> GRPCTrimmedCurve: - """Convert a ``TrimmedCurve`` to a trimmed curve gRPC message. - - Parameters - ---------- - curve : TrimmedCurve - Curve to convert. - - Returns - ------- - GRPCTrimmedCurve - Geometry service gRPC ``TrimmedCurve`` message. - """ - curve_geometry = curve_to_grpc_curve(curve.geometry) - i_start = curve.interval.start - i_end = curve.interval.end - - return GRPCTrimmedCurve( - curve=curve_geometry, - interval_start=i_start, - interval_end=i_end, - ) - - -def line_to_grpc_line(line: Line) -> GRPCLine: - """Convert a ``Line`` to a line gRPC message. - - Parameters - ---------- - line : Line - Line to convert. - - Returns - ------- - GRPCLine - Geometry service gRPC ``Line`` message. - """ - start = line.origin - end = line.origin + line.direction - return GRPCLine(start=point3d_to_grpc_point(start), end=point3d_to_grpc_point(end)) - - -def grpc_material_to_material(material: GRPCMaterial) -> Material: - """Convert a material gRPC message to a ``Material`` class. - - Parameters - ---------- - material : GRPCMaterial - Material gRPC message. - - Returns - ------- - Material - Converted material. - """ - properties = [] - density = Quantity(0, UNITS.kg / UNITS.m**3) - for property in material.material_properties: - mp = grpc_material_property_to_material_property(property) - properties.append(mp) - if mp.type == MaterialPropertyType.DENSITY: - density = mp.quantity - - return Material(material.name, density, properties) - - -def grpc_material_property_to_material_property( - material_property: GRPCMaterialProperty, -) -> MaterialProperty: - """Convert a material property gRPC message to a ``MaterialProperty`` class. - - Parameters - ---------- - material_property : GRPCMaterialProperty - Material property gRPC message. - - Returns - ------- - MaterialProperty - Converted material property. - """ - try: - mp_type = MaterialPropertyType.from_id(material_property.id) - except ValueError: - mp_type = material_property.id - - try: - mp_quantity = Quantity(material_property.value, material_property.units) - except ( - UndefinedUnitError, - TypeError, - ): - mp_quantity = material_property.value - - return MaterialProperty(mp_type, material_property.display_name, mp_quantity) diff --git a/src/ansys/geometry/core/designer/selection.py b/src/ansys/geometry/core/designer/selection.py index abbc6908ff..f326c961d8 100644 --- a/src/ansys/geometry/core/designer/selection.py +++ b/src/ansys/geometry/core/designer/selection.py @@ -24,7 +24,6 @@ from typing import TYPE_CHECKING from ansys.geometry.core.connection.client import GrpcClient -from ansys.geometry.core.connection.conversions import grpc_point_to_point3d from ansys.geometry.core.designer.beam import Beam from ansys.geometry.core.designer.body import Body from ansys.geometry.core.designer.component import Component @@ -204,7 +203,7 @@ def design_points(self) -> list[DesignPoint]: if self._design_points is None: # Get all design points from the named selection self._design_points = [ - DesignPoint(dp_id, f"dp: {dp_id}", grpc_point_to_point3d(dp_point)) + DesignPoint(dp_id, f"dp: {dp_id}", dp_point) for dp_id, dp_point in self._ids_cached["design_points"] ] diff --git a/tests/integration/test_trimmed_geometry.py b/tests/integration/test_trimmed_geometry.py index 9b0d968f29..8a47892f3c 100644 --- a/tests/integration/test_trimmed_geometry.py +++ b/tests/integration/test_trimmed_geometry.py @@ -26,7 +26,6 @@ import pytest from ansys.geometry.core.connection import BackendType -from ansys.geometry.core.designer.design import Design from ansys.geometry.core.designer.face import SurfaceType from ansys.geometry.core.math import Point3D, UnitVector3D from ansys.geometry.core.math.point import Point2D @@ -42,6 +41,7 @@ ) from ansys.geometry.core.sketch.sketch import Sketch + def create_hedgehog(modeler: Modeler): """A helper function that creates the Hedgehog model.""" design = modeler.create_design("Hedgehog") diff --git a/tests/test_connection.py b/tests/test_connection.py index 11f097a907..4becf08420 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -26,24 +26,10 @@ from beartype.roar import BeartypeCallHintParamViolation import grpc -import numpy as np -from pint import Quantity import pytest from ansys.geometry.core.connection.backend import ApiVersions, BackendType from ansys.geometry.core.connection.client import GrpcClient, wait_until_healthy -from ansys.geometry.core.connection.conversions import ( - frame_to_grpc_frame, - plane_to_grpc_plane, - point2d_to_grpc_point, - point3d_to_grpc_point, - sketch_arc_to_grpc_arc, - sketch_circle_to_grpc_circle, - sketch_ellipse_to_grpc_ellipse, - sketch_polygon_to_grpc_polygon, - sketch_segment_to_grpc_line, - unit_vector_to_grpc_direction, -) from ansys.geometry.core.connection.product_instance import ( ProductInstance, _check_minimal_versions, @@ -53,9 +39,6 @@ _manifest_path_provider, prepare_and_start_backend, ) -from ansys.geometry.core.math import Frame, Plane, Point2D, Point3D, UnitVector3D -from ansys.geometry.core.misc import UNITS, Angle -from ansys.geometry.core.sketch import Arc, Polygon, SketchCircle, SketchEllipse, SketchSegment def test_wait_until_healthy(): @@ -78,257 +61,6 @@ def test_invalid_inputs(): GrpcClient(timeout="a") -def test_circle_message_conversion(): - """Test conversion between :class: `SketchCircle - ` and expected gRPC message - type. - """ - circle = SketchCircle( - Point2D([10, 100], UNITS.mm), - Quantity(300, UNITS.mm), - ) - grpc_circle_message = sketch_circle_to_grpc_circle( - circle, Plane(Point3D([10, 100, 1000], UNITS.mm)) - ) - - assert grpc_circle_message.center.x == 0.02 - assert grpc_circle_message.center.y == 0.2 - assert grpc_circle_message.center.z == 1.0 - assert grpc_circle_message.radius == 0.3 - - -def test_ellipse_message_conversion(): - """Test conversion between :class:`SketchEllipse < - ansys.geometry.core.sketch.ellipse.SketchEllipse>` and expected gRPC - message type. - """ - ellipse = SketchEllipse( - Point2D([10, 100], UNITS.mm), - Quantity(300, UNITS.mm), - Quantity(50, UNITS.mm), - ) - grpc_ellipse_message = sketch_ellipse_to_grpc_ellipse( - ellipse, Plane(Point3D([10, 100, 1000], UNITS.mm)) - ) - - assert grpc_ellipse_message.center.x == 0.02 - assert grpc_ellipse_message.center.y == 0.2 - assert grpc_ellipse_message.center.z == 1.0 - assert grpc_ellipse_message.majorradius == 0.3 - assert grpc_ellipse_message.minorradius == 0.05 - assert grpc_ellipse_message.angle == 0 - - rotated_ellipse = SketchEllipse( - Point2D([10, 100], UNITS.mm), - Quantity(300, UNITS.mm), - Quantity(50, UNITS.mm), - angle=Angle(45, UNITS.degrees), - ) - rotated_grpc_ellipse_message = sketch_ellipse_to_grpc_ellipse( - rotated_ellipse, Plane(Point3D([10, 100, 1000], UNITS.mm)) - ) - - assert rotated_grpc_ellipse_message.center.x == 0.02 - assert rotated_grpc_ellipse_message.center.y == 0.2 - assert rotated_grpc_ellipse_message.center.z == 1.0 - assert rotated_grpc_ellipse_message.majorradius == 0.3 - assert rotated_grpc_ellipse_message.minorradius == 0.05 - assert rotated_grpc_ellipse_message.angle == np.pi / 4 - - -def test_segment_message_conversion(): - """Test conversion between :class: `SketchSegment - ` and expected gRPC - message type. - """ - segment = SketchSegment( - Point2D([30, 400], UNITS.mm), - Point2D([500, 600], UNITS.mm), - ) - grpc_line_message = sketch_segment_to_grpc_line( - segment, Plane(Point3D([10, 100, 1000], UNITS.mm)) - ) - - assert grpc_line_message.start.x == 0.04 - assert grpc_line_message.start.y == 0.5 - assert grpc_line_message.start.z == 1.0 - assert grpc_line_message.end.x == 0.51 - assert grpc_line_message.end.y == pytest.approx(0.7000000000000001, 1e-8, 1e-8) - assert grpc_line_message.end.z == 1.0 - - -def test_polygon_message_conversion(): - """Test conversion between :class:`Polygon - ` and expected gRPC message - type. - """ - polygon = Polygon( - Point2D([10, 100], UNITS.mm), - Quantity(300, UNITS.mm), - 5, - ) - grpc_polygon_message = sketch_polygon_to_grpc_polygon( - polygon, Plane(Point3D([10, 100, 1000], UNITS.mm)) - ) - - assert grpc_polygon_message.center.x == 0.02 - assert grpc_polygon_message.center.y == 0.2 - assert grpc_polygon_message.center.z == 1.0 - assert grpc_polygon_message.radius == 0.3 - assert grpc_polygon_message.numberofsides == 5 - assert grpc_polygon_message.angle == 0 - - rotated_polygon = Polygon( - Point2D([10, 100], UNITS.mm), Quantity(300, UNITS.mm), 5, angle=Angle(45, UNITS.degrees) - ) - rotated_grpc_polygon_message = sketch_polygon_to_grpc_polygon( - rotated_polygon, Plane(Point3D([10, 100, 1000], UNITS.mm)) - ) - - assert rotated_grpc_polygon_message.center.x == 0.02 - assert rotated_grpc_polygon_message.center.y == 0.2 - assert rotated_grpc_polygon_message.center.z == 1.0 - assert rotated_grpc_polygon_message.radius == 0.3 - assert rotated_grpc_polygon_message.numberofsides == 5 - assert rotated_grpc_polygon_message.angle == np.pi / 4 - - -def test_point3d_message_conversion(): - """Test conversion between :class:`Point3D - ` and expected gRPC message - type. - """ - point = Point3D([10, 100, 1000], UNITS.mm) - grpc_point_message = point3d_to_grpc_point(point) - - assert grpc_point_message.x == 0.01 - assert grpc_point_message.y == 0.1 - assert grpc_point_message.z == 1.0 - - -def test_point2d_message_conversion(): - """Test conversion between :class:`Point2D` and expected gRPC message - type. - """ - point = Point2D([10, 100], UNITS.mm) - grpc_point_message = point2d_to_grpc_point(Plane(Point3D([10, 100, 1000], UNITS.mm)), point) - - assert grpc_point_message.x == 0.02 - assert grpc_point_message.y == 0.2 - assert grpc_point_message.z == 1.0 - - -def test_unit_vector_message_conversion(): - """Test conversion between :class:`UnitVector3D - ` and expected gRPC message - type. - """ - unit_vector = UnitVector3D([1, 1, 1]) - grpc_unit_vector_message = unit_vector_to_grpc_direction(unit_vector) - - assert grpc_unit_vector_message.x == 0.5773502691896258 - assert grpc_unit_vector_message.y == 0.5773502691896258 - assert grpc_unit_vector_message.z == 0.5773502691896258 - - -def test_arc_message_conversion(): - """Test conversion between :class:`Arc - ` and expected gRPC message type. - """ - arc = Arc( - Point2D([100, 400], UNITS.mm), - Point2D([900, 800], UNITS.mm), - Point2D([500, 600], UNITS.mm), - ) - grpc_arc_message = sketch_arc_to_grpc_arc(arc, Plane(Point3D([10, 100, 1000], UNITS.mm))) - - assert grpc_arc_message.center.x == 0.51 - assert grpc_arc_message.center.y == pytest.approx(0.7000000000000001, 1e-8, 1e-8) - assert grpc_arc_message.center.z == 1.0 - - assert grpc_arc_message.start.x == 0.11 - assert grpc_arc_message.start.y == 0.5 - assert grpc_arc_message.start.z == 1.0 - - assert grpc_arc_message.end.x == 0.91 - assert grpc_arc_message.end.y == 0.9 - assert grpc_arc_message.end.z == 1.0 - - assert grpc_arc_message.axis.x == 0 - assert grpc_arc_message.axis.y == 0 - assert grpc_arc_message.axis.z == 1 - - arc2 = Arc( - Point2D([200, 500], UNITS.mm), - Point2D([1000, 900], UNITS.mm), - Point2D([600, 700], UNITS.mm), - True, - ) - grpc_arc_message2 = sketch_arc_to_grpc_arc(arc2, Plane(Point3D([10, 100, 1000], UNITS.mm))) - - assert grpc_arc_message2.center.x == 0.61 - assert grpc_arc_message2.center.y == pytest.approx(0.8000000000000002, 1e-8, 1e-8) - assert grpc_arc_message2.center.z == 1.0 - - assert grpc_arc_message2.start.x == pytest.approx(0.21000000000000002, 1e-8, 1e-8) - assert grpc_arc_message2.start.y == 0.6 - assert grpc_arc_message2.start.z == 1.0 - - assert grpc_arc_message2.end.x == 1.01 - assert grpc_arc_message2.end.y == 1.0 - assert grpc_arc_message2.end.z == 1.0 - - assert grpc_arc_message2.axis.x == 0 - assert grpc_arc_message2.axis.y == 0 - assert grpc_arc_message2.axis.z == -1 - - -def test_plane_message_conversion(): - """Test conversion between :class:`Plane - ` and expected gRPC message type. - """ - plane = Plane( - Point3D([10, 200, 3000], UNITS.mm), UnitVector3D([1, 1, 0]), UnitVector3D([1, -1, 0]) - ) - grpc_plane_message = plane_to_grpc_plane(plane) - - assert grpc_plane_message.frame.origin.x == 0.01 - assert grpc_plane_message.frame.origin.y == 0.2 - assert grpc_plane_message.frame.origin.z == 3.0 - - assert grpc_plane_message.frame.dir_x.x == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) - assert grpc_plane_message.frame.dir_x.y == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) - assert grpc_plane_message.frame.dir_x.z == 0.0 - - assert grpc_plane_message.frame.dir_y.x == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) - assert grpc_plane_message.frame.dir_y.y == pytest.approx( - -0.7071067811865475, rel=1e-7, abs=1e-8 - ) - assert grpc_plane_message.frame.dir_y.z == 0.0 - - -def test_frame_message_conversion(): - """Test conversion between :class:`Frame - ` and expected gRPC message type. - """ - frame = Frame( - Point3D([10, 200, 3000], UNITS.mm), UnitVector3D([1, 1, 0]), UnitVector3D([1, -1, 0]) - ) - grpc_frame_message = frame_to_grpc_frame(frame) - - assert grpc_frame_message.origin.x == 0.01 - assert grpc_frame_message.origin.y == 0.2 - assert grpc_frame_message.origin.z == 3.0 - - assert grpc_frame_message.dir_x.x == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) - assert grpc_frame_message.dir_x.y == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) - assert grpc_frame_message.dir_x.z == 0.0 - - assert grpc_frame_message.dir_y.x == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) - assert grpc_frame_message.dir_y.y == pytest.approx(-0.7071067811865475, rel=1e-7, abs=1e-8) - assert grpc_frame_message.dir_y.z == 0.0 - - def test_api_versions_reader(): """Checks that the API versions are read correctly for various kinds of input.""" # Read from a string diff --git a/tests/test_conversions.py b/tests/test_v0_conversions.py similarity index 71% rename from tests/test_conversions.py rename to tests/test_v0_conversions.py index 39a0aa1587..9b1ee190b3 100644 --- a/tests/test_conversions.py +++ b/tests/test_v0_conversions.py @@ -30,39 +30,51 @@ Surface as GRPCSurface, SurfaceType as GRPCSurfaceType, ) +import numpy as np from pint import Quantity import pytest from ansys.geometry.core._grpc._services.v0.conversions import ( from_curve_to_grpc_curve, from_design_file_format_to_grpc_part_export_format, + from_frame_to_grpc_frame, from_grpc_backend_type_to_backend_type, from_grpc_curve_to_curve, from_grpc_material_property_to_material_property, + from_grpc_material_to_material, from_grpc_surface_to_surface, + from_plane_to_grpc_plane, + from_point2d_to_grpc_point, + from_point3d_to_grpc_point, + from_sketch_arc_to_grpc_arc, + from_sketch_circle_to_grpc_circle, + from_sketch_ellipse_to_grpc_ellipse, + from_sketch_polygon_to_grpc_polygon, + from_sketch_segment_to_grpc_line, from_sketch_shapes_to_grpc_geometries, from_surface_to_grpc_surface, from_tess_options_to_grpc_tess_options, + from_unit_vector_to_grpc_direction, ) from ansys.geometry.core.connection.backend import BackendType -from ansys.geometry.core.connection.conversions import ( - curve_to_grpc_curve, - grpc_curve_to_curve, - grpc_material_property_to_material_property, - grpc_material_to_material, - grpc_surface_to_surface, - sketch_shapes_to_grpc_geometries, -) from ansys.geometry.core.designer.design import DesignFileFormat from ansys.geometry.core.designer.face import SurfaceType from ansys.geometry.core.materials.material import MaterialPropertyType -from ansys.geometry.core.math import Plane, Point2D, Point3D, UnitVector3D -from ansys.geometry.core.misc.measurements import DEFAULT_UNITS, UNITS +from ansys.geometry.core.math.frame import Frame +from ansys.geometry.core.math.plane import Plane +from ansys.geometry.core.math.point import Point2D, Point3D +from ansys.geometry.core.math.vector import UnitVector3D +from ansys.geometry.core.misc.measurements import DEFAULT_UNITS, Angle from ansys.geometry.core.misc.options import TessellationOptions -from ansys.geometry.core.shapes.curves import Circle, Curve, Ellipse, Line -from ansys.geometry.core.shapes.curves.nurbs import NURBSCurve +from ansys.geometry.core.misc.units import UNITS +from ansys.geometry.core.shapes.curves import Circle, Curve, Ellipse, Line, NURBSCurve from ansys.geometry.core.shapes.surfaces import Cone, Cylinder, PlaneSurface, Sphere, Torus -from ansys.geometry.core.sketch import Arc, Polygon, SketchCircle, SketchEdge, SketchEllipse +from ansys.geometry.core.sketch.arc import Arc +from ansys.geometry.core.sketch.circle import SketchCircle +from ansys.geometry.core.sketch.edge import SketchEdge +from ansys.geometry.core.sketch.ellipse import SketchEllipse +from ansys.geometry.core.sketch.polygon import Polygon +from ansys.geometry.core.sketch.segment import SketchSegment def test_sketch_shapes_to_grpc_geometries_multiple_faces(): @@ -75,7 +87,7 @@ def test_sketch_shapes_to_grpc_geometries_multiple_faces(): faces = [circle, ellipse, polygon] # Call the function - geometries = sketch_shapes_to_grpc_geometries(plane, edges=[], faces=faces) + geometries = from_sketch_shapes_to_grpc_geometries(plane, edges=[], faces=faces) # Assert that the circle, ellipse, and polygon were converted and added assert len(geometries.circles) == 1 @@ -91,7 +103,7 @@ def test_sketch_shapes_to_grpc_geometries_one_curve_lines(): edges = [edge] # Call the function with only_one_curve=True - one_curve_geometry = sketch_shapes_to_grpc_geometries( + one_curve_geometry = from_sketch_shapes_to_grpc_geometries( plane, edges=edges, faces=[], only_one_curve=True ) @@ -107,7 +119,7 @@ def test_sketch_shapes_to_grpc_geometries_one_curve_arcs(): edges = [arc] # Call the function with only_one_curve=True - one_curve_geometry = sketch_shapes_to_grpc_geometries( + one_curve_geometry = from_sketch_shapes_to_grpc_geometries( plane, edges=edges, faces=[], only_one_curve=True ) @@ -123,7 +135,7 @@ def test_sketch_shapes_to_grpc_geometries_one_curve_circles(): faces = [circle] # Call the function with only_one_curve=True - one_curve_geometry = sketch_shapes_to_grpc_geometries( + one_curve_geometry = from_sketch_shapes_to_grpc_geometries( plane, edges=[], faces=faces, only_one_curve=True ) @@ -141,7 +153,7 @@ def test_sketch_shapes_to_grpc_geometries_one_curve_ellipses(): faces = [ellipse] # Call the function with only_one_curve=True - one_curve_geometry = sketch_shapes_to_grpc_geometries( + one_curve_geometry = from_sketch_shapes_to_grpc_geometries( plane, edges=[], faces=faces, only_one_curve=True ) @@ -164,7 +176,7 @@ def test_sketch_shapes_to_grpc_geometries_one_curve_polygons(): faces = [polygon] # Call the function with only_one_curve=True - one_curve_geometry = sketch_shapes_to_grpc_geometries( + one_curve_geometry = from_sketch_shapes_to_grpc_geometries( plane, edges=[], faces=faces, only_one_curve=True ) @@ -266,7 +278,7 @@ def test_grpc_surface_to_surface(surface_type, surface_data, expected_type): )() # Call the function - result = grpc_surface_to_surface(surface, surface_type) + result = from_grpc_surface_to_surface(surface, surface_type) # Assert the result type if expected_type is None: @@ -339,7 +351,7 @@ def test_grpc_curve_to_curve(curve_data, expected_type, expected_attributes): ) # Call the function - result = grpc_curve_to_curve(grpc_curve) + result = from_grpc_curve_to_curve(grpc_curve) # Assert the result type if expected_type is None: @@ -367,7 +379,7 @@ def test_curve_to_grpc_curve_ellipse(): ellipse = Ellipse(origin, major_radius, minor_radius, reference, axis) # Call the function - grpc_curve = curve_to_grpc_curve(ellipse) + grpc_curve = from_curve_to_grpc_curve(ellipse) # Assert the GRPCCurve attributes assert grpc_curve.origin.x == origin.x.m_as(UNITS.m) @@ -406,7 +418,7 @@ def test_grpc_material_to_material(): ) # Call the function - material = grpc_material_to_material(grpc_material) + material = from_grpc_material_to_material(grpc_material) # Assert the Material attributes assert material.name == "Steel" @@ -427,7 +439,7 @@ def test_grpc_material_property_to_material_property_invalid_units(): ) # Call the function - material_property = grpc_material_property_to_material_property(invalid_units_property) + material_property = from_grpc_material_property_to_material_property(invalid_units_property) # Assert fallback logic assert material_property.type == "Density" @@ -785,3 +797,256 @@ def test_from_design_file_format_to_grpc_part_export_format_none(): # Assert that the result is None assert export_format is None + + +def test_circle_message_conversion(): + """Test conversion between :class: `SketchCircle + ` and expected gRPC message + type. + """ + circle = SketchCircle( + Point2D([10, 100], UNITS.mm), + Quantity(300, UNITS.mm), + ) + grpc_circle_message = from_sketch_circle_to_grpc_circle( + circle, Plane(Point3D([10, 100, 1000], UNITS.mm)) + ) + + assert grpc_circle_message.center.x == 0.02 + assert grpc_circle_message.center.y == 0.2 + assert grpc_circle_message.center.z == 1.0 + assert grpc_circle_message.radius == 0.3 + + +def test_ellipse_message_conversion(): + """Test conversion between :class:`SketchEllipse < + ansys.geometry.core.sketch.ellipse.SketchEllipse>` and expected gRPC + message type. + """ + ellipse = SketchEllipse( + Point2D([10, 100], UNITS.mm), + Quantity(300, UNITS.mm), + Quantity(50, UNITS.mm), + ) + grpc_ellipse_message = from_sketch_ellipse_to_grpc_ellipse( + ellipse, Plane(Point3D([10, 100, 1000], UNITS.mm)) + ) + + assert grpc_ellipse_message.center.x == 0.02 + assert grpc_ellipse_message.center.y == 0.2 + assert grpc_ellipse_message.center.z == 1.0 + assert grpc_ellipse_message.majorradius == 0.3 + assert grpc_ellipse_message.minorradius == 0.05 + assert grpc_ellipse_message.angle == 0 + + rotated_ellipse = SketchEllipse( + Point2D([10, 100], UNITS.mm), + Quantity(300, UNITS.mm), + Quantity(50, UNITS.mm), + angle=Angle(45, UNITS.degrees), + ) + rotated_grpc_ellipse_message = from_sketch_ellipse_to_grpc_ellipse( + rotated_ellipse, Plane(Point3D([10, 100, 1000], UNITS.mm)) + ) + + assert rotated_grpc_ellipse_message.center.x == 0.02 + assert rotated_grpc_ellipse_message.center.y == 0.2 + assert rotated_grpc_ellipse_message.center.z == 1.0 + assert rotated_grpc_ellipse_message.majorradius == 0.3 + assert rotated_grpc_ellipse_message.minorradius == 0.05 + assert rotated_grpc_ellipse_message.angle == np.pi / 4 + + +def test_segment_message_conversion(): + """Test conversion between :class: `SketchSegment + ` and expected gRPC + message type. + """ + segment = SketchSegment( + Point2D([30, 400], UNITS.mm), + Point2D([500, 600], UNITS.mm), + ) + grpc_line_message = from_sketch_segment_to_grpc_line( + segment, Plane(Point3D([10, 100, 1000], UNITS.mm)) + ) + + assert grpc_line_message.start.x == 0.04 + assert grpc_line_message.start.y == 0.5 + assert grpc_line_message.start.z == 1.0 + assert grpc_line_message.end.x == 0.51 + assert grpc_line_message.end.y == pytest.approx(0.7000000000000001, 1e-8, 1e-8) + assert grpc_line_message.end.z == 1.0 + + +def test_polygon_message_conversion(): + """Test conversion between :class:`Polygon + ` and expected gRPC message + type. + """ + polygon = Polygon( + Point2D([10, 100], UNITS.mm), + Quantity(300, UNITS.mm), + 5, + ) + grpc_polygon_message = from_sketch_polygon_to_grpc_polygon( + polygon, Plane(Point3D([10, 100, 1000], UNITS.mm)) + ) + + assert grpc_polygon_message.center.x == 0.02 + assert grpc_polygon_message.center.y == 0.2 + assert grpc_polygon_message.center.z == 1.0 + assert grpc_polygon_message.radius == 0.3 + assert grpc_polygon_message.numberofsides == 5 + assert grpc_polygon_message.angle == 0 + + rotated_polygon = Polygon( + Point2D([10, 100], UNITS.mm), Quantity(300, UNITS.mm), 5, angle=Angle(45, UNITS.degrees) + ) + rotated_grpc_polygon_message = from_sketch_polygon_to_grpc_polygon( + rotated_polygon, Plane(Point3D([10, 100, 1000], UNITS.mm)) + ) + + assert rotated_grpc_polygon_message.center.x == 0.02 + assert rotated_grpc_polygon_message.center.y == 0.2 + assert rotated_grpc_polygon_message.center.z == 1.0 + assert rotated_grpc_polygon_message.radius == 0.3 + assert rotated_grpc_polygon_message.numberofsides == 5 + assert rotated_grpc_polygon_message.angle == np.pi / 4 + + +def test_point3d_message_conversion(): + """Test conversion between :class:`Point3D + ` and expected gRPC message + type. + """ + point = Point3D([10, 100, 1000], UNITS.mm) + grpc_point_message = from_point3d_to_grpc_point(point) + + assert grpc_point_message.x == 0.01 + assert grpc_point_message.y == 0.1 + assert grpc_point_message.z == 1.0 + + +def test_point2d_message_conversion(): + """Test conversion between :class:`Point2D` and expected gRPC message + type. + """ + point = Point2D([10, 100], UNITS.mm) + grpc_point_message = from_point2d_to_grpc_point( + Plane(Point3D([10, 100, 1000], UNITS.mm)), point + ) + + assert grpc_point_message.x == 0.02 + assert grpc_point_message.y == 0.2 + assert grpc_point_message.z == 1.0 + + +def test_unit_vector_message_conversion(): + """Test conversion between :class:`UnitVector3D + ` and expected gRPC message + type. + """ + unit_vector = UnitVector3D([1, 1, 1]) + grpc_unit_vector_message = from_unit_vector_to_grpc_direction(unit_vector) + + assert grpc_unit_vector_message.x == 0.5773502691896258 + assert grpc_unit_vector_message.y == 0.5773502691896258 + assert grpc_unit_vector_message.z == 0.5773502691896258 + + +def test_arc_message_conversion(): + """Test conversion between :class:`Arc + ` and expected gRPC message type. + """ + arc = Arc( + Point2D([100, 400], UNITS.mm), + Point2D([900, 800], UNITS.mm), + Point2D([500, 600], UNITS.mm), + ) + grpc_arc_message = from_sketch_arc_to_grpc_arc(arc, Plane(Point3D([10, 100, 1000], UNITS.mm))) + + assert grpc_arc_message.center.x == 0.51 + assert grpc_arc_message.center.y == pytest.approx(0.7000000000000001, 1e-8, 1e-8) + assert grpc_arc_message.center.z == 1.0 + + assert grpc_arc_message.start.x == 0.11 + assert grpc_arc_message.start.y == 0.5 + assert grpc_arc_message.start.z == 1.0 + + assert grpc_arc_message.end.x == 0.91 + assert grpc_arc_message.end.y == 0.9 + assert grpc_arc_message.end.z == 1.0 + + assert grpc_arc_message.axis.x == 0 + assert grpc_arc_message.axis.y == 0 + assert grpc_arc_message.axis.z == 1 + + arc2 = Arc( + Point2D([200, 500], UNITS.mm), + Point2D([1000, 900], UNITS.mm), + Point2D([600, 700], UNITS.mm), + True, + ) + grpc_arc_message2 = from_sketch_arc_to_grpc_arc(arc2, Plane(Point3D([10, 100, 1000], UNITS.mm))) + + assert grpc_arc_message2.center.x == 0.61 + assert grpc_arc_message2.center.y == pytest.approx(0.8000000000000002, 1e-8, 1e-8) + assert grpc_arc_message2.center.z == 1.0 + + assert grpc_arc_message2.start.x == pytest.approx(0.21000000000000002, 1e-8, 1e-8) + assert grpc_arc_message2.start.y == 0.6 + assert grpc_arc_message2.start.z == 1.0 + + assert grpc_arc_message2.end.x == 1.01 + assert grpc_arc_message2.end.y == 1.0 + assert grpc_arc_message2.end.z == 1.0 + + assert grpc_arc_message2.axis.x == 0 + assert grpc_arc_message2.axis.y == 0 + assert grpc_arc_message2.axis.z == -1 + + +def test_plane_message_conversion(): + """Test conversion between :class:`Plane + ` and expected gRPC message type. + """ + plane = Plane( + Point3D([10, 200, 3000], UNITS.mm), UnitVector3D([1, 1, 0]), UnitVector3D([1, -1, 0]) + ) + grpc_plane_message = from_plane_to_grpc_plane(plane) + + assert grpc_plane_message.frame.origin.x == 0.01 + assert grpc_plane_message.frame.origin.y == 0.2 + assert grpc_plane_message.frame.origin.z == 3.0 + + assert grpc_plane_message.frame.dir_x.x == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) + assert grpc_plane_message.frame.dir_x.y == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) + assert grpc_plane_message.frame.dir_x.z == 0.0 + + assert grpc_plane_message.frame.dir_y.x == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) + assert grpc_plane_message.frame.dir_y.y == pytest.approx( + -0.7071067811865475, rel=1e-7, abs=1e-8 + ) + assert grpc_plane_message.frame.dir_y.z == 0.0 + + +def test_frame_message_conversion(): + """Test conversion between :class:`Frame + ` and expected gRPC message type. + """ + frame = Frame( + Point3D([10, 200, 3000], UNITS.mm), UnitVector3D([1, 1, 0]), UnitVector3D([1, -1, 0]) + ) + grpc_frame_message = from_frame_to_grpc_frame(frame) + + assert grpc_frame_message.origin.x == 0.01 + assert grpc_frame_message.origin.y == 0.2 + assert grpc_frame_message.origin.z == 3.0 + + assert grpc_frame_message.dir_x.x == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) + assert grpc_frame_message.dir_x.y == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) + assert grpc_frame_message.dir_x.z == 0.0 + + assert grpc_frame_message.dir_y.x == pytest.approx(0.7071067811865475, rel=1e-7, abs=1e-8) + assert grpc_frame_message.dir_y.y == pytest.approx(-0.7071067811865475, rel=1e-7, abs=1e-8) + assert grpc_frame_message.dir_y.z == 0.0