Resizing a box

edited August 2014 in Ruby Scripting

Hi,

I am trying to resize a box with some ruby code but it simply is not working. I found in the documentation that there should be "left=" and "right=" methods that set the left and right edge of the box, so I use them, but they seem to have absolutely no effect.

I use the following code which takes every selected box and should change the left and right edge so that it gets a width of 50 nm:

width=50
lv.each_object_selected do |obj|

  shape = obj.shape
  if shape.is_box?

    box = shape.box

    mid = box.left + box.width/2
    box.left = mid - width/2
    box.right = mid + width/2


  end
end

When I run the code, no errors, but the box is also not changed.

When I use breakpoints and output 'box.left' in the console I can see that box.left changes from -30005 to -30025, which is correct. But it does not update in the layout.

What am I doing wrong?

Comments

  • edited August 2014

    Hi Nick,

    the behaviour can be explained if you consider that "shape.box" returns a copy of the internal database object. If you manipulate that copy, it won't have an effect on the database object initially. But you can easily fix that by assigning the changed box back to the database object:

    shape.box = box
    

    There are two other ways of achieving this:

    shape.box_p1 = ...
    shape.box_p2 = ...
    shape.box_width = ...
    shape.box_height = ...
    shape.box_center = ...
    

    manipulate the database object directly.

    And

    shape.transform(...)
    

    is also a way to manipulate a shape (not only boxes).

    The background story is that KLayout internally works with an optimized data structure and does not carry the overhead required for scriptable objects. So the database objects are lightweight objects - a box for example occupies as little as 16 Bytes.

    RBA::Shape is the gateway to these objects - it provides access to the database objects. You can extract them into scriptable working objects such as RBA::Box, RBA::Polygon etc. Those objects are fully script-enabled and can be passed around, stored or manipulated, but they are detached from the database.

    RBA::Shape has some direct access methods like "box_center" for convenience. Those are basically identical to the "extract - manipulate - assign" triplet.

    Matthias

  • edited November -1

    Thanks, that makes perfect sense! I figured it had something to do with box being a copy but I didn't think it would be as easy as replacing the copy back (and the Shape.box_width method is even easier).

Sign In or Register to comment.