Export Solution Combination Results to Table

Karri Deepak
Karri Deepak Member, Employee Posts: 3
Photogenic First Comment
✭✭✭

How can I export solution combination results to an Excel sheet?

Answers

  • Karri Deepak
    Karri Deepak Member, Employee Posts: 3
    Photogenic First Comment
    ✭✭✭
    edited December 2024

    The following code can be used:

  • Karri Deepak
    Karri Deepak Member, Employee Posts: 3
    Photogenic First Comment
    ✭✭✭
    import os
    import csv
    
    #Define the directory to save the output file
    directory = 'D:/'
    #Initialize a combined result container
    combined_result = []
    solCombObjs = DataModel.GetObjectsByType(DataModelObjectCategory.SolutionCombination)
    for obj in solCombObjs:
        for solution in obj.Children:
            if solution.Name != 'Solution Information':
                #Activate the solution to access its data
                solution.Activate()
                #Access the Tabular Data pane
                paneTabular = ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData)
                control = paneTabular.ControlUnknown
                #Retrieve row and column counts
                num_columns = control.ColumnsCount+1
                num_rows = control.RowsCount+1
                # Add a header or separator for clarity in the combined result
                combined_result.append([solution.Name])
                #Loop through rows and columns to fetch data
                for row in range(1, num_rows):
                    row_data = []
                    for col in range(1, num_columns):
                        row_data.append(control.cell(row, col).Text)
                    combined_result.append(row_data)
                #Add a blank line as a separator between solution results (optional)
    #Define the output file name
    output_file = os.path.join(directory, 'Solution_Combination_file.csv')
    #Write the combined results to the CSV file
    with open(output_file, 'wb') as f:
        writer = csv.writer(f)
        writer.writerows(combined_result)