Execute mechanical Scripts and there Function from Workbench ACT Console
Hello,
what I want to do is to run some Scripts, which are written as mechanical scripts, over the ACT Console (see fig 1).
What runs is the mecahnical Script, that calls a own written function (see fig.2)
Background:
I have different simulations, they are not connectet, and I want to automated them by calling different functions.
But often, they could use the same functions, so they should use the ONE func_Ans.py
fig 1
fig 2
### mech.py import sys functionPath = "D:\UserData\z003mfjt\Documents\Python_Skripting_Tutorial" + "\\" sys.path.append(functionPath) import func_Ans func_Ans.Initialize(ExtAPI, Ansys, DataModel) func_Ans.test()
func_Ans.py
from module_base import * def Initialize(MyExtAPI, MyAnsys, MyDataModel): global ExtAPI; global Ansys; global DataModel; ExtAPI = MyExtAPI; Ansys = MyAnsys; DataModel = MyDataModel; def test(): print("tested")
Thanks in advance for your help
PS:
How can I switch on the syntax highlighting in codes sections?
Best Answer
-
Hello @Pernelle Marone-Hitz,
unfortunately I didn't know about this behavior
Thanks for the tool, I think I can use it for further purposes.
The script to do the import is now running.
Here the result that works:
import os system = GetSystem(Name="SYS") modelfromACT = system.GetContainer(ComponentName="Model") filename = "ModalRes_Create_Solve_Export.py" installDir = "D:\UserData\z003mfjt\Documents\Python_Skripting_Tutorial" commandFile = os.path.join(installDir,filename) # NO oe, ue, ae in SKRIPT !!! with open(commandFile) as file: lines = [] for line in file: lines.append(line.rstrip().strip('\n')) print("lines = "+ str(len(lines)) + lines[0] + " "+ lines[-1]) # Target analyses (loop for later) modNr = 1 mechScriptCmds=""" import os # for newline command os.linesep # INPUTS modNr = %s content = %s analysis = ExtAPI.DataModel.Project.Model.Analyses[modNr] anSol = analysis.Solution search_string = "PythonCodeEventBased" childs = anSol.Children for i in range(len(childs)): if search_string in str(childs[i].GetType()): childs[i].Delete() pyco = anSol.AddPythonCodeEventBased() #pyco.TargetCallback = PythonCodeTargetCallback.OnAfterSolve pyco.Name = content[0] pyco.Text = "def after_solve(this, analysis):# Do not edit this line" for i in range(len(content)): if str(content[i]).StartsWith("print"): pyco.Text = pyco.Text + " #" + str(content[i]) + os.linesep else: pyco.Text = pyco.Text + " " + str(content[i]) + os.linesep pyco.Text = pyco.Text + " " + "pass" pyco.Connect() anSol.Solve() """ % (modNr,lines) modelfromACT.Edit(Interactive=True) modelfromACT.SendCommand(Language="Python", Command=mechScriptCmds)
--> It runs, but it could be easier.
It is not exctly, how i planned it in the beginning.
It ist possible to say the act console:
1) open mechanical SYS X
2) open the "Mechanical Scrpiting" Editor
3) open a specific script by the "Mechanical Scrpiting"
4 ) start it0
Answers
-
Hi @RaiDaway , what is your question/problem here?
For the colorcoding, check here:0 -
Hello @Pernelle Marone-Hitz,
Thank you for your quick reply and sorry for the confusion.
My problem that I don't know how to run mechanical scripts from the Act console when Mechanical is closed for example.
From here I got the hint that I can send "commands" to mechanical via the ACT console.
My question now is how I can use the ACT-Console to execute scripts in mechanical.
Greetings
0 -
@RaiDaway Thanks for the clarification. You'll have to convert your Mechanical script to a string, and pass it from WB Project Page to Mechanical using SendCommand method. There's an example here:
https://discuss.ansys.com/discussion/1287/add-command-snippet-and-import-file-from-wb-project-schematic?utm_source=community-search&utm_medium=organic-search&utm_term=project+mechanical0 -
Hey,
I have changed the code a little so that it runs from a specific folder.
It runs, but it creates an APDL command file and fills in the code from my Python file -- in my case, mech.py:import os system = GetSystem(Name="SYS") model1 = system.GetContainer(ComponentName="Model") # system1 refers to the first analysis component system installDir = "D:\UserData\z003mfjt\Documents\Python_Skripting_Tutorial" commandFile = os.path.join(installDir,'ModalRes_Create_Solve_Export.txt') commandFileString = "r\'" + str(commandFile) + "\' " mechScriptCmds=""" analysis = ExtAPI.DataModel.Project.Model.Analyses[1] snippet = analysis.AddCommandSnippet() snippet.ImportTextFile(%s) """ % (commandFileString) model1.SendCommand(Language="Python", Command=mechScriptCmds)
I changed the code so that it creates a Python command file.
Unfortunately, it does not import the commandfileString into the Python command that was created:import os system = GetSystem(Name="SYS") model1 = system.GetContainer(ComponentName="Model") # system1 refers to the first analysis component system installDir = "D:\UserData\z003mfjt\Documents\Python_Skripting_Tutorial" commandFile = os.path.join(installDir,'ModalRes_Create_Solve_Export.txt') commandFileString = "r\'" + str(commandFile) + "\' " mechScriptCmds=""" analysis = ExtAPI.DataModel.Project.Model.Analyses[1] snippet = analysis.AddPythonCodeEventBased() snippet.Text = %s """ % (commandFileString) model1.SendCommand(Language="Python", Command=mechScriptCmds)
It creates a python command file with my file path
When I change line10 by snippet.Text ="test", works correctly.0 -
Hey,
I changed the code a little bit, that it runs from specific folder.
It runs, but it creates an APDL command file and puts the code into my Python file -- in my case, mech.pyimport os system = GetSystem(Name="SYS") model1 = system.GetContainer(ComponentName="Model") # system1 refers to the first analysis component system installDir = "D:\UserData\z003mfjt\Documents\Python_Skripting_Tutorial" commandFile = os.path.join(installDir,'ModalRes_Create_Solve_Export.txt') commandFileString = "r\'" + str(commandFile) + "\' " mechScriptCmds=""" analysis = ExtAPI.DataModel.Project.Model.Analyses[1] snippet = analysis.AddCommandSnippet() snippet.ImportTextFile(%s) """ % (commandFileString) model1.SendCommand(Language="Python", Command=mechScriptCmds)
I have changed the code so that it creates a Python command file.
Unfortunately, it does not import the commandfileString into the Python command that was created:import os system = GetSystem(Name="SYS") model1 = system.GetContainer(ComponentName="Model") # system1 refers to the first analysis component system installDir = "D:\UserData\z003mfjt\Documents\Python_Skripting_Tutorial" + "\\" commandFile = os.path.join(installDir,'ModalRes_Create_Solve_Export.txt') commandFileString = "r\'" + str(commandFile) + "\' " mechScriptCmds=""" analysis = ExtAPI.DataModel.Project.Model.Analyses[1] snippet = analysis.AddPythonCodeEventBased() snippet.Text = %s """ % (commandFileString) model1.SendCommand(Language="Python", Command=mechScriptCmds)
It import the path into the python command file, that was created
When I change to "snippet.Text = "test" and without the string formatting, it runs like expected0 -
@RaiDaway , the behavior is as expected:
In both cases, commandFileString is the path to your .txt file. In the MAPDL command snippet,snippet.ImportTextFile(path)
will import the content of the file.
In the Python Code object, snippet.Text = path will indeed write the path to the Python Code. In this second case, you'll have to use Python to open and read the file content and give that content to snippet.Text.
In case what you're trying to develop is an easy way to copy/paste the content of Python Code and Python Result objects, you could use this ACT extension.0 -
Hi @RaiDaway , glad to know it works!
If you want to send the script from the WB ACT console the method you used is the only one that exists.
However another thing you could look into is Python Code objects in Mechanical, where the script would be executed automatically based on specific Mechanical events.0 -
Hello @Pernelle Marone-Hitz,
thanks for helping me on my confusing questions
However another thing you could look into is Python Code objects in Mechanical, where the script would be executed automatically based on specific Mechanical events.
Is there a bundled source or a description of this topic?
0 -
Hi @RaiDaway , happy to help :-) Here's the link to the Python Code documentation: https://ansyshelp.ansys.com/account/secured?returnurl=/Views/Secured/corp/v241/en/wb_sim/ds_python_code.html
0