-
Notifications
You must be signed in to change notification settings - Fork 22
feat: enable get/set persistent ids for stride import/export #1671
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
Changes from all commits
13ab56e
86a4dad
f195066
fd1dcc2
bc3d25a
d22e908
303e715
945b489
95f128d
cc3ccac
0cfbec5
505a5ab
7126305
cbef7c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| enable get/set persistent ids for stride import/export |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| # Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This entire module shouldn't be here... It should go inside the |
||
| # 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) | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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]) | ||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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)) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.