How do I export Tabular data of selected result objects to a CSV?

Options
Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 419
5 Likes First Anniversary Ansys Employee Solution Developer Community of Practice Member
edited January 2023 in General Language Questions

How do I export Tabular data of selected result objects to a CSV?

Comments

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 419
    5 Likes First Anniversary Ansys Employee Solution Developer Community of Practice Member
    edited January 2023
    Options

    The following code can be saved as a Button. Select all the result objects, which you want exported in a CSV file. On being clicked a "data.csv" file is written in the User Files Directory of the project.

    import os
    import wbjn
    
    userfilesdir = wbjn.ExecuteCommand(ExtAPI,"""returnValue(GetUserFilesDirectory())""")
    
    file_name = os.path.join(userfilesdir, "data.csv")
    file_handle = open(file_name, "w")
    
    time_unit = ExtAPI.DataModel.CurrentUnitFromQuantityName("Time")
    temp_unit = ExtAPI.DataModel.CurrentUnitFromQuantityName("Temperature")
    
    for index, temp in enumerate(Tree.ActiveObjects):
        temp.Activate()
        paneTabular=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData)
        control = paneTabular.ControlUnknown
        file_handle.write("Result Object %s - %s\n" % (index, temp.Name))
        file_handle.write("Time[{0}];Minimum[{1}];Maximum[{1}];Average[{1}]\n".format(time_unit, temp_unit))
        for row in range(2, control.RowsCount+1):
            for col in range(2, control.ColumnsCount+1):
                cellText = control.cell(row, col).Text
                file_handle.write("%s;" % cellText)
            file_handle.write("\n")
    
    file_handle.close()
    

    Please refer to Ansys Help link on How to create user-defined button in Mechanical?

  • M
    M Member, Employee Posts: 231
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    edited February 2023
    Options

    The paneTabular method doesn't work on linux, at least it didn't for me and it's not supported.

    Supported and slower method:


    import wbjn, os
    ufDir=wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())")
    analyses = ExtAPI.DataModel.Project.Model.Analyses
    print(ufDir)
      
    with Transaction():# after 2020R1 (suspendClicks=True) to accelerate the process
        for analysis in analyses:
            time_steps =analysis.GetResultsData().ListTimeFreq
            for result in analysis.Solution.Children:
                if 'Results' in result.GetType().ToString():
                    result.By =  SetDriverStyle.Time
                    t_unit = result.Time.Unit
                    first = True
                    saveName = os.path.join(ufDir,analysis.Name + '_' + result.Name + '.csv')
                    with open(saveName,'w') as f:
                        for time in time_steps:
                            result.DisplayTime = Quantity(time,t_unit)
                            result.EvaluateAllResults()
                            Min = result.Minimum
                            Max = result.Maximum
                            Ave = result.Average
                            if first:
                                f.write('time, ' + Min.Unit + ', ' +  Max.Unit + ', ' +  Ave.Unit + '\n' )
                                first = False
                            f.write(str(time) + ', ' + str(Min.Value) + ', ' +  str(Max.Value) + ', ' +  str(Ave.Value) + '\n' )
    
  • Collins
    Collins Member Posts: 17
    First Comment Name Dropper
    edited May 10
    Options

    @M

    I also experienced the same error on Linux

    control = paneTabular.ControlUnknown

    doesn't work.

    Did you find an improved way?

    The suggested code is super slow.