Skip to content
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

FIX: Add explicit interface support #1058

Merged
merged 13 commits into from
Jan 27, 2025
1 change: 1 addition & 0 deletions doc/changelog.d/1058.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add explicit interface support
2 changes: 1 addition & 1 deletion src/ansys/mechanical/core/embedding/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions src/ansys/mechanical/core/embedding/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,6 +45,25 @@
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

Check warning on line 53 in src/ansys/mechanical/core/embedding/runtime.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/runtime.py#L53

Added line #L53 was not covered by tests
except ModuleNotFoundError:
pass

import clr

assembly = clr.AddReference(assembly_name)
from Python.Runtime import BindingManager, BindingOptions
koubaa marked this conversation as resolved.
Show resolved Hide resolved

binding_options = BindingOptions()
binding_options.AllowExplicitInterfaceImplementation = True
BindingManager.SetBindingOptions(assembly, binding_options)


def initialize(version: int) -> None:
"""Initialize the runtime.

Expand All @@ -59,3 +79,5 @@
# function codec is distributed with pymechanical on linux only
# at version 242 or later
__register_function_codec()
koubaa marked this conversation as resolved.
Show resolved Hide resolved

_bind_assembly_for_explicit_interface("Ansys.ACT.WB1")
15 changes: 15 additions & 0 deletions tests/embedding/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
Loading