domain decomposition



  • Hi wsteffe

    Due to gds format supports cell and sub cell structures, which top cell is at view level 0, and sub cells directly under top will be view level 1
    cells under sub cell will have higer level number. This only affects display and does not change the original data.

    The hierarchy and cell concept reference cell objects by their ID and can be invoke repeatly, this greately reduce the data size since the file only need to store location data and corrosponding ID, and calculate the image on the fly.

    For instance, their's 184 identical circular shapes in your layout example, so this requires 5404 data points to define all thoes vias.
    DPolygon([ DPoints x 31 ]) x 184 = 5404 data points

    If we convert circies to cell and reference them by only by Cell ID, then this only takes 215 data points
    DPolygon([ DPoints x 31 ]) + DPoints x 184 = 215 data points

    With reduced view level this filter out some deep or surface level objects, like only show transistors related level, which makes viewing/editing
    becomes easier.

    If you click the small triangle mark on ▶TOP cell it'll unfold all sub elements, by right click on them you can show them as new top to check them individually without the interference of other cells.

    View all levels can be done both manually or through one line of script:

  • Hi RawrRanger, thanks for clarification, the only obscure point is why there are 2 entries in the Levels field.
    To restore visibility I had to increase the value in the sicond (the rightmost) entry. The first one doesn't have any effect.
    Also it is not very clear why the required level for visibility changed (from 1 to 2) when I placed a second layout in the same panel.
    May be klayout has created another virtual (not visible) top level cell below which it has placed the TOP cells of both layouts.

  • edited September 2023

    After having seen the last RawRanger exampes it seems to me the kind of domain decomposition I had in mind seems to be feaseble. But I think that at this point it would be better to organize the required functions in a separate python module (automatically loaded in kalyout) which is able to create a set of menu command.

    I would like to implement the following commands:

    1) Import PCB

    • Open a dxf file
    • Automatically Generate (ln,dt) (dt may be 0) using lexographical order of layer names

    2) Save PCB:

    • To save imported layout in gds format together with an associated (same name) lyp file.
      In the following the user may work with this gds copy avoiding the risc of corrupting the original dxf.

    3) Create new subdomain region:

    • The user assigned name should be appended to the circuit name. In example "circuit_region1"
    • A new Layout with a TOP cell named "circuit_region1" is created in the same panel
    • The new layout is activated for writing on it on layer 0. (I think it is better to use layer 0 for initial drawing clipping polygon)

    3) Create new subdomain.

    • A set af new layers should be created on the layout associated with the new region with same layer propertis as visibible layer of original circuit.
    • Polygon drawn in layer 0 is copied into the new layers and removed from layer 0.
    • The layout associated with the new region is saved into the dxf file circuit_region1.dxf
    • Original dxf (circuit.dxf) and region definition (circuit_region1.dxf) are passed to a python script (based on ezdxf) which will extract from circuit.dxf all the shapes having a bbox that intercepts the clypping polyingon defined in the same layer (same layer name) of circuit_region1.dxf.

    4) Load all subdomains :

    • Loading all the region files with same base suffix name (in example circuit_region1, circuit_region2,...) and placing them in the same panel of circuit.dxf.
  • edited September 2023

    I said:

    it would be better to organize the required functions in a separate python module.

    But probably i should have said:

    it would be better to organize the required functions in a separate package

    I have to learn how to create and manage klayout pakcages

  • edited September 2023

    Hi all, I have just created a project at aimed to the development of a package for the Domain Decomposition of PCB structures.
    I do not know why but the command git push has placed the file "grain.xml" in the subfolder blob/main while the other files are placed in the subfolder tree/main/. I do not have any blob folder in my local copy:

    git ls-files


    My publishing request at fails because it can not find "grain.xml" in the blob folder.

  • Hi wsteffe,

    (1) Import PCB:
    This can be done in two ways, using pre-defined *.lyp, it will automatically match the layer number/datatype if the layer name is same as dxf name, which avoids no number/datatype issue. An *.lyp with layer mapped DXF is as attached

    Another method is to use script to map them, the benefit is you can use custom algorithm to convert them into layer number/datatypt dynamically.

    Following example use a constant dic to map them, but can be change to any layer number generator.

    import pya
    mapping = { 
        "L0D0_0"     : [ 0, 0],
        "STICH-VIAS" : [10, 0],
        "BOTTOM"     : [11, 0],
        "TOP"        : [12, 0],
        "RESISTANCE" : [13, 0],
    layoutView = pya.Application.instance().main_window().current_view()
    cellView   = layoutView.active_cellview()
    layout     = cellView.layout()
    def mapLayers(mapping):
        itr = layoutView.begin_layers()
        while not itr.at_end():
            lyp = itr.current()
            if lyp.cellview() == cellView.index():
                lid = lyp.layer_index()
                lnm = lyp.source_name
                if lnm in mapping:
                    lno, ldt = mapping[ lnm ]
                    layout.set_info(lid, pya.LayerInfo(lno, ldt, lnm))
    def cleanLayers():
        itr = layoutView.begin_layers()
        while not itr.at_end():
            lyp = itr.current()
            if lyp.cellview() == cellView.index():
                if (lyp.source_layer < 0):

    (2) Save PCB can also be embeded in scripts, this saves files onto a gds_output folder with layout file together with *.lyp file.

    import pya
    def saveActiveCell():
        layoutView    = pya.Application.instance().main_window().current_view()
        cellView      = layoutView.active_cellview()
        layout        = cellView.layout()
        cell          = cellView.cell
        filePathSeg   ="\\", "/").split("/")
        path          = "/".join(filePathSeg[0:-1])
        name          = filePathSeg[-1].split(".")[0]
        folderPath    = f"{path}/gds_output/"
        option        = pya.SaveLayoutOptions()
        option.format = "GDS2"
        if cell:
            if not os.path.exists(folderPath) : os.makedirs(folderPath)
            cell.write(f"{folderPath}/{name}.gds", option)

    (3) Can refer to previous clipping with multiple layout example

    (4) By parsing all files with same suffix and combined with following code should able to load multiple layout into same port

  • HI wsteffe,

    for package registration, with additional trunk should work

  • For View level, please check out this example in attached, This file consist of many levels and shoud give you a better idea about the view range selection .
    This is a sequence of screenshot of ramping up lower view range from 0 to 5
    shapes or cells in that is shallow in hierarchy will dissapear during the change

    This ramps down the upper view range from 0 to 5

  • Hi RawRanger thanks again for your valuable contributes.
    If yoy agree you may contribute directly to the project I will sure give you write permission if I find the way to do it. Otherwise , if you prefer, you may create your fork and make pull requests so I may incorporate your changes.

    I have just tried to add to the repository a mapLayer command based on your code. I have following errors which are not related with your code but with the implementation of menu actions and signal slots:

    ERROR: /home/walter/.klayout/salt/layoutDD/pymacros/layoutDD.lym:39: AttributeError: Signal's 'set' method needs a callable object
    /home/walter/.klayout/salt/layoutDD/pymacros/layoutDD.lym:45 (class AttributeError)

  • edited September 2023

    For View level ... Understood, the tho numbers are defining a range of visible levels.

  • Hi wsteffe,

    If you like to add a button in toolbar which triggers a function, this can be achieved with on_triggered

    act.on_triggered(lambda : print("something"))
  • Hi RawRanger please look again at the file layoutDD.lym. Previously it has not been properly updated because I had done the push from a lower directory.
    The error now occurs at line 38. The purpose of lines 36-40 was just to add a main entry for the layoutDD project. So probably no action should not be involved in this part. But I do not know how to add a menu entry without any associated action.

  • HI wsteffe,

    For file import I recommend to add reload function to your code, this will ensure the change you made in import files will take effect immidiately. Klayout seems to run every thing at start and changes that being made on the fly will not always behave as expected.

    import os
    import sys
    from   importlib import reload 
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "Mod"))
    import mapLayers 

    And for line 38, following two methods should work.

    act.on_triggered(lambda : mapLayers.mapLayers())
  • Hi RawrRanger
    I made a few changes in the repository which incorporate your suggestions.
    The default branch is changed to master.

  • I have just added RawrRanger code for layer cleaning after layerMapping.
    Added also automatc (lexicographic) mapping of layer names.

  • Hi RawrRanger, I have just added the file (which is based on your code) in the python/layoutDD folder of project.
    The function importPCB should be called from a menu action but I would like to be able to select the imported dxf file in a GUI window. Any idea how to do that ?

  • Hi wsteffe

    There you can utilize the FileDialog to create a popup dialog for fiel selection.
    the *.dxf parameter limits the user from selecting other formats, this can be changed based on your use case.

    import pya
    layoutView = mainWindow.view(mainWindow.create_view())
    mainWindow = pya.Application.instance().main_window()
    dxfFile = pya.FileDialog.ask_open_file_name("Open DXF files", ".", "*.dxf")
    if dxfFile:
        loadLayout(layoutView, path = dxfFile, lockLayers = True):
  • Yes thanks RawrRanger. Actually, just a few minutes after writinng, I had already found it and added it to the repo.
    Now the only available command is importPCB, but I think I will rename it to importDXF.

  • edited September 2023

    Hi RawrRanger, now I have added also a command named Save Active Cell based on your code.
    You may try the following simple sequence:
    1. Use cmd "layoutDD/Import DXF" pointing, in example, to KaIMUX-PCB.dxf'
    2. Use cmd "layoutDD/Save Active Cell"" which writes KaIMUX-PCB.gds and KaIMUX-PCB.lyp in the same folder

    At this point I would like that active cell were linked to KaIMUX-PCB.gds but it is still linked to the KaIMUX-PCB.dxf.
    When I close the project klayout asks me if I want to save KaIMUX-PCB.dxf and I would like to avoid this behaviour.

  • Solved, I have just to use:

    layoutView.save_as(cellIndex,f"{folderPath}/{name}.gds", option)

    instead of:

    layoutView.write(f"{folderPath}/{name}.gds", option)

  • Hi @wsteffe,

    The cell tree is not expanded, so I cannot see the full hierarchy.

    The level settings indicates the number of hierarchy levels that will be shown. Below that level, only cell boxes are drawn.

    Level 0 .. 0 means only a box for the top cell will be shown. So your whole design is basically hidden and replaced by a box.

    Level 0 .. 1 means the content of the top cell is painted (polygons, boxes, paths ...), but the first-level children are hidden and replaced by boxes.

    Level 0 .. 2 means that the first-level children are painted too, but second-level children are hidden and replaced by boxes.


  • Hi all, I made some progress with my layoutDD package. To test this code the content of folder has to be placed in a folder named ~/.klayout/layoutDD. Then you may start klayout doing the following steps:

    1. Open the here annexed dxf file
    2. Command "Import Layout" from menu layoutDD
    3. Command "New Region" from menu layoutDD
    4. Select layer 0/0@2 associated with Region 1 and draw a polygon in that layer
    5. Command "Make Subdomain" from menu layoutDD.
      The implementation of this command is not yet complete. At the moment it just creates a set of new layers in Region 1 with same (ln/dt/name) of visible layers of the imported dxf file.

    At this point you may close all (selecting all cells).

    1. Command open from menu layoutDD pointing to KaIMUX.gds.
      The Data structure is recreated as left before closing.

    My question is. Has anybody an idea why (as show in the following image) the layer names copied to Region 1 are grayed and are not dark bold as the layer names of the first cellview ?

  • Hi wsteffe

    Layer label with grey texts means currently there's no shapes in the layer

    if those layers were created by *.lyp, then these layers might not being actually created.
    For instance, if you open a empty file and load an *.lyp into the empty layout,
    you'll see layer labels are being created. But if you access their layer_index by code,
    the ID will still be zero.

  • Clear. Thanks RawrRanger.

  • Hi all,
    I have made some progress with this package but I have a problem. I need to import FreeCAD module which currently works on linux but not on windows.

    On windows the FreeCAD module is taken from which, I think, is compiled with MW VS.

    FreeCAD can be built with Msys2-MInGW as explained at:
    but I don't know how to get a msys2 platform with python 3.8 (as used in Klayout build). Any idea ?

  • Hi @wsteffe,

    MSYS is a rolling release which makes it extremely difficult to give advise about how to build it. If there were stable releases I'd say something like "take version 22.4 and compile" but there is no such thing. The only option is to build KLayout entirely yourself on your version of MSYS and include all the packages you like. But be warned that Windows deployment is full or surprises and debugging is a pain.

    The lack of an easy deployment path is a general issue on Windows. I made several attempts to switch to some other system like Anaconda or winget. The latter is promising, but it is purely cmake-based and Ruby is missing. The whole matter is a frustrating experience and I wasted too much time on this already. So, I'll leave that to someone else. I don't like DevOps, but I absolutely hate it when it comes to Windows. I personally will favor Linux forever.


  • There is some hope here though: the next version 0.28.13 will be based on a recent version of MSYS2, so with some luck you can build a module that fits you.


  • Hi Matthias, thanks for hints.
    I have just asked RealThunder (see post if he can add a Windows release built with MSYS2/MINGW64 to the assets published at page
    This would be the ideal solution because FreeCAD has a very big set of depenndencies which must be included in the package. In case he has no time to do that I will try by myself.

  • edited November 2023

    Hi Matthias,
    I have just posted a request on MSYS2 repo for a MSYS2 package based on the FreCAD RT fork I am using (see at
    From the replies it seems that FreeCAD can be built ony in the CLANG64 enviroment of MSYS2 (not MINGW64).
    Do you think that with this choice the FC python modules would be compliant with klayout ?

  • I guess it is, but it is just a matter of time until it is no longer compatible with the pre-built version of KLayout. I'm sorry, but that is the way it is. MSYS is not really made for deploment of complex installations. I use it for the ready-to-go version of KLayout because it is easy to use and debug and close enough to the Linux builds, so I don't have to deal with multiple build systems.


Sign In or Register to comment.