Skip to content

Commit 7a8a716

Browse files
FredAnsgerma89
authored andcommitted
First tentative for plugin mapdl mechanism
1 parent bd1096f commit 7a8a716

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

src/ansys/mapdl/core/mapdl_core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
from ansys.mapdl.core.parameters import Parameters
8686
from ansys.mapdl.core.solution import Solution
8787
from ansys.mapdl.core.xpl import ansXpl
88+
from ansys.mapdl.core.plugin import ansPlugin
8889

8990
from ansys.mapdl.core.post import PostProcessing
9091

@@ -321,6 +322,8 @@ def __init__(
321322

322323
self._xpl: Optional[ansXpl] = None # Initialized in mapdl_grpc
323324

325+
self._plugin: Optional[ansPlugin] = None # Initialized in mapdl_grpc
326+
324327
from ansys.mapdl.core.component import ComponentManager
325328

326329
self._componentmanager: ComponentManager = ComponentManager(self)

src/ansys/mapdl/core/mapdl_grpc.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107

108108
from ansys.mapdl.core.database import MapdlDb
109109
from ansys.mapdl.core.xpl import ansXpl
110+
from ansys.mapdl.core.plugin import ansPlugin
110111

111112
TMP_VAR = "__tmpvar__"
112113
VOID_REQUEST = anskernel.EmptyRequest()
@@ -2799,6 +2800,26 @@ def xpl(self) -> "ansXpl":
27992800
self._xpl = ansXpl(self)
28002801
return self._xpl
28012802

2803+
@property
2804+
def plugin(self) -> "ansPlugin":
2805+
"""MAPDL plugin handler
2806+
2807+
Plugin Manager for MAPDL
2808+
2809+
Examples
2810+
--------
2811+
2812+
>>> from ansys import Mapdl
2813+
>>> mapdl = Mapdl()
2814+
>>> plugin = mapdl.plugin
2815+
>>> plugin.load('PluginDPF')
2816+
"""
2817+
if self._plugin is None:
2818+
from ansys.mapdl.core.plugin import ansPlugin
2819+
2820+
self._plugin = ansPlugin(self)
2821+
return self._plugin
2822+
28022823
@protect_grpc
28032824
def scalar_param(self, pname: str) -> float:
28042825
"""Return a scalar parameter as a float.

src/ansys/mapdl/core/plugin.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)