# copyright= 'Huynh Duong Phuong Vi @ 2013'
# suforyou.vn@gmail.com
module S4U
module S4u_AlignTool
class AlignFace
def initialize
    @alignfix=0
    self.reset(nil)
end
def activate
     self.setstatus(nil)
end
def setstatus(view)
    if @selectface1==nil
        if @alignfix==0
            Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select target face for align - press TAB for align and move")
        else
            Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select target face for align and move - press TAB for align")
        end
    else    
       if @selectface2.length==0
           if @alignfix==0
              Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select aligned faces - press Ctrl for multiselect")
           else
              Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select aligned and moved faces - press Ctrl for multiselect ")
           end         
       else
           if @mulsel==1
              if @alignfix==0
                 Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select aligned faces - press Ctrl finish selecting") 
              else   
                 Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select aligned and moved faces  - press Ctrl finish selecting")
              end
          end          
       end
    end 
    view.invalidate if view!=nil
end
def draw(view) 
    self.drawfaceover(view)
    self.drawfaceselect(view)  
end
def drawfaceselect(view)
     view.line_width = 5
     if @selectface1
          view.drawing_color = "blue"          
          ps = @selectface1.outer_loop.vertices.collect {|p| 
          p.position.transform! @transface1}
          ps=ps.push ps[0]
          view.draw_polyline ps
     end    
     if @selectface2.length>0
           view.drawing_color = "blueviolet"             
           (0..@selectface2.length-1).each{ |i|
              ps=[]
              ps = @selectface2[i].outer_loop.vertices.collect {|p| 
              p.position.transform! @transface2[i]}
              ps=ps.push ps[0]
              view.draw_polyline ps
           }
     end
end
def drawfaceover(view)
    view.drawing_color = "red"
    view.line_width = 5
    if @selectface.is_a?( Sketchup::Face) 
        ps = @selectface.outer_loop.vertices.collect {|p| 
                 p.position.transform! @transface  
                 }         
        ps=ps.push ps[0]
        view.draw_polyline ps
    end
end
def onMouseMove(flags, x, y, view)    
    ph1=view.pick_helper
    ph1.do_pick x,y
    @selectface=ph1.picked_face 
    ph1.do_pick x,y
    @transface= ph1.transformation_at(ph1.count-1)  
    self.setstatus(view)
end
def onLButtonDown(flags, x, y, view)
     ph = view.pick_helper
     ph.do_pick x,y
     element  = ph.best_picked
     selectface=ph.picked_face
     if @selectface1==nil     
         @selectface1=selectface  
         @element1=element
         @transface1= ph.transformation_at(ph.count-1)
     else
         if  @selectface2.length==0 or @mulsel==1        
               @selectface2.push selectface
               @element2.push element
               @transface2.push ph.transformation_at(ph.count-1)                
         end                
     end
end
def onLButtonUp(flags, x, y, view)
    self.align_faceonface if @selectface1 and @selectface2.length>0 and @mulsel==0
    self.setstatus(view)
end
def onKeyUp(key, repeat, flags, view)
  if key==COPY_MODIFIER_KEY or key==13
     if @mulsel==1   
        self.align_faceonface if @selectface1 and @selectface2.length>0 
     else
        @mulsel = 1 if key==COPY_MODIFIER_KEY
     end
  end
  if key==9
     if @alignfix==0
          @alignfix=1
     else
          @alignfix=0
     end 
  end
  self.setstatus(view)
end

def align_faceonface
    model = Sketchup.active_model
    entities = model.active_entities   
    model.start_operation "align_faceonface"
        cp1=self.centerface(@selectface1)
        cp1.transform! @transface1
        if  @element1.is_a?( Sketchup::Face)
            v1=@selectface1.normal
        else         
            ps=nil
            ps = @selectface1.outer_loop.vertices.collect {|p| 
                 p.position.transform! @transface1  }                 
            ps=ps.push ps[0]
            ft=entities.add_face ps
            v1= ft.normal
            normalface1=@selectface1.normal
            normalface1.transform! @transface1
            v1.reverse! if (v1.angle_between normalface1 ) >=Math::PI/ 2
            gt=entities.add_group ft
            entities.erase_entities gt
        end
    new_transform1 = Geom::Transformation.new(cp1,v1)   
    plane = [cp1, v1]
    (0..@selectface2.length-1).each{ |i|
      element2=@element2[i]
      selectface2=@selectface2[i]
      transface2=@transface2[i]
      if not element2.is_a?( Sketchup::Face) 
        cp2=self.centerface(selectface2)
        cp2.transform! transface2 
            ps=nil
            ps = selectface2.outer_loop.vertices.collect {|p| 
                 p.position.transform! transface2  }                 
            ps=ps.push ps[0]
            ft=entities.add_face ps
            v2= ft.normal
            normalface2=selectface2.normal
            normalface2.transform! transface2
            v2.reverse! if (v2.angle_between normalface2)>= Math::PI/ 2
            gt=entities.add_group ft
            entities.erase_entities gt  
        new_transform2 = Geom::Transformation.new(cp2,v2)     
        tmpgrp=entities.add_group
        t=element2.transformation    
        g=tmpgrp.entities.add_group element2
        g.transformation=new_transform2
        element2.transformation=new_transform2.inverse*t   
        
        pointonplane = cp2.project_to_plane plane  
        if @alignfix==1
             new_transform3 = Geom::Transformation.new(cp1 ,v1) 
        else
             new_transform3 = Geom::Transformation.new(pointonplane,v1)            
        end
        g.transformation=new_transform3  
        g.explode       
      else   
        ver=[]
        selectface2.vertices.each{|ver2|
             p=ver2.position
             p1 = p.project_to_plane plane
             v=p.vector_to p1
             t=Geom::Transformation.translation v
             entities.transform_entities( t,ver2) 
             ver.push ver2
        } 
        erasee = []
        ver.each{|v|
              v.faces.each{|f|
                 f.edges.each{|e| 
                    erasee.push e if((e.faces.length==0) or (e.faces.length==2 and e.faces[0].material==e.faces[1].material and e.faces[0].normal.parallel?(e.faces[1].normal)))
                }  
             }                       
        }	          
		erasee.each {|e| e.erase! if(e.deleted? == false)  }   
     end
    }
    self.reset(nil)
    model.commit_operation
end

def reset(view)
    @selectface1=nil
    @selectface2=[]
    @element1=nil
    @element2=[]
    @selectface=nil
    @transface=nil
    @transface1=nil
    @transface2=[]
    @mulsel=0
    self.setstatus(view)
end
def centerface(fa)
      cp=Geom::Point3d.new
      numver=fa.outer_loop.vertices.length
      fa.outer_loop.vertices.each{|v|
        cp.x=cp.x+v.position.x
        cp.y=cp.y+v.position.y
        cp.z=cp.z+v.position.z
      }
      cp.x=cp.x/ numver
      cp.y=cp.y/ numver
      cp.z=cp.z/ numver
      return cp
end
def resume(view)
   self.setstatus(view)
end
end
def self.alignface
    Sketchup.active_model.select_tool AlignFace.new
end
end
end
#S4U::S4u_AlignTool.alignface
