class Array
  def sum
    x=nil
    0.upto(self.length-1) do |e|
      if ((self[e].class==Fixnum) || (self[e].class==Float))
        x = x + self[e] if x!=nil
        x = self[e] if x==nil
      end
    end
    return x
  end
end

class WCO_ADDCPOINT

@@cu_addcpoint = 0
###
def onSetCursor
  UI::set_cursor(@@cu_addcpoint)
end

def initialize

  @edges=[];@vertices=[];@cpoints=[]
  @key=nil
  @status="Add Point:  Pick a New Point on an Edge, Face or another Element:  Edge + SHIFT = Split-Edge:  Face + ALT = Split-Face ..."

 if(@@cu_addcpoint == 0)

   c = File.join(File.dirname(__FILE__),  "WCO_ADDCPOINT", "cursor_addcpoint.png")
   @@cu_addcpoint = UI::create_cursor(c,5,18)
   UI::set_cursor(@@cu_addcpoint)
 end
end#def
def reset
  @edges=[];@vertices=[]
  @ip=Sketchup::InputPoint.new
  @drawn = false

end#def
def activate
  @ip1 = Sketchup::InputPoint.new
  @ip = Sketchup::InputPoint.new

  self.reset
  @model=Sketchup.active_model
  @entities=@model.active_entities
  @ss=@model.selection
  @ss.each{|e|@edges.push(e) if e.typename=="Edge"}
  @model.start_operation("Add Point Markers")
  @edges.each{|e|e.vertices.each{|v|@vertices.push(v)}}
  @vertices.uniq!
  @vertices.each{|v|cp=@entities.add_cpoint(v.position)
  @cpoints.push(cp)}
  @ss.clear
  @cpoints.each{|cp|@ss.add(cp)}
  @model.commit_operation
  Sketchup.set_status_text(@status)
end#def
def onKeyDown(key,repeat,flags,view)
  @key="shift" if key==CONSTRAIN_MODIFIER_KEY###
  @key="alt" if key==ALT_MODIFIER_KEY###
end#def
def onKeyUp(key,repeat,flags,view)
  @key=nil
end#def



def onMouseMove(flags,x,y,view)

#    @ip.pick view, x, y
#    if( @ip.valid? && @ip != @ip1 )
#        @ip1.copy! @ip
#        view.invalidate
#    end
#    view.tooltip = @ip.tooltip if @ip.valid?


  @ip.pick(view,x,y)
  view.invalidate
  if @ip.valid?
   if @ip.tooltip=="" || @ip.tooltip=="From Point"
    view.tooltip=""
   else
    view.tooltip=("Add Point: "+@ip.tooltip)
   end#if
  end#if
  Sketchup.set_status_text(@status)

end#def
def deactivate(view)
  @ss.clear
  view.invalidate if @drawn
#  view.invalidate
end#def
def onLButtonDown(flags,x,y,view)
  @model.start_operation("Add Point")
  ph=@model.active_view.pick_helper
  ph.do_pick(x,y)
  picked_edge=ph.picked_edge
  picked_face=ph.picked_face
  picked_element=ph.picked_element
  @ip.pick(view,x,y)
  if @ip.valid?
    @pt=@ip.position
  else
    picked_edge=nil
    picked_face=nil
    picked_element=nil
  end#if
  if((picked_edge||picked_face)||picked_element)
    cp=nil
    if (picked_face && ((picked_face.parent!=@model)||(@pt.on_plane?(picked_face.edges[0].start,picked_face.normal))))###compo-face or NOT off face
      cp=@entities.add_cpoint(@pt)
    elsif not picked_face
      cp=@entities.add_cpoint(@pt)
    else
      UI.beep
      Sketchup.set_status_text(@status)
      return
    end#if
    if cp
      @cpoints.push(cp)
      @ss.add(cp)
    end#if
  else
    UI.beep
    Sketchup.set_status_text(@status)
    return
  end#if
  Sketchup.set_status_text(@status)
  if picked_edge && @key=="shift"###
    picked_edge.split(@pt)if picked_edge###split select edge at picked point
  end#if
  if (@entities.include?(picked_face))&&(picked_face && @key=="alt")###
    picked_face.edges.each{|e|
      @entities.add_line(@pt,e.start)
      @entities.add_line(@pt,e.end)
    }###split face with new edges
  end#if
  Sketchup.set_status_text(@status)
  @model.commit_operation
end#def

def draw(view)
    # Show the current input point
    if( @ip.valid? && @ip.display? )
        @ip.draw(view)
        @drawn = true
    end
end

###
end#class

toolbar = UI::Toolbar.new "WCO-Toolbar"

cmd = UI::Command.new("Add Construction-Point") {
  Sketchup.active_model.select_tool(WCO_ADDCPOINT.new)
}
cmd.small_icon =  "WCO_ADDCPOINT/icon_addcpoint.png"
cmd.large_icon =  "WCO_ADDCPOINT/icon_addcpoint.png"
cmd.tooltip = "Vertex Tool"
cmd.status_bar_text = "Vertex Tool"
cmd.menu_text = "Vertex Tool"
toolbar.add_item cmd
