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/1671.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enable get/set persistent ids for stride import/export
20 changes: 20 additions & 0 deletions src/ansys/geometry/core/misc/auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@ def __traverse_all_bodies(comp: Union["Design", "Component"]) -> list["Body"]:
return bodies


def get_all_bodies_from_design(design: "Design") -> list["Body"]:
"""Find all the ``Body`` objects inside a ``Design``.

Parameters
----------
design : Design
Parent design for the bodies.

Returns
-------
list[Body]
List of Body objects.

Notes
-----
This method takes a design and gets the corresponding ``Body`` objects.
"""
return __traverse_all_bodies(design)


def get_bodies_from_ids(design: "Design", body_ids: list[str]) -> list["Body"]:
"""Find the ``Body`` objects inside a ``Design`` from its ids.

Expand Down
268 changes: 268 additions & 0 deletions src/ansys/geometry/core/misc/unsupported.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
Copy link
Member

Choose a reason for hiding this comment

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

This entire module shouldn't be here... It should go inside the tools subpackage IMO.

# 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.
"""Unsupported functions for the PyAnsys Geometry library."""

from enum import Enum, unique
from typing import TYPE_CHECKING

from ansys.api.dbu.v0.dbumodels_pb2 import EntityIdentifier
from ansys.api.geometry.v0.unsupported_pb2 import ExportIdRequest, ImportIdRequest
from ansys.api.geometry.v0.unsupported_pb2_grpc import UnsupportedStub
from ansys.geometry.core.connection import GrpcClient
from ansys.geometry.core.errors import protect_grpc
from ansys.geometry.core.misc import auxiliary
from ansys.geometry.core.misc.checks import (
min_backend_version,
)

if TYPE_CHECKING: # pragma: no cover
from ansys.geometry.core.designer.body import Body
from ansys.geometry.core.designer.edge import Edge
from ansys.geometry.core.designer.face import Face


@unique
class PersistentIdType(Enum):
"""Type of persistent id."""

PNAME = 1
PRIME_ID = 700


class UnsupportedCommands:
"""Provides unsupported commands for PyAnsys Geometry.
Parameters
----------
grpc_client : GrpcClient
gRPC client to use for the geometry commands.
"""

@protect_grpc
def __init__(self, grpc_client: GrpcClient, modeler):
"""Initialize an instance of the ``UnsupportedCommands`` class."""
self._grpc_client = grpc_client
self._unsupported_stub = UnsupportedStub(self._grpc_client.channel)
self.__id_map = {}
self.__modeler = modeler
self.__current_design = None

@protect_grpc
@min_backend_version(25, 2, 0)
def __fill_imported_id_map(self, id_type: "PersistentIdType") -> None:
"""Populate the persistent id map for caching.
Parameters
----------
id_type : PersistentIdType
type of id
Notes
-----
This cache should be cleared on design change
"""
request = ImportIdRequest(type=id_type.value)
self.__id_map[id_type] = self._unsupported_stub.GetImportIdMap(request).id_map

@protect_grpc
@min_backend_version(25, 2, 0)
def __clear_cache(self) -> None:
"""Clear the cache of persistent id's.
Notes
-----
This should be called on design change
"""
self.__id_map = {}

@protect_grpc
@min_backend_version(25, 2, 0)
def __is_occurrence(self, master: "EntityIdentifier", occ: "str") -> bool:
"""Determine if the master is the master of the occurrence.
Parameters
----------
master : EntityIdentifier
master moniker
occ : str
occurrence moniker
Returns
-------
bool : true if the master is the master of the occurrence
"""
master_id = occ.split("/")[-1]
return master.id == master_id

@protect_grpc
@min_backend_version(25, 2, 0)
def __get_moniker_from_import_id(
self, id_type: "PersistentIdType", import_id: "str"
) -> "EntityIdentifier | None":
"""Look up the moniker from the id map.
Parameters
----------
id_type : PersistentIdType
type of id
import_id : str
persistent id
Returns
-------
EntityIdentifier : moniker associated with the id or None
Notes
-----
This checks if the design has changed and clears the cache if it has.
"""
if (
self.__current_design != self.__modeler.get_active_design()
or id_type not in self.__id_map
):
self.__fill_imported_id_map(id_type)
moniker = self.__id_map[id_type].get(import_id, None)
return moniker

@protect_grpc
@min_backend_version(25, 2, 0)
def set_export_id(self, moniker: "str", id_type: "PersistentIdType", value: "str") -> None:
"""Set the persistent id for the moniker.
Parameters
----------
moniker : str
moniker to set the id for
id_type : PersistentIdType
type of id
value : str
id to set
Returns
-------
None
"""
request = ExportIdRequest(
moniker=EntityIdentifier(id=moniker), id=value, type=id_type.value
)
self._unsupported_stub.SetExportId(request)
self.__id_map = {}

@protect_grpc
@min_backend_version(25, 2, 0)
def get_body_occurrences_from_import_id(
self, import_id: "str", id_type: "PersistentIdType"
) -> list["Body"]:
"""Get all body occurrences whose master has the given import id.
Parameters
----------
import_id : str
persistent id
id_type : PersistentIdType
type of id
Returns
-------
None
"""
moniker = self.__get_moniker_from_import_id(id_type, import_id)

if moniker is None:
return list()

design = self.__modeler.get_active_design()
return [
body
for body in auxiliary.get_all_bodies_from_design(design)
if self.__is_occurrence(moniker, body.id)
]

@protect_grpc
@min_backend_version(25, 2, 0)
def get_face_occurrences_from_import_id(
self, import_id: "str", id_type: "PersistentIdType"
) -> list["Face"]:
"""Get all face occurrences whose master has the given import id.
Parameters
----------
import_id : str
persistent id
id_type : PersistentIdType
type of id
Returns
-------
None
"""
moniker = self.__get_moniker_from_import_id(id_type, import_id)

if moniker is None:
return list()

design = self.__modeler.get_active_design()
return [
face
for body in auxiliary.get_all_bodies_from_design(design)
for face in body.faces
if self.__is_occurrence(moniker, face.id)
]

@protect_grpc
@min_backend_version(25, 2, 0)
def get_edge_occurrences_from_import_id(
self, import_id: "str", id_type: "PersistentIdType"
) -> list["Edge"]:
"""Get all edge occurrences whose master has the given import id.
Parameters
----------
import_id : str
persistent id
id_type : PersistentIdType
type of id
Returns
-------
None
"""
moniker = self.__get_moniker_from_import_id(id_type, import_id)

if moniker is None:
return list()

design = self.__modeler.get_active_design()
return [
edge
for body in auxiliary.get_all_bodies_from_design(design)
for edge in body.edges
if self.__is_occurrence(moniker, edge.id)
]
7 changes: 7 additions & 0 deletions src/ansys/geometry/core/modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from ansys.geometry.core.logger import LOG
from ansys.geometry.core.misc.checks import check_type, min_backend_version
from ansys.geometry.core.misc.options import ImportOptions
from ansys.geometry.core.misc.unsupported import UnsupportedCommands
from ansys.geometry.core.tools.measurement_tools import MeasurementTools
from ansys.geometry.core.tools.prepare_tools import PrepareTools
from ansys.geometry.core.tools.repair_tools import RepairTools
Expand Down Expand Up @@ -132,6 +133,7 @@ def __init__(
self._repair_tools = RepairTools(self._grpc_client)
self._prepare_tools = PrepareTools(self._grpc_client)
self._geometry_commands = GeometryCommands(self._grpc_client)
self._unsupported = UnsupportedCommands(self._grpc_client, self)

# Maintaining references to all designs within the modeler workspace
self._designs: dict[str, "Design"] = {}
Expand Down Expand Up @@ -511,6 +513,11 @@ def geometry_commands(self) -> "GeometryCommands":
"""Access to geometry commands."""
return self._geometry_commands

@property
def unsupported(self) -> "UnsupportedCommands":
"""Access to unsupported commands."""
return self._unsupported

@min_backend_version(25, 1, 0)
def get_service_logs(
self,
Expand Down
33 changes: 31 additions & 2 deletions tests/integration/test_design_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from ansys.geometry.core.designer.design import DesignFileFormat
from ansys.geometry.core.math import Plane, Point2D, Point3D, UnitVector3D, Vector3D
from ansys.geometry.core.misc import UNITS
from ansys.geometry.core.misc.unsupported import PersistentIdType
from ansys.geometry.core.sketch import Sketch

from .conftest import FILES_DIR, IMPORT_FILES_DIR, skip_if_linux
Expand Down Expand Up @@ -142,12 +143,12 @@ def test_open_file(modeler: Modeler, tmp_path_factory: pytest.TempPathFactory):

# Create car base frame
sketch = Sketch().box(Point2D([5, 10]), 10, 20)
comp2.add_component("Base").extrude_sketch("BaseBody", sketch, 5)
base_body = comp2.add_component("Base").extrude_sketch("BaseBody", sketch, 5)

# Create first wheel
sketch = Sketch(Plane(direction_x=Vector3D([0, 1, 0]), direction_y=Vector3D([0, 0, 1])))
sketch.circle(Point2D([0, 0]), 5)
wheel1.extrude_sketch("Wheel", sketch, -5)
wheel_body = wheel1.extrude_sketch("Wheel", sketch, -5)

# Create 3 other wheels and move them into position
rotation_origin = Point3D([0, 0, 0])
Expand All @@ -170,6 +171,34 @@ def test_open_file(modeler: Modeler, tmp_path_factory: pytest.TempPathFactory):
sketch = Sketch(Plane(Point3D([0, 5, 5]))).box(Point2D([5, 2.5]), 10, 5)
comp1.extrude_sketch("Top", sketch, 5)

modeler.unsupported.set_export_id(base_body.id, PersistentIdType.PRIME_ID, "1")
modeler.unsupported.set_export_id(wheel_body.id, PersistentIdType.PRIME_ID, "2")

modeler.unsupported.set_export_id(base_body.faces[0].id, PersistentIdType.PRIME_ID, "3")
modeler.unsupported.set_export_id(base_body.edges[0].id, PersistentIdType.PRIME_ID, "4")

bodies1 = modeler.unsupported.get_body_occurrences_from_import_id(
"1", PersistentIdType.PRIME_ID
)
bodies2 = modeler.unsupported.get_body_occurrences_from_import_id(
"2", PersistentIdType.PRIME_ID
)

# requires a change to core service, uncomment on next core service update
# assert base_body.id in [b.id for b in bodies1]
# assert wheel_body.id in [b.id for b in bodies2]
assert base_body.id not in [b.id for b in bodies1]
assert wheel_body.id not in [b.id for b in bodies2]

Comment on lines +187 to +192
Copy link
Member

Choose a reason for hiding this comment

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

This is going to prevent the update of the Docker images unless we do it right away.... we are checking that the new images are not breaking the tests before promoting them. This is done on main rather than on blitz. And we are safe because of that but nonetheless, it is going to be problematic on the workflows

faces = modeler.unsupported.get_face_occurrences_from_import_id("3", PersistentIdType.PRIME_ID)
edges = modeler.unsupported.get_edge_occurrences_from_import_id("4", PersistentIdType.PRIME_ID)

# requires a change to core service, uncomment on next core service update
# assert base_body.faces[0].id in [f.id for f in faces]
# assert base_body.edges[0].id in [e.id for e in edges]
assert base_body.faces[0].id not in [f.id for f in faces]
assert base_body.edges[0].id not in [e.id for e in edges]

file = tmp_path_factory.mktemp("test_design_import") / "two_cars.scdocx"
design.download(str(file))

Expand Down
Loading