ansys / pymechanical

Pythonic interface to Ansys Mechanical ™
https://mechanical.docs.pyansys.com/
MIT License
36 stars 18 forks source link

The run_python_script content after connecting Remote session #641

Closed coconutLatte closed 6 months ago

coconutLatte commented 6 months ago

Description of the modifications

VERSION: Ansys 2023R1

Hi there I'm a software engineer now focus on developing some applications that using mechanical ability without GUI. I notice that there has a way call remote session and I'm already find a way to connect the grpc server launched by mechanical, but I don't know how to execute the run_python_script interface. I will show what I've done below:

  1. Open the Workbench;
  2. Create a “Steady-State Thermal”, then double click the "Model";
  3. Click "Automation -> Scripting -> Start recording...";
  4. Recording the whole simulation, then the python scripts got content;
  5. Copy the content to my python project by using remote session.

Turns out the script records cannot used by the remote session mechanical instance run_python_script.

Is there any docs that I could get to learn how to fulfill the run_python_script content for my simulation. Or how the python script recorded by GUI can be used in PyMechanical in any way?

my python script calling remote session like below:

import ansys.mechanical.core as pymechanical

wb_exe = pymechanical.find_mechanical(231)[0]
print("wb_exe: ", wb_exe)
mechanical = pymechanical.launch_mechanical(exec_file=wb_exe, verbose_mechanical=True, batch=True, loglevel="DEBUG", log_file=True, log_mechanical="pymechanical")
print("mechanical launch success: ", mechanical)

try:
    mechanical.run_python_script(
        """
# here fulfill the python script recorded 
        """
    )
except Exception as e:
    print("An error occurred while running the Python script:", e)
finally:
    print("exit mechanical instance")
    mechanical.exit()

Useful links and references

No response

dipinknair commented 6 months ago

HI @coconutLatte If you are looking for the docs that uses run_python_script,then please find them under "remote examples" https://examples.mechanical.docs.pyansys.com/index.html

Mechanical scripts works in remote session without any issues. If you are facing any issues when you are recording the scripts from Mechanical and trying to use the same directly with remote session of pymechanical, then it might be possible you are missing something in mechanical scripts. Can you please do this below.

  1. Run your scripts that are recorded in a new instance of Mechanical and run and see it works with Mechanical GUI.
  2. Check if the recording involves importing files which are correctly recorded
  3. Share the code here if possible so I can have a look at it.

if you need any help with Mechanical scripting, Please have a look at here - scripting guide and also you can ask you scripting related questions here

coconutLatte commented 6 months ago

@dipinknair
Thanks!!! It really help me a lot!