Import other layout by script

Now, klayout could import other layouts in the "File" menu as "Import/Other File Into Current".

Is there a way to use script (python or ruby) to do it? (eg. extra mode import)



  • Hello dion,

    It's simply'filename.gds').

    Here is a snipped of python code I used to get the Tiles cell from a second gds file and place them on the TOP cell of the first gds. The load_options might not be necessary, but it might help if both layout share the same layer scheme.

        layout = pya.Layout()
        lmap ="main-chip.gds")
        TOP = layout.top_cell()
        load_options = pya.LoadLayoutOptions()
        load_options.text_enabled = True
        load_options.set_layer_map(lmap, True)"main-chip-tiles.gds", load_options)
        tiles = layout.cell('TOP_tiles')
                                      pya.DTrans(pya.DTrans.R0, pya.DPoint(0, 0))))

    Hope it helps.

  • Hi tlima,
    Thanks for your suggestion.

    In my case, the top name of tile gds is unknown, so I have to read it first to scan top name. Also, it's name maybe confict with cell name in main chip.

    This is why I need 'import other file' function, which could load gds to cell directly.

    Now, I add patch to, and export to python.

    void import_extra_layout(db::Layout &target, db::cell_index_type target_cell_index, const std::string &setup_in)
        StreamImporter importer;
        StreamImportData data;
        try {
            data.from_string (setup_in);
          } catch (...) {
            data = StreamImportData ();
        data.setup_importer (&importer);
        std::vector <unsigned int> new_layer_ids; (target, target_cell_index, new_layer_ids);
  • Hi all,

    "load atop of another one" is one option, but please be aware that this solution will mix cells with identical names. Like if layout A has top cell "TOPA" and a subcell called "SUBCELL" and the second layout B has top cell "TOPB" and a subcell called "SUBCELL" as well, both "SUBCELL"s will be merged into one. That may not be desired.

    The method is safe, when both layouts are flat and have the same database unit.

    There is no method for "Import layout from another file", because this feature is just using "Cell#copy_tree" or "Cell#copy_tree_shapes":

    • "Cell#copy_tree" will take a cell and it's subcell and replicate the whole tree in the target layout. This is the method used with "Merge", "Extra" and "Instantiate" import mode. These modes just differ in the way the original top cell is created in the target layout.
    • "Cell#copy_tree_shapes" assumes that there is already a target hierarchy and the contents of a cell plus it's subcells is supposed to be mapped to this hierarchy. The kind of mapping is given by the "CellMapping" argument. You can ask the method to create new cells if required (in "full" flavour of the "CellMapping" argument). Apart from this, the "copy_tree_shapes" method just copies shapes. The shape may be done in a smart way such that the resulting layout - as seen from the root cell - looks exactly like the original layout, even if the hierarchy differs. This is the method used with "Merge hierarchy". Technically this corresponds to a "CellMapping" made with "from_geometry_full".


  • I think there are 3 different cases depending on what the user needs:

    • Overwrite ==> Replace existing cell in layout with new cell which potentially has different subcells & shapes & texts, and if they already exist, they get overwritten too. There is the possibility that after the overwriting there are orphaned cells, they are still being written in the GDSII export which is usually not desirable. I am only using "overwrite" if I know exactly what I am doing, there are lots of potential failure modes.

    • Safe merge ==> Import GDS file into a new layout, then analyze that layout and rename all cells which already exist in the target layout. After renaming the two trees can be merged, but the new tree needs to be placed as an instance at a given "transformation" of the old layout.

    • Combine ==> Import GDS file and add (cell by cell) data (cells, shapes, texts) from the imported file to the original file. If the top cell name of the imported file doesn't exist yet, either it needs to be placed as an instance, otherwise no new instance is created.

    We are doing this a lot on test vehicles (block building) and I think the only 100% predictable way is "Safe merge". Overwrite can have side effects due to leaf cells which accidentally have the same name being overwritten. "Combine" can have side effects where interactions (DRC) between shapes get overlooked.

    In any case, DRC needs to be run at some point after importing a GDS file unless the imported file has been specifically designed to be DRC clean (e.g. running a dummy fill utility / tiler).

    So maybe it would be good to have a "safe merge" hard coded in C++ since Python name compares are probably slower than C++. It looks like "cell#copy_tree" implements the "Combine" case. I think I side-stepped issues by "knowing" that there are no cell collisions in my layout and only using very simple imports. Serious merging would require a name check before doing the copy_tree with a couple of warnings if cells already exist, or rename cells, of course this can be implemented in Python and maybe the cell name comparison is fast enough, I haven't tried it. I'll give it a shot.

  • I've done something like this based on a tip by Matthias. Prototype code is in

    This code does the following:
    1. renames all cells of layout 1 temporarily to "" (potential issue with pcells from std library)
    2. imports layout 2 and find name conflicts
    3. if conflict name starts with cache_, replace all instances in layout 1 with those in layout 2. (these cells were identical for me, so no need for duplicates. or else rename it and append a counter
    4. rename cells from layout1 to their original names.

    Hope it helps someone. If this is useful enough, I could try putting it in c++.

  • hi tlima
    This is exactly what I need.
    I have one gds file on disk, i want to copy it to top that I opening one Klayout.
    Can you tell me how to use it? which function I should call from my script, which parameter I should change?
    thank you so much!.

Sign In or Register to comment.