How to know the amount of selected objects?

edited February 2012 in General
Hi Matthias,

Is there a function to get the amount of selected objects(Polygons/Boxes/etc...)? And is it possible to add a function to show the "[current/total number]" in Object Properties window?

About the GUI, in ( Edit => Select ) menu, once we want to select only Polygons or Boxes, we have to repeat the action Edit=>Select=>Images and then Texts and etc... 7 times, because the menu disappear when clicked, then repeat again to select back if we want ot select all.

Is it ok to change the object to "options" or add "select all/unselect all" button?

Thanks for your kindly help~

Best Regards,
--
chhung

Comments

  • edited February 2012

    Hi chhung,

    thats a valueable feedback. In fact, enabling or disabling all shape types is tedious.

    I'll consider that as a new feature. Here is a litte script that adds "Select All" and "Unselect All" to the Edit/Select menu:

    # adds a "unselect all" and "select all" function to the Edit/Select menu:
    app = RBA::Application.instance
    mw = app.main_window
    menu = mw.menu
    
    if !menu.is_valid("edit_menu.select_menu.select_all")
      # create the menu action with event binding (requires KLayout 0.21)
      # use a global variable to keep a reference to the RBA::Action object
      $em_select_all = RBA::Action.new
      $em_select_all.title = "Select All"
      $em_select_all_proc = lambda do
        menu.items("edit_menu.select_menu").each do |p|
          if p =~ /pi_enable/ && !menu.action(p).is_checked?
            menu.action(p).trigger
          end
        end
      end
      $em_select_all.on_triggered(&$em_select_all_proc)
      menu.insert_item("edit_menu.select_menu.end", "select_all", $em_select_all)
    end
    
    if !menu.is_valid("edit_menu.select_menu.unselect_all")
      # create the menu action with event binding (requires KLayout 0.21)
      # use a global variable to keep a reference to the RBA::Action object
      $em_unselect_all = RBA::Action.new
      $em_unselect_all.title = "Unselect All"
      $em_unselect_all_proc = lambda do
        menu.items("edit_menu.select_menu").each do |p|
          if p =~ /pi_enable/ && menu.action(p).is_checked?
            menu.action(p).trigger
          end
        end
      end
      $em_unselect_all.on_triggered(&$em_unselect_all_proc)
      menu.insert_item("edit_menu.select_menu.end", "unselect_all", $em_unselect_all)
    end
    

    (See http://www.klayout.de/useful_scripts.html for a description how to install such scripts).

    Here you also find a script to compute the total area of all shapes selected. It can be adjusted to show the selected shape count for example:

    $count_shapes_in_selection = RBA::Action.new
    $count_shapes_in_selection.title = "Show Count of Selected Objects"
    $count_shapes_in_selection_proc = lambda do
    
      app = RBA::Application.instance
      mw = app.main_window
    
      lv = mw.current_view
      if lv == nil
        raise "No view selected"
      end
    
      count = 0
    
      lv.each_object_selected do |obj|
        count += 1
      end
    
      RBA::MessageBox.info("Count", "Total count of selected objects is #{count}", RBA::MessageBox.b_ok)
    
    end
    
    $count_shapes_in_selection.on_triggered(&$count_shapes_in_selection_proc)
    
    app = RBA::Application.instance
    mw = app.main_window
    
    menu = mw.menu
    menu.insert_separator("tools_menu.end", "sep_count_shapes")
    menu.insert_item("tools_menu.end", "count_shapes_in_selection", $count_shapes_in_selection)
    

    (this is a slightly modified form which does not require the MenuAction helper class but requires KLayout 0.21 to work).

    Best regards,

    Matthias

  • edited November -1
    Hi Matthias,

    Really appreicate your kindly help, the two programs are useful for us.

    About the count_shapes program, the line "count = 0" would crash klayout if we select too many objects(more than 18500 or more), the error messages as follow:


    /usr/local/klayout/rb/count_shapes.rbm:26: [BUG] Segmentation fault
    ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]

    /usr/local/klayout/bin64/klayout.sh: line 66: 29426 Aborted ${KLAYOUT_HOME}/bin64/test/klayout ${ARGUMENTS} ${FILES}


    I think it's caused by the integer declaration of variable "count", so I modified the line to "count = 0.0" to declarate it as a floating point and it works(although the result would show [count].0)

    Best Regards,
    --
    chhung
  • edited February 2012
    Hi Matthias,

    Another strange issue happened...

    If we click Edit=>Select=>Select All(or Unselect All), everything works fine.

    However once we select many objects then run count_shapes function, the error happened when we click Edit=>Select=>Select All(or Unselect All), the error messages as follow:


    *** ERROR: Ruby error: 'undefined method `call' for #<RBA::ObjectInstPath:0x7f08f89c5e70>' (NoMethodError) (class NoMethodError)


    I'm sure both programs work fine separately.

    Best Regards,
    --
    chhung
  • edited February 2012

    Hi chhung,

    thanks for reporting that. Both problems are related. Apparently Ruby garbage collects the Proc objects used to bind the code blocks. I have found a fix for that and it will go into the next release.

    Until then, I have changed the code pieces above so global variables are used to hold references to the Proc objects. That works in my installations even for high shape counts.

    Best regards,

    Matthias

  • edited November -1
    Hi Matthias,

    I tried the two new Ruby programs and the "Ruby error" issue had been fixed, however the "[BUG] Segmentation fault" is still there if "count = 0".

    I have no idea it's caused by different Ruby version or what, and decide to dirty solve it via "count = 0.0" now.

    Really appreciate for your work hard on klayout development, thank you~

    Best Regards,
    --
    chhung
  • edited November -1

    Hi chhung,

    I am using ruby 1.8.7 and 1.9.1 myself, but on 32bit. So it's likely not to be the ruby version you use. I can use it on a million shapes (although it's somewhat slow when selecting them).

    It's also unlikely that "count = 0" is the reason for the crash. What happened before was that the Proc object holding the code was gone and free'd memory was accessed. Hence the effect was pretty random.

    I'll try to reproduce the problem on a 64bit installation.

    Best regards,

    Matthias

  • edited November -1

    Hi chhung,

    I have tried to reproduce the problem on 64bit Ubuntu 10.04 with ruby 1.8.7 and again it works for me. That is how I tried it:

    I copied both code snippets above into two files: select.rbm for the first one and count.rbm for the second on. Then I called KLayout this way:

    klayout -rm count.rbm -rm select.rbm my_file.gds
    

    I can select up to 2.5M shapes and get the count of them. Also "Select All" and "Unselect all" works fine.

    Am I doing something different compare to you?

    Best regards,

    Matthias

  • edited November -1
    Hi Matthias,

    I tried to find out the cause via A/B test and finally figured out the problem is from Ruby Library version.

    Once I update the Ruby Library from v1.8.7 Patch174 to v1.8.7 Patch249, this problem gone.
    It should be a Ruby Library bug and fixed in later version.

    Thank you for your kindly support~ :)

    Best Regards,
    --
    chhung
Sign In or Register to comment.