Skip to content

Commit 7313aef

Browse files
dipinknairpre-commit-ci[bot]klmcadams
authored
Adding transaction for embedding (#542)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Kerry McAdams <[email protected]>
1 parent 9eb34d0 commit 7313aef

File tree

5 files changed

+81
-9
lines changed

5 files changed

+81
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ This document follows the conventions laid out in [Keep a CHANGELOG](https://kee
99

1010
### Added
1111

12-
- Add release note configuration ([#512](https://github.com/ansys/pymechanical/pull/512))- Add 242 to scheduled nightly run ([#519](https://github.com/ansys/pymechanical/pull/519))
12+
- Add release note configuration ([#512](https://github.com/ansys/pymechanical/pull/512))
13+
- Add 242 to scheduled nightly run ([#519](https://github.com/ansys/pymechanical/pull/519))
14+
- Add transaction for embedding ([#542](https://github.com/ansys/pymechanical/pull/542))
1315

1416
### Fixed
1517

doc/source/user_guide_embedding/globals.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ When using Mechanical scripting APIs (in either Mechanical's graphical user inte
88
sending scripts to a remote session of Mechanical), there are many global variables that are
99
by default usable from Python. Some of these are API entry points, like those discussed in
1010
:ref:`ref_user_guide_scripting`, while others are types and namespaces that are used by the
11-
scripting APIs. Examples of those are the ``Quantity`` class or the ``DataModel`` entry point.
11+
scripting APIs. Examples of those are the ``Quantity``, ``Transaction`` class or the ``DataModel`` entry point.
1212

1313
Embedding Mechanical into Python is as simple as constructing an application object. This can
1414
not automatically change the global variables available to the Python scope that constructed
@@ -25,10 +25,10 @@ following code:
2525
globals().update(global_variables(app))
2626
2727
28-
Some enum types are available when scripting inside of mechanical, such as `SelectionTypeEnum`
29-
or `LoadDefineBy`. Because these number in the thousands, by default, these enums are not
28+
Some enum types are available when scripting inside of mechanical, such as ``SelectionTypeEnum``
29+
or ``LoadDefineBy``. Because these number in the thousands, by default, these enums are not
3030
included in these global variables. To include enums, set the second argument of
31-
`global_variables`` to True.
31+
``global_variables`` to True.
3232

3333
.. code:: python
3434

doc/source/user_guide_scripting/index.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ tasks until after a block of commands are run. Here is an example:
6767

6868
.. code:: python
6969
70-
with Transaction():
71-
for obj in Tree:
72-
obj.Name = obj.Name + " suffix"
70+
with Transaction():
71+
for obj in Tree:
72+
obj.Name = obj.Name + " suffix"
7373
7474
API entry points
7575
^^^^^^^^^^^^^^^^

src/ansys/mechanical/core/embedding/imports.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def global_variables(app: "ansys.mechanical.core.App", enums: bool = False) -> t
4242

4343
clr.AddReference("System.Collections")
4444
clr.AddReference("Ansys.ACT.WB1")
45-
from Ansys.ACT.Mechanical import Transaction
45+
# from Ansys.ACT.Mechanical import Transaction
46+
# When ansys-pythonnet issue #14 is fixed, uncomment above
4647
from Ansys.Core.Units import Quantity
4748

4849
import System # isort: skip
@@ -71,3 +72,32 @@ def get_all_enums() -> typing.Dict[str, typing.Any]:
7172
if type(the_enum).__name__ == "CLRMetatype":
7273
enums[attr] = the_enum
7374
return enums
75+
76+
77+
class Transaction: # When ansys-pythonnet issue #14 is fixed, this class will be removed
78+
"""
79+
A class to speed up bulk user interactions using Ansys ACT Mechanical Transaction.
80+
81+
Example
82+
-------
83+
>>> with Transaction() as transaction:
84+
... pass # Perform bulk user interactions here
85+
...
86+
"""
87+
88+
def __init__(self):
89+
"""Initialize the Transaction class."""
90+
import clr
91+
92+
clr.AddReference("Ansys.ACT.WB1")
93+
import Ansys
94+
95+
self._transaction = Ansys.ACT.Mechanical.Transaction()
96+
97+
def __enter__(self):
98+
"""Enter the context of the transaction."""
99+
return self
100+
101+
def __exit__(self, exc_type, exc_val, exc_tb):
102+
"""Exit the context of the transaction and disposes of resources."""
103+
self._transaction.Dispose()

tests/embedding/test_globals.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (C) 2023 - 2024 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+
23+
"""Embedding tests for global variables associated with Mechanical"""
24+
import pytest
25+
26+
from ansys.mechanical.core import global_variables
27+
28+
29+
@pytest.mark.embedding
30+
def test_global_variables(embedded_app):
31+
"""Test the global variables"""
32+
globals_dict = global_variables(embedded_app, True)
33+
assert "ExtAPI" in globals_dict
34+
assert "DataModel" in globals_dict
35+
assert "Model" in globals_dict
36+
assert "Tree" in globals_dict
37+
assert "Quantity" in globals_dict
38+
assert "System" in globals_dict
39+
assert "Ansys" in globals_dict
40+
assert "Transaction" in globals_dict

0 commit comments

Comments
 (0)