Set Analysis Settings from Excel file - demo extension

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 810
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
edited June 2023 in Structures

Below is an example extension that illustrates reading information from an Excel file and defining analysis settings (number of steps, step end tile, number of substeps) based on data written in Excel file.



  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 810
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓

    Excel file is defined as follows:

    Some important points:

    • Tab is called “AnalysisSettings”

    • User must first define number of steps in B6 cell

    • User can add new values (in columns) but should not change the lines (ie, step end time is read in line 10 for example)

    • Some settings are hardcoded in the extension and not read from file (Autotime stepping and “Define By” settings).

    The .xml file is:

            <extension version="1" name="SetAnalysisSettings">
            <guid shortid="SetAnalysisSettings">B8050A4F-DB37-46D9-BB33-349D70027F2D</guid>
              <script src="" />
              <interface context="Mechanical">
                <toolbar name="SetAnalysisSettings" caption="Set Analysis Settings">
                  <entry name="SetAnalysisSettings" icon="testIcon">

    The .py file is:

            def SetAnalysisSettings(analysis):
                import clr
                import Microsoft.Office.Interop.Excel as Excel
                excel = Excel.ApplicationClass()
                #excel.Visible = True # makes the Excel application visible to the user
                #excel.ScreenUpdating = True # enables screen refreshing.
                import Ansys.UI.Toolkit
                ## Create Windows explorer pop-up
                DefaultFolder = r'C:\'           
                FilePath =Ansys.UI.Toolkit.FileDialog.ShowOpenFilesDialog(Ansys.UI.Toolkit.Dialog(),DefaultFolder,'All Files(s)|*.*',0,'Select .fac file',None)
                if str(FilePath[0]) != 'OK':
                filename = list(FilePath[1])[0] # only one Excel file must be selected
                workbook = excel.Workbooks.Open(filename)
                inputdata=workbook.Worksheets("AnalysisSettings").Select()# Select Worksheet by Name 
                ws1 = workbook.Worksheets("AnalysisSettings")
                ## Create analysis settings from reading Excel sheet
                analysisSettings = analysis.AnalysisSettings
                # Read and store values
                numberSteps = int(ws1.Range("B6").Value2)
                for step in range(numberSteps): 
                    # read values    
                    stepEndTime = str(ws1.Cells(10,2+step).Value2) # read step end time
                    autotimeStepping = str(ws1.Cells(11,2+step).Value2) # read autotimeStepping
                    numberSubsteps = str(ws1.Cells(13,2+step).Value2)  # read number of substeps 
                    # store values        
                    stepEndTimeList.append(stepEndTime) # append to list of steps end time
                    autotimeSteppingList.append(autotimeStepping) # append to list of autotimeStepping
                    numberSubstepsList.append(numberSubsteps) # append to list of substeps
                # transform values with needed format
                stepEndTimeList = [Quantity(i+"[sec]") for i in stepEndTimeList]
                numberSubstepsList = [int(float(i)) for i in numberSubstepsList]
                # Apply settings
                for step in range(1,numberSteps+1):             
                    analysisSettings.NumberOfSteps = step
                    analysisSettings.CurrentStepNumber = step # set current step number        
                    analysisSettings.StepEndTime = stepEndTimeList[step-1]# apply step end time        
                    analysisSettings.DefineBy = TimeStepDefineByType.Substeps
                    analysisSettings.NumberOfSubSteps = numberSubstepsList[step-1]
                    analysisSettings.AutomaticTimeStepping = AutomaticTimeStepping.Off
                excel.Application.Quit()            ## Close Only the Excel file.
                excel.Quit()                        ## Close entire Excel