diff --git a/doc/changelog.d/1058.fixed.md b/doc/changelog.d/1058.fixed.md new file mode 100644 index 000000000..da84df383 --- /dev/null +++ b/doc/changelog.d/1058.fixed.md @@ -0,0 +1 @@ +Add explicit interface support \ No newline at end of file diff --git a/src/ansys/mechanical/core/embedding/app.py b/src/ansys/mechanical/core/embedding/app.py index 708b3151a..e1f743457 100644 --- a/src/ansys/mechanical/core/embedding/app.py +++ b/src/ansys/mechanical/core/embedding/app.py @@ -181,8 +181,8 @@ def __init__(self, db_file=None, private_appdata=False, **kwargs): profile.update_environment(os.environ) atexit.register(_cleanup_private_appdata, profile) - self._app = _start_application(configuration, self._version, db_file) runtime.initialize(self._version) + self._app = _start_application(configuration, self._version, db_file) connect_warnings(self) self._poster = None diff --git a/src/ansys/mechanical/core/embedding/runtime.py b/src/ansys/mechanical/core/embedding/runtime.py index f609808c7..89b8a14cb 100644 --- a/src/ansys/mechanical/core/embedding/runtime.py +++ b/src/ansys/mechanical/core/embedding/runtime.py @@ -22,6 +22,7 @@ """Runtime initialize for pythonnet in embedding.""" +from importlib.metadata import distribution import os from ansys.mechanical.core.embedding.logger import Logger @@ -44,6 +45,25 @@ def __register_function_codec(): Ansys.Mechanical.CPython.Codecs.FunctionCodec.Register() +def _bind_assembly_for_explicit_interface(assembly_name: str): + """Bind the assembly for explicit interface implementation.""" + # if pythonnet is not installed, we can't bind the assembly + try: + distribution("pythonnet") + return + except ModuleNotFoundError: + pass + + import clr + + assembly = clr.AddReference(assembly_name) + from Python.Runtime import BindingManager, BindingOptions + + binding_options = BindingOptions() + binding_options.AllowExplicitInterfaceImplementation = True + BindingManager.SetBindingOptions(assembly, binding_options) + + def initialize(version: int) -> None: """Initialize the runtime. @@ -59,3 +79,5 @@ def initialize(version: int) -> None: # function codec is distributed with pymechanical on linux only # at version 242 or later __register_function_codec() + + _bind_assembly_for_explicit_interface("Ansys.ACT.WB1") diff --git a/tests/embedding/test_app.py b/tests/embedding/test_app.py index 910d83c8d..8a442b324 100644 --- a/tests/embedding/test_app.py +++ b/tests/embedding/test_app.py @@ -96,6 +96,21 @@ def test_app_update_globals_after_open(embedded_app, assets): Model.AddNamedSelection() +@pytest.mark.embedding +def test_explicit_interface(embedded_app): + """Test save and open of the Application class.""" + embedded_app.update_globals(globals()) + try: + namedselection = Model.AddNamedSelection() + ids = list(namedselection.Ids) + assert len(ids) == 0, f"Expected an empty Ids list, but got {ids}." + except AttributeError as e: + pytest.fail( + f"{str(e)}. This might be related to pythonnet." + "Uninstall pythonnet and install ansys-pythonnet." + ) + + @pytest.mark.embedding def test_app_version(embedded_app): """Test version of the Application class."""