Subtract layers

edited June 2016 in Python scripting
Dear all,

I would like to subtract two layers. In the following example, I am trying to create two layer with one box in each. Then I would like to put the subtraction of the two layers in a new layer. How should I proceed ? Here is the starting point of the code :

import pya
import math

# Create Layout
layout = pya.Layout()

# Create cell
TopCell = layout.create_cell("Cell1")

# Create layers
Layer0 = layout.layer(1, 0)
Layer1 = layout.layer(2, 0)

# Create boxes
box1 = pya.Box(-2400, -2400, 2400, 1500)
box2 = pya.Box(-10, -10, 210, 210)
TopCell.shapes(Layer0).insert(box1)
TopCell.shapes(Layer1).insert(box2)

layout.write('bla.gds')

How to subtract Layer0 and Layer1 and put them in a new layer ?
Thanks a lot.

Comments

  • edited June 2016

    Hello,

    There is a simple way to do this using a DRC script:

    Layer0 = input(1, 0)
    Layer1 = input(2, 0)
    (Layer0 - Layer1).output(10, 0)
    

    the basic way using plain Python is via the pya.Region object. A Region object is basically a collection of polygons and offers boolean and many other operations:

    l0 = pya.Region()
    l0.insert(pya.Box(-2400, -2400, 2400, 1500)
    l1 = pya.Region()
    l1.insert(pya.Box(-10, -10, 210, 210)
    result = l0 - l1
    
    output = layout.layer(10, 0)
    TopCell.shapes(output).insert(result)
    

    Matthias

  • edited November -1
    Thank you Matthias, this is very helpful.
  • This answer has helped me greatly, thank you! The "region" class is quite hidden in the documentation.

Sign In or Register to comment.