Scripting with Python#
Introduction#
The Lattix Python API is designed as a replacement for the groovy script API.
Further information on how to program Python can be found here: Python Documentation.
Note
The API is only compatible with Python 3 for unix-like, and Python 3.12 for Windows. In order to use the Python API, ensure that the correct version of Python is installed on your system.
Using the Python API#
Setup#
In order to use the Lattix API, you must create a Python virtual environment.
We recommend using the venv virtual environment, which is created by executing the venv module:
$ python -m venv /path/to/new/virtual/environment
To allow access to the python API, the api files are provided in the Lattix
install. These files are pyliblattix.pyi
and pyliblattix.so
for unix-like, and pyliblattix.pyd
for Windows.
Within the created virtual environment, create a file called lattix.pth
in the
.venv/lib/python3.XX/site-packages/
directory, containing the path to your Lattix
assembly directory.
# .venv/lib/python3.10/site-packages/lattix.pth
/opt/lattix/python_api
Activate the virtual environment with:
// Linux and MacOS
$ source .venv/bin/activate
// windows
> .venv\Scripts\ativate.bat
Key Interfaces of the Python API#
Key Classes#
There are a few key classes that are likely to be used most often by scripts:
ProjectModel
Partition
Atom
DependencyEdge/Dependency
Tag
ProjectModel#
Every script needs the current ProjectModel, and can be obtained from the API object:
import pyliblattix
lattix_api = pyliblattix.acquire_lattix_api()
pm = lattix_api.open_lattix_project("PATH/TO/LDZ/FILE")
This function will allow you to access the Lattix Project saved in the specified ldz file.
Partition#
A partition represents the basic element of a Lattix project that is visible in a DSM or a CAD
Atom#
The Atom represents a basic domain element. Partitions contain one or more Atoms.
DependencyEdge/Dependency#
A DependencyEdge represents a dependency between a source and a target Atom. Remember that a target Atom can be an external Atom.
Tag#
A Partition can be tagged (labelled) in order to aid analysis and selection. There is a many-to-many relationship between tags and partitions.
Example Scripts#
An example script to tag a named partition:
# Import the python API
import pyliblattix
# Get the Lattix API
lattix_api = pyliblattix.acquire_lattix_api()
# Get the Project Model
pm:pyliblattix.ProjectModel = lattix_api.open_lattix_project("PATH/TO/LDZ/FILE")
# Create a tag called MY_TAG_NAME
tag = pm.create_or_find_tag("MY_TAG_NAME")
# Get the partition name LIB_PARTITION/util_funcs.cpp
pc = pm.get_partitions_by_name("LIB_PARTITON/util_funcs.cpp")
# Tag that partition
pm.add_partitions_to_tag(tag, pc)
# Save the changes to the LDZ file
pm.save_project()
Further example scripts and API signatures are available in your Lattix install directory.