# copyright= 'Huynh Duong Phuong Vi @ 2013'
# suforyou.vn@gmail.com
#require 'langhandler.rb'
#$s4u_selectoolStrings=LanguageHandler.new ("s4u_selectool.strings")
module S4U::S4u_SelecTool
 def self.selectface(angle1)
	model =Sketchup.active_model
    sel=[]
	select =model.selection 
    if select.length==0 
        model.active_entities.each{|s| sel.push s}  
    else
        select.each{|s| sel.push s}
    end
    t=model.edit_transform
	faces=[]
    anglemin=-1
    anglemax=-1
	vectorz= Geom::Vector3d.new t.zaxis
    if not angle1 
               prompts = [$s4u_selectoolStrings.GetString("Min slope?"),$s4u_selectoolStrings.GetString("Max slope?")]
               defaults = ["0","180"]
               input = UI.inputbox(prompts, defaults, $s4u_selectoolStrings.GetString("Enter slope"))              
               if input
                 anglemin=input[0]
                 anglemax=input[1]
                 anglemin=anglemin.to_f
                 anglemax=anglemax.to_f
               else
                  return
               end                                        
    else
        angle=angle1
    end        
	sel.each{|e|  
        if e.is_a?Sketchup::Face             
            a=e.normal.angle_between vectorz               
            a=self.roundnumber(a.radians,10)
            if angle1
                faces.push e  if (a == angle ) or (a==(180-angle))
            else
                faces.push e  if (a >= anglemin ) and (a <= anglemax ) 
            end
        end
	} 
	select.clear
    if faces.length==0 
          Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected face")
          return
    else
         Sketchup.set_status_text $s4u_selectoolStrings.GetString(faces.length.to_s + " selected faces")
    end	
	faces.each{|f| select.add f.edges ;select.add f }
  end
  
   def self.selectedge(angle1)
	model =Sketchup.active_model
	sel=[]
	select =model.selection 
    if select.length==0 
        model.active_entities.each{|s| sel.push s}  
    else
        select.each{|s| sel.push s}
    end
    t=model.edit_transform
	edges=[]
    anglemin=-1
    anglemax=-1
	vectorz= Geom::Vector3d.new t.zaxis
    if not angle1 
               prompts = [$s4u_selectoolStrings.GetString("Min slope?"),$s4u_selectoolStrings.GetString("Max slope?")]
               defaults = ["-90","90"]
               input = UI.inputbox(prompts,defaults,$s4u_selectoolStrings.GetString("Enter slope"))
               if input
                 anglemin=input[0]
                 anglemax=input[1]
                 anglemin=anglemin.to_f
                 anglemax=anglemax.to_f
               else
                  return
               end                                        
    else
        angle=angle1
    end        
	sel.each{|e|  
        if e.is_a?Sketchup::Edge  
            ve=e.start.position.vector_to e.end.position            
            a=ve.angle_between vectorz 
            a=Math::PI/ 2-a
            a=self.roundnumber(a.radians,10)
            if angle1
                edges.push e  if (a == angle ) or (a==(-angle))
            else
                edges.push e  if (a >= anglemin ) and (a <= anglemax ) 
            end
        end
	} 
	select.clear
    if edges.length==0 
          Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected edge")
          return
    else
         Sketchup.set_status_text $s4u_selectoolStrings.GetString(edges.length.to_s + " selected edges")
    end	
	edges.each{|f| select.add f }
  end
  def self.roundnumber(num,e)
       if e==0 
           e=10000000000 
       else
           e=e*10
       end
       return (num*e).round / e.to_f
  end
  def self.selectface_numvers
	model =Sketchup.active_model
    sel=[]
	select =model.selection 
    if select.length==0 
        model.active_entities.each{|s| sel.push s}  
    else
        select.each{|s| sel.push s}
    end
	faces=[]
    num=0
    if sel.length>0
               prompts = [$s4u_selectoolStrings.GetString("Number?(>=3)")]
               defaults = [3]
               input = UI.inputbox prompts, defaults, $s4u_selectoolStrings.GetString("Enter number vertices")                
               if input
                  num=input[0].to_i                  
                  if num<3 
                           Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected face")
                           return
                  end
               else
                   Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected face")
                  return
               end                                        
    else
        Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected face")
        return
    end        
	sel.each{|e|  
        if e.is_a?Sketchup::Face             
            numface=self.get_numvers(e)    
            faces.push e  if (numface == num)
        end
	} 
    select.clear
    if faces.length==0 
          Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected face")
          return
    else
         Sketchup.set_status_text $s4u_selectoolStrings.GetString(faces.length.to_s + " selected faces")
    end	
	faces.each{|f| select.add f.edges ;select.add f }
  end
  def self.get_numvers(face)
     ver=[]
     vertices=[]    
     vertices =face.vertices*2    
     (1..face.vertices.length).each { |i|         
         p1=vertices[i].position
         p2=vertices[i+1].position
         p3=vertices[i+2].position
         v1=p1.vector_to p2
         v2=p2.vector_to p3
         ver.push vertices[i+1].position if not v1.samedirection? v2
     }
     return ver.length
   end
   def self.select_area 
	model =Sketchup.active_model
        sel=[]
	select =model.selection 
	if select.length==0 
        	model.active_entities.each{|s| sel.push s}  
	else
        	select.each{|s| sel.push s}
    	end
	faces=[]
    	areamin=-1
    	areamax=-1
        prompts = [$s4u_selectoolStrings.GetString("Min area?"),$s4u_selectoolStrings.GetString("Max area?")]
        defaults = ["0","100"]
        input = UI.inputbox(prompts, defaults, $s4u_selectoolStrings.GetString("Enter area face"))
        if input
        	areamin=input[0]
                areamax=input[1]
                areamin=areamin.to_f
                areamax=areamax.to_f
        else
                return
        end                                               
	sel.each{|e|  
        if e.is_a?Sketchup::Face             
            a=Sketchup.format_area e.area              
            a=a.to_f           
            if a>0
                faces.push e  if (a >= areamin ) and (a <= areamax ) 
            end
        end
	} 
	select.clear
    	if faces.length==0 
         	 Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected face")
         	 return
    	else
        	Sketchup.set_status_text $s4u_selectoolStrings.GetString(faces.length.to_s + " selected faces")
    	end	
	faces.each{|f| select.add f.edges ;select.add f }
  end
  def self.select_same_area 
	model =Sketchup.active_model
    sel=[]
    model.active_entities.each{|s| sel.push s}
    face=nil
	select =model.selection 
    	if select.length==1
        	 face=select[0] if select[0].is_a?Sketchup::Face
          if face==nil
             select.clear
       		 return 
          end
        else
          select.clear
          return
    	end        
    	faces=[]
    area_face=self.roundnumber(face.area,10)    
	sel.each{|e|  
        if e.is_a?Sketchup::Face                      
            a=self.roundnumber(e.area,10)            
            faces.push e  if a==area_face
        end
	} 
	select.clear
    	if faces.length==0 
         	 Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected face")
         	 return
    	else
        	Sketchup.set_status_text $s4u_selectoolStrings.GetString(faces.length.to_s + " selected faces")
    	end	
	faces.each{|f| select.add f.edges ;select.add f }
  end
  def self.select_length
	model =Sketchup.active_model
	sel=[]
	select =model.selection 
    	if select.length==0 
        	model.active_entities.each{|s| sel.push s}  
    	else
       		 select.each{|s| sel.push s}
    	end
    	edges=[]
    	min=-1
    	max=-1   
        prompts = [$s4u_selectoolStrings.GetString("Min length?"),$s4u_selectoolStrings.GetString("Max length?")]
        defaults = ["0","10"]
        input = UI.inputbox(prompts,defaults,$s4u_selectoolStrings.GetString("Enter length edge"))
        if input
         	min=input[0]
                max=input[1]
                min=min.to_l
                max=max.to_l
        else
                return
        end  
	sel.each{|e|  
        if e.is_a?Sketchup::Edge              
            a=Sketchup.format_length e.length
            a=a.to_l
            if a > 0 
                edges.push e  if (a >= min ) and (a <= max ) 
            end
        end
	} 
	select.clear
    	if edges.length==0 
         	 Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected edge")
        	 return
    	else
         	Sketchup.set_status_text $s4u_selectoolStrings.GetString(edges.length.to_s + " selected edges")
    	end	
	edges.each{|f| select.add f }
  end
  def self.select_same_length
	model =Sketchup.active_model
	sel=[]
    model.active_entities.each{|s| sel.push s}
    line=nil
	select =model.selection 
    	if select.length==1
        	 line=select[0] if select[0].is_a?Sketchup::Edge
          if line==nil
             select.clear
       		 return 
          end
        else
          select.clear
          return
    	end        
    	edges=[]
        l=self.roundnumber(line.length,10)
	sel.each{|e|  
        if e.is_a?Sketchup::Edge              
            a=self.roundnumber(e.length,10)            
            edges.push e  if a==l
        end
	} 
	select.clear
    	if edges.length==0 
         	 Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected edge")
        	 return
    	else
         	Sketchup.set_status_text $s4u_selectoolStrings.GetString(edges.length.to_s + " selected edges")
    	end	
	edges.each{|f| select.add f }
  end
  def self.selectface_parallel
	model =Sketchup.active_model
    sel=[]
    model.active_entities.each{|s| sel.push s}
    face=nil
	select =model.selection 
    	if select.length==1
        	 face=select[0] if select[0].is_a?Sketchup::Face
          if face==nil
             select.clear
       		 return 
          end
        else
          select.clear
          return
    	end        
    	faces=[]   
	sel.each{|e|  
        if e.is_a?Sketchup::Face                               
            faces.push e  if face.normal.parallel? e.normal
        end
	} 
	select.clear
    	if faces.length==0 
         	 Sketchup.set_status_text $s4u_selectoolStrings.GetString("No selected face")
         	 return
    	else
        	Sketchup.set_status_text $s4u_selectoolStrings.GetString(faces.length.to_s + " selected faces")
    	end	
	faces.each{|f| select.add f.edges ;select.add f }
  end
end
#S4U::S4u_SelecTool.select_area
#S4U::S4u_SelecTool.select_length
#S4U::S4u_SelecTool.selectface( nil )
#S4U::S4u_SelecTool.selectedge( nil )
#S4U::S4u_SelecTool.selectface_numvers
#S4U::S4u_SelecTool.select_same_length
#S4U::S4u_SelecTool.select_same_area
#S4U::S4u_SelecTool.selectface_parallel
