=begin
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.

This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Author: Rami_lpm 
e-Mail: rami.lpm[at]google[dot]com
Version: 0.1
Usage : go to 'plugins' and click 'Step Extrude'
Function: This tool allows stepped push pull. pick one face, it gets extruded one step. pick the next it gets extruded two steps.
You can change the step amount by typing it right after selecting the tool.
Changelog:
v0.0
..first version
v0.1
..added support for moving lines as suggested by Chris Fullmer

Thanks to sdmitch for the code to handle the moving of lines.
=end

require 'sketchup.rb' 

module RamTools_StepEx
	unless file_loaded?( __FILE__ )
		cmd = UI::Command.new('RamTools_StepEx') {self.activate_tool}
		cmd.menu_text = 'Step Extrude'
		UI.menu("Plugins").add_item cmd
	end
	
	def self.activate_tool
		my_tool = StepEx.new
		Sketchup.active_model.tools.push_tool(my_tool)
	end

	class StepEx
		def activate
			@step= 0.1.m
			Sketchup::set_status_text "Step set to #{@step}", SB_VCB_LABEL
			@drawn = false
		end
		def initialize
			@clickno = 0
			@step= 0
		end
		def deactivate(view)
				view.invalidate if @drawn
		end		
		def onMouseMove(flags, x, y, view)
			#mytext = x.to_s + ' - ' + y.to_s + ' - ' 
			#Sketchup::set_status_text mytext, SB_VCB_VALUE
			@pos = [x,y,0]
			view.invalidate			
		end
		def onLButtonDown(flags, x, y, view)		
			Sketchup.active_model.start_operation('Step Extrude',true)
			#mytext = x.to_s + ' - ' + y.to_s			
			#puts @clickno.to_s
			#puts 'you clicked on ' + mytext
			ph = view.pick_helper
			ph.do_pick(x, y)
			best = ph.picked_face
			unless best==nil
				@clickno += 1 
				#puts best.area/1550.0031000062 #sqM
				dist=@clickno*@step
				best.pushpull dist,false
				#snippet by sdmitch
				else
				 best = ph.picked_edge
				 unless best==nil
					@clickno += 1
					dist=@clickno*@step
					trans=Geom::Transformation.translation([0,0,dist])
					unless best.curve
					 edges=best.all_connected
					 verts=[];edges.each{|e| verts<<e.vertices}
					 verts.flatten!; verts.uniq!
					 verts.each{|v| Sketchup.active_model.entities.transform_entities(trans,v)}
					else
					 Sketchup.active_model.entities.transform_entities(trans,best.all_connected)
					end
				 end
				#end snippet
			end
			Sketchup.active_model.commit_operation
		end
		def draw(view)
			pos = @pos
			pos[0] += 20
			pos[1] -= 20
			#str = pos[0].to_s + ' - '+pos[1].to_s 
			dist=@clickno*@step.to_m
			str = "Level #{@clickno}, final height #{dist}m" 
			view.draw_text(pos, str)
			@drawn = true
		end		
		def onUserText(text, view)
			begin
				@step = text.to_l
				Sketchup::set_status_text "Step set to #{@step}", SB_VCB_LABEL
			rescue
				# Error parsing the text
				UI.beep
				puts "Cannot convert #{text} to a Length"
				@step = nil
				Sketchup::set_status_text "", SB_VCB_VALUE
			end
			return if !@step		 
		end		
		def enableVCB?
			return true
		end		
	end #class StepEx
	
end #module
file_loaded( __FILE__ )