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

feat: converting chained APDL commands to PyMAPDL context manager #3154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/ansys/mapdl/core/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@
+ list(self._enum_block_commands)
)

self._chained_commands = 0
self.chained_commands = False
self._block_count = 0
self._block_count_target = 0
self._in_block = False
Expand Down Expand Up @@ -684,6 +686,17 @@

def translate_line(self, line):
"""Converts a single line from an ANSYS APDL script"""

if "$" in line:
# these are chained commands.
lines = line.split("$")
self.start_chained_commands()
for each_line in lines:
self.translate_line(each_line)

self.end_chained_commands()
return

self.comment = ""
original_line = line.replace("\r\n", "").replace(
"\n", ""
Expand Down Expand Up @@ -1133,6 +1146,21 @@
self.indent = self.indent[4:]
self.non_interactive = False

def start_chained_commands(self):
self._chained_commands += 1
if self.chained_commands:
return

Check warning on line 1152 in src/ansys/mapdl/core/convert.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/convert.py#L1152

Added line #L1152 was not covered by tests
line = f"{self.indent}with {self.obj_name}.chain_commands:"
self.lines.append(line)
self.chained_commands = True
self.indent = self.indent + " "

def end_chained_commands(self):
self._chained_commands -= 1
if self._chained_commands <= 0:
self.indent = self.indent[4:]
self.chained_commands = False

def output_to_file(self, line):
"""Return if an APDL line is redirecting to a file."""
if line[:4].upper() == "/OUT":
Expand Down
32 changes: 32 additions & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,35 @@ def test_convert_sf_all_inf():

def test_convert_slash_typef():
assert "mapdl.slashtype()" in convert_apdl_block("/TYPE", only_commands=True)


def test_chained_commands():
assert """with mapdl.chain_commands:
mapdl.type(11)
mapdl.real(11)
mapdl.mat(11)""" in convert_apdl_block(
"type,11 $real,11 $mat,11", only_commands=True
)

assert """with mapdl.chain_commands:
mapdl.esel("s")
mapdl.real(11)
mapdl.mat(22)
mapdl.com("hi")
# hello""" in convert_apdl_block(
"esel,s $real,11 $mat,22 $/com hi $!hello", only_commands=True
)

assert """mapdl.esel("s")
with mapdl.chain_commands:
mapdl.real(11)
mapdl.mat(22)
mapdl.com("hi")
# hello
mapdl.nsel()""" in convert_apdl_block(
"""
esel,s
real,11 $mat,22 $/com hi $!hello
nsel,""",
only_commands=True,
)
Loading