Introduction to CPython
CPython can be used to:
- Launch Ansys Electronics Desktop (InitializeNew)
- Connect with a running instance of Ansys Electronics Desktop (Initialize)
- Execute Ansys Electronics Desktop script functions
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:
- Either InitializeNew() or Initialize(), as described below.
- Any desired Electronics Desktop scripting commands.
- Closing command, as described below.
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:
-
Connect(projectPath)
- Connect to the opened project.
- Launch ansysedt.exe, open the project, then connect to it.
- Error if the project does not exist.
- Connect(portNumber) Connect to running ansysedt process in the local machine. Error if no ansysedt process listening on this port.
- 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.
- 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:
- NonGraphical – Specifies whether to launch Electronics Desktop in non-graphical mode.
- Module – Behavior remains unchanged from Iron Python and should be left defaulted to "None." See the code in ScriptEnv.py for more details.
- Machine – Currently an empty string, as InitializeNew() will only launch Electronics Desktop on the current machine.
- Port – Electronics Desktop will launch using the first unused port it finds starting at <Port#>. If Port = 0, the starting port will be 50051.
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:
- Name – Ignored.
- NG – If launch is necessary, specifies whether to launch Electronics Desktop in non-graphical mode.
- Machine – The machine on which to launch/connect. For current machine, pass empty string or use localhost.
- Port – If port is nonzero, the script tries to connect to an existing instance on <port#> running on <machine>. If there is no instance running on that <port>, a new instance of Electronics Desktop launches on that port and then connects to it. If port = 0, the new instance is launched on the first free port, starting at 50051.
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.