# mx_constructtools.rb (C) 2014 by Henry Schorradt, email@henryschorradt.de, http://www.henryschorradt.de

Sketchup::require 'sketchup.rb'
Sketchup::require 'extensions.rb'

$mxhm_plugin_loader = nil

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

module MX_constructtools

class WCO_ADDCICLEPOINT
  def WCO_ADDCICLEPOINT::start
    sel = Sketchup.active_model.selection
    ptss = []

    sel.each {|entity|
      if entity.class == Sketchup::Edge

      curve = entity.curve
        if curve.class == Sketchup::ArcCurve
        po =  curve.center
        x= po.x
        y= po.y
        z= po.z
        print x.to_s + ','+ y.to_s + ',' + z.to_s + "\n"
        ptss.push( po )
        end
      end
    }

#    Sketchup.active_model.active_entities.add_cpoint(curve.center)
    temp = []
    0.upto(ptss.length-1) do |i|
        x= ptss[i].x
        y= ptss[i].y
        z= ptss[i].z
    if temp.include?(x.to_s + ','+ y.to_s + ',' + z.to_s)

    else
        Sketchup.active_model.active_entities.add_cpoint ptss[i]
        print x.to_s + ','+ y.to_s + ',' + z.to_s + "----\n"
        temp.push(x.to_s + ','+ y.to_s + ',' + z.to_s)
    end


    end
    Sketchup.active_model.commit_operation


  end #def
end #class

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__), "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)
  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

class WCO_ADDENDPOINTS
  def WCO_ADDENDPOINTS::start
    # get the active model
    model = Sketchup.active_model
    #get currently selected objects
    selection_set = model.selection
    vertices = []

    Sketchup.active_model.start_operation "Pointify edges"
    selection_set.each {|edge|
      if edge.typename == "Edge" then
        vertices = vertices + edge.vertices
      end
    }
    vertices.uniq!
    0.upto(vertices.length-1) do |i|
      Sketchup.active_model.active_entities.add_cpoint vertices[i]
    end
    Sketchup.active_model.commit_operation
  end #def
end #class

class WCO_CLINETOOL

  @@ltcursor = 0
  # This is the standard Ruby initialize method that is called when you create
  # a new object.
  def initialize
    @ip1 = nil
    @ip2 = nil
    @xdown = 0
    @ydown = 0
    if @@ltcursor == 0
      c = File.join(File.dirname(__FILE__), "clinetool_cursor.png")
      @@ltcursor = UI::create_cursor(c, 1, 30)
    end
  end

  def onSetCursor
    UI::set_cursor(@@ltcursor)
  end

  def activate
    @ip1 = Sketchup::InputPoint.new
    @ip2 = Sketchup::InputPoint.new
    @ip = Sketchup::InputPoint.new
    @drawn = false

    Sketchup::set_status_text "Length", SB_VCB_LABEL

    self.reset(nil)
  end

  def deactivate(view)
    view.invalidate if @drawn
  end
  def onMouseMove(flags, x, y, view)
    if( @state == 0 )
      @ip.pick view, x, y
      if( @ip != @ip1 )
  view.invalidate if( @ip.display? or @ip1.display? )
  @ip1.copy! @ip

  view.tooltip = @ip1.tooltip
      end
    else
      @ip2.pick view, x, y, @ip1
      view.tooltip = @ip2.tooltip if( @ip2.valid? )
      view.invalidate
      if( @ip2.valid? )
  length = @ip1.position.distance(@ip2.position)
  Sketchup::set_status_text length.to_s, SB_VCB_VALUE
      end
      if( (x-@xdown).abs > 10 || (y-@ydown).abs > 10 )
  @dragging = true
      end
    end
  end
  def onLButtonDown(flags, x, y, view)
    if( @state == 0 )
      @ip1.pick view, x, y
      if( @ip1.valid? )
  @state = 1
  Sketchup::set_status_text "Select second end", SB_PROMPT
  @xdown = x
  @ydown = y
      end
    else
      # create the line on the second click
      if( @ip2.valid? )
  self.create_geometry(@ip1.position, @ip2.position,view)
  self.reset(view)
      end
    end

    # Clear any inference lock
    view.lock_inference
  end

  def onLButtonUp(flags, x, y, view)
    if( @dragging && @ip2.valid? )
      self.create_geometry(@ip1.position, @ip2.position,view)
      self.reset(view)
    end
  end

  def onKeyDown(key, repeat, flags, view)
    if( key == CONSTRAIN_MODIFIER_KEY && repeat == 1 )
      @shift_down_time = Time.now

      if( view.inference_locked? )
  view.lock_inference
      elsif( @state == 0 && @ip1.valid? )
  view.lock_inference @ip1
      view.line_width = 3
      elsif( @state == 1 && @ip2.valid? )
  view.lock_inference @ip2, @ip1
      view.line_width = 3
      end
    end
  end

  def onKeyUp(key, repeat, flags, view)
    if( key == CONSTRAIN_MODIFIER_KEY &&
       view.inference_locked? &&
       (Time.now - @shift_down_time) > 0.5 )
      view.lock_inference
    end
  end

  def onUserText(text, view)
    return if not @state == 1
    return if not @ip2.valid?

    begin
      value = text.to_l
    rescue
      UI.beep
      puts "Cannot convert #{text} to a Length"
      value = nil
      Sketchup::set_status_text "", SB_VCB_VALUE
    end
    return if !value

    pt1 = @ip1.position
    vec = @ip2.position - pt1
    if( vec.length == 0.0 )
      UI.beep
      return
    end
    vec.length = value
    pt2 = pt1 + vec

    # Create a line
    self.create_geometry(pt1, pt2, view)
    self.reset(view)
  end

  def draw(view)
    if( @ip1.valid? )
      if( @ip1.display? )
  @ip1.draw(view)
  @drawn = true
      end

      if( @ip2.valid? )
  @ip2.draw(view) if( @ip2.display? )

  view.set_color_from_line(@ip1, @ip2)
  self.draw_geometry(@ip1.position, @ip2.position, view)
  @drawn = true
      end
    end
  end

  def onCancel(flag, view)
    self.reset(view)
  end
  def reset(view)
    @state = 0

    Sketchup::set_status_text("Select first end", SB_PROMPT)
    @ip1.clear
    @ip2.clear
    @ip.clear

    if( view )
      view.tooltip = nil
      view.invalidate if @drawn
    end

    @drawn = false
    @dragging = false
  end
  def create_geometry(p1, p2, view)
    view.model.entities.add_cpoint(p1)
    view.model.entities.add_cpoint(p2)
    view.model.entities.add_cline(p1,p2)

  end
  def draw_geometry(pt1, pt2, view)
    view.draw_line(pt1, pt2)
  end

end # class LineTool

end #module

if( not file_loaded?(__FILE__) && $mxct_plugin_loader != nil)then
	$mxct_plugin_loader.setButtonMenu
end
file_loaded(__FILE__)