Proper way to copy shapes

Hi, Matthias!

What is proper way to copy shape in same layout to another layer?

Probably I misunderstood documentation, but I tried next code (pya.Logger.info calls were added for debug purposes):

        for selected_object in current_view.each_object_selected():
            if selected_object.is_cell_inst(): # Ensure valid shape.
                continue
            shape = selected_object.shape
            if not shape.has_prop_id():
                continue
            pya.Logger.info(f'Old before {shape} {shape.layer}')
            new_shape = shape.dup()
            new_shape.layer = layer_index
            pya.Logger.info(f'Old after {shape} {shape.layer}')
            pya.Logger.info(f'New {new_shape} {new_shape.layer}')

It look like original shape is invalidated after dup() call and pya.Logger.info call confirm my suspicion.

../../../klayout-0.29.7/src/tl/tl/tlReuseVector.h,278,mp_v->is_used (m_n)
RuntimeError: RuntimeError: RuntimeError: Internal error: ../../../klayout-0.29.7/src/tl/tl/tlReuseVector.h:278 mp_v->is_used (m_n) was not true in Shape.__str__

I use custom build of KLayout 0.29.7.

Comments

  • Hi @EugeneZelenko,

    first of all it's not as simple, because the shapes can be selected inside the hierarchy - if you select a shape in one cell instance, but not in another, what is the copy supposed to do then?

    But your code is wrong, because "Shape#dup" copies the Shape object, not the shape itself. Think of "Shape" as a pointer - you create another pointer, but when you change the layer, the original pointer becomes invalid. Hence the message.

    Assuming you want to copy the shape locally in the cell it sits, you have to create a new shape first:

       ...
       # take the shape from "shape" and insert into the same cell, but a different layer
       cell = selected_object.layout(selected_object.cell_index())
       new_shape = cell.shapes(layer_index).insert(shape)
    

    But note, that this will copy the shape in all instances of the cell - that means it will appear in places where you may not have selected it.

    Also: it could happen that you have selected the same shape twice in different cell instances. In that case, the shape is copied twice. Likely not what you want to have.

    A potential solution is to copy that shape into the top cell, using the path transformation to make the shape appear in the same position than before. But by doing so, you shift shapes around in the hierarchy.

    Matthias

  • Hi, Matthias!

    Thank you for help! It works!!! Just little clarification :-)

    cell = selected_object.layout().cell(selected_object.cell_index())
    

    My layout is non-hierarchical and all cells are unique, so it simplifies task a lot :-)

  • Related question: it seems that Shape does not have API to clear prop_id. It will be nice to have, but I have workaround to ignore shapes layers with copied shapes.

  • Hi Eugene,

    you can set prop_id to 0. That is equivalent to clearing.

    Matthias

  • edited July 18

    Hi, Matthias!

    Thank you for explanation! I tried and it works!

    However, shape with prop_id set to 0 still reports True for has_prop_id. I think will be good idea to fix this too.

  • Also a little bit simple way to add new shape:

    new_shape = shape.cell.shapes(layer_index).insert(shape)
    
  • edited July 23

    Ah, yes. That is because there is still an empty property set attached. BTW: if you really want to know if a shape has properties, ask for "has_prop_id = True && prop_id != 0". Because the API differentiates between shapes with an empty property set and shapes without properties. That is because not having properties at all saves a few bytes.

    The last one: is that a feature request? Basically, a Shape can come from a standalone Shapes container, so that feature makes sense only for certain type of Shape objects.

    Matthias

  • Hi, Matthias!

    Thank you for explanations!

    I think clarification in documentation about both checks should be enough.

Sign In or Register to comment.