Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add polydata conversors to edges and faces #759

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 9 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
44 changes: 43 additions & 1 deletion src/ansys/geometry/core/designer/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

from ansys.api.dbu.v0.dbumodels_pb2 import EntityIdentifier
from ansys.api.geometry.v0.edges_pb2_grpc import EdgesStub
from beartype.typing import TYPE_CHECKING, List
from beartype.typing import TYPE_CHECKING, List, Union
from pint import Quantity
import pyvista as pv

from ansys.geometry.core.connection.client import GrpcClient
from ansys.geometry.core.connection.conversions import grpc_curve_to_curve
from ansys.geometry.core.errors import protect_grpc
from ansys.geometry.core.logger import LOG
from ansys.geometry.core.math.point import Point3D
from ansys.geometry.core.misc.checks import ensure_design_is_active
from ansys.geometry.core.misc.measurements import DEFAULT_UNITS
Expand All @@ -40,6 +42,8 @@
if TYPE_CHECKING: # pragma: no cover
from ansys.geometry.core.designer.body import Body
from ansys.geometry.core.designer.face import Face
from ansys.geometry.core.shapes.curves.circle import Circle
from ansys.geometry.core.shapes.curves.ellipse import Ellipse


@unique
Expand Down Expand Up @@ -168,3 +172,41 @@ def faces(self) -> List["Face"]:
)
for grpc_face in grpc_faces
]

def _to_polydata(self) -> Union[pv.PolyData, None]:
"""
Return the edge as polydata.

This is useful to represent the edge in a PyVista plotter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is useful to represent the edge in a PyVista plotter.
This method is useful to represent the edge in a PyVista plotter.


Returns
-------
Union[pv.PolyData, None]
Edge as polydata
"""
if self._curve_type == CurveType.CURVETYPE_UNKNOWN or CurveType.CURVETYPE_LINE:
return pv.Line(pointa=self.start_point, pointb=self.end_point)
elif self._curve_type == CurveType.CURVETYPE_CIRCLE:
self.shape.geometry: Circle
point_a = self.shape.geometry.evaluate(0)
point_b = self.shape.geometry.evaluate(1)
half_a = pv.CircularArc(
center=self.shape.geometry.center,
pointa=point_a,
pointb=point_b,
)
half_b = pv.CircularArc(
center=self.shape.geometry.center,
pointa=point_a,
pointb=point_b,
negative=True,
)
pv_circle = pv.MultiBlock([half_a, half_b])
return pv_circle

elif self._curve_type == CurveType.CURVETYPE_ELLIPSE:
self.shape.geometry: Ellipse

else:
LOG.warning("Non linear edges not supported.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LOG.warning("Non linear edges not supported.")
LOG.warning("Non-linear edges not supported.")

return None
32 changes: 31 additions & 1 deletion src/ansys/geometry/core/designer/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
from ansys.api.geometry.v0.faces_pb2 import CreateIsoParamCurvesRequest
from ansys.api.geometry.v0.faces_pb2_grpc import FacesStub
from ansys.api.geometry.v0.models_pb2 import Edge as GRPCEdge
from beartype.typing import TYPE_CHECKING, List
from beartype.typing import TYPE_CHECKING, List, Union
from pint import Quantity
import pyvista as pv

from ansys.geometry.core.connection.client import GrpcClient
from ansys.geometry.core.connection.conversions import grpc_curve_to_curve, grpc_surface_to_surface
from ansys.geometry.core.designer.edge import Edge
from ansys.geometry.core.errors import protect_grpc
from ansys.geometry.core.logger import LOG
from ansys.geometry.core.math.point import Point3D
from ansys.geometry.core.math.vector import UnitVector3D
from ansys.geometry.core.misc.checks import ensure_design_is_active
Expand Down Expand Up @@ -358,6 +360,34 @@ def __grpc_edges_to_edges(self, edges_grpc: List[GRPCEdge]) -> List[Edge]:
)
return edges

def _to_polydata(self) -> Union[pv.PolyData, None]:
"""
Return the face as polydata.

This is useful to represent the face in a PyVista plotter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is useful to represent the face in a PyVista plotter.
This method is useful to represent the face in a PyVista plotter.


Returns
-------
Union[pv.PolyData, None]
Face as polydata
"""
if self.surface_type != SurfaceType.SURFACETYPE_PLANE:
LOG.warning("Only planes surfaces are supported")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LOG.warning("Only planes surfaces are supported")
LOG.warning("Only planes surfaces are supported.")

return None
else:
# get vertices from edges
vertices = [
vertice
for edge in self.edges
for vertice in [edge.start_point.flat, edge.end_point.flat]
]
# TODO remove duplicate vertices
# build the PyVista face
vertices_order = [len(vertices)]
vertices_order.extend(range(len(vertices)))

return pv.PolyData(vertices, faces=vertices_order, n_faces=1)

def create_isoparametric_curves(
self, use_u_param: bool, parameter: float
) -> List[TrimmedCurve]:
Expand Down
10 changes: 10 additions & 0 deletions src/ansys/geometry/core/designer/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,22 @@ def __init__(
[ids.add(beam.id) for beam in beams]
[ids.add(dp.id) for dp in design_points]

self._selections = [bodies, faces, edges, beams, design_points]

# flatten list
self._selections = [item for sublist in self._selections for item in sublist]

named_selection_request = CreateRequest(name=name, members=ids)
self._grpc_client.log.debug("Requesting creation of named selection.")
new_named_selection = self._named_selections_stub.Create(named_selection_request)
self._id = new_named_selection.id
self._name = new_named_selection.name

@property
def selections(self):
"""Return all the selected elements."""
return self._selections

@property
def id(self) -> str:
"""ID of the named selection."""
Expand Down
Loading