Skip to content

Commit

Permalink
Converting chained APDL commands to PyMAPDL context manager. (#3154)
Browse files Browse the repository at this point in the history
  • Loading branch information
germa89 authored Jun 5, 2024
1 parent 3dcb798 commit c61036a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
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 @@ def __init__(
+ 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 line_ending(self, line_ending):

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 @@ def end_non_interactive(self):
self.indent = self.indent[4:]
self.non_interactive = False

def start_chained_commands(self):
self._chained_commands += 1
if self.chained_commands:
return
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,
)

0 comments on commit c61036a

Please sign in to comment.