Set Analysis Settings from Excel file - demo extension

Member, Moderator, Employee Posts: 873
100 Answers 500 Comments 250 Likes Second Anniversary
✭✭✭✭
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.

Tagged:

Answers

  • Member, Moderator, Employee Posts: 873
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭
    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:

    1. <extension version="1" name="SetAnalysisSettings">
    2. <guid shortid="SetAnalysisSettings">B8050A4F-DB37-46D9-BB33-349D70027F2D</guid>
    3. <script src="main.py" />
    4. <interface context="Mechanical">
    5. <images>images</images>
    6. <toolbar name="SetAnalysisSettings" caption="Set Analysis Settings">
    7. <entry name="SetAnalysisSettings" icon="testIcon">
    8. <callbacks>
    9. <onclick>SetAnalysisSettings</onclick>
    10. </callbacks>
    11. </entry>
    12. </toolbar>
    13. </interface>
    14. </extension>

    The .py file is:

    1. def SetAnalysisSettings(analysis):
    2. import clr
    3. clr.AddReference("Ans.UI.Toolkit")
    4. clr.AddReference("Microsoft.Office.Interop.Excel")
    5. import Microsoft.Office.Interop.Excel as Excel
    6. excel = Excel.ApplicationClass()
    7. #excel.Visible = True # makes the Excel application visible to the user
    8. #excel.ScreenUpdating = True # enables screen refreshing.
    9. import Ansys.UI.Toolkit
    10. ## Create Windows explorer pop-up
    11. DefaultFolder = r'C:\'
    12. FilePath =Ansys.UI.Toolkit.FileDialog.ShowOpenFilesDialog(Ansys.UI.Toolkit.Dialog(),DefaultFolder,'All Files(s)|*.*',0,'Select .fac file',None)
    13. if str(FilePath[0]) != 'OK':
    14. return
    15. filename = list(FilePath[1])[0] # only one Excel file must be selected
    16. workbook = excel.Workbooks.Open(filename)
    17. inputdata=workbook.Worksheets("AnalysisSettings").Select()# Select Worksheet by Name
    18. ws1 = workbook.Worksheets("AnalysisSettings")
    19.  
    20. ## Create analysis settings from reading Excel sheet
    21. analysisSettings = analysis.AnalysisSettings
    22. # Read and store values
    23. numberSteps = int(ws1.Range("B6").Value2)
    24. stepEndTimeList=[]
    25. autotimeSteppingList=[]
    26. numberSubstepsList=[]
    27. for step in range(numberSteps):
    28. # read values
    29. stepEndTime = str(ws1.Cells(10,2+step).Value2) # read step end time
    30. autotimeStepping = str(ws1.Cells(11,2+step).Value2) # read autotimeStepping
    31. numberSubsteps = str(ws1.Cells(13,2+step).Value2) # read number of substeps
    32. # store values
    33. stepEndTimeList.append(stepEndTime) # append to list of steps end time
    34. autotimeSteppingList.append(autotimeStepping) # append to list of autotimeStepping
    35. numberSubstepsList.append(numberSubsteps) # append to list of substeps
    36. # transform values with needed format
    37. stepEndTimeList = [Quantity(i+"[sec]") for i in stepEndTimeList]
    38. numberSubstepsList = [int(float(i)) for i in numberSubstepsList]
    39. # Apply settings
    40. analysisSettings.Activate()
    41. for step in range(1,numberSteps+1):
    42. analysisSettings.NumberOfSteps = step
    43. analysisSettings.CurrentStepNumber = step # set current step number
    44. analysisSettings.StepEndTime = stepEndTimeList[step-1]# apply step end time
    45. analysisSettings.DefineBy = TimeStepDefineByType.Substeps
    46. analysisSettings.NumberOfSubSteps = numberSubstepsList[step-1]
    47. analysisSettings.AutomaticTimeStepping = AutomaticTimeStepping.Off
    48. ExtAPI.Log.WriteMessage(str(numberSubstepsList[step-1]))
    49. ExtAPI.Log.WriteMessage(str(analysisSettings.NumberOfSubSteps))
    50. ExtAPI.DataModel.Tree.Refresh()
    51.  
    52. excel.Application.Quit() ## Close Only the Excel file.
    53. excel.Quit() ## Close entire Excel

Welcome!

It looks like you're new here. Sign in or register to get started.