How to export Time History tabular data of Force Reaction in Mechanical as a csv file?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 175
100 Comments Photogenic Ansys Employee 5 Likes
✭✭✭✭

I have several Force reaction objects in Mechanical and I want to export the time history for each Force Reaction object as a separate csv file which should have the columns Time, Fx, Fy and F_Total

Comments

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 175
    100 Comments Photogenic Ansys Employee 5 Likes
    ✭✭✭✭

    Please use the below script in Mechanical Scripting console to export the Time history of each force reaction object as a separate csv file in the User Files directory of the Workbench Project.
    Please note that this script will work only in GUI mode (i.e. when Mechanical window is open), but will not work as a part of a batch script (when Mechanical is closed).

    import os
    import wbjn
    import re
    
    currAnalysis = DataModel.AnalysisList[0]
    UserFilesDir = wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())")
    
    def writeCSV(t1,fileName):
        ExtAPI.Log.WriteMessage('my File name ' + fileName)
        with open(fileName , 'w') as f:
            for line in t1:
                for col in line:
                    #For Non-German
                    f.write(str(col) + ', ')
                    #For German OS 
                    #f.write(str(col).replace(".",",") + '; ')
                f.write('\n')
    
    def getTableData(t0,colNum):
        t0.Activate()
        tempTable = []
        paneTabular=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData)
        control = paneTabular.ControlUnknown
        for row in range(1,control.RowsCount+1):
            tempRow = []
            for col in range(colNum,colNum+1):
                cellText= control.cell(row ,col ).Text
                tempRow.append(cellText)
            tempTable.append(tempRow)
        return tempTable
    
    ForceReactionCurrAnalysis = [child for child in currAnalysis.Solution.Children if child.DataModelObjectCategory == DataModelObjectCategory.ForceReaction]
    
    for result in ForceReactionCurrAnalysis:
        result.Activate()
        timeCol = [a[0] for a in getTableData(result,2)]
        xReaction =[a[0] for a in getTableData(result,3)]
        yReaction =[a[0] for a in getTableData(result,4)]
        zReaction =[a[0] for a in getTableData(result,5)]
        totalReaction =[a[0] for a in getTableData(result,6)]
        matrix_full = [[timeCol[i], xReaction[i], yReaction[i], zReaction[i], totalReaction[i]] for i in range(len(timeCol))]
        saveName = os.path.join(UserFilesDir, result.Name + '.csv')
        writeCSV(matrix_full,saveName)
    
  • M
    M Member, Employee Posts: 235
    100 Comments Photogenic 5 Likes Name Dropper
    ✭✭✭✭

    Note GetPane is not supported and might not work in all cases.

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 175
    100 Comments Photogenic Ansys Employee 5 Likes
    ✭✭✭✭

    Hi @M, yes, GetPane will only work when Mechanical GUI is open. But not when Mechanical GUI is closed (Batch runs, Design Point run etc). I already pointed this in my previous answer.

  • Vishnu
    Vishnu Member, Employee Posts: 216
    Solution Developer Community of Practice Member First Anniversary Name Dropper 100 Comments
    ✭✭✭✭
    edited April 18

    please refer the below post if you want to get around the batch not working issue. You may have to adapt the code to reaction force

    https://discuss.ansys.com/discussion/comment/1989#Comment_1989?utm_source=community-search&utm_medium=organic-search&utm_term=export+csv+dpf