# copyright= 'Huynh Duong Phuong Vi @ 2013'
# suforyou.vn@gmail.com
module S4U
module S4u_AlignTool
class AlignEdge
def initialize
    @alignfix=0
    self.reset(nil)
end
def activate
     self.setstatus(nil)
end
def setstatus(view)
    if  @selectedge1==nil
        if @alignfix==0
            Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select target edge for align - press Shift for align and move")
        else
            Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select target edge for align and move - press Shift for align")
        end
    else      
       if @selectedge2.length==0 
           if @alignfix==0
              Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select aligned edges - press Ctrl for multiselect")
           else
              Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select aligned and moved edges - press Ctrl for multiselect ")
           end  
       else
          if @mulsel==1
              if @alignfix==0
                 Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select aligned edges - press Ctrl finish selecting") 
              else   
                 Sketchup.status_text = $s4u_aligntoolStrings.GetString("Select aligned and moved edges  - press Ctrl finish selecting")
              end
          end
       end
    end
    view.invalidate  if view!=nil
end
def draw(view) 
    self.drawedgeover(view)
    self.drawedgeselect(view)   
end
def onMouseMove(flags, x, y, view)    
    ph1=view.pick_helper
    ph1.do_pick x,y
    @element = ph1.best_picked
    @selectedge=ph1.picked_edge  
    @transedge= ph1.transformation_at(ph1.count-1)
    self.setstatus(view)
end

def drawedgeselect(view)    
     view.line_width = 5
     if @selectedge1      
          view.drawing_color = "blue"
          selectedge=@selectedge1 
          cpstart=selectedge.start.position      
          cpend=selectedge.end.position
          if not @element1.is_a?(Sketchup::Edge) 
              cpstart=cpstart.transform @transedge1
              cpend=cpend.transform @transedge1
          end
          view.draw_line(cpstart ,cpend) 
     end    
     if @selectedge2.length>0
          view.drawing_color = "blueviolet"
        (0..@selectedge2.length-1).each{ |i|
          selectedge=@selectedge2[i]
          cpstart=selectedge.start.position        
          cpend=selectedge.end.position
          if not @element2[i].is_a?( Sketchup::Edge) 
            cpstart=cpstart.transform @transedge2[i]
            cpend=cpend.transform @transedge2[i]   
          end
          view.draw_line(cpstart ,cpend) 
        }
     end
end
def drawedgeover(view)
    view.drawing_color = "red"
    view.line_width = 5
    if @selectedge
        ed=@selectedge
        p1=ed.start.position
        p2=ed.end.position
        if not @element.is_a?( Sketchup::Edge)             
            p1=p1.transform @transedge       
            p2=p2.transform @transedge
        end
        view.draw_line(p1,p2)  
     end
end
def onLButtonUp(flags, x, y, view)
      self.align_edgeonedge if @selectedge1 and @selectedge2.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_edgeonedge if @selectedge1 and @selectedge2.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 onLButtonDown(flags, x, y, view)
     ph = view.pick_helper
     ph.do_pick x,y
     element  = ph.best_picked
     selectedge=ph.picked_edge
     if @selectedge1==nil       
         @selectedge1=selectedge  
         @element1=element
         @transedge1= ph.transformation_at(ph.count-1)      
     else
         if  @selectedge2.length==0 or @mulsel==1         
               @selectedge2.push selectedge
               @element2.push element
               @transedge2.push ph.transformation_at(ph.count-1)                            
         end                
     end
end

def align_edgeonedge
    model = Sketchup.active_model
    entities = model.active_entities   
    model.start_operation "align_edgeonedge"
        cp1start=@selectedge1.start.position
        cp1end=@selectedge1.end.position   
        if not @element1.is_a?( Sketchup::Edge)
            cp1start=cp1start.transform @transedge1
            cp1end=cp1end.transform @transedge1 
        end
        v1=cp1start.vector_to cp1end 
        line = [cp1start,cp1end]
        midedge1=Geom::Point3d.new( (cp1start.x+cp1end.x)/ 2,(cp1start.y+cp1end.y)/ 2,(cp1start.z+cp1end.z)/ 2)
    (0..@selectedge2.length-1).each{ |i|
        selectedge2=@selectedge2[i]
        element2=@element2[i]
        transedge2=@transedge2[i]      
        cp2start=selectedge2.start.position    
        cp2end=selectedge2.end.position
        if not element2.is_a?( Sketchup::Edge)  
          cp2start=cp2start.transform transedge2
          cp2end=cp2end.transform transedge2
          midedge2=Geom::Point3d.new( (cp2start.x+cp2end.x)/ 2,(cp2start.y+cp2end.y)/ 2,(cp2start.z+cp2end.z)/ 2)
          v2=cp2start.vector_to cp2end 
          new_transform2 = Geom::Transformation.new(midedge2,v2)             
          tmpgrp=entities.add_group   
          t=element2.transformation          
          g=tmpgrp.entities.add_group element2
          g.transformation=new_transform2
          element2.transformation=new_transform2.inverse*t          
          pointonline1 = cp2start.project_to_line line
          pointonline2 = cp2end.project_to_line line
          midonline=midedge2.project_to_line line
          if pointonline1==pointonline2
              v3=v1
          else
              v3=pointonline1.vector_to pointonline2
          end      
          if @alignfix==1
               new_transform3 = Geom::Transformation.new(midedge1,v3) 
          else
               new_transform3 = Geom::Transformation.new(midonline,v3)            
          end       
          g.transformation=new_transform3  
          g.explode
      else
          pointonline1 = cp2start.project_to_line line
          pointonline2 = cp2end.project_to_line line                    
          if @alignfix==1
               v1 = cp2start.vector_to cp1start          
               v2 = cp2end.vector_to cp1end          
          else
               v1 = cp2start.vector_to pointonline1
               v2 = cp2end.vector_to pointonline2
          end  
          ver=[]
          entities.transform_entities(v1,selectedge2.vertices[0])
          ver.push selectedge2.vertices[0] 
          entities.transform_entities(v2,selectedge2.vertices[1])
          ver.push selectedge2.vertices[1]
          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)
    @selectedge1=nil
    @selectedge2=[]
    @element1=nil
    @element2=[]
    @selectedge=nil
    @element=nil
    @transedge=nil
    @transedge1=nil
    @transedge2=[]
    @mulsel=0
    self.setstatus(view)
end
def resume(view)
   self.setstatus(view)
end
end
def self.alignedge
    Sketchup.active_model.select_tool AlignEdge.new
end
end
end
#S4U::S4u_AlignTool.alignedge
