Can I launch a PyMechanical tool from within Mechanical?

Landon Mitchell Kanner
Landon Mitchell Kanner Member, Employee Posts: 297
25 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭

My colleague has created an app using PyMechanical. I want to launch the app from within Mechanical and have it connect to the existing Mechanical session. Is this possible?

Best Answer

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 297
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited July 24 Answer ✓

    Yes. First you will start Mechanical's GRPC Server using the command
    port_number = Ansys.ACT.Mechanical.MechanicalAPI.Instance.ApplicationAPI.StartGrpcServer()

    Then you will need to pass the port_number to the PyMechanical tool so it can connect using the command
    mechanical = launch_mechanical(start_instance=False, port=port_number, cleanup_on_exit=False)

    Let's look at an example.
    First create a PyMechanical script called 'PyMechanicalScript.py' with the following code:

    import sys
    import os
    from ansys.mechanical.core import launch_mechanical
    
    port = sys.argv[1]
    
    print(__name__)
    print(f'Connecting to Mechanical on port = {port}')
    
    mechanical = launch_mechanical(start_instance=False, port=port, cleanup_on_exit=False)
    print(mechanical)
    

    Then run this code from the Mechanical Scripting Console as follows:

    import subprocess 
    
    port_number = Ansys.ACT.Mechanical.MechanicalAPI.Instance.ApplicationAPI.StartGrpcServer()
    
    Cpython_path = r'C:\Users\lmkanner\AppData\Local\Programs\Python\Python310\python.exe' # Must include ansys.mechanical.core
    Cpython_file = r'<path to PyMechanicalScript.py>'
    
    subprocess.Popen(args=[Cpython_path,'-i',
            Cpython_file,str(port_number)]
            ,stdin=None, stdout=None, stderr=None)
    

    This will open a Python interpreter window that is connected to your Mechanical session via the variable 'mechanical'. For example, you could solve the first analysis in your open Mechanical session by copy/pasting the following in to the Python window:
    mechanical.run_python_script("ExtAPI.DataModel.Project.Model.Analyses[0].Solve()")

    Attached is the same example encapsulated as an ACT extension.

Answers

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 345
    25 Answers 100 Comments 25 Likes First Anniversary
    ✭✭✭✭

    @Landon Mitchell Kanner , can you also comment on what versions of Mechanical/PyMechanical are required for this?

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 297
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited July 25

    @Mike.Thompson said:
    @Landon Mitchell Kanner , can you also comment on what versions of Mechanical/PyMechanical are required for this?

    As written, it works starting at 2023R1. At 2022R2, you would have to specify the port number:

    port_number = 8000
    Ansys.ACT.Mechanical.MechanicalAPI.Instance.ApplicationAPI.StartGrpcServer(port_number)