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

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 80
25 Answers 25 Likes 10 Comments First Anniversary

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: 80
    25 Answers 25 Likes 10 Comments First Anniversary

    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)