PRIMER
Getting started
As Python is running outside PRIMER, the first thing a script needs to do is to either start an instance of PRIMER, or to connect to an already running instance of PRIMER. At the end of the script you should then either disconnect again or terminate the PRIMER instance.
A skeleton Python script to start PRIMER (Installed at C:\install_directory\primerXX_x64.exe) and then terminate it is:
Python |
import Oasys.PRIMER |
By default PRIMER will use port 50051 to communicate with Python and will allocate 25MB of memory for running scripts. These can be changed by adding port and memory arguments to the start function. e.g:
Python |
connection = Oasys.PRIMER.start(abspath="C:\\install_directory\\primerXX_x64.exe", port=1234, memory=100) |
PRIMER can also be started in batch mode so that the main graphics window is not shown by using a batch argument:
Python |
connection = Oasys.PRIMER.start(abspath="C:\\install_directory\\primerXX_x64.exe", batch=True) |
To connect to an instance of PRIMER that is already running, PRIMER must currently have been started in a special mode telling it to listen on a port for gRPC messages. See further information here.
A skeleton script to connect to PRIMER and disconnect again would then be:
Python |
import Oasys.PRIMER |
or if you want to terminate the instance of PRIMER use terminate instead of disconnect.
Python API
The JS API has been available for several years, is stable and works well, so the Python API has been designed to have the same classes, methods and properties as the JS API.
However, the following classes are not available:
PopupWindow (GUIs not available from Python)
Widget (GUIs not available from Python)
WidgetItem (GUIs not available from Python)
Check (Custom checks not available from Python)
File (use Python i/o instead)
Graphics (Graphics drawing not available from Python)
Ssh (use Python modules instead)
XlsxWorkbook (use Python modules instead)
XlsxWorksheet (use Python modules instead)
XMLParser (use Python modules instead)
Zip (use Python modules instead)
and the following methods are not available:
ForEach
Error
Warning
If an argument in the JS API is an object then the equivalent in Python will be a dict, and if an array in JS, the equivalent in Python will be a list.
Simple example
The following example shows how to make a model containing some nodes and shells using the JS API and the Python API :
|
|
|---|
All the classes and methods or functions used in the Python script above can be found in the PRIMER Python API documentation, in the following pages:
- Connection methods (to start an instance of PRIMER and to disconnect Python from it at the end)
- Model class (to create a model, and to update graphics)
- Functions (to print a message)
- Node class (to create nodes)
- Shell class (to create shells)
- View class (to change the view and autoscale)