Write max vm stress for each part in named selection to file

Member, Moderator, Employee Posts: 311
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭
edited July 2024 in Structures

In short using mech scripting, we would like to write e.g., max vm stress for each part in named selection to a text file.

Best Answers

  • Member, Moderator, Employee Posts: 311
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited July 2024 Answer ✓

    One way of doing this is presented below:

    1. model=ExtAPI.DataModel.Project.Model # refer to Model
    2. analysis = model.Analyses[0]
    3. solution = analysis.Solution
    4. f1=open("D:\\testpartvmstress.txt","w") #open file in user directory change as needed
    5. ns=model.NamedSelections.Children[0] # change named selection as needed / picks the first one in tree, contains all parts to be used below
    6.  
    7. nIds=ns.Location.Ids
    8. for body in nIds:
    9. res=solution.AddEquivalentStress()
    10. selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    11. selection.Ids=[body]
    12. res.Location=selection
    13. selection=ExtAPI.SelectionManager.ClearSelection()
    14. res.EvaluateAllResults()
    15. partname=res.MinimumOccursOn
    16. parmax=res.Maximum
    17. f1.write(partname + " , " + str(parmax) +","+ "\n")
    18. f1.close()
    19.  

    If we want to do it for all parts in Mechanical (so no need to add relevant parts in Named Selection):

    1. model=ExtAPI.DataModel.Project.Model # refer to Model
    2. analysis = model.Analyses[0]
    3. solution = analysis.Solution
    4. f1=open("D:\\testpartvmstress.txt","w") #open file in user directory change as needed
    5.  
    6. for assembly in ExtAPI.DataModel.GeoData.Assemblies:
    7. for part in assembly.Parts:
    8. for body in part.Bodies:
    9. res=solution.AddEquivalentStress()
    10. selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    11. selection.Ids=[body.Id]
    12. res.Location=selection
    13. selection=ExtAPI.SelectionManager.ClearSelection()
    14. res.EvaluateAllResults()
    15. partname=res.MinimumOccursOn
    16. parmax=res.Maximum
    17. f1.write(partname + " , " + str(parmax) +","+ "\n")
    18. f1.close()
  • Member, Moderator, Employee Posts: 311
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    Finally if we want to loop over named selections and get the maximum in each named selection:

    1. model=ExtAPI.DataModel.Project.Model # refer to Model
    2. analysis = model.Analyses[0]
    3. solution = analysis.Solution
    4. f1=open("D:\\testpartvmstress.txt","w") #open file in user directory change as needed
    5. ns=model.NamedSelections.Children
    6.  
    7. for nsi in ns:
    8. nIds=nsi.Location.Ids
    9. res=solution.AddEquivalentStress()
    10. res.Location=nsi
    11. res.EvaluateAllResults()
    12. partname=res.MinimumOccursOn
    13. parmax=res.Maximum
    14. f1.write(str(nsi.Name) + " , " + str(partname) + " , " + str(parmax) +" , "+ "\n")
    15. f1.close()
This discussion has been closed.