Automation for file import and export

Hello all,

I'd like to have your valuable opinion about some scripting.
What I'm going to do will be like the flow below.

(1) Open 1.OAS
(2) Hide specific layer, for example, 91/0 and 92/0
(3) Save as .txt file with displayed layers only
(4) Repeat (1)~(3) with rest of OAS file
(Filename will be the order of number like 1.oas ,2.oas ,3.oas .....1000.oas)

Is this kind of flow is possible to automate using the script?
If this is available, please let me know a simple example.
I'll try to adopt a script and study for a more complex process.

Thank you,
EO

Comments

  • What do you mean with 'save .txt'? You want to save the layer names or the geometries?

    For the rest I suggest making a Python script like:

    from klayout import db
    import sys
    filename = sys.argv[1] # Get filename from commandline argument
    
    layout = db.Layout()
    layout.read(filename) # Read .OAS here
    
    delete_layers = [(91,0), (92, 0)]
    
    # Delete the layers
    for idx,dtype in delete_layers:
       layer = layout.find_layer(idx, dtype)
       if layer is not None:
          layout.delete_layer(layer)
    
    # Save to something
    output_filename = filename + '.txt'
    # Not sure what you want here...
    

    Then run python3 myscriptfile.py mylayout.OAS.
    May be need to install klayout module first: pip3 install --user klayout

  • Hi tok,

    thanks for this suggestion!

    There is an alternative way to select layers: you can instruct the reader to only read certain layers. For big files this will avoid the memory overhead implied by reading layers you're going to delete anyway.

    The way you do this is:

    options = pya.LoadLayoutOptions()
    options.layer_map.map(pya.LayerInfo(91, 0), 0)
    options.layer_map.map(pya.LayerInfo(92, 0), 1)
    options.read_all_other_layers = False
    layout.read(filename, options)
    

    The first argument to "LayerMap#map" is the layer specification (here: a LayerInfo object) of the layer to select. The second argument is a logical layer - typically this is an incremental number. You could map two layers to one by giving the same number twice.

    "read_all_other_layers" needs to be set to False to instruct the reader to ignore all other layers.

    Matthias

  • Dear tok and Matthias,

    I'm really thank you for your comment.
    I'll try your suggestion.

    For a question from tok, I'll save the geometry as a text file(GDS text file).
    (Required information: coordinates of the vertex from all polygon in specific layers)

    The reason why I'm doing this workflow is to calculate the effective overlap area between polygons
    (After saving a GDS text file, I'll draw all polygons with extracted coordinates and calculate overlap area between specific polygons I have an interest. I need to repeat this work with more than hundreds of files to get statistic data. I'll use R or Python for this post-process)

    As tok's suggestion, using Python API will be effective to handle both KLayout and script itself.

    If you have any further suggestions, please let me know.

    PS.
    Always thanks for your great effort on KLayout, Matthias!

  • Overlap area? This really sounds like something that KLayout can do for you and I guess there is no need for exporting to txt-gds. What kind of overlap? Different layers or polygons on the same layer?
    KLayout supports Boolean operations on polygons. The logical AND operation could be used to find overlaps. And calculating the area of the result is very simple with the KLayout API ( something.area() ).

    The db.Region class can be used for Boolean operations (https://www.klayout.de/doc-qt4/code/class_Region.html)

    For an introduction into using the geometry library of KLayout checkout: https://www.klayout.de/doc-qt4/programming/geometry_api.html
    It is written for the Ruby API but it translates almost 1-to-1 to the Python API.

  • Thank you for the information, tok.

    I didn't know that the area calculation is available in KLayout.
    I'll check the class index to complete my script.

    And I'll add my comment for more details. Please give your opinion if you have a more simplistic idea via KLayout-Python API.

    As the attached sample image(this image is an only example for explanation), we have two layers(blue and green one). In this scheme, I'll calculate the intersected area of each green and blue polygon.(At #1, #2, #3)

    This work will be repeated with the number of files and I'll collect the area value
    at each point(#1~#3) from all files because of the overlap area is slightly different in every file.

    Thanks again for your suggestion.
    I'll keep this up.

  • How is the hierarchy of your layouts? Is there just one top cell?

  • For now I assume you have one cell in your layout. If you have a more complicated layout and cells might overlap, then I think it is easiest to flatten the layout first.

    So here is what I would try:

    import klayout.db as db
    layout = db.Layout()
    layout.read(filename)
    
    # Get the first cell.
    cell_id = next(layout.each_cell_bottom_up())
    cell = layout.cell(cell_id)
    
    # Get the two layers that are the inputs for the intersection operation.
    l1 = layout.layer(1, 0)
    l2 = layout.layer(2, 0)
    
    # Get shapes on the two layers.
    s1 = cell.shapes(l1)
    s2 = cell.shapes(l2)
    
    # Compute intersection.
    intersection = db.Region(s1) & db.Region(s2)
    
    # Compute area.
    area = intersection.area()
    
Sign In or Register to comment.