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

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 249
50 Answers 100 Comments Second Anniversary 25 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: 249
    50 Answers 100 Comments Second Anniversary 25 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: 256
    50 Answers 100 Comments 100 Likes Second Anniversary
    ✭✭✭✭

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

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 249
    50 Answers 100 Comments Second Anniversary 25 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: 222
    100 Comments 100 Likes Second Anniversary Name Dropper
    ✭✭✭✭
    edited April 2024

    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

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 249
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    modified version of code to also account for scientific numbers in tabular data in Mechanical.
    Works only for UI mode, works only for point as a decimal (i.e. typically non german os)

    import os
    import wbjn
    import re
    
    currAnalysis = DataModel.AnalysisList[0]
    UserFilesDir = wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())")
    
    import codecs
    
    def writeCSV(t1, fileName):
        ExtAPI.Log.WriteMessage('my File name ' + fileName)
    
        with codecs.open(fileName, 'w', 'utf-8') as f:
            for line in t1:
                formatted_line = []
                for col in line:
                    try:
                        # Convert scientific notation to a readable format
                        if isinstance(col, float):
                            value = "%.10f" % col  # Adjust precision as needed
                        else:
                            value = str(col)
    
                        formatted_line.append(value)
                    except Exception as e:
                        ExtAPI.Log.WriteMessage("Encoding Error: {} - {}".format(col, e))
                        formatted_line.append("ERROR")
    
                f.write(", ".join(formatted_line) + '\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.MomentReaction]
    
    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)