How to Connect Python Code to an Opened Mechanical Project or Launch a Specific One? #996
-
I have a static structural project in Workbench, and when I open the model, I can use Mechanical scripting to perform some calculations. However, I want to execute my script externally from VS Code instead of writing it directly in Mechanical scripting. To achieve this, I’m first trying to connect from my Python code to my already opened Mechanical instance. I’ve attempted using the methods connect_to_mechanical and launch_mechanical, but I’m unsure if I use them correctly. I suspect I might be using the wrong port or missing a step to find the correct one. Once the connection is established, I believe I can use the run_python_script_from_file method to execute my script externally. My question is:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi Can not manage also on the 1st, but perhaps @koubaa can help here. For 2nd question: see here in section Launch Mechanical and import Mechanical scripting variables: https://developer.ansys.com/blog/create-report-saved-mechanical-session-mechdb-or-mechdat so use: e.g., : app.open(os.path.join(work_dir,'fileacp.mechdat')) to open an existing mechanical data file |
Beta Was this translation helpful? Give feedback.
-
Hi @thanborb . I am answering all your questions assuming you are using pymechanical remote session method. For your first question,
For the second question, Note: You can also launch Mechanical instance using In above comment, @ekostson provided another method that involves embedding an instance of PyMechanical, which creates a Python object of Mechanical. You can find examples of both methods below. Remote method examples : https://examples.mechanical.docs.pyansys.com/examples/index.html |
Beta Was this translation helpful? Give feedback.
Hi @thanborb .
I am answering all your questions assuming you are using pymechanical remote session method.
For your first question,
If you want to connect to A mechanical instance that is already running, you need to start Grpc Server on that instance. These are the steps to start grpc server and connect.
Ansys.ACT.Mechanical.MechanicalAPI.Instance.ApplicationAPI.StartGrpcServer(10000)
where 10000 is the port numberconnect_to_mechanical
using same 10000 port or alternately like this.For the second question,
If you have a Mechanical Project file saved as
.mechdb
or.mechdat
then you can start new …