How can I run a Mechanical script in batch mode?

Landon Mitchell Kanner
Landon Mitchell Kanner Member, Employee Posts: 290
100 Comments 25 Answers 25 Likes Photogenic
✭✭✭✭
edited June 2023 in Structures

I have a Mechanical script that I can run by opening Mechanical and clicking Scripting>>Open script>>Run. How can I run this script in a batch mode?

Best Answer

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 290
    100 Comments 25 Answers 25 Likes Photogenic
    ✭✭✭✭
    edited May 2023 Answer ✓

    Here are two options:


    1) Using PyMechanical:

    In Python:

    ## import pyMech
    from ansys.mechanical.core import launch_mechanical
    ## Launch Mechanical
    mechanical = launch_mechanical()
     
    ## Load mechdb file (optional)
    mechDBfile = r'D:\Temp\SYS.mechdb'
    cmd = 'ExtAPI.DataModel.Project.Open(r"{}")'.format(mechDBfile)
    mechanical.run_python_script(cmd)
     
    ## Run Script
    mechScriptfile = r'D:\Temp\MechScript.py'
    mechanical.run_python_script_from_file(mechScriptfile)
    


    2) Through Workbench:

    In command line (-F is optional to open an existing project):

    "C:\Program Files\ANSYS Inc\v212\Framework\bin\Win64\RunWB2.exe" -B -F "D:\Temp\MyProject.wbpj" -R D:\Temp\main.wbjn"
    

     Main.wbjn:

    with open(r'D:\Temp\MechScript.py') as f:
      MechCMDS= f.read()
    system1 = GetAllSystems()[0]
    model1 = system1.GetContainer(ComponentName="Model")
    model1.Edit(Hidden=True)
    model1.SendCommand(Command=MechCMDS,Language="Python")
    
    


    NOTE: When using Mechanical to mesh a Fluid system, change

    model1.Edit(Hidden=True)
    

    to

    model1.Edit(Interactive=False)