Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/2263.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Grpc rearchitecutre - components model
28 changes: 28 additions & 0 deletions src/ansys/geometry/core/_grpc/_services/_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .base.beams import GRPCBeamsService
from .base.bodies import GRPCBodyService
from .base.commands import GRPCCommandsService
from .base.components import GRPCComponentsService
from .base.coordinate_systems import GRPCCoordinateSystemService
from .base.curves import GRPCCurvesService
from .base.dbuapplication import GRPCDbuApplicationService
Expand Down Expand Up @@ -88,6 +89,7 @@ def __init__(self, channel: grpc.Channel, version: GeometryApiProtos | str | Non
self._beams = None
self._bodies = None
self._commands = None
self._components = None
self._coordinate_systems = None
self._curves = None
self._dbu_application = None
Expand Down Expand Up @@ -234,6 +236,32 @@ def commands(self) -> GRPCCommandsService:

return self._commands

@property
def components(self) -> GRPCComponentsService:
"""
Get the components service for the specified version.

Returns
-------
GRPCComponentsService
The components service for the specified version.
"""
if not self._components:
# Import the appropriate components service based on the version
from .v0.components import GRPCComponentsServiceV0
from .v1.components import GRPCComponentsServiceV1

if self.version == GeometryApiProtos.V0:
self._components = GRPCComponentsServiceV0(self.channel)
elif self.version == GeometryApiProtos.V1: # pragma: no cover
# V1 is not implemented yet
self._components = GRPCComponentsServiceV1(self.channel)
else: # pragma: no cover
# This should never happen as the version is set in the constructor
raise ValueError(f"Unsupported version: {self.version}")

return self._components

@property
def coordinate_systems(self) -> GRPCCoordinateSystemService:
"""
Expand Down
74 changes: 74 additions & 0 deletions src/ansys/geometry/core/_grpc/_services/base/components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 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 containing the components service implementation (abstraction layer)."""

from abc import ABC, abstractmethod

import grpc


class GRPCComponentsService(ABC): # pragma: no cover
"""Components service for gRPC communication with the Geometry server.

Parameters
----------
channel : grpc.Channel
The gRPC channel to the server.
"""

def __init__(self, channel: grpc.Channel):
"""Initialize the GRPCComponentsService class."""

@abstractmethod
def create(self, **kwargs) -> dict:
"""Create a component."""
pass

@abstractmethod
def set_name(self, **kwargs) -> dict:
"""Set the name of a component."""
pass

@abstractmethod
def set_placement(self, **kwargs) -> dict:
"""Set the placement of a component."""
pass

@abstractmethod
def set_shared_topology(self, **kwargs) -> dict:
"""Set the shared topology of a component."""
pass

@abstractmethod
def delete(self, **kwargs) -> dict:
"""Delete a component."""
pass

@abstractmethod
def import_groups(self, **kwargs) -> dict:
"""Import groups from a component."""
pass

@abstractmethod
def make_independent(self, **kwargs) -> dict:
"""Make a component independent."""
pass
63 changes: 61 additions & 2 deletions src/ansys/geometry/core/_grpc/_services/v0/beams.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
from ansys.geometry.core.errors import protect_grpc

from ..base.beams import GRPCBeamsService
from .conversions import from_point3d_to_grpc_point
from ..base.conversions import to_distance
from .conversions import (
from_grpc_curve_to_curve,
from_grpc_frame_to_frame,
from_grpc_material_to_material,
from_grpc_point_to_point3d,
from_point3d_to_grpc_point,
)


class GRPCBeamsServiceV0(GRPCBeamsService):
Expand Down Expand Up @@ -83,6 +90,8 @@ def create_descriptive_beam_segments(self, **kwargs) -> dict: # noqa: D102
from ansys.api.geometry.v0.commands_pb2 import CreateBeamSegmentsRequest
from ansys.api.geometry.v0.models_pb2 import Line

from ansys.geometry.core.shapes.parameterization import Interval, ParamUV

# Create the gRPC Line objects
lines = []
for segment in kwargs["segments"]:
Expand All @@ -105,7 +114,57 @@ def create_descriptive_beam_segments(self, **kwargs) -> dict: # noqa: D102

# Return the response - formatted as a dictionary
return {
"created_beams": resp.created_beams,
"created_beams": [
{
"cross_section": {
"section_anchor": beam.cross_section.section_anchor,
"section_angle": beam.cross_section.section_angle,
"section_frame": from_grpc_frame_to_frame(beam.cross_section.section_frame),
"section_profile": [
[
{
"geometry": from_grpc_curve_to_curve(curve.curve),
"start": from_grpc_point_to_point3d(curve.start),
"end": from_grpc_point_to_point3d(curve.end),
"interval": Interval(curve.interval_start, curve.interval_end),
"length": to_distance(curve.length).value,
}
for curve in curve_list.curves
]
for curve_list in beam.cross_section.section_profile
],
},
"properties": {
"area": beam.properties.area,
"centroid": ParamUV(beam.properties.centroid_x, beam.properties.centroid_y),
"warping_constant": beam.properties.warping_constant,
"ixx": beam.properties.ixx,
"ixy": beam.properties.ixy,
"iyy": beam.properties.iyy,
"shear_center": ParamUV(
beam.properties.shear_center_x, beam.properties.shear_center_y
),
"torsional_constant": beam.properties.torsional_constant,
},
"id": beam.id.id,
"start": from_grpc_point_to_point3d(beam.shape.start),
"end": from_grpc_point_to_point3d(beam.shape.end),
"name": beam.name,
"is_deleted": beam.is_deleted,
"is_reversed": beam.is_reversed,
"is_rigid": beam.is_rigid,
"material": from_grpc_material_to_material(beam.material),
"shape": {
"geometry": from_grpc_curve_to_curve(beam.shape.curve),
"start": from_grpc_point_to_point3d(beam.shape.start),
"end": from_grpc_point_to_point3d(beam.shape.end),
"interval": Interval(beam.shape.interval_start, beam.shape.interval_end),
"length": to_distance(beam.shape.length).value,
},
"beam_type": beam.type,
}
for beam in resp.created_beams
],
}

@protect_grpc
Expand Down
183 changes: 183 additions & 0 deletions src/ansys/geometry/core/_grpc/_services/v0/components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# 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 containing the components service implementation for v0."""

import grpc

from ansys.geometry.core.errors import protect_grpc

from ..base.components import GRPCComponentsService
from ..base.conversions import from_measurement_to_server_angle
from .conversions import (
build_grpc_id,
from_grpc_matrix_to_matrix,
from_point3d_to_grpc_point,
from_unit_vector_to_grpc_direction,
)


class GRPCComponentsServiceV0(GRPCComponentsService):
"""Components service for gRPC communication with the Geometry server.

This class provides methods to call components in the
Geometry server using gRPC. It is specifically designed for the v0
version of the Geometry API.

Parameters
----------
channel : grpc.Channel
The gRPC channel to the server.
"""

@protect_grpc
def __init__(self, channel: grpc.Channel): # noqa: D102
from ansys.api.geometry.v0.components_pb2_grpc import ComponentsStub

self.stub = ComponentsStub(channel)

@protect_grpc
def create(self, **kwargs) -> dict: # noqa: D102
from ansys.api.geometry.v0.components_pb2 import CreateRequest

# Create the request - assumes all inputs are valid and of the proper type
request = CreateRequest(
name=kwargs["name"],
parent=kwargs["parent_id"],
template=kwargs["template_id"],
instance_name=kwargs["instance_name"],
)

# Call the gRPC service
response = self.stub.Create(request)

# Return the response - formatted as a dictionary
return {
"id": response.component.id,
"name": response.component.name,
"instance_name": response.component.instance_name,
}

@protect_grpc
def set_name(self, **kwargs) -> dict: # noqa: D102
from ansys.api.geometry.v0.models_pb2 import SetObjectNameRequest

# Create the request - assumes all inputs are valid and of the proper type
request = SetObjectNameRequest(id=build_grpc_id(kwargs["id"]), name=kwargs["name"])

# Call the gRPC service
_ = self.stub.SetName(request)

# Return the response - formatted as a dictionary
return {}

@protect_grpc
def set_placement(self, **kwargs) -> dict: # noqa: D102
from ansys.api.geometry.v0.components_pb2 import SetPlacementRequest

# Create the direction and point objects
translation = (
from_unit_vector_to_grpc_direction(kwargs["translation"].normalize())
if kwargs["translation"] is not None
else None
)
origin = (
from_point3d_to_grpc_point(kwargs["rotation_axis_origin"])
if kwargs["rotation_axis_origin"] is not None
else None
)
direction = (
from_unit_vector_to_grpc_direction(kwargs["rotation_axis_direction"])
if kwargs["rotation_axis_direction"] is not None
else None
)

# Create the request - assumes all inputs are valid and of the proper type
request = SetPlacementRequest(
id=kwargs["id"],
translation=translation,
rotation_axis_origin=origin,
rotation_axis_direction=direction,
rotation_angle=from_measurement_to_server_angle(kwargs["rotation_angle"]),
)

# Call the gRPC service
response = self.stub.SetPlacement(request)

# Return the response - formatted as a dictionary
return {"matrix": from_grpc_matrix_to_matrix(response.matrix)}

@protect_grpc
def set_shared_topology(self, **kwargs) -> dict: # noqa: D102
from ansys.api.geometry.v0.components_pb2 import SetSharedTopologyRequest

# Create the request - assumes all inputs are valid and of the proper type
request = SetSharedTopologyRequest(
id=kwargs["id"],
share_type=kwargs["share_type"].value,
)

# Call the gRPC service
_ = self.stub.SetSharedTopology(request)

# Return the response - formatted as a dictionary
return {}

@protect_grpc
def delete(self, **kwargs) -> dict: # noqa: D102
# Create the request - assumes all inputs are valid and of the proper type
request = build_grpc_id(kwargs["id"])

# Call the gRPC service
_ = self.stub.Delete(request)

# Return the response - formatted as a dictionary
return {}

@protect_grpc
def import_groups(self, **kwargs) -> dict: # noqa: D102
from ansys.api.geometry.v0.components_pb2 import ImportGroupsRequest

# Create the request - assumes all inputs are valid and of the proper type
request = ImportGroupsRequest(
id=build_grpc_id(kwargs["id"]),
)

# Call the gRPC service
_ = self.stub.ImportGroups(request)

# Return the response - formatted as a dictionary
return {}

@protect_grpc
def make_independent(self, **kwargs) -> dict: # noqa: D102
from ansys.api.geometry.v0.components_pb2 import MakeIndependentRequest

# Create the request - assumes all inputs are valid and of the proper type
request = MakeIndependentRequest(
ids=[build_grpc_id(id) for id in kwargs["ids"]],
)

# Call the gRPC service
_ = self.stub.MakeIndependent(request)

# Return the response - formatted as a dictionary
return {}
Loading
Loading