Boolean in DRC without deleting all other layers

Hi,
I'm new to DRC scripting, so this may be a simple issue. I can't find anything on the discussion board.

I would like to write a short script (to be run at the command window) that does the following:

  • input a GDS that contains a large and variable number of layers/datatypes
  • perform a Boolean, replacing one of the layers with the results of the Boolean. Let's say for now that I want Layer 1, Datatype 0 to be replaced by (Layer 1, Datatype 0) NOT (Layer 2, Datatype 0).
  • save to the same GDS filename as the original

The best I can figure out is:

source($input)
target($output)
input(1, 0).not(input(2, 0)).output(1, 0)

This properly performs the Boolean and places it in Layer (1, 0) of the output file...but the output file only contains the Boolean results; all other layers are missing.
It seems as if the problem is that the "output" layout doesn't exist other than the results of the Boolean. But when I try something like

output = input

before the Boolean, I get a massive amount of errors.
What's the correct method here? Thanks in advance.

Comments

  • What if you extended your script to add (bearing in mind that
    my advice on scripting is not knowledge based) a 'foreach' that
    runs through all the "not_the_target" layers and outputs them all
    as-was? Maybe it needs a "null operation" to make a layer
    "active" for output? A GDS-II hierarchical DB won't know what
    all is on a layer, until you make it look all the way to the bottom?
    Loading, is not that. Only establishes the hierarchy, upon which
    other operations (display, menus, script logic) can now act.

    I would not recommend overwriting the input file. I'd use a
    datestamped suffix or something and let the user decide
    whether to 'mv' or 'cp' the product file back onto the original
    name. Particularly during debug you might like the opportunity
    to check outcomes before you blow away the source DB. Of
    couese I suppose you could stash a copy beforehand.

    Moving some of the activity out into the shell could make
    things easier, in some cases (like file system manipulation
    based on conditionals)?

  • A problem with this approach is that I have no idea how to enumerate all of the "not_the_target" layers. This code needs to work when there are an arbitrary number of arbitrarily-numbered "not_the_target" layers.

    I feel like there should be a simple solution to this, since it's easy to do it manually in the GUI (select layers A and B, choose the option A NOT B, send the output to layer A, overwriting the previous layer A).

    Even these steps would be fine:

    1. perform the boolean and save it to layer X of a new gds filename (which would only contain the boolean results on layer X and nothing else, according to my present knowledge)

    2. delete layer X from the original gds (since I was going to replace it anyhow)

    3. merge the two gds into one (i.e. open them both and combine all of their layers)

    This would accomplish the same thing. But I don't know how to do either of step 2 or 3. Step 2 should be trivial, but I can't find anything in the KL documentation. Step 3 might not be common.

  • edited February 2022

    @BWest When you have a target layout specified, it will only contain the layers that are output. And as @dick_freebird pointed out, if you overwrite the file the other layers will be gone :(

    Luckily, KLayout's DRC script language is actually a Ruby script executed dynamically. So you can add a loop and if's to handle all layers and conditionally enable special treatment.

    This is how it's done:

    source(...)
    target(...)
    
    # preserve hierarchy as far as possible
    deep
    
    source.layers.each do |lp|
    
      if lp.layer == 1 && lp.datatype == 0
        # modify 1/0
        (input(lp) - input(2, 0)).output(lp)
      else
        # copy all other layers
        input(lp).output(lp)
      end
    
    end
    

    Note the "deep" mode which will preserve hierarchy as far as possible both for the manipulated and copied layers. You can skip this if you have flat layouts only.

    Matthias

  • Excellent, that's what I was looking for. Thanks very much!

Sign In or Register to comment.