# Supports Organizer.rb
=begin
Copyright 2009, Author
All Rights Reserved

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.

License:   		Matt666

Author:      	Matthieu NOBLET
Organization:   Matt666
Name:      		Remove_CG_Mat
Version:   		1.4
SU Version:   	Work on v6 & v7. I don't know on v5
Date:      		30/01/09
Description:   	Remove all materials from a group or a component instance. Possibility to apply G/C material 
				to the inside geometry.
History:
	1.4: 		More speed
				Add the RickW's collaborative effort ;), and support Organizer.rb, of course!
	1.5:		Can choose default color. Thank you thomthom for the request.
=end

require 'sketchup.rb'

unless file_loaded?(__FILE__)
   file_loaded(__FILE__)
   $submenu ? (organizer = $submenu) : (organizer = UI.menu("Plugins"))
   organizer.add_item("Remove C-G materials"){Matt_Tools.remove_CG_mat}
end

module Matt_Tools
	def self.remove_CG_mat
		model = Sketchup.active_model
		ents = model.active_entities
		materials=model.materials
		l_mat=[]
		@n = 0
		if Sketchup.version[0,1].to_i >= 7
			model.start_operation("Remove comp & group materials",true)
		else
			model.start_operation("Remove comp & group materials")
		end
		Sketchup.set_status_text "Program is running...", SB_PROMPT
		l_mat=Sketchup::Color.names.sort.push("Default")
		action=l_mat.join('|')
		prompts = ["Apply G/C material to the default geometry? ","Default Color: "]
		infos=["Yes|No",action]
		values=["No","Default"]
		results = inputbox(prompts, values, infos, "Remove C/G material")
		return if not results
		if results[1]=="Default"
			self.remove_CG_mat_boucle_put_on_comp_material(ents,nil) if results[0]=="Yes"
			self.remove_CG_mat_boucle(ents,nil)
		else
			self.remove_CG_mat_boucle(ents,results[1])
			self.remove_CG_mat_boucle_put_on_comp_material(ents,results[1])
		end
		Sketchup.set_status_text "Done!", SB_PROMPT
		model.active_view.invalidate
		UI.messagebox("Done! "+@n.to_s+" material components/groups deleted.")
		model.commit_operation
	end
	
	def self.remove_CG_mat_boucle_put_on_comp_material(sel,mat)
		for e in sel
			if ["Group","ComponentInstance"].index e.typename
				compEnts = e.entities if e.typename == "Group"
				compEnts = e.definition.entities if e.typename == "ComponentInstance"
				if (e.parent.class == Sketchup::ComponentDefinition) and (not e.material)
					e.material = e.parent.instances[0].material
				end
				self.remove_CG_mat_boucle_put_on_comp_material(compEnts,mat)
			elsif (e.parent.class == Sketchup::ComponentDefinition) and (not e.material)
				if material = e.parent.instances[0].material
					e.material = material
					e.back_material = material if e.respond_to? "back_material"
				else
					e.material=mat
					e.back_material = mat if e.respond_to? "back_material"
				end
			end
		end
	end
	
	def self.remove_CG_mat_boucle(sel,mat)
		for e in sel
			if ["Group","ComponentInstance"].index e.typename
				compEnts = e.entities if e.typename == "Group"
				compEnts = e.definition.entities if e.typename == "ComponentInstance"
				@n += 1 if e.material				
				e.material = nil
				self.remove_CG_mat_boucle(compEnts,mat)
			end
		end
	end
end