From b9847517727859a3a55d2ce64236133c39056a79 Mon Sep 17 00:00:00 2001 From: dkunhamb Date: Fri, 11 Jul 2025 10:45:00 -0500 Subject: [PATCH 1/3] add option for pep8 aliases --- .../mechanical/core/embedding/runtime.py | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/ansys/mechanical/core/embedding/runtime.py b/src/ansys/mechanical/core/embedding/runtime.py index e61ef46ad..534e4136c 100644 --- a/src/ansys/mechanical/core/embedding/runtime.py +++ b/src/ansys/mechanical/core/embedding/runtime.py @@ -45,8 +45,20 @@ 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.""" +def _bind_assembly( + assembly_name: str, explicit_interface: bool = False, pep8_aliases: bool = False +) -> None: + """Bind the assembly for explicit interface and/or pep8 aliases. + + Parameters + ---------- + assembly_name : str + The name of the assembly to bind. + explicit_interface : bool, optional + If True, allows explicit interface implementation. Default is False. + pep8_aliases : bool, optional + If True, enables PEP 8 aliases. Default is False. + """ # if pythonnet is not installed, we can't bind the assembly try: distribution("pythonnet") @@ -64,7 +76,10 @@ def _bind_assembly_for_explicit_interface(assembly_name: str): from Python.Runtime import BindingManager, BindingOptions binding_options = BindingOptions() - binding_options.AllowExplicitInterfaceImplementation = True + if explicit_interface: + binding_options.AllowExplicitInterfaceImplementation = True + if pep8_aliases: + binding_options.Pep8Aliases = True BindingManager.SetBindingOptions(assembly, binding_options) @@ -86,4 +101,4 @@ def initialize(version: int) -> None: __register_function_codec() Logger.debug("Registered function codec") - _bind_assembly_for_explicit_interface("Ansys.ACT.WB1") + _bind_assembly("Ansys.ACT.WB1", True, True) From 0bf6c64896d646d49874877f4b6b9254affcc594 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Fri, 11 Jul 2025 15:48:01 +0000 Subject: [PATCH 2/3] chore: adding changelog file 1234.added.md [dependabot-skip] --- doc/changelog.d/1234.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/1234.added.md diff --git a/doc/changelog.d/1234.added.md b/doc/changelog.d/1234.added.md new file mode 100644 index 000000000..366e34f3e --- /dev/null +++ b/doc/changelog.d/1234.added.md @@ -0,0 +1 @@ +Add option for pep8 aliases in binding \ No newline at end of file From 907da208dd010655f9c172240578db5c0f76fb88 Mon Sep 17 00:00:00 2001 From: dkunhamb Date: Mon, 14 Jul 2025 13:31:41 -0500 Subject: [PATCH 3/3] update pep8 opt in --- src/ansys/mechanical/core/embedding/app.py | 5 +++- .../mechanical/core/embedding/runtime.py | 23 ++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/ansys/mechanical/core/embedding/app.py b/src/ansys/mechanical/core/embedding/app.py index e9d65cbfb..542ebf544 100644 --- a/src/ansys/mechanical/core/embedding/app.py +++ b/src/ansys/mechanical/core/embedding/app.py @@ -144,6 +144,8 @@ class App: Whether to enable logging. Default is True. log_level : str, optional The logging level for the application. Default is "WARNING". + pep8 : bool, optional + Whether to enable PEP 8 style binding for the assembly. Default is False. Examples -------- @@ -231,7 +233,8 @@ def __init__(self, db_file=None, private_appdata=False, **kwargs): profile = UniqueUserProfile(new_profile_name, copy_profile=copy_profile) profile.update_environment(os.environ) - runtime.initialize(self._version) + _pep8_alias = kwargs.get("pep8", False) + runtime.initialize(self._version, pep8_aliases=_pep8_alias) 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 534e4136c..fbb78ae6d 100644 --- a/src/ansys/mechanical/core/embedding/runtime.py +++ b/src/ansys/mechanical/core/embedding/runtime.py @@ -66,24 +66,23 @@ def _bind_assembly( return except ModuleNotFoundError: pass - - Logger.debug(f"Binding assembly for explicit interface {assembly_name}") import clr - Logger.debug(f"Binding assembly for explicit interface, Loading {assembly_name}") + Logger.debug(f"Binding assembly {assembly_name}") assembly = clr.AddReference(assembly_name) - Logger.debug(f"Binding assembly for explicit interface, Loaded {assembly_name}") from Python.Runtime import BindingManager, BindingOptions binding_options = BindingOptions() if explicit_interface: + Logger.debug(f"Binding explicit interface for {assembly_name}") binding_options.AllowExplicitInterfaceImplementation = True if pep8_aliases: + Logger.debug(f"Setting PEP 8 aliases for {assembly_name}") binding_options.Pep8Aliases = True BindingManager.SetBindingOptions(assembly, binding_options) -def initialize(version: int) -> None: +def initialize(version: int, pep8_aliases: bool = False) -> None: """Initialize the runtime. Pythonnet is already initialized but we need to @@ -101,4 +100,16 @@ def initialize(version: int) -> None: __register_function_codec() Logger.debug("Registered function codec") - _bind_assembly("Ansys.ACT.WB1", True, True) + explicit_interface = True # Currently no option to disable this + pep8_aliases = pep8_aliases + if os.environ.get("PYMECHANICAL_BINDING", "0") == "1": + pep8_aliases = True + explicit_interface = True + if os.environ.get("PYMECHANICAL_EXPLICIT_INTERFACE", "0") == "1": + explicit_interface = True + if os.environ.get("PYMECHANICAL_PEP8_ALIASES", "0") == "1": + pep8_aliases = True + + _bind_assembly( + "Ansys.ACT.WB1", explicit_interface=explicit_interface, pep8_aliases=pep8_aliases + )