How to add force/moment values from a file to a remote force and moment load using scripting?

Erik Kostson
Erik Kostson Member, Moderator, Employee Posts: 331
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭
edited April 16 in Structures

Say we have a remote force and moment defined inside mechanical, and we want to automatically assign magnitude values to these by reading this data from an excel file. Further more we would like to automate this process inside mechanical using scripting.

Best Answer

  • Erik Kostson
    Erik Kostson Member, Moderator, Employee Posts: 331
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited April 16 Answer ✓

    One possible way of doing this is shown below:

    import clr
    
    clr.AddReference("Microsoft.Office.Interop.Excel")
    import Microsoft.Office.Interop.Excel as Excel
    excel = Excel.ApplicationClass()
    
    
    ## Create Windows explorer pop-up
    filename = r'D:\myload.xlsx' # open this file
    workbook = excel.Workbooks.Open(filename)
    inputdata=workbook.Worksheets("ForceMoment").Select()# Select Worksheet by Name 
    ws1 = workbook.Worksheets("ForceMoment")
    
    Fx=((ws1.Cells(1,1).Value2)) # read first row and first column
    Fy=((ws1.Cells(1,2).Value2)) # etc
    Fz=((ws1.Cells(1,3).Value2))
    
    Mx=((ws1.Cells(1,4).Value2))
    My=((ws1.Cells(1,5).Value2))
    Mz=((ws1.Cells(1,6).Value2))
    
    excel.Application.Quit()
    excel.Quit()
    
    rf=ExtAPI.DataModel.GetObjectsByName('Remote Force')[0] # get force
    rm=ExtAPI.DataModel.GetObjectsByName('Moment')[0] # get moment
    
    unF=ExtAPI.DataModel.CurrentConsistentUnitFromQuantityName('Force')
    unM=ExtAPI.DataModel.CurrentConsistentUnitFromQuantityName('Moment')
    
    rf.XComponent.Output.SetDiscreteValue(0, Quantity(Fx, unF)) # assign loads
    rf.YComponent.Output.SetDiscreteValue(0, Quantity(Fy, unF))
    rf.ZComponent.Output.SetDiscreteValue(0, Quantity(Fz, unF))
    
    rm.XComponent.Output.SetDiscreteValue(0, Quantity(Mx, unM))
    rm.YComponent.Output.SetDiscreteValue(0, Quantity(My, unM))
    rm.ZComponent.Output.SetDiscreteValue(0, Quantity(Mz, unM))
    
This discussion has been closed.