@@ -84,6 +84,7 @@ def convert_script(
84
84
cleanup_output = True ,
85
85
header = True ,
86
86
print_com = True ,
87
+ only_commands = False ,
87
88
):
88
89
"""Converts an ANSYS input file to a python PyMAPDL script.
89
90
@@ -144,6 +145,12 @@ def convert_script(
144
145
Print command ``/COM`` arguments to python console.
145
146
Defaults to ``True``.
146
147
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
+
147
154
Returns
148
155
-------
149
156
list
@@ -190,6 +197,7 @@ def convert_script(
190
197
cleanup_output = cleanup_output ,
191
198
header = header ,
192
199
print_com = print_com ,
200
+ only_commands = only_commands ,
193
201
)
194
202
195
203
translator .save (filename_out )
@@ -210,6 +218,7 @@ def convert_apdl_block(
210
218
cleanup_output = True ,
211
219
header = True ,
212
220
print_com = True ,
221
+ only_commands = False ,
213
222
):
214
223
"""Converts an ANSYS input string to a python PyMAPDL string.
215
224
@@ -273,6 +282,12 @@ def convert_apdl_block(
273
282
Print command ``/COM`` arguments to python console.
274
283
Defaults to ``True``.
275
284
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
+
276
291
Returns
277
292
-------
278
293
list
@@ -308,6 +323,7 @@ def convert_apdl_block(
308
323
cleanup_output = cleanup_output ,
309
324
header = header ,
310
325
print_com = print_com ,
326
+ only_commands = only_commands ,
311
327
)
312
328
313
329
if isinstance (apdl_strings , str ):
@@ -329,8 +345,14 @@ def _convert(
329
345
cleanup_output = True ,
330
346
header = True ,
331
347
print_com = True ,
348
+ only_commands = False ,
332
349
):
333
350
351
+ if only_commands :
352
+ auto_exit = False
353
+ add_imports = False
354
+ header = False
355
+
334
356
translator = FileTranslator (
335
357
loglevel ,
336
358
line_ending ,
@@ -586,36 +608,40 @@ def translate_line(self, line):
586
608
# Cleaning ending empty arguments.
587
609
# Because of an extra comma added to toffst command when generating ds.dat.
588
610
line_ = line .split ("," )[::- 1 ] # inverting order
611
+
589
612
for ind , each in enumerate (line_ ):
590
- if each :
613
+ if each . strip (): # strip to remove spaces in empty arguments
591
614
break
592
- else :
593
- line_ .pop (ind )
594
- line = "," .join (line_ [::- 1 ])
615
+
616
+ line = "," .join (line_ [ind :][::- 1 ])
595
617
596
618
# remove trailing comma
597
619
line = line [:- 1 ] if line [- 1 ] == "," else line
620
+ line_upper = line .upper ()
598
621
599
- cmd_ = line .split ("," )[0 ].upper ()
622
+ cmd_caps = line .split ("," )[0 ].upper ()
623
+ cmd_caps_short = cmd_caps [:4 ]
600
624
601
- if cmd_ [:4 ] in ["SOLV" , "LSSO" ] and self ._comment_solve :
625
+ items = line .split ("," )
626
+
627
+ if cmd_caps_short in ["SOLV" , "LSSO" ] and self ._comment_solve :
602
628
self .store_command (
603
629
"com" , ["The following line has been commented due to `comment_solve`:" ]
604
630
)
605
631
self .store_command ("com" , [line ])
606
632
return
607
633
608
- if cmd_ [: 4 ] == "/COM" :
634
+ if cmd_caps_short == "/COM" :
609
635
# It is a comment
610
636
self .store_command ("com" , [line [5 :]])
611
637
return
612
638
613
- if cmd_ == "*DO" :
639
+ if cmd_caps == "*DO" :
614
640
self .start_non_interactive ()
615
641
self .store_run_command (line )
616
642
return
617
643
618
- if cmd_ in ["*ENDDO" , "*ENDIF" ]:
644
+ if cmd_caps in ["*ENDDO" , "*ENDIF" ]:
619
645
self .store_run_command (line )
620
646
self .end_non_interactive ()
621
647
return
@@ -631,13 +657,13 @@ def translate_line(self, line):
631
657
self .end_non_interactive ()
632
658
return
633
659
634
- if cmd_ == "/VERIFY" :
660
+ if cmd_caps == "/VERIFY" :
635
661
self .store_run_command ("FINISH" )
636
662
self .store_run_command (line )
637
663
self .store_run_command ("/PREP7" )
638
664
return
639
665
640
- if line [: 4 ]. upper () == "*REP" :
666
+ if cmd_caps_short == "*REP" :
641
667
if not self .non_interactive :
642
668
prev_cmd = self .lines .pop (- 1 )
643
669
self .start_non_interactive ()
@@ -651,33 +677,40 @@ def translate_line(self, line):
651
677
self .end_non_interactive ()
652
678
return
653
679
654
- if line [: 4 ]. upper () in COMMANDS_TO_NOT_BE_CONVERTED :
680
+ if cmd_caps_short in COMMANDS_TO_NOT_BE_CONVERTED :
655
681
self .store_run_command (line )
656
682
return
657
683
658
- if line [: 4 ]. upper () == "/TIT" : # /TITLE
684
+ if cmd_caps_short == "/TIT" : # /TITLE
659
685
parameters = line .split ("," )[1 :]
660
686
return self .store_command ("title" , ["" .join (parameters ).strip ()])
661
687
662
- if line [: 4 ]. upper () == "*GET" :
688
+ if cmd_caps_short == "*GET" :
663
689
if self .non_interactive : # gives error
664
690
self .store_run_command (line )
665
691
return
666
692
else :
667
693
parameters = line .split ("," )[1 :]
668
694
return self .store_command ("get" , parameters )
669
695
670
- if line [: 4 ]. upper () == "/NOP" :
696
+ if cmd_caps_short == "/NOP" :
671
697
self .comment = (
672
698
"It is not recommended to use '/NOPR' in a normal PyMAPDL session."
673
699
)
674
700
self .store_under_scored_run_command (line )
675
701
return
676
702
677
- if line [:6 ].upper () == "/PREP7" :
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" :
678
711
return self .store_command ("prep7" , [])
679
712
680
- if "*END" in line :
713
+ if "*END" in line_upper :
681
714
if self .macros_as_functions :
682
715
self .store_empty_line ()
683
716
self .store_empty_line ()
@@ -693,7 +726,7 @@ def translate_line(self, line):
693
726
return
694
727
695
728
# check for if statement
696
- if line [:3 ]. upper () == "*IF" or "*IF" in line . upper () :
729
+ if cmd_caps [:3 ] == "*IF" or "*IF" in line_upper :
697
730
self .start_non_interactive ()
698
731
self .store_run_command (line )
699
732
return
@@ -712,16 +745,14 @@ def translate_line(self, line):
712
745
): # To escape cmds that require (XX) but they are not in block
713
746
self .end_non_interactive ()
714
747
return
715
- elif line [:4 ] == "*USE" and self .macros_as_functions :
716
- items = line .split ("," )
748
+ elif cmd_caps_short == "*USE" and self .macros_as_functions :
717
749
func_name = items [1 ].strip ()
718
750
if func_name in self ._functions :
719
751
args = ", " .join (items [2 :])
720
752
self .lines .append (f"{ func_name } ({ args } )" )
721
753
return
722
754
723
755
# check if a line is setting a variable
724
- items = line .split ("," )
725
756
if "=" in items [0 ]: # line sets a variable:
726
757
self .store_run_command (line )
727
758
return
@@ -732,54 +763,67 @@ def translate_line(self, line):
732
763
self .store_empty_line ()
733
764
return
734
765
735
- if line == "-1" or line == "END PREAD" : # End of block commands
766
+ if line == "-1" or line_upper == "END PREAD" : # End of block commands
736
767
self .store_run_command (line )
737
768
self ._in_block = False
738
769
self .end_non_interactive ()
739
770
return
740
771
741
772
# check valid command
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 :
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 :
753
779
self ._in_block = True
754
780
self ._block_count = 0
755
781
self ._block_count_target = 0
756
782
757
- elif cmd in self ._enum_block_commands :
783
+ elif cmd_caps_short in self ._enum_block_commands :
758
784
self ._in_block = True
759
785
self ._block_count = 0
760
- if cmd == "CMBL" : # In cmblock
786
+ if cmd_caps_short == "CMBL" : # In cmblock
761
787
# CMBLOCK,Cname,Entity,NUMITEMS,,,,,KOPT
762
788
numitems = int (line .split ("," )[3 ])
763
789
_block_count_target = (
764
790
numitems // 8 + 1 if numitems % 8 != 0 else numitems // 8
765
791
)
766
792
self ._block_count_target = (
767
793
_block_count_target + 2
768
- ) # because the cmd line and option line.
794
+ ) # because the cmd_caps_short line and option line.
769
795
770
- self ._block_current_cmd = cmd
796
+ self ._block_current_cmd = cmd_caps_short
771
797
self .start_non_interactive ()
772
798
773
- if self ._in_block and cmd not in self ._non_interactive_commands :
799
+ if self ._in_block and cmd_caps_short not in self ._non_interactive_commands :
774
800
self .store_run_command (original_line )
775
801
else :
776
802
self .store_run_command (line )
777
803
778
804
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
+
779
818
self .store_command (command , parameters )
780
819
else :
781
820
self .store_run_command (line )
782
821
822
+ def _pymapdl_command (self , command ):
823
+ if command [0 ] in ["/" , "*" ]:
824
+ command = command [1 :]
825
+ return command
826
+
783
827
def start_function (self , func_name ):
784
828
self ._functions .append (func_name )
785
829
self .store_empty_line ()
0 commit comments