How do I export average values of all results in a folder to a CSV file in Mechanical?

Options
Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 414
First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
edited August 2023 in Structures

How do I export average values of all results in a folder to a CSV file in Mechanical?

Comments

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 414
    First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
    Options

    The following code can be as is in the Mechanical Scripting console to get a csv file written out in the Solver Files Directory. Make sure that all the results you want exported are grouped together in a Folder called "Export"

    import os
    
    export_folder = ExtAPI.DataModel.GetObjectsByName("Export")[0]
    all_res = export_folder.Children
    
    analysis = Model.Analyses[0]
    unit = analysis.CurrentConsistentUnitFromQuantityName("%s" % all_res[0].OutputUnit)
    
    out_dir = analysis.WorkingDir
    out_file_path = os.path.join(out_dir, "all_avg_res.csv")
    out_file_handle = open(out_file_path, "w")
    
    out_file_handle.write("Result Name;Value in [%s]\n" % unit)
    
    for res in all_res:
        out_file_handle.write("{0}; {1}\n".format(res.Name, res.Average.Value))
    
    out_file_handle.close()