Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Creating an array with rotation

Hello everyone,

1) My question is about possibility to create an array of objects not only giving them x/y shift, but also a rotation. And maybe dependence of x coordinate (of the centre of the object) from the y coordiante. Is this possible in the klayout?

2) I stumbled upon this question during concrete problem: I want to make this kind of structures routinely.

Any other approach to achieve the pattern on the picture without drawing and rotating parts by hand is welcome.


  • Hi,

    arrays are basic features of GDS/OASIS and don't offer this option :-(

    Coding comes to mind: such structures are easy to generate within a PCell. If you invest some time in setting this up, you'll get a flexible solution and you can even edit the star structure later.

    Here is such a PCell code:

    # Star PCell
    # This sample PCell implements a library called "StarPCellLibrary" with a single PCell that
    # provides a circular ring of "rays". The parameters are:
    #  l: the layer
    #  r1: the inner radius of the ring in µm
    #  r2: the outer radius of the ring in µm
    #  n: the number of "rays"
    #  da: the angle one "ray" covers
    module StarPCellLibrary
      include RBA
      # Remove any definition of our classes (this helps when 
      # reexecuting this code after a change has been applied)
      StarPCellLibrary.constants.member?(:StarPCellLibrary) && remove_const(:StarPCellLibrary)
      StarPCellLibrary.constants.member?(:StarPCell) && remove_const(:StarPCell)
      # The PCell declaration for the circle
      class StarPCell < PCellDeclarationHelper
        include RBA
        def initialize
          # Important: initialize the super class
          # declare the parameters
          param(:l, TypeLayer, "Layer", :default => LayerInfo::new(1, 0))
          param(:r1, TypeDouble, "Inner radius", :default => 1, :unit => "µm")
          param(:r2, TypeDouble, "Outer radius", :default => 5, :unit => "µm")
          param(:n, TypeInt, "Number of rays", :default => 32)     
          param(:da, TypeInt, "Ray angle", :default => 5, :unit => "deg")
        def display_text_impl
          # Provide a descriptive text for the cell
          "StarPCell(L=#{l.to_s},R1=#{'%.3f' % r1.to_f},R2=#{'%.3f' % r2.to_f},N=#{'%d' % n.to_i},DA=#{'%.6g' % da.to_f})"
        def produce_impl
          # This is the main part of the implementation: create the layout
          # compute the ray parts and produce the polygons
          d = Math::PI * da * 0.5 / 180.0
          a = 0.0
          n.times do |i|
            dpts = [
              DPoint::new(r1 * Math.cos(a - d), r1 * Math.sin(a - d)),
              DPoint::new(r1 * Math.cos(a + d), r1 * Math.sin(a + d)),
              DPoint::new(r2 * Math.cos(a + d), r2 * Math.sin(a + d)),
              DPoint::new(r2 * Math.cos(a - d), r2 * Math.sin(a - d))
            a += Math::PI * 2 / n
      # The library where we will put the PCell into 
      class StarPCellLibrary < Library
        def initialize  
          # Set the description
          self.description = "Star PCell Library"
          # Create the PCell declarations
          layout.register_pcell("StarPCell", StarPCell::new)
          # That would be the place to put in more PCells ...
          # Register us with the name "MyLib".
          # If a library with that name already existed, it will be replaced then.
      # Instantiate and register the library
  • And here is how it looks like with the default parameters (r1=1, r2=5, n=32, da=5)

  • Thank you very much for your response!

    I have only one trouble now, I only used the GUI in the editor of the klayout. How do I use such a PCell (put simply, where to past the code? :smile: ). I see the main math part I can modify for my needs. Also, if you have a link to the examples how this works (Defining your own parametrized cells ) and how to start, I would be appreciate.

    Thank you again for your efforts and amazing answer.

  • Klayout has a full documentation that you should read for finding such answers:
    Basically, in your case: press F5, go to "Ruby > [Local]", create a new general macro by clicking on the "+" sign, paste the script above, and press the "Run" button.

  • edited June 11

    KLayout Package Index is a good place to start searching where and how to build your own pcells. Complete example of tech-pcell combination are KLayoutPhotonicPCells packages combined. Also a lot of tips and utilities are hidden inside ShapeLib, which for me as a package was very helpful.

    Of course KLayout's pcell macros templates and the documentation in general, are always showing the correct path you could follow for fast results.


  • edited June 12

    I got the Macrodevelopment window and the code was run (I assume so, since no errors were detected). But how do I get the actual design out in the editor or in the separate file?

    Also I can not even locate the macrosfile itself.

    I tried to follow the links you gave me, but in an hour I could not find the answer :neutral: .

    Also klayout has a specific folder for pymacros. But not for Ruby scripts, such as one from Matthias.

    Sorry in advance if I missed something simple :smile:

  • Hi alljust4forfun,

    An intro about script programming is here:

    I tried to record the steps for installing the macro and using it here:


  • Thank you very much! Finally it worked!

    Funny, that I was doing it almost exactly like in the video.

    Maybe it will be useful for someone else: For me the problem was, because I was using old version of the klayout (namely 0.24.10). Then the output was like this:

    But the newest version does it with no problems:

  • Oh yes ... 0.24.10 is really quite old now :-)

    Thanks for this feedback.


Sign In or Register to comment.