|
| 1 | +# Copyright (C) 2016 - 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 | +"""Contains the ansPlugin class.""" |
| 24 | +import json |
| 25 | +import pathlib |
| 26 | +import weakref |
| 27 | + |
| 28 | +from ansys.api.mapdl.v0 import mapdl_pb2 |
| 29 | + |
| 30 | +from .common_grpc import ANSYS_VALUE_TYPE |
| 31 | +from .errors import MapdlRuntimeError |
| 32 | +from .misc import random_string |
| 33 | + |
| 34 | +class ansPlugin: |
| 35 | + """ |
| 36 | + ANSYS MAPDL Plugin Manager. |
| 37 | +
|
| 38 | + Examples |
| 39 | + -------- |
| 40 | + >>> from ansys.mapdl.core import launch_mapdl |
| 41 | + >>> mapdl = launch_mapdl() |
| 42 | + >>> plugin = mapdl.plugin |
| 43 | +
|
| 44 | + Load a plugin in the MAPDL Session |
| 45 | + """ |
| 46 | + |
| 47 | + def __init__(self, mapdl): |
| 48 | + """Initialize the class.""" |
| 49 | + from ansys.mapdl.core.mapdl_grpc import MapdlGrpc |
| 50 | + |
| 51 | + if not isinstance(mapdl, MapdlGrpc): # pragma: no cover |
| 52 | + raise TypeError("Must be initialized using MapdlGrpc class") |
| 53 | + |
| 54 | + self._mapdl_weakref = weakref.ref(mapdl) |
| 55 | + self._filename = None |
| 56 | + self._open = False |
| 57 | + |
| 58 | + @property |
| 59 | + def _mapdl(self): |
| 60 | + """Return the weakly referenced instance of mapdl.""" |
| 61 | + return self._mapdl_weakref() |
| 62 | + |
| 63 | + |
| 64 | + def load(self, plugin_name: str, feature: str = "CMD") -> str: |
| 65 | + """ |
| 66 | + Loads a plugin into MAPDL. |
| 67 | +
|
| 68 | + Parameters |
| 69 | + ---------- |
| 70 | + plugin_name : str |
| 71 | + Name of the plugin to load. |
| 72 | + feature : str |
| 73 | + Feature or module to activate in the plugin. |
| 74 | +
|
| 75 | + Returns |
| 76 | + ------- |
| 77 | + str |
| 78 | + Confirmation message about the loaded plugin. |
| 79 | +
|
| 80 | + Raises |
| 81 | + ------ |
| 82 | + PluginLoadError |
| 83 | + If the plugin fails to load. |
| 84 | + """ |
| 85 | + |
| 86 | + command = f"*PLUG,LOAD,{plugin_name},{feature}" |
| 87 | + response = self._mapdl.run(command) |
| 88 | + if "error" in response.lower(): |
| 89 | + raise PluginLoadError(f"Failed to load plugin '{plugin_name}' with feature '{feature}'.") |
| 90 | + return f"Plugin '{plugin_name}' with feature '{feature}' loaded successfully." |
| 91 | + |
| 92 | + |
| 93 | + def unload(self, plugin_name: str) -> str: |
| 94 | + """ |
| 95 | + Unloads a plugin from MAPDL. |
| 96 | +
|
| 97 | + Parameters |
| 98 | + ---------- |
| 99 | + plugin_name : str |
| 100 | + Name of the plugin to unload. |
| 101 | +
|
| 102 | + Returns |
| 103 | + ------- |
| 104 | + str |
| 105 | + Confirmation message about the unloaded plugin. |
| 106 | +
|
| 107 | + Raises |
| 108 | + ------ |
| 109 | + PluginUnloadError |
| 110 | + If the plugin fails to unload. |
| 111 | + """ |
| 112 | + |
| 113 | + command = f"*PLUG,UNLOAD,{plugin_name}" |
| 114 | + response = self._mapdl.run(command) |
| 115 | + if "error" in response.lower(): |
| 116 | + raise PluginUnloadError(f"Failed to unload plugin '{plugin_name}'.") |
| 117 | + return f"Plugin '{plugin_name}' unloaded successfully." |
| 118 | + |
| 119 | + |
| 120 | + def list(self) -> list: |
| 121 | + """ |
| 122 | + Lists all currently loaded plugins in MAPDL. |
| 123 | +
|
| 124 | + Returns |
| 125 | + ------- |
| 126 | + list |
| 127 | + A list of loaded plugin names. |
| 128 | +
|
| 129 | + Raises |
| 130 | + ------ |
| 131 | + RuntimeError |
| 132 | + If the plugin list cannot be retrieved. |
| 133 | + """ |
| 134 | + |
| 135 | + command = "*PLUG,LIST" |
| 136 | + response = self._mapdl.run(command) |
| 137 | + if "error" in response.lower(): |
| 138 | + raise RuntimeError("Failed to retrieve the list of loaded plugins.") |
| 139 | + # Parse response and extract plugin names (assuming response is newline-separated text) |
| 140 | + plugins = [line.strip() for line in response.splitlines() if line.strip()] |
| 141 | + return plugins |
0 commit comments