=begin
[i]TIG (c) 2015[/i]  

All rights reserved.    

**TIG-LayerWatcher.rb**  

#Installation:  

The RBZ installs from Preferences > Extensions > Install... button  
into the Plugins folder.  
It Autoloads with SketchUp.  

#Purpose:  

It waits for certain actions...  

It adds an Observer onto the Model's Layers.  
If the Current-Layer is changed to be anything other than "Layer0",  
then a dialog asks you if you really want to do that.  
"Yes" will change it, "No" will make the Current-Layer "Layer0".  
The purpose is to prevent you from modeling raw Geometry on other Layers -  
99.9% of the time it should be on "Layer0", other Layers are to control the  
visibility of Groups and Components which have been assigned those Layers.  
SketchUp Layers only control visibility, they do not separate Geometry -  
that is the role of Groups and Components...  

It also adds Observers onto ALL Entities contexts.  
Any 'added' Geometry [Edges or Faces] will always be assigned "Layer0",  
irrespective of the Current-Layer.  
This is useful to prevent accidental Layer reassignment, when Exploding Groups  
or Components, as it ensures that the resulting Geometry is always assigned  
"Layer0", irrespective of the container's Layer.  
You can still reassign Geometry to an alternative Layer using "Entity Info"  
[if you must], but if that Geometry is inside a Group or Component which is  
subsequently Exploded, then that Geometry is always re-assigned to "Layer0"  
in its new context.  
It only affects Geometry - other Entities are ignored.  

From v2.0 it also adds a Context-Menu item if the current Selection contains  
Geometry [Edges and Faces] not assigned to "Layer0", it is called -  
"Selected-Geometry-To-Layer0" - it reassigns that Geometry's Layer[s] to be  
"Layer0".  The Layers of other Entities in the Selection - e.g. Groups -  
are ignored, only Geometry in the Selection is changed.  
However, if the Selection contains Groups or Components, AND they in turn  
contain Geometry that is not assigned to "Layer0", then you are then prompted  
Yes|No to fix that... These changes mine down into any nested containers too...  
These Layer changes are one-step undo-able.  

#Version:  

1.0 20150602 First issue.  
2.0 20150603 Selected-Geometry-To-Layer0 Context-Menu added.  
3.0 20150611 Trapped for clash with ACT stupidly making a new layer, activating it for a millisecond and then reverting to the previous layer! Also skips all non-native tools when layer/geometry making.  
4.0 20150801 Trapped obscure errors from some other plugins' operations affecting its observers etc.  
Made into an Extension.  
5.0 20151106 Future-proofed with signing at EWH.  
6.0 20151114 More security tweaks. No better reporting on to-layer0 option.   
=end
###
require('sketchup.rb')
require('extensions.rb')
###
module TIG
  module LayerWatcher
		if defined?(Encoding)
			PLUGINS = File.dirname(__FILE__).force_encoding("UTF-8") ### v2014 lashup
		else
			PLUGINS = File.dirname(__FILE__)
		end
		FNAME = File.basename(__FILE__, ".*")
		FOLDER = File.join(PLUGINS, FNAME)
		DATA = File.join(FOLDER, "Data")
		### set Constants
		VERSION = "6.0"
		NAME = "TIG-LayerWatcher"
		CREATOR = "TIG"
		COPYRIGHT = "#{CREATOR} © #{Time.now.year}"
		DESC = "Questions resetting of the Current-Layer away from \"Layer0\". Assigns \"Layer0\" to all added Geometry [Edges and Faces] irrespective of Current-Layer, including Geometry resulting from Exploding Groups/Components. Also Context-Menu \"Selected-Geometry-To-Layer0\"."
		###
		EXT=SketchupExtension.new(NAME, File.join(FOLDER, "#{FNAME}_code"))
		EXT.name = NAME
		EXT.description = DESC
		EXT.version = VERSION
		EXT.creator = CREATOR
		EXT.copyright = COPYRIGHT
		ext = Sketchup.register_extension(EXT, true) # show on 1st install
		###
  end#module
end#module
