#        Name : erase_lonely_edges
# Description : projects the selected set of faces perpendicular to the selected face
#      Author : Didier Bur
#       Usage : select faces and segments, the script will eliminate all segments of the selection
#             : which doesn't belong to a face
#        Date : 24.Sept.2oo4
#        Type : tool

require 'sketchup.rb'

def erase_lonely_edges

model = Sketchup.active_model
model.start_operation "erase lonely edges"
entities = model.active_entities
ss = model.selection
ss_len = ss.length
if ss.empty? 
  UI.messagebox("No selection.")
  return nil
end

# Who are the faces and who are the segments ?
i = 0
k = 0
objects_to_erase = Array.new
Sketchup.set_status_text(ss_len.to_s + " objects to process, please wait...\n")

0.upto( ss_len - 1 ) do |that|
  element = ss[i]
  if( element.typename == "Edge") and (element.faces.length == 0)
    objects_to_erase[k] = element
    k = k + 1
  end

  i = i + 1
end # of upto

# Erase the elements

Sketchup.set_status_text( "Objects erased: " + objects_to_erase.length.to_s )
j = 0
0.upto( objects_to_erase.length - 1 ) do |e|
#puts objects_to_erase[j].to_s + "\n"
  e = objects_to_erase[j]
  
  e.erase!
  j = j + 1
end #of upto

end # of def

if( not file_loaded?("erase_lonely_edges.rb") )
    add_separator_to_menu("Plugins")
    UI.menu("Plugins").add_item("Eliminate lonely segments") { erase_lonely_edges }

end

#-----------------------------------------------------------------------------
file_loaded("erase_lonely_edges.rb")