Introduction to CPython

CPython can be used to:

One advantage of CPython is the large set of libraries and tools that are available. See below for instructions on modifying a script so that it can be launched with CPython interpreters.

CPython Script Engine and ansysedt process are communicated using GRPC, the ansysedt process started as GRPC server, the Script Script Engine acted as the GRPC client.

Creating an External Script

While the same as IronPython when run externally, a CPython recorded script must be modified by adding the following lines to the beginning of your script before import ScriptEnv

import sys

# Imports the sys module containing system-specific functions native to Python.

sys.path.append(r"<InstallationPath>/PythonFiles/DesktopPlugin")

# Adds the PythonFiles/DesktopPlugin subfolder to the list of directories Python searches for modules and files.

Those lines are followed by:

import ScriptEnv

# This imports ScriptEnv.py from the installation path specified above.

Follow that with:

Start ansysedt as GRPC server

But default ansysedt process will be started as GRPC server. When there is no argument provided ansysedt.exe it start listening on the first available port from 50051.

-grpcsrv Flag

ansysedt -grpcsrv <optional port number or port range>.

ansysedt –grpcsrv

If no Port Number specified, same as the default like no grpcsrv flag

ansysedt –grpcsrv portNumber

ansysedt process will be listening on the port number, but report error as the port is used by other application.

ansysedt –grpcsrv FirstPortNumber:LastPortNumber

ansysedt process will be listening on the first available port within the port range, but report error if all ports in the range used.

ansysedt –grpcsrv FirstPortNumber:NumberOfPorts

ansysedt process will be listening on the first available port within the port range, but report error if all ports in the range used.

Note: If the last number if smaller than the first number the last number will be treated as the number of ports. (50051: 50250 has the same range as 50051:200)

Connect Functions:

  1. Connect(projectPath)

    1. Connect to the opened project.
    2. Launch ansysedt.exe, open the project, then connect to it.
    3. Error if the project does not exist.
  1. Connect(portNumber) Connect to running ansysedt process in the local machine. Error if no ansysedt process listening on this port.
  2. Connect(machine, projectPath) connect to an open project in remote Machine. projectPath must be a network path. Error if no ansysedt process opened the project.
  3. Connect(machine, portNumber) connect to a running ansysedt process on the remote machine. Error if no ansysedt process listening on this port.

Example:

import sys

sys.path.append(r"<InstallationPath>/PythonFiles/DesktopPlugin")

import ScriptEnv

ScriptEnv.Connect(r”c:\myProjects\Project1.aedt”)

oProject = oDesktop.GetActiveProject()

print(oProject.GetName())

Launching Electronics Desktop

To launch a new instance of Electronics Desktop and connect oApplication and oDesktop to it:

InitializeNew(NonGraphical = <True|False>, Module = None, Machine = "", Port = <Port#>)

Where:

Note:

InitializeNew() will always launch a new instance of Electronics Desktop. Please use Initialize() to connect to an existing instance. See below.

Connecting with a Running Instance of Electronics Desktop

To connect oApplication and oDesktop to an existing Electronics Desktop instance, or launch a new instance and connect to it if necessary:

Initialize(name, NG = <True|False>, machine = "", port = <Port#>)

Where:

Closing Electronics Desktop/Ending the Script

To close Electronics Desktop, add the following line to the end of the script:

ScriptEnv.Shutdown()

# This stops ScriptEnv.py. If you are running multiple scripts, include this only at the end of the last script.

-grpcsrv Flag

This flag will launch the application in a mode where the executable serves as a scripting server that can be used for CPython scripting in conjunction with the CPython stand alone scripting instructions that were mentioned earlier. The
-ng flag can be combined with -grpcsrv.

On Windows:

ansysedt.exe -grpcsrv <optional port number>.

On Linux:

ansysedt -grpcsrv <optional port number>.

If the port number is omitted, the default of 50051 will be used.

With -grpcsrv, a message will be displayed in the Messages window indicating that the server was started. If the requested port is in use by another application, starting the sever may fail.