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

Member, Moderator, Employee Posts: 242
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

Welcome!

It looks like you're new here. Sign in or register to get started.

Comments

  • Member, Moderator, Employee Posts: 242
    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).

    1. import os
    2. import wbjn
    3. import re
    4.  
    5. currAnalysis = DataModel.AnalysisList[0]
    6. UserFilesDir = wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())")
    7.  
    8. def writeCSV(t1,fileName):
    9. ExtAPI.Log.WriteMessage('my File name ' + fileName)
    10. with open(fileName , 'w') as f:
    11. for line in t1:
    12. for col in line:
    13. #For Non-German
    14. f.write(str(col) + ', ')
    15. #For German OS
    16. #f.write(str(col).replace(".",",") + '; ')
    17. f.write('\n')
    18.  
    19. def getTableData(t0,colNum):
    20. t0.Activate()
    21. tempTable = []
    22. paneTabular=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData)
    23. control = paneTabular.ControlUnknown
    24. for row in range(1,control.RowsCount+1):
    25. tempRow = []
    26. for col in range(colNum,colNum+1):
    27. cellText= control.cell(row ,col ).Text
    28. tempRow.append(cellText)
    29. tempTable.append(tempRow)
    30. return tempTable
    31.  
    32. ForceReactionCurrAnalysis = [child for child in currAnalysis.Solution.Children if child.DataModelObjectCategory == DataModelObjectCategory.ForceReaction]
    33.  
    34. for result in ForceReactionCurrAnalysis:
    35. result.Activate()
    36. timeCol = [a[0] for a in getTableData(result,2)]
    37. xReaction =[a[0] for a in getTableData(result,3)]
    38. yReaction =[a[0] for a in getTableData(result,4)]
    39. zReaction =[a[0] for a in getTableData(result,5)]
    40. totalReaction =[a[0] for a in getTableData(result,6)]
    41. matrix_full = [[timeCol[i], xReaction[i], yReaction[i], zReaction[i], totalReaction[i]] for i in range(len(timeCol))]
    42. saveName = os.path.join(UserFilesDir, result.Name + '.csv')
    43. writeCSV(matrix_full,saveName)
  • Member, Employee Posts: 252
    50 Answers 100 Comments 100 Likes Second Anniversary
    ✭✭✭✭

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

  • Member, Moderator, Employee Posts: 242
    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.

  • 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

  • Member, Moderator, Employee Posts: 242
    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)

    1. import os
    2. import wbjn
    3. import re
    4.  
    5. currAnalysis = DataModel.AnalysisList[0]
    6. UserFilesDir = wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())")
    7.  
    8. import codecs
    9.  
    10. def writeCSV(t1, fileName):
    11. ExtAPI.Log.WriteMessage('my File name ' + fileName)
    12.  
    13. with codecs.open(fileName, 'w', 'utf-8') as f:
    14. for line in t1:
    15. formatted_line = []
    16. for col in line:
    17. try:
    18. # Convert scientific notation to a readable format
    19. if isinstance(col, float):
    20. value = "%.10f" % col # Adjust precision as needed
    21. else:
    22. value = str(col)
    23.  
    24. formatted_line.append(value)
    25. except Exception as e:
    26. ExtAPI.Log.WriteMessage("Encoding Error: {} - {}".format(col, e))
    27. formatted_line.append("ERROR")
    28.  
    29. f.write(", ".join(formatted_line) + '\n')
    30.  
    31.  
    32. def getTableData(t0,colNum):
    33. t0.Activate()
    34. tempTable = []
    35. paneTabular=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData)
    36. control = paneTabular.ControlUnknown
    37. for row in range(1,control.RowsCount+1):
    38. tempRow = []
    39. for col in range(colNum,colNum+1):
    40. cellText= control.cell(row ,col ).Text
    41. tempRow.append(cellText)
    42. tempTable.append(tempRow)
    43. return tempTable
    44.  
    45. ForceReactionCurrAnalysis = [child for child in currAnalysis.Solution.Children if child.DataModelObjectCategory == DataModelObjectCategory.MomentReaction]
    46.  
    47. for result in ForceReactionCurrAnalysis:
    48. result.Activate()
    49. timeCol = [a[0] for a in getTableData(result,2)]
    50. xReaction =[a[0] for a in getTableData(result,3)]
    51. yReaction =[a[0] for a in getTableData(result,4)]
    52. zReaction =[a[0] for a in getTableData(result,5)]
    53. totalReaction =[a[0] for a in getTableData(result,6)]
    54. matrix_full = [[timeCol[i], xReaction[i], yReaction[i], zReaction[i], totalReaction[i]] for i in range(len(timeCol))]
    55. saveName = os.path.join(UserFilesDir, result.Name + '.csv')
    56. writeCSV(matrix_full,saveName)

Welcome!

It looks like you're new here. Sign in or register to get started.