Changing The Layer Visibility by using Ruby .

edited February 2014 in Ruby Scripting
Hi All,
Once again, I need your help.
How to make the visibility of the
layer "METAL1.drawing - 16/0" and "METAL2.drawing - 18/0" to become invisible
in the active cellview by using Ruby ?
I'm using kLayout 0.23.2 .


Best Regards,
Marben

Comments

  • edited 10:22AM

    Hi Marben,

    that is a little bit more tricky since layers are not addressed directly. Instead the layer list is an object by its own and the concept is that you traverse the layer list tree with a LayerPropertiesIterator in a read-only fashion. Once you have identified that layer you want to change, you can modify it by assigning new layer properties to the current node, i.e.

    view = RBA::LayoutView::current
    
    # The following loop traverses the tree/list
    li = view.begin_layers
    while !li.at_end?
    
      # Identify the current properties and they match with the ones we look for
      # change them
      lp = li.current
      if lp.source_layer == 16 && lp.source_datatype == 0
    
        # To change the properties, create a copy, modify the attributes
        # and replace the current properties 
        new_lp = lp.dup
        new_lp.visible = false
        view.set_layer_properties(li, new_lp)
    
      end
    
      li.next
    
    end
    

    Regards,

    Matthias

  • edited 10:22AM
    Hi Matthias,
    It works .
    How about if I want to make all layers to be invisible,
    and make the layer "METAL1.drawing - 16/0" to be the layer
    that is visible.

    Best Regards,
    Marben
  • edited 10:22AM
    Hi Mathias,
    I think I have solve my problem.
    First I make the all layers invisible.
    Then I make "METAL1.drawing - 16/0" or any other layers that I want to be visible.
    My code is like these :


    # Enter your Ruby code here

    view = RBA::LayoutView::current

    # The following loop traverses the tree/list
    # Make all layers invisible .
    li = view.begin_layers
    while !li.at_end?

    # Identify the current properties and they match with the ones we look for
    # change them
    lp = li.current


    # To change the properties, create a copy, modify the attributes
    # and replace the current properties
    new_lp = lp.dup
    new_lp.visible = false

    view.set_layer_properties(li, new_lp)



    li.next

    end # while


    # Begin the display of layer 16 and layer 17 .


    view = RBA::LayoutView::current

    # The following loop traverses the tree/list
    li = view.begin_layers
    while !li.at_end?

    # Identify the current properties and they match with the ones we look for
    # change them
    lp = li.current
    # commented
    if lp.source_layer == 16 && lp.source_datatype == 0 || lp.source_layer == 17 && lp.source_datatype == 0

    # To change the properties, create a copy, modify the attributes
    # and replace the current properties
    new_lp = lp.dup
    new_lp.visible = true



    view.set_layer_properties(li, new_lp)

    end #If

    li.next

    end #while




    Best Regards,
    Marben
  • edited February 2014

    Hi Marben

    bascially you could have it as simple as that:

    view = RBA::LayoutView::current
    
    li = view.begin_layers
    while !li.at_end?
    
      lp = li.current
      new_lp = lp.dup
      new_lp.visible = (lp.source_layer == 16 && lp.source_datatype == 0)
      view.set_layer_properties(li, new_lp)
    
      li.next
    
    end
    

    Matthias

  • edited 10:22AM
    Hi Matthias,
    Thank you very much for your reply.
    But there is an error .
    "lp = li.current" is missing.
    I add it in your code, it is now working like a charm.
    Modified code is below.


    # Enter your Ruby code here

    view = RBA::LayoutView::current

    li = view.begin_layers
    while !li.at_end?
    # Identify the current properties and they match with the ones we look for
    # change them
    lp = li.current
    new_lp = li.current.dup
    new_lp.visible = (lp.source_layer == 16 && lp.source_datatype == 0)
    view.set_layer_properties(li, new_lp)

    li.next

    end


    Best Regards,
    Marben
  • edited 10:22AM

    You're right. I edited the code above.

    Thanks, Matthias

  • edited 10:22AM
    Hi,

    I have a question related with this. I have tried to selectively hide some layers and save the image of the layout with the following code:


    main_window = RBA::Application::instance.main_window
    main_window.load_layout($input, 0)
    layer_view = main_window.current_view
    layer_view.max_hier

    cell_view = RBA::LayoutView::current

    layer_prop = cell_view.begin_layers

    while layer_prop != layer_view.end_layers

    layer_node = layer_prop.current

    if layer_node.source_datatype == 0 && layer_node.source_layer == 1
    dup = layer_node.dup
    dup.visible = false
    cell_view.set_layer_properties(layer_prop, dup)
    break
    end

    layer_prop = layer_prop.next

    end

    cell_view.save_image($output, 1153, 618)


    Here is the command line used:
    klayout.exe -z -r test.rb -rd input=test.gds -rd output=test.png

    But the output image has all the layers shown. I am very new to both Ruby and KLayout so there can be a lot of coding errors. I'd appreciate it if you can help me on this.

    Thanks
  • edited April 2014

    Hallo,

    I noticed that you might need "cell_view.update_content" before "save_image". That is not quite obvious and it should not be necessary, but "update_content" (among other things) basically synchronizes changes in the layer properties with the renderer and that may help here.

    Matthias

  • edited 10:22AM
    It is working after inserting "cell_view.update_content" before "save_image".

    Thanks you so much!
Sign In or Register to comment.