Step by Step Xsection

edited May 2018 in KLayout Support
Hey team,

Just to set the expectations, I'm new to KLayout as well as Ruby scripting. In my job role I have been tasked to see if there is a possibility to run the Xsection add-on available within KLayout to achieve a step by step processing of the layout. In other words stopping the "cmos.xs" ( after every step like deposition or etch or Litho to look at the process flow step by step.

In the language of code:
Is there a way to execute the "output" command one by one, controlled by a user input? The user input is expected to click a button, where the button(I'm envisioning) could be a QtDialog box awaiting the user to click "Next". From my self guided learning on Ruby and the code it seems difficult because the whole Xsection is an event and cannot have "pause and play" kind functions.

Please excuse, if my question is unclear or if it is a repeat question. I did try to search the forums but couldn't find any thing similar.


  • edited 10:07PM
    Hi Asharma:

    If you want to look at the mask output you first would have to execute the fab boolean operations on the layout. For this you would have to parse the boolean, and then combine layers into mask data. I think the functionality of stepping through the flow can be done after reading the use interface part of the KLayout reference.
    I used Python instead of Ruby because there is a free very high quality parser generator available (written by David Beazley).

    Also I used PyCharm as the IDE and drive KLayout with it. This is by far the fastest development environment for layout in the industry, since PyCharm is the by far best IDE (= Android Studio etc.). I think there may also be a Ruby plugin ...

    Cheers, Erwin
  • edited 10:07PM

    Hi Asharma,

    In general, a cross-section script has to prepared differently when you want to show the process progress. The concept is currently to keep the computed material outline internally and visualize it in a final step. If you put the output statements in the right places, supply a "wait" function and patch the output function to allow overriding outputs, a simple solution can be implemented.

    This is an example for a modified .xs script which provides this feature:

    # patched version of the original output function which 
    # can be called multiple times. It will override the 
    # output already present.
    def output(layer_spec, layer_data)
      ls = string_to_layerinfo(layer_spec)
      li = @target_layout.layer(ls)
      shapes = @target_layout.cell(@target_cell).shapes(li)
      shapes.clear  # new feature
      # confine the shapes to the region of interest
      @ep.boolean_to_polygon([ ],, RBA::EdgeProcessor::mode_and, true, true).each do |polygon|
    # Shows a message box with the given message
    def wait(text)
      # Show all layers which were created in between
      if @lyp_file
      if RBA::QMessageBox::question(nil, "Continue", "#{text}\n\nPress Ok to continue", RBA::QMessageBox::Ok | RBA::QMessageBox::Cancel) != RBA::QMessageBox::Ok
        raise "XSection script interrupted"
    # ------------------------------------------------------------
    # Actual xs starts here:    
    # Prepare input layers
    layer_TRENCH = layer("2/0")
    layer_IMPLANT = layer("3/0")
    substrate = bulk
    output("1/0", substrate)
    wait("Substrate Step")
    # First epitaxial layer
    epi = deposit(0.5)
    output("2/0", epi)
    wait("Epi Step")
    # Second epitaxial layer
    epi2 = deposit(0.5)
    output("3/0", epi2)
    wait("Epi2 Step")
    # Etch substrate on mask with thickness 0.7µm and angle 30°
    mask(layer_TRENCH).etch(0.7, :taper => 30, :into => [substrate, epi, epi2])
    # Note that we output the layer once again to present the etched version 
    output("2/0", epi)
    output("3/0", epi2)
    wait("Etch Step")
    # Create an implant by growing the mask into the substrate material and both epitaxial layers.
    implant=mask(layer_IMPLANT).grow(0.2, 0.05, :mode => :round, :into => [substrate, epi, epi2])
    output("6/0", implant)
    wait("Implant Step")



  • edited May 2018
    @Erwin: thanks for your prompt feedback and suggestions. Unfortunately I'm not familiar with Python coding too and so starting over seems daunting. So for now I would like to stick with Ruby, especially since I could get Matthias suggestion to work.

    @Matthias: Thanks to you too for the work around, it worked like a charm! I was able to stick your modified "output" and accomplish what I had set out to do. Also I forgot to mention this in my first comment, a big thanks for creating the Xsection as a whole, since it is a savior for my team and we are heavily dependent on it. So keep up the awesome work.

  •   Hello,

    I would like to use this file.xs.

    In which directory I have to copy it under .klayout folder?

    Many thanks in advance

  • Hi Luciano,

    you don't need to copy it. Just browse to it. But you need to install the XSection package first.


  • Hi Matthias,

    I copied the xsection.lym file from following link

    to macros directory on .klayout folder.

    I tried to use the xsection function on my gds file, using the file in attach, but I received the error message into the image
    I'm using klayout on version 0.25

    Can You explain me what am I doing wrong?

    Many thanks in advance.


  • You should rename "CMOS3JP_XSEM.lyp.txt" to "CMOS3JP_XSEM.lyp".


  • Hi Luciano,

    you can zip files for attaching them.

    Anyway - don't use SourceForge anymore. Just install the xsection package (in Tools/Package Manager). Maintenance happens on GitHub (

    For your issue, try:

    layers_file(File.join(File::dirname(__FILE__), "CMOS3JP_XSEM.lyp"))

    This should load the .lyp file relative to the .xs file.


  • Hello!

    Thank you very much for all your useful tools and tips.
    I am using both the step by step xsection approach and the SVG exporter you kindly provided. I am wondering if there is a way to combine the two scripts so that at each step of the xsection the SVG exporter is lauched and saves a png file of the current cross-section. In the end, there should be all the png files generated by the SVG exporter at each step defined by the step by step xsection file.
    Thank you in advance for your help.


Sign In or Register to comment.