How can we write some tabular result data to a file , where each result type is next to each other?

Erik Kostson
Member, Moderator, Employee Posts: 312
✭✭✭✭
Say we have several different result objects and each of them are time dependent. We want to write some tabular result data to a file. Each result object results data should come next to each other, so if we have 4 columns per results object and 2 result objects (say displacement and stress), then the second stress result object should start at the position highlighted below. So the written data will be following the below pattern.
0
Best Answers
-
One possible way of doing this is shown in the sample script below:
ResultsOfIn = [] ResultsOfIn.append('Directional Deformation') ResultsOfIn.append('Equivalent Stress') myd='' results = DataModel.GetObjectsByType(DataModelObjectCategory.Result) res = DataModel.GetObjectsByName(ResultsOfInterest[0]) [0] res.Activate() Pane=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData) Con = Pane.ControlUnknown rows=Con.RowsCount with open('D:\mycsvfile2.csv', 'a') as file: for R in range(0,rows,1): print(R) for ires in ResultsOfIn: res = DataModel.GetObjectsByName(ires) [0] res.Activate() Pane=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData) Con = Pane.ControlUnknown for C in range(2,Con.ColumnsCount+1): myd=str(myd)+ ','+(Con.cell(R+1,C).Text) file.write(myd+'\n') myd=''
1 -
another possible solution...
##### INPUTS ##### #region Importing modules: import csv import wbjn #endregion ResultsOfInterest = [] ResultsOfInterest.append('Directional Deformation') ResultsOfInterest.append('Equivalent Stress') ResultsOfInterest.append('Equivalent Stress 2') ##### EXECUTION ##### def writeCSV(filename, data): # Function to write python list to a csv file with open(filename, 'wb') as csvfile: spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for row in data: spamwriter.writerow(row) cmd = 'returnValue(GetUserFilesDirectory())' user_dir = wbjn.ExecuteCommand(ExtAPI, cmd) AnalysisNumber=0 data=[] column=0 results = DataModel.GetObjectsByType(DataModelObjectCategory.Result) for i in ResultsOfInterest: res = DataModel.GetObjectsByName(i) [0] res.Activate() Pane=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData) Con = Pane.ControlUnknown for R in range(1,Con.RowsCount+1): data.append([]) for C in range(2,Con.ColumnsCount+1): data[-1].append(Con.cell(R,C).Text) # Step 1: Find indices where new datasets start dataset_indices = [i for i, row in enumerate(data) if row[0] == "Time [s]"] dataset_indices.append(len(data)) # Add end index for last dataset # Step 2: Extract all datasets datasets = [data[start:end] for start, end in zip(dataset_indices, dataset_indices[1:])] # Step 3: Merge datasets column-wise merged_data = [sum([ds[0] for ds in datasets], [])] # Merge headers for rows in zip(*[ds[1:] for ds in datasets]): # Merge data rows merged_data.append(sum(rows, [])) writeCSV(user_dir + "/" + Model.Analyses[AnalysisNumber].Name + " - " + ".csv", data) writeCSV(user_dir + "/" + Model.Analyses[AnalysisNumber].Name + " - 2" + ".csv", merged_data) print("Script has completed!") print("") print("Open File: " + chr(34) + user_dir + chr(92) + Model.Analyses[AnalysisNumber].Name + " - " + ".csv" + chr(34))
1
This discussion has been closed.