How can I get a user button to create a Python results with its script and properties?

Member, Moderator, Employee Posts: 108
25 Answers Second Anniversary 10 Comments 25 Likes
✭✭✭✭
edited June 2023 in Structures

A power user has created a Python result and would like to share it with other people. How can he do this?

Tagged:

Answers

  • Member, Moderator, Employee Posts: 108
    25 Answers Second Anniversary 10 Comments 25 Likes
    ✭✭✭✭
    Answer ✓

    Well, here's one potential solution, as there is no "export button" or "generate user button" option.

    First run this script that will store the Python results script and property provider:

    1. pr=ExtAPI.DataModel.GetObjectsByName('Python Result')[0]
    2.  
    3. script=pr.Text
    4. provider_text=pr.PropertyProviderText
    5.  
    6. f=open(r'wherever\result_script.txt','w')
    7. f.write(script)
    8. f.write('\n***** delimiter\n') # use whatever delimiter
    9. f.write(provider_text)
    10. f.close()

    Then use this script to create the user button:

    1. current_object=ExtAPI.DataModel.Tree.FirstActiveObject
    2.  
    3. if current_object.GetType()==Ansys.ACT.Automation.Mechanical.Solution: # user has selected a 'solution' under which the python result will be inserted
    4. new_presult=current_object.AddPythonResult()
    5. f=open(r'wherever\result_script.txt','r') # script content stored somewhere
    6. script=''
    7. provider_text=''
    8. line=f.readline()
    9. # read script text
    10. while line.find('***** delimiter')==-1: # same delimiter as above
    11. script+=line
    12. line=f.readline()
    13. line=f.readline()
    14. # read provider text
    15. while line:
    16. provider_text+=line
    17. line=f.readline()
    18. f.close()
    19. new_presult.PropertyProviderText=provider_text
    20. new_presult.Text=script
    21. new_presult.Connect()
    22. new_presult.ReloadProperties()

Welcome!

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