#-----------------------------------------------------------------------------
#        Name : extrude_lines
# Description : Creates faces along selected lines at a given height
#      Author : Didier Bur
#       Usage : select lines, select "extrude lines" from the Plugins menu, 
#               enter height, make group or not, and here you go
#        Date : 18.Oct.2oo4, updated 12.Sept.2oo7
#        Type : tool
#-----------------------------------------------------------------------------

require 'sketchup.rb'

def extrude_lines

model = Sketchup.active_model
model.start_operation "Extrude lines"
entities = model.active_entities
ss = model.selection
curves = Set.new

if ss.empty? 
  UI.messagebox("No selection.")
  return nil
end

# Selection error checking
others = 0
i = 0
ss.each do |obj|
  if( obj.typename != "Edge")
    others = others + 1
    ss.remove obj
  end
end

if( others != 0 )
  UI.messagebox( others.to_s + " object(s) in the selection aren't lines and will be ignored.")
end

# sets the default setting
$el_height = 100.inch if not $el_height
$el_grp = "No" if not $el_grp
dropdowns=[["Yes","No"].join("|")]
# Dialog box
prompts = ["Make group: ", "Extrusion height: "]
values = [$el_grp, $height]
results = inputbox prompts, values, dropdowns, "Extrusion parameters"
return if not results
$height = results[1].to_l
$el_grp = results[0]

if $el_grp == "Yes"
  new_group = entities.add_group
  new_ents = new_group.entities
  else
  new_ents = entities
end
ss.each do |line|
  if( line.typename == "Edge" ) and line.curve == nil
    # not a curve
    pts = line.vertices
    v1 = pts[0].position
    v2 = pts[1].position
    v1_top = Geom::Point3d.new(v1.x, v1.y, v1.z + $el_height)
    v2_top = Geom::Point3d.new(v2.x, v2.y, v2.z + $el_height)
    new_ents.add_face(v1, v1_top, v2_top, v2)
  else
    # part of a curve, add to set
    curves.insert line.curve
  end
end # of upto

curves.each do |c|
  edges = c.edges
  edges.each do |e|
    e.smooth = true
    e.soft = true
    v1 = e.start.position
    v2 = e.end.position
    v1_top = Geom::Point3d.new(v1.x, v1.y, v1.z + $el_height)
    v2_top = Geom::Point3d.new(v2.x, v2.y, v2.z + $el_height)
    face = new_ents.add_face(v1, v1_top, v2_top, v2)
    face.edges.each do |edge|
      edge.smooth = true
      edge.soft = true
    end
  end
end

model.commit_operation
end
#-----------------------------------------------------------------------------
# Menu item
if( not file_loaded?("extrude_lines.rb") )
    add_separator_to_menu("Plugins")
    UI.menu("Plugins").add_item("Extrude lines and curves") { extrude_lines }
end
#-----------------------------------------------------------------------------
file_loaded("extrude_lines.rb")
