Skip to content

Commit f6f7409

Browse files
committed
Revert "Converter star slash improvements (#1478)"
This reverts commit 2ff3e06.
1 parent 02f77f0 commit f6f7409

File tree

2 files changed

+42
-126
lines changed

2 files changed

+42
-126
lines changed

src/ansys/mapdl/core/convert.py

Lines changed: 38 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def convert_script(
8484
cleanup_output=True,
8585
header=True,
8686
print_com=True,
87-
only_commands=False,
8887
):
8988
"""Converts an ANSYS input file to a python PyMAPDL script.
9089
@@ -145,12 +144,6 @@ def convert_script(
145144
Print command ``/COM`` arguments to python console.
146145
Defaults to ``True``.
147146
148-
only_commands : bool, optional
149-
If ``True``, it converts only the commands, meaning that header
150-
(``header=False``), imports (``add_imports=False``),
151-
and exit commands are NOT included (``auto_exit=False``).
152-
Overrides ``header``, ``add_imports`` and ``auto_exit``.
153-
154147
Returns
155148
-------
156149
list
@@ -197,7 +190,6 @@ def convert_script(
197190
cleanup_output=cleanup_output,
198191
header=header,
199192
print_com=print_com,
200-
only_commands=only_commands,
201193
)
202194

203195
translator.save(filename_out)
@@ -218,7 +210,6 @@ def convert_apdl_block(
218210
cleanup_output=True,
219211
header=True,
220212
print_com=True,
221-
only_commands=False,
222213
):
223214
"""Converts an ANSYS input string to a python PyMAPDL string.
224215
@@ -282,12 +273,6 @@ def convert_apdl_block(
282273
Print command ``/COM`` arguments to python console.
283274
Defaults to ``True``.
284275
285-
only_commands : bool, optional
286-
If ``True``, it converts only the commands, meaning that header
287-
(``header=False``), imports (``add_imports=False``),
288-
and exit commands are NOT included (``auto_exit=False``).
289-
Overrides ``header``, ``add_imports`` and ``auto_exit``.
290-
291276
Returns
292277
-------
293278
list
@@ -323,7 +308,6 @@ def convert_apdl_block(
323308
cleanup_output=cleanup_output,
324309
header=header,
325310
print_com=print_com,
326-
only_commands=only_commands,
327311
)
328312

329313
if isinstance(apdl_strings, str):
@@ -345,14 +329,8 @@ def _convert(
345329
cleanup_output=True,
346330
header=True,
347331
print_com=True,
348-
only_commands=False,
349332
):
350333

351-
if only_commands:
352-
auto_exit = False
353-
add_imports = False
354-
header = False
355-
356334
translator = FileTranslator(
357335
loglevel,
358336
line_ending,
@@ -608,40 +586,36 @@ def translate_line(self, line):
608586
# Cleaning ending empty arguments.
609587
# Because of an extra comma added to toffst command when generating ds.dat.
610588
line_ = line.split(",")[::-1] # inverting order
611-
612589
for ind, each in enumerate(line_):
613-
if each.strip(): # strip to remove spaces in empty arguments
590+
if each:
614591
break
615-
616-
line = ",".join(line_[ind:][::-1])
592+
else:
593+
line_.pop(ind)
594+
line = ",".join(line_[::-1])
617595

618596
# remove trailing comma
619597
line = line[:-1] if line[-1] == "," else line
620-
line_upper = line.upper()
621598

622-
cmd_caps = line.split(",")[0].upper()
623-
cmd_caps_short = cmd_caps[:4]
599+
cmd_ = line.split(",")[0].upper()
624600

625-
items = line.split(",")
626-
627-
if cmd_caps_short in ["SOLV", "LSSO"] and self._comment_solve:
601+
if cmd_[:4] in ["SOLV", "LSSO"] and self._comment_solve:
628602
self.store_command(
629603
"com", ["The following line has been commented due to `comment_solve`:"]
630604
)
631605
self.store_command("com", [line])
632606
return
633607

634-
if cmd_caps_short == "/COM":
608+
if cmd_[:4] == "/COM":
635609
# It is a comment
636610
self.store_command("com", [line[5:]])
637611
return
638612

639-
if cmd_caps == "*DO":
613+
if cmd_ == "*DO":
640614
self.start_non_interactive()
641615
self.store_run_command(line)
642616
return
643617

644-
if cmd_caps in ["*ENDDO", "*ENDIF"]:
618+
if cmd_ in ["*ENDDO", "*ENDIF"]:
645619
self.store_run_command(line)
646620
self.end_non_interactive()
647621
return
@@ -657,13 +631,13 @@ def translate_line(self, line):
657631
self.end_non_interactive()
658632
return
659633

660-
if cmd_caps == "/VERIFY":
634+
if cmd_ == "/VERIFY":
661635
self.store_run_command("FINISH")
662636
self.store_run_command(line)
663637
self.store_run_command("/PREP7")
664638
return
665639

666-
if cmd_caps_short == "*REP":
640+
if line[:4].upper() == "*REP":
667641
if not self.non_interactive:
668642
prev_cmd = self.lines.pop(-1)
669643
self.start_non_interactive()
@@ -677,40 +651,33 @@ def translate_line(self, line):
677651
self.end_non_interactive()
678652
return
679653

680-
if cmd_caps_short in COMMANDS_TO_NOT_BE_CONVERTED:
654+
if line[:4].upper() in COMMANDS_TO_NOT_BE_CONVERTED:
681655
self.store_run_command(line)
682656
return
683657

684-
if cmd_caps_short == "/TIT": # /TITLE
658+
if line[:4].upper() == "/TIT": # /TITLE
685659
parameters = line.split(",")[1:]
686660
return self.store_command("title", ["".join(parameters).strip()])
687661

688-
if cmd_caps_short == "*GET":
662+
if line[:4].upper() == "*GET":
689663
if self.non_interactive: # gives error
690664
self.store_run_command(line)
691665
return
692666
else:
693667
parameters = line.split(",")[1:]
694668
return self.store_command("get", parameters)
695669

696-
if cmd_caps_short == "/NOP":
670+
if line[:4].upper() == "/NOP":
697671
self.comment = (
698672
"It is not recommended to use '/NOPR' in a normal PyMAPDL session."
699673
)
700674
self.store_under_scored_run_command(line)
701675
return
702676

703-
if cmd_caps_short == "*CRE": # creating a function
704-
if self.macros_as_functions:
705-
self.start_function(items[1].strip())
706-
return
707-
else:
708-
self.start_non_interactive()
709-
710-
if cmd_caps == "/PREP7":
677+
if line[:6].upper() == "/PREP7":
711678
return self.store_command("prep7", [])
712679

713-
if "*END" in line_upper:
680+
if "*END" in line:
714681
if self.macros_as_functions:
715682
self.store_empty_line()
716683
self.store_empty_line()
@@ -726,7 +693,7 @@ def translate_line(self, line):
726693
return
727694

728695
# check for if statement
729-
if cmd_caps[:3] == "*IF" or "*IF" in line_upper:
696+
if line[:3].upper() == "*IF" or "*IF" in line.upper():
730697
self.start_non_interactive()
731698
self.store_run_command(line)
732699
return
@@ -745,14 +712,16 @@ def translate_line(self, line):
745712
): # To escape cmds that require (XX) but they are not in block
746713
self.end_non_interactive()
747714
return
748-
elif cmd_caps_short == "*USE" and self.macros_as_functions:
715+
elif line[:4] == "*USE" and self.macros_as_functions:
716+
items = line.split(",")
749717
func_name = items[1].strip()
750718
if func_name in self._functions:
751719
args = ", ".join(items[2:])
752720
self.lines.append(f"{func_name}({args})")
753721
return
754722

755723
# check if a line is setting a variable
724+
items = line.split(",")
756725
if "=" in items[0]: # line sets a variable:
757726
self.store_run_command(line)
758727
return
@@ -763,67 +732,54 @@ def translate_line(self, line):
763732
self.store_empty_line()
764733
return
765734

766-
if line == "-1" or line_upper == "END PREAD": # End of block commands
735+
if line == "-1" or line == "END PREAD": # End of block commands
767736
self.store_run_command(line)
768737
self._in_block = False
769738
self.end_non_interactive()
770739
return
771740

772741
# check valid command
773-
if (
774-
self._pymapdl_command(command) not in self._valid_commands
775-
or cmd_caps_short in self._non_interactive_commands
776-
):
777-
if cmd_caps_short in self._non_interactive_commands:
778-
if cmd_caps_short in self._block_commands:
742+
if command not in self._valid_commands:
743+
cmd = line[:4].upper()
744+
if cmd == "*CRE": # creating a function
745+
if self.macros_as_functions:
746+
self.start_function(items[1].strip())
747+
return
748+
else:
749+
self.start_non_interactive()
750+
751+
elif cmd in self._non_interactive_commands:
752+
if cmd in self._block_commands:
779753
self._in_block = True
780754
self._block_count = 0
781755
self._block_count_target = 0
782756

783-
elif cmd_caps_short in self._enum_block_commands:
757+
elif cmd in self._enum_block_commands:
784758
self._in_block = True
785759
self._block_count = 0
786-
if cmd_caps_short == "CMBL": # In cmblock
760+
if cmd == "CMBL": # In cmblock
787761
# CMBLOCK,Cname,Entity,NUMITEMS,,,,,KOPT
788762
numitems = int(line.split(",")[3])
789763
_block_count_target = (
790764
numitems // 8 + 1 if numitems % 8 != 0 else numitems // 8
791765
)
792766
self._block_count_target = (
793767
_block_count_target + 2
794-
) # because the cmd_caps_short line and option line.
768+
) # because the cmd line and option line.
795769

796-
self._block_current_cmd = cmd_caps_short
770+
self._block_current_cmd = cmd
797771
self.start_non_interactive()
798772

799-
if self._in_block and cmd_caps_short not in self._non_interactive_commands:
773+
if self._in_block and cmd not in self._non_interactive_commands:
800774
self.store_run_command(original_line)
801775
else:
802776
self.store_run_command(line)
803777

804778
elif self.use_function_names:
805-
if command[0] == "/":
806-
slash_command = f"slash{command[1:]}"
807-
if slash_command in dir(Commands):
808-
command = slash_command
809-
else:
810-
command = command[1:]
811-
elif command[0] == "*":
812-
star_command = f"star{command[1:]}"
813-
if star_command in dir(Commands):
814-
command = star_command
815-
else:
816-
command = command[1:]
817-
818779
self.store_command(command, parameters)
819780
else:
820781
self.store_run_command(line)
821782

822-
def _pymapdl_command(self, command):
823-
if command[0] in ["/", "*"]:
824-
command = command[1:]
825-
return command
826-
827783
def start_function(self, func_name):
828784
self._functions.append(func_name)
829785
self.store_empty_line()

tests/test_convert.py

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,10 @@ def test_comment_solve():
244244

245245

246246
def test_macro_to_function():
247-
output = convert_apdl_block(APDL_MACRO, macros_as_functions=True)
248-
assert "def SLV(" in output
249-
assert "SLV()" in output
250-
assert "\n\n\ndef SLV" in output
251-
assert "\n\n\nSLV" in output
247+
assert "def SLV(" in convert_apdl_block(APDL_MACRO, macros_as_functions=True)
248+
assert "SLV()" in convert_apdl_block(APDL_MACRO, macros_as_functions=True)
249+
assert "\n\n\ndef SLV" in convert_apdl_block(APDL_MACRO, macros_as_functions=True)
250+
assert "\n\n\nSLV" in convert_apdl_block(APDL_MACRO, macros_as_functions=True)
252251

253252

254253
def test_out():
@@ -362,42 +361,3 @@ def test_print_com_in_converter():
362361
assert "print_com=True" in convert_apdl_block("/prep7\nN,,,,") # Default
363362
assert "print_com=True" in convert_apdl_block("/prep7\nN,,,,", print_com=True)
364363
assert "print_com=True" not in convert_apdl_block("/prep7\nN,,,,", print_com=False)
365-
366-
367-
def test_only_commands():
368-
output = convert_apdl_block(
369-
"/view,1,1,1",
370-
only_commands=True,
371-
add_imports=True,
372-
auto_exit=True,
373-
header="asdf",
374-
)
375-
assert "mapdl.view(1, 1, 1)" in output
376-
assert "launch_mapdl" not in output
377-
assert "import" not in output
378-
assert "mapdl.exit" not in output
379-
380-
381-
@pytest.mark.parametrize(
382-
"parameters",
383-
[
384-
["/view,1,1,1", "mapdl.view(1, 1, 1)"],
385-
["/view,1,,1,1", 'mapdl.view(1, "", 1, 1)'],
386-
["/view,1,,1, ,1", 'mapdl.view(1, "", 1, "", 1)'],
387-
["*get,1,1,1", "mapdl.get(1, 1, 1)"],
388-
["*get,1,asdf,,1,qwert", 'mapdl.get(1, "asdf", "", 1, "qwert")'],
389-
["*get,1,asdf,,1,qwert", 'mapdl.get(1, "asdf", "", 1, "qwert")'],
390-
["vget,1,,'asdf',", 'mapdl.vget(1, "", "asdf")'],
391-
["*vget,1,,'asdf',", 'mapdl.starvget(1, "", "asdf")'],
392-
["*vget,1,,'asdf',,,,,", 'mapdl.starvget(1, "", "asdf")'],
393-
[
394-
"*vget,1,,,,,,,'asdf',,,,,",
395-
'mapdl.starvget(1, "", "", "", "", "", "", "asdf")',
396-
],
397-
["solve", "mapdl.solve()"],
398-
["/solu", "mapdl.slashsolu()"],
399-
["solu", "mapdl.solu()"],
400-
],
401-
)
402-
def test_convert_star_slash(parameters):
403-
assert convert_apdl_block(parameters[0], only_commands=True) == parameters[1]

0 commit comments

Comments
 (0)