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

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
-
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)
1 -
Note GetPane is not supported and might not work in all cases.
0 -
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.
1 -
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
1 -
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)
0