Database Units - python script


Very new to Klayout scripting. I have a question about defining the units.

For example, I would like to draw a box with a length of 100um and a width of 100um. I came across a python example for a different shape where the code is :

Create layer #'s

l_1x1_outline = layout.layer(1, 0) # 1x1 Outline
l_metal = layout.layer(11, 0) # Metal

Metal dimensions

line_width = 5 * 1000 # 5 um
pitch = 100 * 1000 # 100 um ----->this is the box I am trying to make

Draw outline

outline = UNIT.shapes(l_1x1_outline).insert( pya.Box(0, 0, pitch, pitch) )

Draw metal legs

leg1 = UNIT.shapes(l_metal).insert( pya.Box(0, 0, line_width, pitch) )
leg2 = UNIT.shapes(l_metal).insert( pya.Box(0, pitch-line_width, pitch, pitch) )

When comparing with Klayout GUI with database units as 0.001um (1nm) - I input 100 by 100 manually (without multiplying by 1000), the resulting box's dimension is the same as given in the script "pitch = 100*1000 # 100 um".

Does the GUI automatically take 1000 into account? Just want to make sure that I am inputting the right values in um units.

When I run the above script by changing "pitch = 100 # 100 um", my box in the layout is 0.1 in size (much smaller than what I want).



  • Hi @nishtha50376,

    First of all, you can format source code here by putting a single line with triple backticks before and after the code. This makes it easier to read.

    Regarding your question: the UI accepts micrometer units. It is automatically converted into database units.

    The concept is this: the Layout object stores all shapes with integer types. The database unit is the factor that converts that integer into micrometers. So a coordinate/length/pitch .. of 1000 with a database unit of 0.001µm will convert into a coordinate/length/pitch .. of 1µm.

    To convert a length of 1µm back to integer units you basically have to divide by that database unit (or multiply by 1000 if your database unit is 0.001). The UI accepts micrometer units (unless you check the "database units" option) and does this computation for you.

    In the script you can do this computation by means of script code. But there is another option: When you use "Box" objects, you are using integer coordinates. You can also use "DBox" objects which uses floating-point coordinates. These are read as micrometers when you insert them into a shapes collection and the DBU conversion is done for you.


    pitch = 100.0 / layout.dbu
    outline = cell.shapes(layer).insert( pya.Box(0, 0, pitch, pitch) )

    is equivalent to

    pitch = 100.0
    outline = cell.shapes(layer).insert( pya.DBox(0, 0, pitch, pitch) )

    For most objects there is a "D" alternative (DPolygon, DText, DPath ..). Using these you can consistently work with micrometer units.


Sign In or Register to comment.