=begin
Copyright 2006-2014, TIG
Permission to use, copy, modify, and distribute this software for 
any purpose and without fee is hereby granted, provided the above
copyright notice appear in all copies.
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        : Elev45shadows.rb
Description : Adds 45 degree top-left/right shadows to elevations etc.
Menu Item   : View > Elevation 45 Shadows > Left, > Right
Usage       :
Set up your plan or elevation-view scenes(s).  
Woks best with perspective OFF.
Tip: Select an elevation face and right-click and pick Align View to get a 
non-orthogonal elevation [i.e. non-F/B/L/R].  
Then select this tool from the menu and it will automatically work out a 
location, time of day etc, and in conjunction with the view's camera 
direction, add shadows as if the sun is from the top-left or top-right of 
the view, at 45 degrees, in elevation or in plan...
Note aligning views to 3D sloping faces will have unexpected affects.
Shadow setting changes are NOT Undo-able automatically.
Because it changes the geo-location, North etc, only use it on a copy 
of the model's SKP.
It will realign North in each processed view, but all recent versions of 
SketchUp allow only one North at a time, so you'll need to copy the model's 
SKP for each shadowed view and use the scene-tabs to get proper shadows...

Version:
1.4 20140304 Improved format, typo fixed, now in TIG module etc. 
	Now two versions Left & Right chosen from sub-menu.

=end
require 'sketchup.rb'
###
module TIG
	###
	### menu
	unless file_loaded?(__FILE__)
	   mn = UI.menu("View")
	   mn.add_separator
	   sm = mn.add_submenu("Elevation 45 Shadows")
	   sm.add_item("Left"){  self.elev45shadows(false) }
	   sm.add_item("Right"){ self.elev45shadows(true) }
	end#if
	file_loaded(__FILE__)
	###
	def self.elev45shadows(from_right=false)
		###
		model = Sketchup.active_model
		shadowinfo = model.shadow_info
		view = model.active_view
		direction = view.camera.direction
		x = direction.x
		y = direction.y
		z = direction.z
		a = Math.atan2(y,x)
		a = a.abs*57.295779513082320900 ### into abs degrees
		if y == 0
		   if x > 0 ###looking east (axis)
			  angle= 90 + 45
		   end
		   if x < 0 ###looking west
			  angle= 270 + 45
		   end
		end
		if y > 0
		   angle= 0 + 45 if x == 0 ###looking north
		   if x > 0 ### ne quadrant
			  angle= 90 - a + 45###
		   end
		   if x < 0 ### nw quadrant
			  angle= 180 - a - 90 + 45###
		   end
		end
		if y < 0
		   angle= 180 + 45 if x == 0 ###looking south
		   if x > 0.0 ### se quadrants
			 angle= a + 180 - 45###
		   end
		   if x < 0 ### sw quadrant
			  angle= a + 90 + 45###
		   end
		end
		angle = angle - 90 if from_right ###
		angle= 90 + 45 if z != 0 && x == 0 && y == 0 ### plan
		###
		shadowinfo["Country"] = "Elevation 45 shadows" ###
		shadowinfo["City"] = "Elevation 45 shadows" ###
		shadowinfo["TZOffset"] = 0 ###
		shadowinfo["UseSunForAllShading"] = true ###
		shadowinfo["DisplayShadows"] = true
		shadowinfo["Latitude"] = 55.1800 ### why ??? but it works !!!
		shadowinfo["Longitude"] = 0.1000 ### why ??? but it works !!!
		shadowinfo["NorthAngle"] = angle
		shadowinfo["ShadowTime"] = Time.gm(2000,"mar",21,12,06,50) ### why ??? but it works !!!
		###
		begin
			view.refresh
		rescue
		end
		###
	end#def
	###
end#TIG
