Skip to content

Commit 402661a

Browse files
feat: capability to close designs (also on modeler.exit()) (#1409)
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
1 parent 3044faa commit 402661a

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

doc/changelog.d/1409.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
capability to close designs (also on ``modeler.exit()``)

src/ansys/geometry/core/designer/design.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def __init__(self, name: str, modeler: Modeler, read_existing_design: bool = Fal
132132
self._beam_profiles = {}
133133
self._design_id = ""
134134
self._is_active = False
135+
self._is_closed = False
135136
self._modeler = modeler
136137

137138
# Check whether we want to process an existing design or create a new one.
@@ -170,6 +171,28 @@ def is_active(self) -> bool:
170171
"""Whether the design is currently active."""
171172
return self._is_active
172173

174+
@property
175+
def is_closed(self) -> bool:
176+
"""Whether the design is closed."""
177+
return self._is_closed
178+
179+
def close(self) -> None:
180+
"""Close the design."""
181+
# Check if the design is already closed
182+
if self._is_closed:
183+
self._grpc_client.log.warning(f"Design {self.name} is already closed.")
184+
return
185+
186+
# Attempt to close the design
187+
try:
188+
self._design_stub.Close(EntityIdentifier(id=self._design_id))
189+
except Exception as err:
190+
self._grpc_client.log.warning(f"Design {self.name} could not be closed. Error: {err}.")
191+
self._grpc_client.log.warning("Ignoring response and assuming the design is closed.")
192+
193+
# Consider the design closed (even if the close request failed)
194+
self._is_closed = True
195+
173196
@protect_grpc
174197
def _activate(self, called_after_design_creation: bool = False) -> None:
175198
"""Activate the design."""

src/ansys/geometry/core/misc/checks.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def ensure_design_is_active(method):
4343

4444
def wrapper(self, *args, **kwargs):
4545
import ansys.geometry.core as pyansys_geometry
46+
from ansys.geometry.core.errors import GeometryRuntimeError
4647

4748
if pyansys_geometry.DISABLE_MULTIPLE_DESIGN_CHECK:
4849
# If the user has disabled the check, then we can skip it
@@ -66,12 +67,16 @@ def get_design_ref(obj) -> "Design":
6667
# Get the design reference
6768
design = get_design_ref(self)
6869

70+
# Verify whether the Design has been closed on the backend
71+
if design.is_closed:
72+
raise GeometryRuntimeError(
73+
"The design has been closed on the backend. Cannot perform any operations on it."
74+
)
75+
6976
# Activate the design if it is not active
7077
if not design.is_active:
7178
# First, check the backend allows for multiple documents
7279
if not design._grpc_client.multiple_designs_allowed:
73-
from ansys.geometry.core.errors import GeometryRuntimeError
74-
7580
raise GeometryRuntimeError(
7681
"The design is not active and multiple designs are "
7782
"not allowed with the current backend."

src/ansys/geometry/core/modeler.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,34 @@ def read_existing_design(self) -> "Design":
225225
)
226226
return self._designs[design.design_id]
227227

228-
def close(self) -> None:
229-
"""Access the client's close method."""
228+
def close(self, close_designs: bool = True) -> None:
229+
"""Access the client's close method.
230+
231+
Parameters
232+
----------
233+
close_designs : bool, default: True
234+
Whether to close all designs before closing the client.
235+
"""
236+
# Close all designs (if requested)
237+
[design.close() for design in self._designs.values() if close_designs]
238+
239+
# Close the client
230240
self.client.close()
231241

232-
def exit(self) -> None:
242+
def exit(self, close_designs: bool = True) -> None:
233243
"""Access the client's close method.
234244
235245
Notes
236246
-----
237247
This method is calling the same method as
238248
:func:`close() <ansys.geometry.core.modeler.Modeler.close>`.
249+
250+
Parameters
251+
----------
252+
close_designs : bool, default: True
253+
Whether to close all designs before closing the client.
239254
"""
240-
self.close()
255+
self.close(close_designs=close_designs)
241256

242257
def _upload_file(
243258
self,

0 commit comments

Comments
 (0)