Iterating a script through a selection of objects

Continuing the discussion from Dynamic Component Error - Can anyone confirm:

I had a great script written by jim_foltz that fixed a bug in dynamic components reporting incorrect LenX,Y,Z sizes.

Jims solution has been working a treat, however I need to run the script on each dynamic component individually.

Does anyone know of a way I can have this script execute for each dynamic component from a selection. ie; select a number of entities & this script iterates through each dynamic component within the selection (including nested components)…

Any help - even a push in the right direction would be helpful.

Cheers Nathan…

(Moved to Ruby API category.)

FYI, code needs to be wrapped in triple backtick lines, or the forum engine will swallow characters.
The attribute names in the pasted text above lost the underscore characters.

Try something like:

# encoding: UTF-8

  UI.add_context_menu_handler do |menu|
    dict = 'dynamic_attributes'
    sel = Sketchup.active_model.selection
    if !sel.empty?
      set = sel.grep(Sketchup::ComponentInstance)
      if !set.empty?
        dcs = set.find_all {|c| c.definition.attribute_dictionary(dict) }
        if !dcs.empty?

          menu.add_item('Selected DCs Actual Size') do
            lxn = '_lenx_nominal'
            lyn = '_leny_nominal'
            lzn = '_lenz_nominal'
            dcs.each {|ins|
              ins.definition.delete_attribute(dict,lxn)
              ins.definition.delete_attribute(dict,lyn)
              ins.definition.delete_attribute(dict,lzn)
              $dc_observers.get_latest_class.redraw_with_undo(ins)
            }
          end

        end
      end
    end
  end # menu handler

EDITED, so the attribute name strings are created before loop. (No sense in re-creating them via literal expression in every iteration.)

1 Like

Thanks Dan, much appreciated, worked a treat & I’ll be sure to add the triple backtick lines next time…

Cheers

1 Like