Skip to content

Commit

Permalink
Post repair design sync (#975)
Browse files Browse the repository at this point in the history
Co-authored-by: Roberto Pastor Muela <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 7, 2024
1 parent 0251ca3 commit 7a60f03
Show file tree
Hide file tree
Showing 7 changed files with 463 additions and 85 deletions.
35 changes: 35 additions & 0 deletions src/ansys/geometry/core/designer/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ def __repr__(self) -> str:
def __read_existing_design(self) -> None:
"""Read an existing ``Design`` located on the server."""
#
# TODO: This might go out of sync with the _update_design_inplace method.
# Ensure that the two methods are in sync. Especially regarding cleanup.
#
# TODO: Not all features implemented yet. Status is as follows
#
# Windows:
Expand Down Expand Up @@ -759,3 +762,35 @@ def __read_existing_design(self) -> None:
self._grpc_client.log.debug(f"SharedTopologyTypes set: {num_created_shared_topologies}")

self._grpc_client.log.debug(f"\nSuccessfully read design in: {end - start} s")

def _update_design_inplace(self) -> None:
"""
Update the design to align with the server side.
Notes
-----
This method is used to update the design inside repair tools.
Its usage is not recommended for other purposes.
"""
# Clear all the existing information
#
# TODO: This might go out of sync with the __read_existing_design method
# if the latter is updated and this method is not. Ensure that
# the two methods are in sync.
#
self._components = []
self._bodies = []
self._materials = []
self._named_selections = {}
self._coordinate_systems = {}

# Get the previous design id
previous_design_id = self._design_id

# Read the existing design
self.__read_existing_design()

# If the design id has changed, update the design id in the Modeler
if previous_design_id != self._design_id:
self._modeler._designs[self._design_id] = self
self._modeler._designs.pop(previous_design_id)
10 changes: 10 additions & 0 deletions src/ansys/geometry/core/misc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
"""Provides the PyAnsys Geometry miscellaneous subpackage."""

from ansys.geometry.core.misc.accuracy import ANGLE_ACCURACY, LENGTH_ACCURACY, Accuracy
from ansys.geometry.core.misc.auxiliary import (
get_bodies_from_ids,
get_design_from_body,
get_design_from_component,
get_design_from_edge,
get_design_from_face,
get_edges_from_ids,
get_faces_from_ids,
)
from ansys.geometry.core.misc.checks import (
check_is_float_int,
check_ndarray_is_all_nan,
Expand All @@ -30,6 +39,7 @@
check_ndarray_is_not_none,
check_pint_unit_compatibility,
check_type,
check_type_all_elements_in_iterable,
check_type_equivalence,
)
from ansys.geometry.core.misc.measurements import DEFAULT_UNITS, Angle, Distance
Expand Down
185 changes: 185 additions & 0 deletions src/ansys/geometry/core/misc/auxiliary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# Copyright (C) 2023 - 2024 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.
"""Auxiliary functions for the PyAnsys Geometry library."""

from beartype.typing import TYPE_CHECKING, List

if TYPE_CHECKING:
from ansys.geometry.core.designer.body import Body
from ansys.geometry.core.designer.component import Component
from ansys.geometry.core.designer.design import Design
from ansys.geometry.core.designer.edge import Edge
from ansys.geometry.core.designer.face import Face


def get_design_from_component(component: "Component") -> "Design":
"""
Get the ``Design`` of the given ``Component`` object.
Parameters
----------
component : Component
The component object for which to find the ``Design``.
Returns
-------
Design
The ``Design`` of the provided component object.
"""
# Get the design of the component
while component.parent_component is not None:
component = component.parent_component

# Return the design
return component


def get_design_from_body(body: "Body") -> "Design":
"""
Get the ``Design`` of the given ``Body`` object.
Parameters
----------
body : Body
The body object for which to find the ``Design``.
Returns
-------
Design
The ``Design`` of the provided body object.
"""
# Get the parent component of the body
component = body.parent_component

# Get the design of the component
return get_design_from_component(component)


def get_design_from_face(face: "Face") -> "Design":
"""
Get the ``Design`` of the given ``Face`` object.
Parameters
----------
face : Face
The face object for which to find the ``Design``.
Returns
-------
Design
The ``Design`` of the provided face object.
"""
# Get the parent body of the face
body = face.body

# Get the design of the body
return get_design_from_body(body)


def get_design_from_edge(edge: "Edge") -> "Design":
"""
Get the ``Design`` of the given ``Edge`` object.
Parameters
----------
edge : Edge
The edge object for which to find the ``Design``.
Returns
-------
Design
The ``Design`` of the provided edge object.
"""
# Get the one of the bodies of the edge
body = edge.faces[0].body

# Get the design of the body
return get_design_from_body(body)


def get_bodies_from_ids(design: "Design", body_ids: List[str]) -> List["Body"]:
"""
Find the ``Body`` objects inside a ``Design`` from its ids.
Notes
-----
This method takes a design and body ids, and gets their corresponding ``Body`` object.
Parameters
----------
design : Design
Parent design for the faces.
body_ids : List[str]
List of body ids.
Returns
-------
List[Body]
List of Body objects.
"""
return [body for body in design.bodies if body.id in body_ids]


def get_faces_from_ids(design: "Design", face_ids: List[str]) -> List["Face"]:
"""
Find the ``Face`` objects inside a ``Design`` from its ids.
Notes
-----
This method takes a design and face ids, and gets their corresponding ``Face`` object.
Parameters
----------
design : Design
Parent design for the faces.
face_ids : List[str]
List of face ids.
Returns
-------
List[Face]
List of Face objects.
"""
return [face for body in design.bodies for face in body.faces if face.id in face_ids]


def get_edges_from_ids(design: "Design", edge_ids: List[str]) -> List["Edge"]:
"""
Find the ``Edge`` objects inside a ``Design`` from its ids.
Notes
-----
This method takes a design and edge ids, and gets their corresponding ``Edge`` objects.
Parameters
----------
design : Design
Parent design for the edges.
edge_ids : List[str]
List of edge ids.
Returns
-------
List[Edge]
List of Edge objects.
"""
return [edge for body in design.bodies for edge in body.edges if edge.id in edge_ids]
24 changes: 23 additions & 1 deletion src/ansys/geometry/core/misc/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Provides functions for performing common checks."""
from beartype.typing import TYPE_CHECKING, Any, Optional, Tuple, Union
from beartype.typing import TYPE_CHECKING, Any, Iterable, Optional, Tuple, Union
import numpy as np
from pint import Unit

Expand Down Expand Up @@ -280,3 +280,25 @@ def check_type(input: object, expected_type: Union[type, Tuple[type, Any]]) -> N
raise TypeError(
f"Provided type {type(input)} is invalid. Type {expected_type} is expected."
)


def check_type_all_elements_in_iterable(
input: Iterable, expected_type: Union[type, Tuple[type, Any]]
) -> None:
"""
Check if all elements in an iterable are of the same type as expected types.
Parameters
----------
input : Iterable
Input iterable.
expected_type : Union[type, Tuple[type, ...]]
One or more types to compare the input object against.
Raises
------
TypeError
If one of the elements in the iterable does not match the one or more expected types.
"""
for elem in input:
check_type(elem, expected_type)
Loading

0 comments on commit 7a60f03

Please sign in to comment.