How to export the table pane contents to an excel using mechanical scripting?

Vishnu
Vishnu Member, Employee Posts: 222
100 Comments 100 Likes Name Dropper First Anniversary
✭✭✭✭
edited June 2023 in Structures

You could do the same for multiple result objects,forceobjects,ACT objects: Please see below script to add multiple items in one go.

Export to excel

Basically any object which has contents in tabular pane can be exported in similar way

Tagged:

Best Answers

  • Vishnu
    Vishnu Member, Employee Posts: 222
    100 Comments 100 Likes Name Dropper First Anniversary
    ✭✭✭✭
    Answer ✓

    Make Sure you edit result_names argument in last line before you execute the below script as per your requirement

    def extractresultdata_to_excel(result_names):
        '''
        **************************************************************************
        extracts the result table to an excel and store them in user files dir
        use like: 
        extractresultdata_to_excel(['Total Deformation', 'Equivalent Stress'])
        **************************************************************************
        '''
        #Get the alphabet number corresponding to excel cell
        def Cells(a, b):
            return str(chr(a+96) + str(b))
        #Import statements
        import clr
        clr.AddReference("Microsoft.Office.Interop.Excel")
        import Microsoft.Office.Interop.Excel as Excel
        import os
        from System import Array
        # Loop every object and extract table pane contents
        for result_name in result_names:
            resultobject = ExtAPI.DataModel.GetObjectsByName(result_name)[0]
            obj = resultobject
            while obj.GetType()!= Ansys.ACT.Automation.Mechanical.Analysis:obj = obj.Parent
            analysis = obj
            user_files_dir = os.path.join(
                analysis.WorkingDir, '../../../user_files')
            file_name = os.path.join(user_files_dir, result_name+'_result'+'.xlsx')
            resultobject.Activate()
            paneTabular = ExtAPI.UserInterface.GetPane(
                MechanicalPanelEnum.TabularData)
            control = paneTabular.ControlUnknown
            num_columns = control.ColumnsCount+1
            num_rows = control.RowsCount+1
            result = []
            for row in range(1, num_rows):
                r = []
                for col in range(1, num_columns):
                    r.append(control.cell(row, col).Text)
                result.append(r)
            #Write the table pane contents to excel
            ex = Excel.ApplicationClass()
            ex.Visible = False
            ex.DisplayAlerts = False
            workbook = ex.Workbooks.Add()
            worksheet = workbook.Worksheets[1]
            worksheet.Name = "Results"
            array = Array.CreateInstance(object, len(result), len(result[0]))
            for rowcount, row in enumerate(result):
                for columncount, column in enumerate(row):
                    array[rowcount, columncount] = column
            worksheet.Range[Cells(1, 1), Cells(
                len(result[0]), len(result))].Value2 = array
            worksheet.Columns("A:AA").Autofit()
            worksheet.Columns("A:AA").HorizontalAlignment = -4108
            worksheet.Columns("A:AA").VerticalAlignment = -4108
            worksheet.Columns("A:AA").Font.Bold = True
            ex.DisplayAlerts = False
            #save excel in user files dir
            workbook.SaveAs(file_name)
            ex.DisplayAlerts = True
            ex.Visible = True  # Comment if do not want to see excel popping up
    
    #Use the above function. Please edit result names argument below as per your case
    extractresultdata_to_excel(
        ['Total Deformation', 'Equivalent Stress','Force'])
    #note: you could as well add force or displacement objects to export too
    
    

    - Also Sharing a button .

    After importing button, All you need to do is select objects you need to to export table contents in mechanical to an excel and click the button. (Make sure the objects selected has table pane contents available)

    Export_table_button

  • M
    M Member, Employee Posts: 239
    50 Answers 100 Comments 100 Likes Second Anniversary
    ✭✭✭✭
    Answer ✓

    Note that the GetPane method is not supported.

  • Vishnu
    Vishnu Member, Employee Posts: 222
    100 Comments 100 Likes Name Dropper First Anniversary
    ✭✭✭✭
    Answer ✓

    for a more modern way without using GetPane refer below link:

    https://discuss.ansys.com/discussion/comment/1989#Comment_1989?utm_source=community-search&utm_medium=organic-search&utm_term=export+csv+dpf

    This should also work in batch mode