# Copyright 2004,2005 Rick Wilson 
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Name :          roofpaint.rb 1.5
# Description :   paint all sloped faces
# Author(s) :        Rick Wilson, D. Bur
# Usage :         1.	Install into the plugins directory or into the 
#				Plugins/examples directory and manually load from the
#				ruby console "load 'examples/roofpaint.rb'" 
#                 2.	Run "Roofpaint" from the plugins menu
# Date :          22.Sep.2004
# Type :          Tool
# History:
#			1.5 (9.June.2005) - revised to add vertical face painting
#			1.4 (23.Sep.2004) - revised for material or color selections
#			1.3 (23.Sep.2004) - revised to "remember" previous color
#						  selections and to separate top and
#						  bottom faces (RickW)
#			1.2 (23.Sep.2004) - added dialog box for color selection (Didier Bur)
#			1.1 (22.Sep.2004) - second version - paint eaves blue
#			1.0 (22.Sep.2004) - first version
# 
require 'sketchup.rb'

def roofpaint

	model=Sketchup.active_model
	view=model.active_view
	ents=model.active_entities
	materials=model.materials
	all_colors = Sketchup::Color.names
	mat_names=materials.collect {|m| m.name}
	if mat_names.empty?
		enums = [[all_colors.push("none").join("|")],[all_colors.join("|")],[all_colors.join("|")]]
	else
		enums = [[mat_names.push("none").join("|")],[mat_names.join("|")],[mat_names.join("|")]]
	end

	# User input values

	prompts = ["Base color for sloped top faces", "Base color for sloped bottom faces", "Base color for walls"]
	# default values in the input box
	if not $base_color
		values = ["Red","Blue","Green"]
	else
		values = [$base_color,$back_color,$wall_color]
	end
	results = inputbox(prompts, values, enums, "Color choice")

	# User cancels
	return if not results
	# Otherwise
	$base_color, $back_color, $wall_color = results

	if mat_names.empty?
		col1 = Sketchup::Color.new $base_color.to_s
		col2 = Sketchup::Color.new $back_color.to_s
		col3 = Sketchup::Color.new $wall_color.to_s
	else
		col1 = $base_color
		col2 = $back_color
		col3 = $wall_color
	end	
	
	for e in ents
		if e.class == Sketchup::Face
			if ((e.normal.z.to_f < 1.0) && (e.normal.z.to_f > 0.0))
				e.material = col1 if not col1=="none"
				#e.material = $base_color
			end
			if ((e.normal.z.to_f > -1.0) && (e.normal.z.to_f < 0.0))
				e.material = col2 if not col2=="none"
				#e.material = $back_color
			end
			if ((e.normal.z.to_f == 0.0))
				e.material = col3 if not col3=="none"
			end
		end
	end
end

if( not file_loaded?("roofpaint_v.rb") )
		UI.menu("Plugins").add_item("Paint roofs (sloped faces)") { roofpaint }
end
#-----------------------------------------------------------------------------
file_loaded("roofpaint_v.rb")


