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

API Method to Retrieve File Name and Status #384

Merged
merged 9 commits into from
Oct 8, 2024
16 changes: 16 additions & 0 deletions src/ansys/motorcad/core/methods/rpc_methods_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# SOFTWARE.

"""RPC methods for variables."""
from warnings import warn


class _RpcMethodsVariables:
Expand Down Expand Up @@ -136,3 +137,18 @@ def set_array_variable(self, array_name, array_index, variable_value):
method = "SetArrayVariable"
params = [array_name, array_index, {"variant": variable_value}]
return self.connection.send_and_receive(method, params)

def get_file_name(self):
"""Get current .mot file name and path.

Returns
-------
str
Current .mot file path and name
"""
method = "GetMotorCADFileName"
if self.connection.send_and_receive(method) == "":
warn("No file has been loaded in this MotorCAD instance")
return None
else:
return self.connection.send_and_receive(method)
24 changes: 22 additions & 2 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from os import path, remove

import pytest

from RPC_Test_Common import reset_to_default_file
from ansys.motorcad.core import MotorCADError
from RPC_Test_Common import get_dir_path, reset_to_default_file
from ansys.motorcad.core import MotorCAD, MotorCADError


def test_get_variable(mc):
Expand Down Expand Up @@ -123,3 +125,21 @@ def test_restore_compatibility_settings(mc):

mc.restore_compatibility_settings()
assert mc.get_variable(test_compatibility_setting) == improved_method


def test_get_file_name():
mc = MotorCAD()

file_path = get_dir_path() + r"\test_files\temp_files\Get_File_Name.mot"

if path.exists(file_path):
remove(file_path)

assert path.exists(file_path) is False

with pytest.warns():
mc.get_file_name()

mc.save_to_file(file_path)
assert mc.get_file_name() == file_path
remove(file_path)
Loading