Report Automation with PyMechanical
I'm in the beginning stages of developing a tool to automate large portions of the report creation process. I've installed PyMechanical and am trying to figure out the best path forward. Since it doesn't require the duplication of the model file (if I'm understanding correctly), I'd like to use the embedded method (described here: link). The difficulty I'm having is figuring out the best means for the user to specify the mechanical model to use. I think the most straightforward for them would be to specify the WBPJ file and select the system from a list of options.
The example here (link) explains how to integrate PyMechanical into PyWorkbench, but it requires the use of the remote method, which doesn't seem like it would work very well with large models that the user hasn't archived, etc. It seems like if I knew the correct place(s) to look in the XML of the WBPJ file, I should be able to get a list of the systems in the file to offer to the user as a list to select from, but I'm struggling to decipher the XML file.
To be clear, I know the user can find the location of the MECHDB file from the Workbench UI, but I was hoping to make it simpler for them by allowing them to specify the WBPJ file and select from a list of options (making use of the system name and letter) for available MECHDB files.
My question therefore is, can you provide any guidance on how to navigate the XML of the WBPJ file to get a list of the systems in the file and the location of their corresponding MECHDB files?
Best Answers
-
The upload/download tools are only necessary to move files between client and server. If everything is running locally, they should not be necessary. For example:
from ansys.workbench.core import launch_workbench wb = launch_workbench() wb.run_script_string('Open(FilePath=r"D:/Temp/shell_bending.wbpj")')
1 -
You need to add the following lines to the end of the WB script (get_syss_dict.py) in order to pass syss_dict back to the external Python instance:
import json wb_script_result = json.dumps(syss_dict)
See the example script here: https://github.com/ansys/pyworkbench-examples/blob/main/examples/pymechanical-integration/assets/project.wbjn
1
Answers
-
I am not sure that I understand your objection to using remote mode. Neither method requires duplication of files AFAIK.
But you should be able to use PyWorkbench to open the WBPJ file, "get a list of the systems in the file to offer to the user as a list to select from", and then open the selected MECHDB in a new standalone (not linked to WB) Mechanical session using embedded mode. This methodology is probably more robust than trying to read the WBPJ file as a text file.
Another idea could be to launch your report tool from within Mechanical using ACT. This tool can serve as a template.
FYI. You might be interested in this blog post.
0 -
Thanks, Landon.
I guess I might be misunderstanding what the remote option does. Does uploading a model to the remote session result (either directly or indirectly) in more physical space being consumed on the hard drive? For example, I don't want the user to have to create an archive of the model, and when the model gets uploaded, I don't want it to consume storage space on the hard drive equal to what is being uploaded.
Alternatively, if I can upload only the WBPJ file (i.e., orphaned from the
*_files
directory) and still get the list of systems with associated MECHDB files and the locations of the files on the hard drive, then I'm not that worried about space since the WBPJ files are so small.0 -
Ah! Okay. Thanks!
0 -
Okay, so the script below gets the information I need from Workbench, but I'm having trouble figuring out how to correctly run the script.
import os syss_dict = {} for sys in GetAllSystems(): sys_mdl = sys.GetContainer("Model").GetMechanicalModel() syss_dict[sys.DisplayText] = os.path.join(sys_mdl.File.Directory, sys_mdl.File.FileName) print(syss_dict)
I tried running it with both of the following commands (as well as a couple other variations):
syss_dict_str = wb.run_script_file(r"C:\path\to\my\script\get_syss_dict.py", log_level="debug")
wb.upload_file(r"C:\path\to\my\script\get_syss_dict.py") syss_dict_str = wb.run_script_file("get_syss_dict.py", log_level="debug")
All of them returned an empty dictionary to
syss_dict_str
.0 -
Doing some more experimentation, it seems like the problem is the variable not being returned to the command line. The
syss_dict
variable is printed to the Command Window, but nothing is being returned to the command line.0