How can I open an existing .wbpj file by using an IronPython script?

Hi Everyone,
I want to automatically run hundreds of transient thermal simulation and export the result data, what I have done until now includes:
1) successfully wrote an IronPython script that could command the ANSYS Mechanical to run the simulations in turn and export the data files. This script was written in Mechanical-Automation-Scripting, which means the script was run in the Mechanical environment.
2) finished a Matlab script which could start the ANSYS Workbench (by RunWB2.exe) and call the IronPython script.
The this is: I did not wrote all the settings in the IronPython script, some of settings were done manually in a workbench project file (.wbpj), which means I need to open the .wbpj and then run the simulations in this project file. In conclusion, my plan is:
1) run Matlab script
2) open the Workbench exe (in Matlab script)
3) call the IronPython script (in Matlab script)
4) open the existing .wbpj file (in IronPython script)
5) run the simulations as script command (in IronPython script)
Now I am stuck in the step 4, where I cannot open the existing .wbpj.
My Matlab script is:
ansysPath='D:\Program Files\ANSYS Inc\v202\Framework\bin\Win64\RunWB2.exe';
system(['start "" "',ansysPath,'"']);
pause(15);
ironpython_path='D:\Program Files\ANSYS Inc\v202\commonfiles\IronPython\ipy.exe';
pythonScriptPath='my_path\ScriptingTest1.py';
setenv('IRONPYTHONPATH', 'D:\Program Files\ANSYS Inc\v202\aisol\bin\winx64');
command=sprintf('"%s" "%s"',ironpython_path,pythonScriptPath);
status=system(command);
The beginning of my IronPython script is:
import os
import sys
import clr
from System import Array
app = MechanicalApp()
project = app.OpenProject(r"my_path\simu1.wbpj")
model = ExtAPI.DataModel.Project.Model
TRANSIENT_THERMAL = DataModel.AnalysisByName("Transient Thermal")
Now after running the Matlab script, a brand new workbench was opened but the script is terminated and return a error message "NameError: global name 'MechanicalApp' is not defined". Is there any method that can open the existing .wbpj by IronPython script?
Thanks for all suggestions and helps!
Answers
-
Some ideas:
embed your mechanical script (ironpy) in a python code object in mechanical. Sending code to mechanical is bit of a pain. You can use, almost 1 to 1, ACT scripting in python code objects.use cypthon when you can. If you are running a script to call/open ansys products, start with the most modern python version you have available. The call to run ansys can be done with any python version.
example: assuming you have a box.py or wbpj file in D that has your code needed for WB (save the script below to D and name it box.py, and check the quotes!):
### Reset() X=1000 geoCmd = """ P0 = Point.Create(MM(0),MM(0),MM(0)) P1 = Point.Create(MM(%s),MM(%s),MM(%s)) BlockBody.Create(P0,P1) """%(X,X,X) myTemplate = GetTemplate(TemplateName="Static Structural",Solver="ANSYS") mySystem = myTemplate.CreateSystem() geometry = mySystem.GetContainer(ComponentName="Geometry") geometry.Edit(IsSpaceClaimGeometry = True, Interactive = True) geometry.SendCommand(Language="Python", Command = geoCmd) geometry.Exit() modelContainer = mySystem.GetContainer(ComponentName="Model") modelComponent = mySystem.GetComponent(Name="Model") mechcmd = """ ExtAPI.Application.ActiveUnitSystem = MechanicalUnitSystem.StandardNMM # Mesh M = ExtAPI.DataModel.Project.Model.Mesh M.ElementSize = Quantity('500 [mm]') M.CreateParameter('ElementSize') """ modelContainer.SendCommand(Language="Python", Command=mechcmd) modelContainer.Edit(Interactive=True)
You can use any python and the subprocess module to start it.
import subprocess as sub command = r'C:\\Program Files\\ANSYS Inc\\v242\\Framework\\bin\\Win64\\runwb2.exe -I -R D:\box.py' sub.Popen(command)
Just add lines to the mechcmd to get what you want out of the analyses.
0