Skip to content

KLayout Python Module

Matthias Köfferlein edited this page Nov 29, 2018 · 3 revisions

The standalone module is found in the master branch. This build is enabled to produce a Python module. Continuous deployment of the master branch happens to https://www.klayout.org/downloads/master/.

There are basically two ways of producing Python modules:

Auto-generated documentation for the Functions is here:

Using the Python package on Windows

The Python package (either from PyPI or https://www.klayout.org/downloads/master/) needs some 3rd party libraries to be functional:

1.) Download the 3rd party package from https://www.klayout.org/downloads/klayout-bits-1.0.zip

2.) Unzip (e.g. under c:\klayout-bits-1.0)

3.) Install klayout package (here: from PyPI)

pip install klayout

4.) Add the 3rd-party packages to the path (sorry, lengthy):

set PATH=C:\klayout-bits-1.0\msvc2017\x64\curl\bin;c:\klayout-bits-1.0\msvc2017\x64\expat\bin;c:\klayout-bits-1.0\msvc2017\x64\ptw\bin;c:\klayout-bits-1.0\msvc2017\x64\zlib\bin;%PATH%

Building with setuptools

# From the project's root directory
sudo python setup.py install

This build configuration is currently hard configured this way:

  • No Qt required and no Qt related functionality produced
  • Requires expat for XML parsing
  • Required curl for HTTP support
  • Requires pthread for multithread support

This build will produce complete packages for klayout.tl, klayout.db and klayout.rdb.

Building together with the main application

For building with the build script use:

# From the project's root directory
./build.sh -bin <your-installation-path> 

If you want it fast and don't need to Qt bindings, use:

# From the project's root directory
./build.sh -bin <your-installation-path> -without-qtbinding

For even faster build without any UI functionality, you can use a Qt-less build:

# From the project's root directory
./build.sh -bin <your-installation-path> -without-qt -libcurl -libexpat

In fact the minimum build is without HTTP and XML support:

# From the project's root directory
./build.sh -bin <your-installation-path> -without-qt

Without XML support however, some formats are not readable (such as layer property files, PCB projects etc.).

For <your-installation-path> pick a destination where you want KLayout to be installed. For example you can use /usr/local/klayout for the destination, but you will need sudo permissions then:

# Build and install in /usr/local/klayout
sudo ./build.sh -bin /usr/local/klayout -without-qtbinding

The klayout module will be built together with KLayout. After KLayout installation, the klayout module resides inside the installation tree in the "pymod" subfolder. To use it, either copy the klayout folder to your Python installation or set PYTHONPATH to the location of the klayout folder:

export PYTHONPATH=<your-installation-path>/pymod
python
>>> import klayout.db as db
>>> print(str(db.DBox(0, 0, 100, 200))
(0,0;100,200)

Using the module

Use the new modules this way:

import klayout.tl as tl

timer = klayout.tl.Timer()
import klayout.db as db

# instantiate a KLayout object
box = db.DBox(0, 0, 100, 200)

Since lay has dependencies on Qt modules, you need to use the following call for this module:

import klayout.QtCore
import klayout.QtGui
# On Qt5 only:
import klayout.QtWidgets
import klayout.lay

...

To get an index of the classes present in the modules, use:

import klayout.tl as tl
import klayout.db as db
import klayout.QtCore
import klayout.QtGui
# On Qt5 only:
import klayout.QtWidgets
import klayout.lay

print "\n".join(klayout.tl.__all__)
print "\n".join(klayout.db.__all__)
print "\n".join(klayout.lay.__all__)

The Qt binding classes are available to. Essentially this provides an alternative to PyQt:

from klayout.QtCore import *
from klayout.QtGui import *
from klayout.QtWidgets import *
from klayout.lay import *

app = QApplication([ "my_app" ])

w = LayoutView()
w.load_layout("myfile.gds", False)
w.max_hier()
w.show()

app.exec_()
# ensures that w is destroyed before app.
# Otherwise Qt segfaults.
w = None

Project log

2018-06-20:

  • The Python module name now is "klayout"
  • Module assignment is pretty much fixed now (core modules are tl, db and rdb)
  • Build is feasible on CentOS6, 7, Ubuntu 16 and Windows - some unit tests are failing

2018-07-15:

  • The pymod branch now supports Qt-less builds. libexpat, libpthread and libcurl are used to supply the missing functionality.
  • The distutils-bases setup.py script is provided now
  • Build of setup.py was tested on CentOS6, 7, Ubuntu 16, 18 and MacOS (HighSierra)
  • Windows test still ongoing

Further roadmap

  • Provide a generated documentation online
  • Provide pre-build packages for exploration
  • Explore how to make PyCharm aware of the methods inside the klayout modules

Future:

  • Introduction of enhanced interfaces for the user interface components (instantiation, configuration, etc.)
  • Automated generation and on-demand extraction of the Qt API for rich Qt version support of the Qt binding API
  • Documentation extraction of the Qt binding API