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 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 e61ef46ad..fbb78ae6d 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") @@ -54,21 +66,23 @@ def _bind_assembly_for_explicit_interface(assembly_name: str): 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() - binding_options.AllowExplicitInterfaceImplementation = True + 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 @@ -86,4 +100,16 @@ def initialize(version: int) -> None: __register_function_codec() Logger.debug("Registered function codec") - _bind_assembly_for_explicit_interface("Ansys.ACT.WB1") + 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 + )