Python script from Ansys Workbench to read from Excel, update and re-read

Jharming

New Member
Joined
May 28, 2014
Messages
16
Hi,

I am using the code below to pick up parameters values in excel, They are then insertedin Ansys workbench. Ansys workbench use the parameters to do an simulation, and then the simulation results are written in the same excel document. As seen there is a commanbutton in Excel that activate the update sequence.

This works really well, but I would like to add some more to this script. I would like to be able to set up more simulation trials, i.e. first read one set of parameters, do a simulation write them, then read the next trial parameters, update the simulation and write them next to the previous results and so on.

One idea I have is to write all input parameters in a column and output parameters in a second column. Then use a IF sentence to see if there is a second row of parameters if there is then rerun if not then end. Is this something that sounds the right way to go or is there suggestion for a different approach?

Thx for any help

Jakob

Code:
# IronPython imports to enable Excel interop
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as Excel


workingDir = AbsUserPathName("H:\\Projects\\Ansys\\workbench_projects\\SPARTA_UPM3\\")


def updateHandler():


    ex.Application.DisplayAlerts = False


    # Define key ranges in the Workbook
    t_media    = worksheet.Range["E11"]
    t_ambient = worksheet.Range["F11"]
    p_stator = worksheet.Range["A7"]
    p_coil = worksheet.Range["B7"]
    p_diode = worksheet.Range["C7"]
    p_powermodule = worksheet.Range["D7"]
    p_relay = worksheet.Range["E7"]
    p_micro = worksheet.Range["F7"]
    p_fuse = worksheet.Range["G7"]
    k_silicone = worksheet.Range["A11"]
    T01 = worksheet.Range["B14"]
    T02 = worksheet.Range["B15"]
    T03 = worksheet.Range["B16"]
    T04 = worksheet.Range["B17"]
    T05 = worksheet.Range["B18"]
    T06 = worksheet.Range["B19"]
    T07 = worksheet.Range["B20"]
    T08 = worksheet.Range["B21"]
    T09 = worksheet.Range["B22"]
    T10 = worksheet.Range["B23"]
    T11 = worksheet.Range["B24"]
    T12 = worksheet.Range["B25"]
    T14 = worksheet.Range["B26"]
    T15 = worksheet.Range["B27"]
    T16 = worksheet.Range["B28"]
    T20 = worksheet.Range["B29"]
    inside_1 = worksheet.Range["B30"]
    inside_2 = worksheet.Range["B31"]
    control_center = worksheet.Range["B32"]


    # Get the Workbench Parameters
    t_media_param = Parameters.GetParameter(Name="P1")
    t_ambient_param = Parameters.GetParameter(Name="P2")
    p_stator_param = Parameters.GetParameter(Name="P3")
    p_coil_param = Parameters.GetParameter(Name="P4")
    p_diode_param = Parameters.GetParameter(Name="P5")
    p_powermodule_param = Parameters.GetParameter(Name="P6")
    p_relay_param = Parameters.GetParameter(Name="P7")
    p_micro_param = Parameters.GetParameter(Name="P8")
    p_fuse_param = Parameters.GetParameter(Name="P9")
    k_silicone_param = Parameters.GetParameter(Name="P10")
    T01_param = Parameters.GetParameter(Name="P12")
    T02_param = Parameters.GetParameter(Name="P13")
    T03_param = Parameters.GetParameter(Name="P14")
    T04_param = Parameters.GetParameter(Name="P15")
    T05_param = Parameters.GetParameter(Name="P16")
    T06_param = Parameters.GetParameter(Name="P17")
    T07_param = Parameters.GetParameter(Name="P18")
    T08_param = Parameters.GetParameter(Name="P19")
    T09_param = Parameters.GetParameter(Name="P20")
    T10_param = Parameters.GetParameter(Name="P21")
    T11_param = Parameters.GetParameter(Name="P22")
    T12_param = Parameters.GetParameter(Name="P23")
    T14_param = Parameters.GetParameter(Name="P24")
    T15_param = Parameters.GetParameter(Name="P25")
    T16_param = Parameters.GetParameter(Name="P26")
    T20_param = Parameters.GetParameter(Name="P27")
    inside_1_param = Parameters.GetParameter(Name="P28")
    inside_2_param = Parameters.GetParameter(Name="P29")
    control_cente_param = Parameters.GetParameter(Name="P30")


    # Assign values to the input parameters
    t_media_param.Expression = t_media.Value2.ToString()
    t_ambient_param.Expression = t_ambient.Value2.ToString()
    p_stator_param.Expression = p_stator.Value2.ToString()
    p_coil_param.Expression    = p_coil.Value2.ToString()
    p_diode_param.Expression = p_diode.Value2.ToString()
    p_powermodule_param.Expression = p_powermodule.Value2.ToString()
    p_relay_param.Expression = p_relay.Value2.ToString()
    p_micro_param.Expression = p_micro.Value2.ToString()
    p_fuse_param.Expression    = p_fuse.Value2.ToString()
    k_silicone_param.Expression = k_silicone.Value2.ToString()


    # Mark the parameter as updating in the workbook
    T01.Value2="Updating..."


    # Run the project update
    Update()


    # Update the workbook value from the WB parameter
    T01.Value2 = T01_param.Value.Value
    T02.Value2 = T02_param.Value.Value
    T03.Value2 = T03_param.Value.Value
    T04.Value2 = T04_param.Value.Value
    T05.Value2 = T05_param.Value.Value
    T06.Value2 = T06_param.Value.Value
    T07.Value2 = T07_param.Value.Value
    T08.Value2 = T08_param.Value.Value
    T09.Value2 = T09_param.Value.Value
    T10.Value2 = T10_param.Value.Value
    T11.Value2 = T11_param.Value.Value
    T12.Value2 = T12_param.Value.Value
    T14.Value2 = T14_param.Value.Value
    T15.Value2 = T15_param.Value.Value
    T16.Value2 = T16_param.Value.Value
    T20.Value2 = T20_param.Value.Value
    inside_1.Value2 = inside_1_param.Value.Value
    inside_2.Value2 = inside_2_param.Value.Value
    control_center.Value2 = control_cente_param.Value.Value


# Open the Workbench Project
Open(FilePath = workingDir + "SPARTA_UPM3.wbpj")


# Open Excel and the workbook
ex = Excel.ApplicationClass()
ex.Visible = True
workbook = ex.Workbooks.Open(workingDir + "SPARTA_UPM3.xlsx")
worksheet=workbook.ActiveSheet


#Apply the update handler to the workbook button
OLEbutton = worksheet.OLEObjects("CommandButton1")
commandButton = OLEbutton.Object
commandButton.CLICK += updateHandler
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi again,

I have rethought the problem I described above.

I think it might be easier to have an dedicated VBA macro to run in excel.

The code need to do the following:

1. Read input values in columnC row3-7
2. Copy input values from ColumnC row3-7 into ColumnA Row 3-7
3. Press command button
4. Wait until data has been written in ColumnA Row15-25
5. Copy data from ColumnA row15-25 to columnC row15-25
6. See if there is data in ColumnD row3-7 if there is data then copy to ColumnA row3-7
7. Press command button
8. Wait until data has been updated in ColumnA Row15-25
9. Copy data from ColumnA row15-25 to columnD row15-25
10. See if there is data in ColumnE row3-7 if there is data then copy to ColumnA row3-7
11. etc... until no data


Any guidance will be very nice!
 
Upvote 0

Forum statistics

Threads
1,215,572
Messages
6,125,605
Members
449,238
Latest member
wcbyers

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top