require 'sketchup.rb'
#--------------------------------------------------------------------------
#    Copyright (C) 2008 Uli Tessel (utessel@gmx.de)
#--------------------------------------------------------------------------
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#--------------------------------------------------------------------------

def DoExplodeEntity( entity )

  # only explode groups
  if (entity.is_a? Sketchup::Group) and (not entity.locked?)    
    DoExplodeEntities( entity.entities )
    entity.explode()
  end
  
end

#--------------------------------------------------------------------------
def DoExplodeEntities( entities )
  entities.each{ |entity| DoExplodeEntity( entity ) }
end

#-----------------------------------------------------------------------------
def recursiveExploder( )
    model=Sketchup.active_model
    what=model.selection

    # nothing selected? nothing to do!
    if (what.count==0)
      return
    end

    model.start_operation("Recursive Explode")
    begin
      DoExplodeEntities( what )
      model.commit_operation
    rescue => bang
      model.abort_operation
      UI.messagebox "Error: " + bang
    end
end
#--------------------------------------------------------------------------

def exploder_selected_group
    ss = Sketchup.active_model.selection
    ss.each { |entity| if (entity.is_a? Sketchup::Group) then return true; end }
    return nil
end

#--------------------------------------------------------------------------
# Register within Sketchup
if(file_loaded("exploder.rb"))
# 	menu = UI.menu("Plugins");
#	menu.add_item("Recursive Explode") { recursiveExploder() }
	
	UI.add_context_menu_handler do |menu|
        if( exploder_selected_group )
            menu.add_item("Recursive Explode") { recursiveExploder() }
        end
    end
end

#--------------------------------------------------------------------------
file_loaded("exploder.rb")
