# See the loader file for license and author info

require 'sketchup.rb'
module CLF_Extensions_NS

  module Clf_Extrapolate_Colors

    def self.main

      model = Sketchup.active_model
      ents = model.active_entities
      sel = model.selection
      model.start_operation( "Extrapolate Colors", true)
      faces = []
      mats = []
      mat = []

      sel.each do |e|
        faces << e if e.is_a? Sketchup::Face
      end
      
      if faces.length == 0
        UI.messagebox "Please select faces to paint, then run Extrapolate Colors."
        return nil
      end
      
      faces.each do |e|
        mats << e.material if e.material
        mats << e.back_material if e.back_material
      end
      
      if mats.length == 0
        UI.messagebox "Some of the selected faces must be painted already.  Please make sure that some faces are already painted."
        return nil
      end      

      faces.each do |e|
        mat = mats[rand(mats.length)]
        if e.material
          mat = e.material
        else
          e.material = mat
        end
        unless e.back_material
          e.back_material=mat
        end
      end
      model.commit_operation
    end # method
  end # module
end




