Combin39 in mechanical automatically using input force vs deflection data from an Excel file

Erik Kostson
Erik Kostson Member, Employee Posts: 202
50 Answers 100 Comments Photogenic 5 Likes
✭✭✭✭
edited December 2023 in General Language Questions

How can we automatically (say via mechanical scripting) read some data from an excel file (where the first column in that file gives the displacements and the second one contains spring force values, like shown below), and then assign those values to a nonlinear spring (combin39) inside mechanical?

Comments

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 330
    25 Answers First Anniversary 100 Comments 25 Likes
    ✭✭✭✭

    @Erik Kostson , this question really has two distinct questions in it. I will address the idea of interacting with MS Excel first, as this is done in this post:
    https://discuss.ansys.com/discussion/2195/how-to-interact-with-excel-from-mechanical-or-other-applications

  • Erik Kostson
    Erik Kostson Member, Employee Posts: 202
    50 Answers 100 Comments Photogenic 5 Likes
    ✭✭✭✭
    edited December 2023

    There are many ways of doing this. An attempt of such script is seen below. It assumes that we have a force vs deflection file (combin.xlsx), and an existing spring object in mechanical (change as needed, but called here: Longitudinal - Solid To Solid ).

    It will read in the force vs deflection data from the excel file , and based on this, create the apdl command snippet needed to define the nonlinear spring (combin39).

    import clr
    clr.AddReferenceByName('Microsoft.Office.Interop.Excel')
    from Microsoft.Office.Interop import Excel
    excel = Excel.ApplicationClass()
    excel.Visible = False # makes the Excel application visible to the user
    excel.ScreenUpdating = False     # Enables screen refreshing
    # opening a workbook
    filename = r"D:\combin.xlsx" #Specify existing Excel document
    workbook = excel.Workbooks.Open(filename)
    # adding a worksheet
    ws=workbook.worksheets("Sheet1")
    
    
    tempn='h'
    ii=1
    jj=1 # counter
    disp=[]
    forc=[]
    while tempn!='':
        tempn=ws.Cells(ii,1).Text
        tempm=ws.Cells(ii,2).Text
        disp.append(str(tempn))
        forc.append(tempm)
        ii=ii+1
    disp.pop()
    forc.pop()
    
    excel.Application.Quit()            ## Close Only the Excel file.
    excel.Quit()                        ## Close entire Excel 
    
    ment=0
    mytext=["RMORE,"]
    spring = DataModel.GetObjectsByName("Longitudinal - Solid To Solid")[0]
    cs= spring.AddCommandSnippet()
    cs.AppendText("et,_sid,39\n")
    cs.AppendText("keyopt,_sid,4,1\n")
    cs.AppendText("R," +"_sid," +str(disp[0]) + "," + str(forc[0]) +"," +str(disp[1]) + "," + str(forc[1]) +"," + str(disp[2]) + "," + str(forc[2]) + "\n")
    ii=3
    while ii <=len(disp)-1:
        if jj<4:
            mytext[ment]=mytext[ment] +str(disp[ii]) + "," + str(forc[ii]) +","
            jj=jj+1
        else :
            jj=1
            ment=ment+1
            mytext.append("RMORE,")
            ii=ii-1
        ii=ii+1
    for line in mytext:
        cs.AppendText(line+"\n")
    
    
This discussion has been closed.