=begin
btm_Sculpt7_Lock.rb

Copyright 2009, btmsketchup@hotmail.com

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.

Created by •BTM on 23/07/09.

Name:			Grab (sculpt tools)
Version:		1.0
Description:	Sets 'planar locks' which will lock vertices on created planes.
Usage:			3 point click to make a rectangle, defining the plane. Do not move this rectangle, it won't change a thing, but you can delete it.
=end
class LockSculpt

	def activate
	@model = Sketchup.active_model
	@entities = @model.active_entities
	@selection = @model.selection
	@times = 0
	@facepoints = []
	
	@ip1 = Sketchup::InputPoint.new
    @ip2 = Sketchup::InputPoint.new
	end
	
	def onLButtonUp(flags,x,y,view)
		if @times == 0
		@model.start_operation "Sculpt (Create Lock)"
		@ip1 = view.inputpoint x,y
		@facepoints << @ip1.position
		@times = 1
		
		elsif @times == 1
		@ip2 = view.inputpoint x,y
		@facepoints << @ip2.position
		@times = 2
		
		elsif @times == 2
		@ip3 = view.inputpoint x,y
		@facepoints << @ip3.position
		
		p2vec = @facepoints[1].vector_to @facepoints[2]
		
		forpoint = @facepoints[0].offset p2vec
		
		@planegroup = @entities.add_group
		
		pvec = @facepoints[1].vector_to @facepoints[0]
		endplane = [@facepoints[1], pvec]
		nextpoint = @mouse_point.project_to_plane endplane
		p2vec = @facepoints[1].vector_to nextpoint
		forpoint = @facepoints[0].offset p2vec
		
		@plane = @planegroup.entities.add_face @facepoints[0],  @facepoints[1], nextpoint, forpoint
		@plane.material = "#FF4400"
		@plane.material.alpha = 0.4
			@plane.edges.each do |e|
			e.hidden = true
			end
		
		@planegroup.set_attribute "sculpt", "lockgroup", @plane.plane
		@facepoints = []
		@times = 0
		end
	end
	
	def onMouseMove(flags,x,y,view)
	@ip1.pick view, x, y
	@mouse_point = @ip1.position
		if @ip1 != @ip2
		view.invalidate
		@ip2.copy! @ip1
		end
	end
	def draw(view)
	@ip1.draw view
	view.line_stipple = "-"
	view.drawing_color = "#FF4400"
		if @times == 1
		view.draw_line @facepoints[0], @mouse_point
		elsif @times == 2
		
		pvec = @facepoints[1].vector_to @facepoints[0]
		endplane = [@facepoints[1], pvec]
		nextpoint = @mouse_point.project_to_plane endplane
		p2vec = @facepoints[1].vector_to nextpoint
		forpoint = @facepoints[0].offset p2vec
		
		view.draw GL_LINE_LOOP, [@facepoints[0], @facepoints[1], nextpoint, forpoint]
		end
	end
end

		mgp = UI::Command.new("Planar Lock") { Sketchup.active_model.select_tool LockSculpt.new }
		$btm_sculpt_menu.add_item mgp
		mgp.large_icon = "./plane_24.png"
		mgp.small_icon = "./plane_16.png"
		mgp.tooltip = "Planar Lock"
		toolbar = $btm_sculpt_toolbar.add_item mgp
		case toolbar.get_last_state
		when 1
			toolbar.restore
		when -1   
			toolbar.show
		end