Skip to content

Commit 370ebf8

Browse files
smereupre-commit-ci[bot]pyansys-ci-botRobPasMueMaxJPRey
authored
feat: Implementation of inspect & repair geometry (#1712)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Roberto Pastor Muela <[email protected]> Co-authored-by: Maxime Rey <[email protected]>
1 parent e91ffe2 commit 370ebf8

File tree

6 files changed

+262
-2
lines changed

6 files changed

+262
-2
lines changed

doc/changelog.d/1712.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implementation of inspect & repair geometry

src/ansys/geometry/core/modeler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def __init__(
132132
self._measurement_tools = MeasurementTools(self._grpc_client)
133133

134134
# Enabling tools/commands for all: repair and prepare tools, geometry commands
135-
self._repair_tools = RepairTools(self._grpc_client)
135+
self._repair_tools = RepairTools(self._grpc_client, self)
136136
self._prepare_tools = PrepareTools(self._grpc_client)
137137
self._geometry_commands = GeometryCommands(self._grpc_client)
138138
self._unsupported = UnsupportedCommands(self._grpc_client, self)
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
"""Module for repair tool message."""
23+
24+
from typing import TYPE_CHECKING
25+
26+
from ansys.api.geometry.v0.repairtools_pb2 import RepairGeometryRequest
27+
from ansys.api.geometry.v0.repairtools_pb2_grpc import RepairToolsStub
28+
from ansys.geometry.core.connection.client import GrpcClient
29+
from ansys.geometry.core.tools.repair_tool_message import RepairToolMessage
30+
31+
if TYPE_CHECKING: # pragma: no cover
32+
from ansys.geometry.core.designer.body import Body
33+
34+
35+
class GeometryIssue:
36+
"""Provides return message for the repair tool methods."""
37+
38+
def __init__(
39+
self,
40+
message_type: str,
41+
message_id: str,
42+
message: str,
43+
edges: list[str],
44+
faces: list[str],
45+
):
46+
"""Initialize a new instance of a geometry issue found during geometry inspect.
47+
48+
Parameters
49+
----------
50+
message_type: str
51+
Type of the message (warning, error, info).
52+
message_id: str
53+
Identifier for the message.
54+
message
55+
Message that describes the geometry issue.
56+
edges: list[str]
57+
List of edges (if any) that are part of the issue.
58+
modified_bodies: list[str]
59+
List of faces that are part of the issue.
60+
"""
61+
self._message_type = message_type
62+
self._message_id = message_id
63+
self._message = message
64+
self._edges = edges
65+
self._faces = faces
66+
67+
@property
68+
def message_type(self) -> str:
69+
"""The type of the message (warning, error, info)."""
70+
return self._message_type
71+
72+
@property
73+
def message_id(self) -> str:
74+
"""The identifier for the message."""
75+
return self._message_id
76+
77+
@property
78+
def message(self) -> str:
79+
"""The content of the message."""
80+
return self._message
81+
82+
@property
83+
def edges(self) -> list[str]:
84+
"""The List of edges (if any) that are part of the issue."""
85+
return self._edges
86+
87+
@property
88+
def faces(self) -> list[str]:
89+
"""The List of faces (if any) that are part of the issue."""
90+
return self._faces
91+
92+
93+
class InspectResult:
94+
"""Provides the result of the inspect geometry operation."""
95+
96+
def __init__(self, grpc_client: GrpcClient, body: "Body", issues: list[GeometryIssue]):
97+
"""Initialize a new instance of the result of the inspect geometry operation.
98+
99+
Parameters
100+
----------
101+
body: Body
102+
Body for which issues are found.
103+
issues: list[GeometryIssue]
104+
List of issues for the body.
105+
"""
106+
self._body = body
107+
self._issues = issues
108+
self._repair_stub = RepairToolsStub(grpc_client.channel)
109+
110+
@property
111+
def body(self) -> "Body":
112+
"""The body for which issues are found."""
113+
return self._body
114+
115+
@property
116+
def issues(self) -> list[GeometryIssue]:
117+
"""The list of issues for the body."""
118+
return self._issues
119+
120+
def repair(self) -> RepairToolMessage:
121+
"""Repair the problem area.
122+
123+
Returns
124+
-------
125+
RepairToolMessage
126+
Message containing created and/or modified bodies.
127+
"""
128+
if not self.body:
129+
return RepairToolMessage(False, [], [])
130+
131+
repair_result_response = self._repair_stub.RepairGeometry(
132+
RepairGeometryRequest(bodies=[self.body._grpc_id])
133+
)
134+
135+
return RepairToolMessage(repair_result_response.result.success, [], [])

src/ansys/geometry/core/tools/repair_tools.py

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
from google.protobuf.wrappers_pb2 import BoolValue, DoubleValue
2727

2828
from ansys.api.geometry.v0.bodies_pb2_grpc import BodiesStub
29+
from ansys.api.geometry.v0.models_pb2 import (
30+
InspectGeometryMessageId,
31+
InspectGeometryMessageType,
32+
InspectGeometryResult,
33+
InspectGeometryResultIssue,
34+
)
2935
from ansys.api.geometry.v0.repairtools_pb2 import (
3036
FindAdjustSimplifyRequest,
3137
FindDuplicateFacesRequest,
@@ -37,6 +43,8 @@
3743
FindSmallFacesRequest,
3844
FindSplitEdgesRequest,
3945
FindStitchFacesRequest,
46+
InspectGeometryRequest,
47+
RepairGeometryRequest,
4048
)
4149
from ansys.api.geometry.v0.repairtools_pb2_grpc import RepairToolsStub
4250
from ansys.geometry.core.connection import GrpcClient
@@ -52,6 +60,7 @@
5260
check_type_all_elements_in_iterable,
5361
min_backend_version,
5462
)
63+
from ansys.geometry.core.tools.check_geometry import GeometryIssue, InspectResult
5564
from ansys.geometry.core.tools.problem_areas import (
5665
DuplicateFaceProblemAreas,
5766
ExtraEdgeProblemAreas,
@@ -69,16 +78,18 @@
6978

7079
if TYPE_CHECKING: # pragma: no cover
7180
from ansys.geometry.core.designer.body import Body
81+
from ansys.geometry.core.modeler import Modeler
7282

7383

7484
class RepairTools:
7585
"""Repair tools for PyAnsys Geometry."""
7686

77-
def __init__(self, grpc_client: GrpcClient):
87+
def __init__(self, grpc_client: GrpcClient, modeler: "Modeler"):
7888
"""Initialize a new instance of the ``RepairTools`` class."""
7989
self._grpc_client = grpc_client
8090
self._repair_stub = RepairToolsStub(self._grpc_client.channel)
8191
self._bodies_stub = BodiesStub(self._grpc_client.channel)
92+
self._modeler = modeler
8293

8394
@protect_grpc
8495
def find_split_edges(
@@ -598,3 +609,91 @@ def find_and_fix_split_edges(
598609
response.modified_bodies_monikers,
599610
)
600611
return message
612+
613+
@protect_grpc
614+
@min_backend_version(25, 2, 0)
615+
def inspect_geometry(self, bodies: list["Body"] = None) -> list[InspectResult]:
616+
"""Return a list of geometry issues organized by body.
617+
618+
This method inspects the geometry and returns a list of the issues grouped by
619+
the body where they are found.
620+
621+
Parameters
622+
----------
623+
bodies : list[Body]
624+
List of bodies to inspect the geometry for.
625+
All bodies are inspected if the argument is not given.
626+
627+
Returns
628+
-------
629+
list[IssuesByBody]
630+
List of objects representing geometry issues and the bodies where issues are found.
631+
"""
632+
parent_design = self._modeler.get_active_design()
633+
body_ids = [] if bodies is None else [body._grpc_id for body in bodies]
634+
inspect_result_response = self._repair_stub.InspectGeometry(
635+
InspectGeometryRequest(bodies=body_ids)
636+
)
637+
return self.__create_inspect_result_from_response(
638+
parent_design, inspect_result_response.issues_by_body
639+
)
640+
641+
def __create_inspect_result_from_response(
642+
self, design, inspect_geometry_results: list[InspectGeometryResult]
643+
) -> list[InspectResult]:
644+
inspect_results = []
645+
for inspect_geometry_result in inspect_geometry_results:
646+
body = get_bodies_from_ids(design, [inspect_geometry_result.body.id])
647+
issues = self.__create_issues_from_response(inspect_geometry_result.issues)
648+
inspect_result = InspectResult(
649+
grpc_client=self._grpc_client, body=body[0], issues=issues
650+
)
651+
inspect_results.append(inspect_result)
652+
653+
return inspect_results
654+
655+
def __create_issues_from_response(
656+
self,
657+
inspect_geometry_result_issues: list[InspectGeometryResultIssue],
658+
) -> list[GeometryIssue]:
659+
issues = []
660+
for inspect_result_issue in inspect_geometry_result_issues:
661+
message_type = InspectGeometryMessageType.Name(inspect_result_issue.message_type)
662+
message_id = InspectGeometryMessageId.Name(inspect_result_issue.message_id)
663+
message = inspect_result_issue.message
664+
665+
issue = GeometryIssue(
666+
message_type=message_type,
667+
message_id=message_id,
668+
message=message,
669+
faces=[face.id for face in inspect_result_issue.faces],
670+
edges=[edge.id for edge in inspect_result_issue.edges],
671+
)
672+
issues.append(issue)
673+
return issues
674+
675+
@protect_grpc
676+
@min_backend_version(25, 2, 0)
677+
def repair_geometry(self, bodies: list["Body"] = None) -> RepairToolMessage:
678+
"""Attempt to repair the geometry for the given bodies.
679+
680+
This method inspects the geometry for the given bodies and attempts to repair them.
681+
682+
Parameters
683+
----------
684+
bodies : list[Body]
685+
List of bodies where to attempt to repair the geometry.
686+
All bodies are repaired if the argument is not given.
687+
688+
Returns
689+
-------
690+
RepairToolMessage
691+
Message containing success of the operation.
692+
"""
693+
body_ids = [] if bodies is None else [body._grpc_id for body in bodies]
694+
repair_result_response = self._repair_stub.RepairGeometry(
695+
RepairGeometryRequest(bodies=body_ids)
696+
)
697+
698+
message = RepairToolMessage(repair_result_response.result.success, [], [])
699+
return message
Binary file not shown.

tests/integration/test_repair_tools.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,28 @@ def test_find_and_fix_extra_edges(modeler: Modeler):
453453
for body in design.bodies:
454454
final_edge_count += len(body.edges)
455455
assert final_edge_count == 36
456+
457+
458+
def test_inspect_geometry(modeler: Modeler):
459+
"""Test the result of the inspect geometry query and the ability to repair one issue"""
460+
modeler.open_file(FILES_DIR / "InspectAndRepair01.scdocx")
461+
inspect_results = modeler.repair_tools.inspect_geometry()
462+
assert len(inspect_results) == 1
463+
issues = len(inspect_results[0].issues)
464+
assert issues == 7
465+
result_to_repair = inspect_results[0]
466+
result_to_repair.repair()
467+
# Reinspect the geometry
468+
inspect_results = modeler.repair_tools.inspect_geometry()
469+
# All issues should have been fixed
470+
assert len(inspect_results) == 0
471+
472+
473+
def test_repair_geometry(modeler: Modeler):
474+
"""Test the ability to repair a geometry. Inspect geometry is called behind the scenes"""
475+
modeler.open_file(FILES_DIR / "InspectAndRepair01.scdocx")
476+
modeler.repair_tools.repair_geometry()
477+
# Reinspect the geometry
478+
inspect_results = modeler.repair_tools.inspect_geometry()
479+
# All issues should have been fixed
480+
assert len(inspect_results) == 0

0 commit comments

Comments
 (0)