Move shapes in RBA code / macro

edited December 2012 in General


I want to move the selected shapes in the current layout view by an offset (x,y).
I already managed to select all shapes in the current view programmatically.
Now i can iterate over each ObjectInstPath object, like that

lv.each_object_selected do |sel|



But how can the shapes now be transformed / moved?
I already tried to use the ObjectInstPath.trans object. But it doesnt work or i missed something important.
I would appreciate your help.



  • edited December 2012

    Hi Indigo,

    actually there is a simple or a complex case.

    The complex case occurs if you want to move shapes that are located in child cells. In the general case that involves variant building because a cell might be instantiated with different orientations. If you want to move the shapes in the same direction (as seen on from the parent cell) you'll have to apply a different transformation for each orientation variant and before you can do so you'll have to create copies of that cell. Since I assume you are dealing with the simple case where that is not required, I'll skip that topic here.

    The simple case oocurs if you have only objects local in the current cell or inside child cells which are instantiated without rotation. In that case, the loop to move the selection is simply

    trans = Trans::new(Point::new(100, 0))   # (example: move by 100 DBU in x direction)
    lv.each_object_selected do |sel|  
      if !sel.is_cell_inst?
        sel.shape.shapes.transform(sel.shape, trans)
        sel.inst.cell.transforma(sel.inst, trans)

    That loops handles both shapes and instances.

    Please note that this sample does not include undo/redo capabilities. You can add this by wrapping your code into a transaction .. commit block like that:

    lv.transaction("Description of your operation")
      ... some code ...



  • edited 6:00AM

    Hi Matthias,

    That was excatly what i was looking for!
    Thank's for the fast answer!


  • Hi @Matthias,

    Sorry for necrobumping, but I suppose it is better to ask my question here. I'm trying to do some actions with the selected objects, similarly to Indigo, and as a reference, I used your code to iterate over the selection. The boxes/polygons/etc are moved nicely; however, if selection contains instances, the error comes out: Caught the following exception: Trying to replace an object in a list that it does not belong to in Cell::transform (Class RuntimeError).

    So the question is how to modify this code to iterate over selection? I use KLayout 0.27.3.


Sign In or Register to comment.