=begin
Name: PurgeAll.rb
Description: Plugins > Purge...
Author: TIG (c) 2014
Usage: Pick "Purge...", or use a Shortcut Key to it.
	It Purges all UNUSED Components, Layers, Materials, Styles.
	Choose which type(s) to purge from the dialog (Yes/No).
	It only works in >= v6.
Version:
1.0 2070225	First issue.
1.1 20070225 v.6000277 allowed !
1.2 20070226 A 4 options dialog added, now Menu= Purge...
1.3 20070226 'Make Report' option added.
1.4 20070304 Glitch on 'default' options fixed.
1.5 20110313 Extended reporting log option added.
1.6 20140214 Futureproofed, log report output 'sorted' etc.
1.7 20140219 Minor cosmetic tweaks.
=end
###
require 'sketchup.rb'
###
UI.start_timer(0.1, false){
	begin
		SCFapi.key='T87GcvasDHDa432hBgfP'
		SCFapi.store_event('PurgeAll', 'Load')
	rescue
	end
}
###
module TIG
	### menu bits
	unless file_loaded?(__FILE__)
		UI.menu("Plugins").add_item("Purge..."){ self.purgeAll() }
	end
	file_loaded(__FILE__)
	###
	def self.purgeAll()
		### show VCB and status info
		Sketchup::set_status_text(("Purge Unused..."),SB_PROMPT)
		Sketchup::set_status_text("",SB_VCB_LABEL)
		Sketchup::set_status_text("",SB_VCB_VALUE)
		###
		if Sketchup.version_number < 6000277
			Sketchup::set_status_text(("Purge... ERROR..."),SB_PROMPT)
			UI.messagebox("Sorry, but 'Purge...'\nis for Version 6+ only.")
			return nil
		end#if
		### dialog ###v1.2
		@compos = "Yes" unless @compos
		@layers = "Yes" unless @layers
		@maters = "Yes" unless @maters
		@styles = "Yes" unless @styles
		@report = "Yes" unless @report
		prompts = ["Purge Components: ","Purge Layers: ","Purge Materials: ","Purge Styles: ","Make Report: "]
		values = [@compos, @layers, @maters, @styles, @report]
		yn = "Yes|No"
		ynl = "Yes|No|Log"
		popups = [yn, yn, yn, yn, ynl]
		title = "Purge Unused Options"
		results = inputbox(prompts, values, popups, title)
		return nil unless results
		### do processing of results
		@compos, @layers, @maters, @styles, @report = results
		###
		model = Sketchup.active_model
		definitions = model.definitions
		layers = model.layers
		materials = model.materials
		styles = model.styles
		###
		begin
			model.start_operation("Purge Unused...", true)
		rescue
			model.start_operation("Purge Unused...")
		end
		###
		if @report == "Log"
			path = model.path
			if ! path || path.empty?
				path = Dir.pwd
				title = "Untitled"
			else
				path = File.dirname(path)
				title = model.title 
			end
			log = File.join(path, "#{title}-PurgeLog.txt").tr("\\", "/")
			@log = File.open(log, "w")
			@log.puts(log)
		end#if
		###
		if @compos == "Yes"
			compos1 = definitions.count
			csIN = []
			definitions.each{|e|csIN << e.name}
			definitions.purge_unused
			csOUT = []
			definitions.each{|e|csOUT << e.name}
			csGONE = csIN - csOUT
			co = compos1 - definitions.count
			cmsg = "\n Components:\t #{co} of #{compos1}"
		else
			cmsg = "\n ..."
		end#if
		###
		if @layers == "Yes"
			layers1 = layers.count
			lsIN = []
			layers.each{|e|lsIN << e.name}
			layers.purge_unused
			lsOUT = []
			layers.each{|e|lsOUT << e.name}
			lsGONE = lsIN - lsOUT
			la = layers1 - layers.count
			lmsg = "\n Layers:\t\t #{la} of #{layers1}"
		else
			lmsg = "\n ..."
		end#if
		###
		if @maters == "Yes"
			materials1 = materials.count
			msIN = []
			materials.each{|e|msIN << e.display_name}
			materials.purge_unused
			msOUT = []
			materials.each{|e|msOUT << e.display_name}
			msGONE = msIN - msOUT
			ma = materials1 - materials.count
			mmsg  = "\n Materials:\t\t #{ma} of #{materials1}"
		else
			mmsg="\n ..."
		end#if
		if @styles == "Yes"
			styles1 = styles.count
			ssIN = []
			styles.each{|e|ssIN << e.name}
			styles.purge_unused
			ssOUT = []
			styles.each{|e|ssOUT << e.name}
			ssGONE = ssIN - ssOUT
			st = styles1 - styles.count
			smsg="\n Styles:\t\t #{st} of #{styles1}"
		else
			smsg="\n ...\n"
		end#if
		### report
		if @compos == "No" && @layers == "No" && @maters == "No" && @styles == "No"
			UI.messagebox("You have set NOTHING to Purge !")
			if @report == "Log"
				@log.puts("\nNOTHING set to Purge !\n")
				@log.close
				UI.openURL("file:///#{log}")
			end
			model.abort_operation
			return nil
		end#if
		###
		if @report == "Yes"
			UI.messagebox(" Purged:\n"+cmsg+lmsg+mmsg+smsg+"\t")
		elsif @report == "Log"
			UI.messagebox(" Purged:\n"+cmsg+lmsg+mmsg+smsg+"\t")
			@log.puts("\nPurged Unused...")
			@log.puts(cmsg)
			@log.puts("\n") if csGONE && csGONE[0]
			csGONE.sort.each{|e|@log.puts(e)} if csGONE
			@log.puts(lmsg)
			@log.puts("\n") if lsGONE && lsGONE[0]
			lsGONE.sort.each{|e|@log.puts(e)} if lsGONE
			@log.puts(mmsg)
			@log.puts("\n") if msGONE && msGONE[0]
			msGONE.sort.each{|e|@log.puts(e)} if msGONE
			@log.puts(smsg)
			@log.puts("\n") if ssGONE && ssGONE[0]
			ssGONE.sort.each{|e|@log.puts(e)} if ssGONE
			@log.puts("\n#EOF")
			@log.close
			UI.openURL("file:///#{log}")
		end#if
		###
		model.commit_operation ###for undo
		###
		begin
			SCFapi.key='T87GcvasDHDa432hBgfP'
			SCFapi.store_event('PurgeAll', 'Run')
			SCFapi.send_events()
		rescue
		end
		###
	end #def
end #module
###
