diff --git a/src/ansys/mapdl/core/convert.py b/src/ansys/mapdl/core/convert.py index bff911e66a..55d364a3a9 100644 --- a/src/ansys/mapdl/core/convert.py +++ b/src/ansys/mapdl/core/convert.py @@ -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 @@ -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", "" @@ -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": diff --git a/tests/test_convert.py b/tests/test_convert.py index 2ccd298696..1c2e93d6c2 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -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, + )